Ver Fonte

multiboot2: TagType impls Ord + renamed module

This commit renamed the module header to tag_type because this better fits. This is not visible to the public API. The enum TagType implements Ord now which enables the usage of the type in BTreeSet.
Philipp Schuster há 3 anos atrás
pai
commit
5f8424a570

+ 1 - 1
multiboot2/src/elf_sections.rs

@@ -1,4 +1,4 @@
-use crate::header::Tag;
+use crate::tag_type::Tag;
 use core::fmt::{Debug, Formatter};
 
 /// This tag contains section header table from an ELF kernel.

+ 1 - 1
multiboot2/src/framebuffer.rs

@@ -1,4 +1,4 @@
-use crate::header::Tag;
+use crate::tag_type::Tag;
 use crate::Reader;
 use core::slice;
 

+ 12 - 4
multiboot2/src/lib.rs

@@ -45,9 +45,6 @@ pub use elf_sections::{
     ElfSection, ElfSectionFlags, ElfSectionIter, ElfSectionType, ElfSectionsTag,
 };
 pub use framebuffer::{FramebufferColor, FramebufferField, FramebufferTag, FramebufferType};
-pub use header::TagType;
-pub use header::MULTIBOOT2_BOOTLOADER_MAGIC;
-use header::{Tag, TagIter};
 pub use image_load_addr::ImageLoadPhysAddr;
 pub use memory_map::{
     EFIMemoryAreaType, EFIMemoryDesc, EFIMemoryMapTag, MemoryArea, MemoryAreaIter, MemoryAreaType,
@@ -55,6 +52,8 @@ pub use memory_map::{
 };
 pub use module::{ModuleIter, ModuleTag};
 pub use rsdp::{RsdpV1Tag, RsdpV2Tag};
+pub use tag_type::TagType;
+use tag_type::{Tag, TagIter};
 pub use vbe_info::{
     VBECapabilities, VBEControlInfo, VBEDirectColorAttributes, VBEField, VBEInfoTag,
     VBEMemoryModel, VBEModeAttributes, VBEModeInfo, VBEWindowAttributes,
@@ -68,13 +67,22 @@ mod command_line;
 mod efi;
 mod elf_sections;
 mod framebuffer;
-mod header;
 mod image_load_addr;
 mod memory_map;
 mod module;
 mod rsdp;
+mod tag_type;
 mod vbe_info;
 
+/// Magic number that a multiboot2-compliant boot loader will store in `eax` register
+/// right before handoff to the payload (the kernel). This value can be used to check,
+/// that the kernel was indeed booted via multiboot2.
+///
+/// Caution: You might need some assembly code (e.g. GAS or NASM) first, which
+/// moves `eax` to another register, like `edi`. Otherwise it probably happens,
+/// that the Rust compiler output changes `eax` before you can access it.
+pub const MULTIBOOT2_BOOTLOADER_MAGIC: u32 = 0x36d76289;
+
 /// Load the multiboot boot information struct from an address.
 ///
 /// This is the same as `load_with_offset` but the offset is omitted and set

+ 1 - 1
multiboot2/src/module.rs

@@ -1,4 +1,4 @@
-use crate::header::{Tag, TagIter, TagType};
+use crate::tag_type::{Tag, TagIter, TagType};
 use core::fmt::{Debug, Formatter};
 
 /// This tag indicates to the kernel what boot module was loaded along with

+ 35 - 33
multiboot2/src/header.rs → multiboot2/src/tag_type.rs

@@ -1,21 +1,14 @@
+//! Module for [`TagType`].
+
 use core::fmt::{Debug, Formatter};
-use core::hash::{Hash, Hasher};
+use core::hash::Hash;
 use core::marker::PhantomData;
 
-/// Magic number that a multiboot2-compliant boot loader will store in `eax` register
-/// right before handoff to the payload (the kernel). This value can be used to check,
-/// that the kernel was indeed booted via multiboot2.
-///
-/// Caution: You might need some assembly code (e.g. GAS or NASM) first, which
-/// moves `eax` to another register, like `edi`. Otherwise it probably happens,
-/// that the Rust compiler output changes `eax` before you can access it.
-pub const MULTIBOOT2_BOOTLOADER_MAGIC: u32 = 0x36d76289;
-
 /// Possible types of a Tag in the Multiboot2 Information Structure (MBI), therefore the value
 /// of the the `typ` property. The names and values are taken from the example C code
 /// at the bottom of the Multiboot2 specification.
 #[repr(u32)]
-#[derive(Copy, Clone, Debug, Eq)]
+#[derive(Copy, Clone, Debug, Eq, Ord, PartialOrd, PartialEq, Hash)]
 pub enum TagType {
     /// Marks the end of the tags.
     End = 0,
@@ -110,19 +103,6 @@ impl PartialEq<TagType> for u32 {
     }
 }
 
-impl PartialEq<TagType> for TagType {
-    fn eq(&self, other: &TagType) -> bool {
-        *self as u32 == *other as u32
-    }
-}
-
-// impl required because this type is used in a hashmap in `multiboot2-header`
-impl Hash for TagType {
-    fn hash<H: Hasher>(&self, state: &mut H) {
-        state.write_u32(*self as u32);
-    }
-}
-
 /// All tags that could passed via the Multiboot2 information structure to a payload/program/kernel.
 /// Better not confuse this with the Multiboot2 header tags. They are something different.
 #[derive(Clone, Copy)]
@@ -185,14 +165,36 @@ mod tests {
     use super::*;
 
     #[test]
-    fn test_hash() {
-        let mut hashset = std::collections::HashSet::new();
-        hashset.insert(TagType::Cmdline);
-        hashset.insert(TagType::ElfSections);
-        hashset.insert(TagType::BootLoaderName);
-        hashset.insert(TagType::LoadBaseAddr);
-        hashset.insert(TagType::LoadBaseAddr);
-        assert_eq!(hashset.len(), 4);
-        println!("{:#?}", hashset);
+    fn test_hashset() {
+        let mut set = std::collections::HashSet::new();
+        set.insert(TagType::Cmdline);
+        set.insert(TagType::ElfSections);
+        set.insert(TagType::BootLoaderName);
+        set.insert(TagType::LoadBaseAddr);
+        set.insert(TagType::LoadBaseAddr);
+        assert_eq!(set.len(), 4);
+        println!("{:#?}", set);
+    }
+
+    #[test]
+    fn test_btreeset() {
+        let mut set = std::collections::BTreeSet::new();
+        set.insert(TagType::Cmdline);
+        set.insert(TagType::ElfSections);
+        set.insert(TagType::BootLoaderName);
+        set.insert(TagType::LoadBaseAddr);
+        set.insert(TagType::LoadBaseAddr);
+        assert_eq!(set.len(), 4);
+        for (current, next) in set.iter().zip(set.iter().skip(1)) {
+            assert!(current < next);
+        }
+        println!("{:#?}", set);
+    }
+
+    /// Tests for equality when one type is u32 and the other the enum representation.
+    #[test]
+    fn test_partial_eq_u32() {
+        assert_eq!(21, TagType::LoadBaseAddr);
+        assert_eq!(TagType::LoadBaseAddr, 21);
     }
 }