Selaa lähdekoodia

Added ModuleIter, .gitignore change

Why I was originally wary about creating ModuleIter is because I
have to make Tag and TagIter public so that it can work. I can
change this (by practically rewriting TagIter in ModuleIter, but
I think that this works fine (issue/comment if you feel otherwise).

I also made a very small change to .gitignore so that I would not
accidentally commit my .swp files.
Calvin Lee 8 vuotta sitten
vanhempi
commit
020277ce00
3 muutettua tiedostoa jossa 27 lisäystä ja 5 poistoa
  1. 1 0
      .gitignore
  2. 5 5
      src/lib.rs
  3. 21 0
      src/module.rs

+ 1 - 0
.gitignore

@@ -1,2 +1,3 @@
 target
 Cargo.lock
+*.swp

+ 5 - 5
src/lib.rs

@@ -4,7 +4,7 @@ pub use boot_loader_name::BootLoaderNameTag;
 pub use elf_sections::{ElfSectionsTag, ElfSection, ElfSectionIter, ElfSectionType, ElfSectionFlags};
 pub use elf_sections::{ELF_SECTION_WRITABLE, ELF_SECTION_ALLOCATED, ELF_SECTION_EXECUTABLE};
 pub use memory_map::{MemoryMapTag, MemoryArea, MemoryAreaIter};
-pub use module::{ModuleTag};
+pub use module::{ModuleTag, ModuleIter};
 
 #[macro_use]
 extern crate bitflags;
@@ -44,8 +44,8 @@ impl BootInformation {
         self.get_tag(6).map(|tag| unsafe{&*(tag as *const Tag as *const MemoryMapTag)})
     }
 
-    pub fn module_tag(&self) -> Option<&'static ModuleTag> {
-        self.get_tag(3).map(|tag| unsafe{&*(tag as *const Tag as *const ModuleTag)})
+    pub fn module_tags(&self) -> ModuleIter {
+        ModuleIter::new(self.tags())
     }
 
     pub fn boot_loader_name_tag(&self) -> Option<&'static BootLoaderNameTag> {
@@ -72,13 +72,13 @@ impl BootInformation {
 }
 
 #[repr(C)]
-struct Tag {
+pub struct Tag {
     typ: u32,
     size: u32,
     // tag specific fields
 }
 
-struct TagIter {
+pub struct TagIter {
     current: *const Tag,
 }
 

+ 21 - 0
src/module.rs

@@ -1,3 +1,5 @@
+use {Tag, TagIter};
+
 #[repr(packed)]
 #[derive(Debug)]
 pub struct ModuleTag {
@@ -29,3 +31,22 @@ impl ModuleTag {
         self.mod_end
     }
 }
+
+pub struct ModuleIter {
+    iter: TagIter,
+}
+
+impl ModuleIter {
+    pub fn new(iter: TagIter) -> ModuleIter {
+        ModuleIter{ iter: iter }
+    }
+}
+
+impl Iterator for ModuleIter {
+    type Item = &'static ModuleTag;
+
+    fn next(&mut self) -> Option<&'static ModuleTag> {
+        self.iter.find(|x| x.typ == 3)
+            .map(|tag| unsafe{&*(tag as *const Tag as *const ModuleTag)})
+    }
+}