Explorar el Código

multiboot2: fix all warnings of rustc and clippy

Philipp Schuster hace 8 meses
padre
commit
fb6c12b0d3

+ 36 - 37
multiboot2/src/builder/information.rs

@@ -7,7 +7,6 @@ use crate::{
     EFISdt32Tag, EFISdt64Tag, ElfSectionsTag, EndTag, FramebufferTag, ImageLoadPhysAddrTag,
     MemoryMapTag, ModuleTag, RsdpV1Tag, RsdpV2Tag, SmbiosTag, TagTrait, TagType, ALIGNMENT,
 };
-use alloc::boxed::Box;
 use alloc::vec::Vec;
 use core::fmt::{Display, Formatter};
 use core::mem::size_of;
@@ -200,32 +199,32 @@ impl InformationBuilder {
 
     /// Adds a 'basic memory information' tag (represented by [`BasicMemoryInfoTag`]) to the builder.
     #[must_use]
-    pub fn basic_memory_info_tag(self, tag: BasicMemoryInfoTag) -> Self {
-        self.add_tag(&tag).unwrap()
+    pub fn basic_memory_info_tag(self, tag: &BasicMemoryInfoTag) -> Self {
+        self.add_tag(tag).unwrap()
     }
 
     /// Adds a 'bootloader name' tag (represented by [`BootLoaderNameTag`]) to the builder.
     #[must_use]
-    pub fn bootloader_name_tag(self, tag: Box<BootLoaderNameTag>) -> Self {
-        self.add_tag(&*tag).unwrap()
+    pub fn bootloader_name_tag(self, tag: &BootLoaderNameTag) -> Self {
+        self.add_tag(tag).unwrap()
     }
 
     /// Adds a 'command line' tag (represented by [`CommandLineTag`]) to the builder.
     #[must_use]
-    pub fn command_line_tag(self, tag: Box<CommandLineTag>) -> Self {
-        self.add_tag(&*tag).unwrap()
+    pub fn command_line_tag(self, tag: &CommandLineTag) -> Self {
+        self.add_tag(tag).unwrap()
     }
 
     /// Adds a 'EFI 32-bit system table pointer' tag (represented by [`EFISdt32Tag`]) to the builder.
     #[must_use]
-    pub fn efisdt32_tag(self, tag: EFISdt32Tag) -> Self {
-        self.add_tag(&tag).unwrap()
+    pub fn efisdt32_tag(self, tag: &EFISdt32Tag) -> Self {
+        self.add_tag(tag).unwrap()
     }
 
     /// Adds a 'EFI 64-bit system table pointer' tag (represented by [`EFISdt64Tag`]) to the builder.
     #[must_use]
-    pub fn efisdt64_tag(self, tag: EFISdt64Tag) -> Self {
-        self.add_tag(&tag).unwrap()
+    pub fn efisdt64_tag(self, tag: &EFISdt64Tag) -> Self {
+        self.add_tag(tag).unwrap()
     }
 
     /// Adds a 'EFI boot services not terminated' tag (represented by [`EFIBootServicesNotExitedTag`]) to the builder.
@@ -236,69 +235,69 @@ impl InformationBuilder {
 
     /// Adds a 'EFI 32-bit image handle pointer' tag (represented by [`EFIImageHandle32Tag`]) to the builder.
     #[must_use]
-    pub fn efi_image_handle32(self, tag: EFIImageHandle32Tag) -> Self {
-        self.add_tag(&tag).unwrap()
+    pub fn efi_image_handle32(self, tag: &EFIImageHandle32Tag) -> Self {
+        self.add_tag(tag).unwrap()
     }
 
     /// Adds a 'EFI 64-bit image handle pointer' tag (represented by [`EFIImageHandle64Tag`]) to the builder.
     #[must_use]
-    pub fn efi_image_handle64(self, tag: EFIImageHandle64Tag) -> Self {
-        self.add_tag(&tag).unwrap()
+    pub fn efi_image_handle64(self, tag: &EFIImageHandle64Tag) -> Self {
+        self.add_tag(tag).unwrap()
     }
 
     /// Adds a 'EFI Memory map' tag (represented by [`EFIMemoryMapTag`]) to the builder.
     #[must_use]
-    pub fn efi_memory_map_tag(self, tag: Box<EFIMemoryMapTag>) -> Self {
-        self.add_tag(&*tag).unwrap()
+    pub fn efi_memory_map_tag(self, tag: &EFIMemoryMapTag) -> Self {
+        self.add_tag(tag).unwrap()
     }
 
     /// Adds a 'ELF-Symbols' tag (represented by [`ElfSectionsTag`]) to the builder.
     #[must_use]
-    pub fn elf_sections_tag(self, tag: Box<ElfSectionsTag>) -> Self {
-        self.add_tag(&*tag).unwrap()
+    pub fn elf_sections_tag(self, tag: &ElfSectionsTag) -> Self {
+        self.add_tag(tag).unwrap()
     }
 
     /// Adds a 'Framebuffer info' tag (represented by [`FramebufferTag`]) to the builder.
     #[must_use]
-    pub fn framebuffer_tag(self, tag: Box<FramebufferTag>) -> Self {
-        self.add_tag(&*tag).unwrap()
+    pub fn framebuffer_tag(self, tag: &FramebufferTag) -> Self {
+        self.add_tag(tag).unwrap()
     }
 
     /// Adds a 'Image load base physical address' tag (represented by [`ImageLoadPhysAddrTag`]) to the builder.
     #[must_use]
-    pub fn image_load_addr(self, tag: ImageLoadPhysAddrTag) -> Self {
-        self.add_tag(&tag).unwrap()
+    pub fn image_load_addr(self, tag: &ImageLoadPhysAddrTag) -> Self {
+        self.add_tag(tag).unwrap()
     }
 
     /// Adds a (*none EFI*) 'memory map' tag (represented by [`MemoryMapTag`]) to the builder.
     #[must_use]
-    pub fn memory_map_tag(self, tag: Box<MemoryMapTag>) -> Self {
-        self.add_tag(&*tag).unwrap()
+    pub fn memory_map_tag(self, tag: &MemoryMapTag) -> Self {
+        self.add_tag(tag).unwrap()
     }
 
     /// Adds a 'Modules' tag (represented by [`ModuleTag`]) to the builder.
     /// This tag can occur multiple times in boot information.
     #[must_use]
-    pub fn add_module_tag(self, tag: Box<ModuleTag>) -> Self {
-        self.add_tag(&*tag).unwrap()
+    pub fn add_module_tag(self, tag: &ModuleTag) -> Self {
+        self.add_tag(tag).unwrap()
     }
 
     /// Adds a 'ACPI old RSDP' tag (represented by [`RsdpV1Tag`]) to the builder.
     #[must_use]
-    pub fn rsdp_v1_tag(self, tag: RsdpV1Tag) -> Self {
-        self.add_tag(&tag).unwrap()
+    pub fn rsdp_v1_tag(self, tag: &RsdpV1Tag) -> Self {
+        self.add_tag(tag).unwrap()
     }
 
     /// Adds a 'ACPI new RSDP' tag (represented by [`RsdpV2Tag`]) to the builder.
     #[must_use]
-    pub fn rsdp_v2_tag(self, tag: RsdpV2Tag) -> Self {
-        self.add_tag(&tag).unwrap()
+    pub fn rsdp_v2_tag(self, tag: &RsdpV2Tag) -> Self {
+        self.add_tag(tag).unwrap()
     }
 
     /// Adds a 'SMBIOS tables' tag (represented by [`SmbiosTag`]) to the builder.
     #[must_use]
-    pub fn smbios_tag(self, tag: Box<SmbiosTag>) -> Self {
-        self.add_tag(&*tag).unwrap()
+    pub fn smbios_tag(self, tag: &SmbiosTag) -> Self {
+        self.add_tag(tag).unwrap()
     }
 
     const fn tag_is_allowed_multiple_times(tag_type: TagType) -> bool {
@@ -322,18 +321,18 @@ mod tests {
         assert_eq!(builder.expected_len(), expected_len);
 
         // the most simple tag
-        builder = builder.basic_memory_info_tag(BasicMemoryInfoTag::new(640, 7 * 1024));
+        builder = builder.basic_memory_info_tag(&BasicMemoryInfoTag::new(640, 7 * 1024));
         expected_len += 16;
         assert_eq!(builder.expected_len(), expected_len);
         // a tag that has a dynamic size
-        builder = builder.command_line_tag(CommandLineTag::new("test"));
+        builder = builder.command_line_tag(&CommandLineTag::new("test"));
         expected_len += 8 + 5 + 3; // padding
         assert_eq!(builder.expected_len(), expected_len);
         // many modules
-        builder = builder.add_module_tag(ModuleTag::new(0, 1234, "module1"));
+        builder = builder.add_module_tag(&ModuleTag::new(0, 1234, "module1"));
         expected_len += 16 + 8;
         assert_eq!(builder.expected_len(), expected_len);
-        builder = builder.add_module_tag(ModuleTag::new(5678, 6789, "module2"));
+        builder = builder.add_module_tag(&ModuleTag::new(5678, 6789, "module2"));
         expected_len += 16 + 8;
         assert_eq!(builder.expected_len(), expected_len);
 

+ 1 - 1
multiboot2/src/command_line.rs

@@ -6,7 +6,7 @@ use core::fmt::{Debug, Formatter};
 use core::mem;
 use core::str;
 #[cfg(feature = "builder")]
-use {crate::new_boxed, alloc::boxed::Box, alloc::vec::Vec};
+use {crate::new_boxed, alloc::boxed::Box};
 
 const METADATA_SIZE: usize = mem::size_of::<TagHeader>();
 

+ 4 - 4
multiboot2/src/elf_sections.rs

@@ -2,7 +2,7 @@
 
 use crate::{TagHeader, TagTrait, TagType};
 use core::fmt::{Debug, Formatter};
-use core::marker::{PhantomData, PhantomPinned};
+use core::marker::PhantomData;
 use core::mem;
 use core::str::Utf8Error;
 #[cfg(feature = "builder")]
@@ -35,7 +35,7 @@ impl ElfSectionsTag {
     }
 
     /// Get an iterator of loaded ELF sections.
-    pub(crate) fn sections(&self) -> ElfSectionIter {
+    pub(crate) const fn sections(&self) -> ElfSectionIter {
         let string_section_offset = (self.shndx * self.entry_size) as isize;
         let string_section_ptr =
             unsafe { self.sections.as_ptr().offset(string_section_offset) as *const _ };
@@ -44,7 +44,7 @@ impl ElfSectionsTag {
             remaining_sections: self.number_of_sections,
             entry_size: self.entry_size,
             string_section: string_section_ptr,
-            _phantom_data: PhantomData::default(),
+            _phantom_data: PhantomData,
         }
     }
 }
@@ -90,7 +90,7 @@ impl<'a> Iterator for ElfSectionIter<'a> {
                 inner: self.current_section,
                 string_section: self.string_section,
                 entry_size: self.entry_size,
-                _phantom: PhantomData::default(),
+                _phantom: PhantomData,
             };
 
             self.current_section = unsafe { self.current_section.offset(self.entry_size as isize) };

+ 4 - 2
multiboot2/src/lib.rs

@@ -12,7 +12,7 @@
 // now allow a few rules which are denied by the above statement
 // --> They are either ridiculous, not necessary, or we can't fix them.
 #![allow(clippy::multiple_crate_versions)]
-// #![deny(missing_docs)]
+#![deny(missing_docs)]
 #![deny(missing_debug_implementations)]
 #![deny(rustdoc::all)]
 // --- END STYLE CHECKS ---
@@ -100,7 +100,9 @@ pub use smbios::SmbiosTag;
 pub use tag::TagHeader;
 pub use tag_trait::TagTrait;
 pub use tag_type::{TagType, TagTypeId};
-pub use util::{new_boxed, parse_slice_as_string, StringError};
+#[cfg(feature = "alloc")]
+pub use util::new_boxed;
+pub use util::{parse_slice_as_string, StringError};
 pub use vbe_info::{
     VBECapabilities, VBEControlInfo, VBEDirectColorAttributes, VBEField, VBEInfoTag,
     VBEMemoryModel, VBEModeAttributes, VBEModeInfo, VBEWindowAttributes,

+ 2 - 2
multiboot2/src/memory_map.rs

@@ -43,7 +43,7 @@ impl MemoryMapTag {
         let entry_version = 0_u32.to_ne_bytes();
         let areas = {
             let ptr = areas.as_ptr().cast::<u8>();
-            let len = areas.len() * size_of::<MemoryArea>();
+            let len = mem::size_of_val(areas);
             unsafe { slice::from_raw_parts(ptr, len) }
         };
         new_boxed(&[&entry_size, &entry_version, areas])
@@ -322,7 +322,7 @@ impl EFIMemoryMapTag {
     pub fn new_from_descs(descs: &[EFIMemoryDesc]) -> Box<Self> {
         let efi_mmap = {
             let ptr = descs.as_ptr().cast::<u8>();
-            let len = descs.len() * size_of::<EFIMemoryDesc>();
+            let len = mem::size_of_val(descs);
             unsafe { slice::from_raw_parts(ptr, len) }
         };
 

+ 1 - 1
multiboot2/src/module.rs

@@ -5,7 +5,7 @@ use crate::{parse_slice_as_string, StringError, TagTrait, TagType};
 use core::fmt::{Debug, Formatter};
 use core::mem;
 #[cfg(feature = "builder")]
-use {crate::new_boxed, alloc::boxed::Box, alloc::vec::Vec};
+use {crate::new_boxed, alloc::boxed::Box};
 
 const METADATA_SIZE: usize = mem::size_of::<TagHeader>() + 2 * mem::size_of::<u32>();
 

+ 7 - 9
multiboot2/src/tag.rs

@@ -57,7 +57,7 @@ impl TagHeader {
 ///   is [`ALIGNMENT`]-aligned
 #[derive(Clone, Debug, PartialEq, Eq)]
 #[repr(transparent)]
-pub(crate) struct TagBytesRef<'a>(&'a [u8]);
+pub struct TagBytesRef<'a>(&'a [u8]);
 
 impl<'a> TryFrom<&'a [u8]> for TagBytesRef<'a> {
     type Error = MemoryError;
@@ -124,18 +124,16 @@ impl GenericTag {
         let dst_len = Self::dst_len(header);
         assert_eq!(header.size as usize, Self::BASE_SIZE + dst_len);
 
-        let generic_tag: *const GenericTag =
-            ptr_meta::from_raw_parts(bytes.as_ptr().cast(), dst_len);
-        let generic_tag = unsafe { &*generic_tag };
-
-        generic_tag
+        let generic_tag: *const Self = ptr_meta::from_raw_parts(bytes.as_ptr().cast(), dst_len);
+        unsafe { &*generic_tag }
     }
 
-    pub fn header(&self) -> &TagHeader {
+    pub const fn header(&self) -> &TagHeader {
         &self.header
     }
 
-    pub fn payload(&self) -> &[u8] {
+    #[cfg(all(test, feature = "builder"))]
+    pub const fn payload(&self) -> &[u8] {
         &self.payload
     }
 
@@ -346,7 +344,7 @@ mod tests {
         // Guaranteed wrong alignment
         let unaligned_slice = &slice[3..];
         assert_eq!(
-            TagBytesRef::try_from(&unaligned_slice[..]),
+            TagBytesRef::try_from(unaligned_slice),
             Err(MemoryError::WrongAlignment)
         );
 

+ 1 - 1
multiboot2/src/test_util.rs

@@ -9,7 +9,7 @@ use core::ops::Deref;
 /// information or just the bytes for simple tags, in a manual and raw approach.
 #[cfg(test)]
 #[repr(C, align(8))]
-pub(crate) struct AlignedBytes<const N: usize>(pub [u8; N]);
+pub struct AlignedBytes<const N: usize>(pub [u8; N]);
 
 impl<const N: usize> AlignedBytes<N> {
     pub const fn new(bytes: [u8; N]) -> Self {

+ 13 - 8
multiboot2/src/util.rs

@@ -1,11 +1,14 @@
 //! Various utilities.
 
-use crate::tag::GenericTag;
-use crate::{TagHeader, TagTrait, TagType, ALIGNMENT};
+use crate::ALIGNMENT;
 use core::fmt;
 use core::fmt::{Display, Formatter};
 use core::str::Utf8Error;
-use core::{ptr, slice};
+#[cfg(feature = "alloc")]
+use {
+    crate::{TagHeader, TagTrait},
+    core::{mem, ptr},
+};
 #[cfg(feature = "builder")]
 use {alloc::alloc::Layout, alloc::boxed::Box};
 
@@ -45,13 +48,14 @@ impl core::error::Error for StringError {
 ///   without additional padding in-between. You don't need to add the bytes
 ///   for [`TagHeader`], but only additional ones.
 #[cfg(feature = "alloc")]
+#[must_use]
 pub fn new_boxed<T: TagTrait + ?Sized>(additional_bytes_slices: &[&[u8]]) -> Box<T> {
     let additional_size = additional_bytes_slices
         .iter()
         .map(|b| b.len())
         .sum::<usize>();
 
-    let size = size_of::<TagHeader>() + additional_size;
+    let size = mem::size_of::<TagHeader>() + additional_size;
     let alloc_size = increase_to_alignment(size);
     let layout = Layout::from_size_align(alloc_size, ALIGNMENT).unwrap();
     let heap_ptr = unsafe { alloc::alloc::alloc(layout) };
@@ -62,7 +66,7 @@ pub fn new_boxed<T: TagTrait + ?Sized>(additional_bytes_slices: &[&[u8]]) -> Box
         heap_ptr.cast::<u32>().add(1).write(size as u32);
     }
 
-    let mut write_offset = size_of::<TagHeader>();
+    let mut write_offset = mem::size_of::<TagHeader>();
     for &bytes in additional_bytes_slices {
         unsafe {
             let len = bytes.len();
@@ -101,8 +105,8 @@ pub const fn increase_to_alignment(size: usize) -> usize {
 #[cfg(test)]
 mod tests {
     use super::*;
-    use crate::tag::GenericTag;
-    use crate::CommandLineTag;
+    #[cfg(feature = "alloc")]
+    use {crate::tag::GenericTag, crate::CommandLineTag};
 
     #[test]
     fn test_parse_slice_as_string() {
@@ -140,6 +144,7 @@ mod tests {
     }
 
     #[test]
+    #[cfg(feature = "alloc")]
     fn test_new_boxed() {
         let tag = new_boxed::<GenericTag>(&[&[0, 1, 2, 3]]);
         assert_eq!(tag.header().typ, GenericTag::ID);
@@ -150,7 +155,7 @@ mod tests {
         assert_eq!(tag.header().typ, GenericTag::ID);
         assert_eq!(tag.payload(), &[0, 1, 2, 3]);
 
-        let tag = new_boxed::<CommandLineTag>(&["hello\0".as_bytes()]);
+        let tag = new_boxed::<CommandLineTag>(&[b"hello\0"]);
         assert_eq!(tag.cmdline(), Ok("hello"));
     }
 }