浏览代码

multiboot2: Implement Debug and PartialEq for packed structs

Niklas Sombert 2 年之前
父节点
当前提交
6379b426c9
共有 3 个文件被更改,包括 57 次插入3 次删除
  1. 14 1
      multiboot2/src/elf_sections.rs
  2. 31 1
      multiboot2/src/framebuffer.rs
  3. 12 1
      multiboot2/src/memory_map.rs

+ 14 - 1
multiboot2/src/elf_sections.rs

@@ -12,7 +12,7 @@ const METADATA_SIZE: usize = size_of::<TagTypeId>() + 4 * size_of::<u32>();
 /// This tag contains section header table from an ELF kernel.
 ///
 /// The sections iterator is provided via the `sections` method.
-#[derive(Debug, ptr_meta::Pointee)]
+#[derive(ptr_meta::Pointee)]
 #[repr(C, packed)]
 pub struct ElfSectionsTag {
     typ: TagTypeId,
@@ -83,6 +83,19 @@ impl StructAsBytes for ElfSectionsTag {
     }
 }
 
+impl Debug for ElfSectionsTag {
+    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
+        f.debug_struct("ElfSectionsTag")
+            .field("typ", &{ self.typ })
+            .field("size", &{ self.size })
+            .field("number_of_sections", &{ self.number_of_sections })
+            .field("entry_size", &{ self.entry_size })
+            .field("shndx", &{ self.shndx })
+            .field("sections", &self.sections(0))
+            .finish()
+    }
+}
+
 /// An iterator over some ELF sections.
 #[derive(Clone)]
 pub struct ElfSectionIter {

+ 31 - 1
multiboot2/src/framebuffer.rs

@@ -1,5 +1,6 @@
 use crate::{Reader, Tag, TagTrait, TagType, TagTypeId};
 
+use core::fmt::Debug;
 use core::mem::size_of;
 use core::slice;
 use derive_more::Display;
@@ -17,7 +18,7 @@ const METADATA_SIZE: usize = size_of::<TagTypeId>()
     + 2 * size_of::<u8>();
 
 /// The VBE Framebuffer information Tag.
-#[derive(Debug, PartialEq, Eq, ptr_meta::Pointee)]
+#[derive(Eq, ptr_meta::Pointee)]
 #[repr(C, packed)]
 pub struct FramebufferTag {
     typ: TagTypeId,
@@ -156,6 +157,35 @@ impl StructAsBytes for FramebufferTag {
     }
 }
 
+impl Debug for FramebufferTag {
+    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
+        f.debug_struct("FramebufferTag")
+            .field("typ", &{ self.typ })
+            .field("size", &{ self.size })
+            .field("buffer_type", &self.buffer_type())
+            .field("address", &{ self.address })
+            .field("pitch", &{ self.pitch })
+            .field("width", &{ self.width })
+            .field("height", &{ self.height })
+            .field("bpp", &self.bpp)
+            .finish()
+    }
+}
+
+impl PartialEq for FramebufferTag {
+    fn eq(&self, other: &Self) -> bool {
+        ({ self.typ } == { other.typ }
+            && { self.size } == { other.size }
+            && { self.address } == { other.address }
+            && { self.pitch } == { other.pitch }
+            && { self.width } == { other.width }
+            && { self.height } == { other.height }
+            && { self.bpp } == { other.bpp }
+            && { self.type_no } == { other.type_no }
+            && self.buffer == other.buffer)
+    }
+}
+
 /// Helper struct for [`FramebufferType`].
 #[derive(Debug, PartialEq, Eq)]
 #[repr(u8)]

+ 12 - 1
multiboot2/src/memory_map.rs

@@ -1,6 +1,7 @@
 use crate::{Tag, TagTrait, TagType, TagTypeId};
 
 use core::convert::TryInto;
+use core::fmt::Debug;
 use core::marker::PhantomData;
 use core::mem;
 
@@ -187,7 +188,6 @@ impl<'a> Iterator for MemoryAreaIter<'a> {
 /// (which had a 24-bit address bus) could use, historically.
 /// Nowadays, much bigger chunks of continuous memory are available at higher
 /// addresses, but the Multiboot standard still references those two terms.
-#[derive(Debug)]
 #[repr(C, packed)]
 pub struct BasicMemoryInfoTag {
     typ: TagTypeId,
@@ -222,6 +222,17 @@ impl StructAsBytes for BasicMemoryInfoTag {
     }
 }
 
+impl Debug for BasicMemoryInfoTag {
+    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
+        f.debug_struct("BasicMemoryInfoTag")
+            .field("typ", &{ self.typ })
+            .field("size", &{ self.size })
+            .field("memory_lower", &{ self.memory_lower })
+            .field("memory_upper", &{ self.memory_upper })
+            .finish()
+    }
+}
+
 /// EFI memory map as per EFI specification.
 #[derive(Debug)]
 #[repr(C)]