Skip to content

Commit fb6c12b

Browse files
committed
multiboot2: fix all warnings of rustc and clippy
1 parent 78d42b8 commit fb6c12b

File tree

9 files changed

+69
-65
lines changed

9 files changed

+69
-65
lines changed

multiboot2/src/builder/information.rs

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use crate::{
77
EFISdt32Tag, EFISdt64Tag, ElfSectionsTag, EndTag, FramebufferTag, ImageLoadPhysAddrTag,
88
MemoryMapTag, ModuleTag, RsdpV1Tag, RsdpV2Tag, SmbiosTag, TagTrait, TagType, ALIGNMENT,
99
};
10-
use alloc::boxed::Box;
1110
use alloc::vec::Vec;
1211
use core::fmt::{Display, Formatter};
1312
use core::mem::size_of;
@@ -200,32 +199,32 @@ impl InformationBuilder {
200199

201200
/// Adds a 'basic memory information' tag (represented by [`BasicMemoryInfoTag`]) to the builder.
202201
#[must_use]
203-
pub fn basic_memory_info_tag(self, tag: BasicMemoryInfoTag) -> Self {
204-
self.add_tag(&tag).unwrap()
202+
pub fn basic_memory_info_tag(self, tag: &BasicMemoryInfoTag) -> Self {
203+
self.add_tag(tag).unwrap()
205204
}
206205

207206
/// Adds a 'bootloader name' tag (represented by [`BootLoaderNameTag`]) to the builder.
208207
#[must_use]
209-
pub fn bootloader_name_tag(self, tag: Box<BootLoaderNameTag>) -> Self {
210-
self.add_tag(&*tag).unwrap()
208+
pub fn bootloader_name_tag(self, tag: &BootLoaderNameTag) -> Self {
209+
self.add_tag(tag).unwrap()
211210
}
212211

213212
/// Adds a 'command line' tag (represented by [`CommandLineTag`]) to the builder.
214213
#[must_use]
215-
pub fn command_line_tag(self, tag: Box<CommandLineTag>) -> Self {
216-
self.add_tag(&*tag).unwrap()
214+
pub fn command_line_tag(self, tag: &CommandLineTag) -> Self {
215+
self.add_tag(tag).unwrap()
217216
}
218217

219218
/// Adds a 'EFI 32-bit system table pointer' tag (represented by [`EFISdt32Tag`]) to the builder.
220219
#[must_use]
221-
pub fn efisdt32_tag(self, tag: EFISdt32Tag) -> Self {
222-
self.add_tag(&tag).unwrap()
220+
pub fn efisdt32_tag(self, tag: &EFISdt32Tag) -> Self {
221+
self.add_tag(tag).unwrap()
223222
}
224223

225224
/// Adds a 'EFI 64-bit system table pointer' tag (represented by [`EFISdt64Tag`]) to the builder.
226225
#[must_use]
227-
pub fn efisdt64_tag(self, tag: EFISdt64Tag) -> Self {
228-
self.add_tag(&tag).unwrap()
226+
pub fn efisdt64_tag(self, tag: &EFISdt64Tag) -> Self {
227+
self.add_tag(tag).unwrap()
229228
}
230229

231230
/// Adds a 'EFI boot services not terminated' tag (represented by [`EFIBootServicesNotExitedTag`]) to the builder.
@@ -236,69 +235,69 @@ impl InformationBuilder {
236235

237236
/// Adds a 'EFI 32-bit image handle pointer' tag (represented by [`EFIImageHandle32Tag`]) to the builder.
238237
#[must_use]
239-
pub fn efi_image_handle32(self, tag: EFIImageHandle32Tag) -> Self {
240-
self.add_tag(&tag).unwrap()
238+
pub fn efi_image_handle32(self, tag: &EFIImageHandle32Tag) -> Self {
239+
self.add_tag(tag).unwrap()
241240
}
242241

243242
/// Adds a 'EFI 64-bit image handle pointer' tag (represented by [`EFIImageHandle64Tag`]) to the builder.
244243
#[must_use]
245-
pub fn efi_image_handle64(self, tag: EFIImageHandle64Tag) -> Self {
246-
self.add_tag(&tag).unwrap()
244+
pub fn efi_image_handle64(self, tag: &EFIImageHandle64Tag) -> Self {
245+
self.add_tag(tag).unwrap()
247246
}
248247

249248
/// Adds a 'EFI Memory map' tag (represented by [`EFIMemoryMapTag`]) to the builder.
250249
#[must_use]
251-
pub fn efi_memory_map_tag(self, tag: Box<EFIMemoryMapTag>) -> Self {
252-
self.add_tag(&*tag).unwrap()
250+
pub fn efi_memory_map_tag(self, tag: &EFIMemoryMapTag) -> Self {
251+
self.add_tag(tag).unwrap()
253252
}
254253

255254
/// Adds a 'ELF-Symbols' tag (represented by [`ElfSectionsTag`]) to the builder.
256255
#[must_use]
257-
pub fn elf_sections_tag(self, tag: Box<ElfSectionsTag>) -> Self {
258-
self.add_tag(&*tag).unwrap()
256+
pub fn elf_sections_tag(self, tag: &ElfSectionsTag) -> Self {
257+
self.add_tag(tag).unwrap()
259258
}
260259

261260
/// Adds a 'Framebuffer info' tag (represented by [`FramebufferTag`]) to the builder.
262261
#[must_use]
263-
pub fn framebuffer_tag(self, tag: Box<FramebufferTag>) -> Self {
264-
self.add_tag(&*tag).unwrap()
262+
pub fn framebuffer_tag(self, tag: &FramebufferTag) -> Self {
263+
self.add_tag(tag).unwrap()
265264
}
266265

267266
/// Adds a 'Image load base physical address' tag (represented by [`ImageLoadPhysAddrTag`]) to the builder.
268267
#[must_use]
269-
pub fn image_load_addr(self, tag: ImageLoadPhysAddrTag) -> Self {
270-
self.add_tag(&tag).unwrap()
268+
pub fn image_load_addr(self, tag: &ImageLoadPhysAddrTag) -> Self {
269+
self.add_tag(tag).unwrap()
271270
}
272271

273272
/// Adds a (*none EFI*) 'memory map' tag (represented by [`MemoryMapTag`]) to the builder.
274273
#[must_use]
275-
pub fn memory_map_tag(self, tag: Box<MemoryMapTag>) -> Self {
276-
self.add_tag(&*tag).unwrap()
274+
pub fn memory_map_tag(self, tag: &MemoryMapTag) -> Self {
275+
self.add_tag(tag).unwrap()
277276
}
278277

279278
/// Adds a 'Modules' tag (represented by [`ModuleTag`]) to the builder.
280279
/// This tag can occur multiple times in boot information.
281280
#[must_use]
282-
pub fn add_module_tag(self, tag: Box<ModuleTag>) -> Self {
283-
self.add_tag(&*tag).unwrap()
281+
pub fn add_module_tag(self, tag: &ModuleTag) -> Self {
282+
self.add_tag(tag).unwrap()
284283
}
285284

286285
/// Adds a 'ACPI old RSDP' tag (represented by [`RsdpV1Tag`]) to the builder.
287286
#[must_use]
288-
pub fn rsdp_v1_tag(self, tag: RsdpV1Tag) -> Self {
289-
self.add_tag(&tag).unwrap()
287+
pub fn rsdp_v1_tag(self, tag: &RsdpV1Tag) -> Self {
288+
self.add_tag(tag).unwrap()
290289
}
291290

292291
/// Adds a 'ACPI new RSDP' tag (represented by [`RsdpV2Tag`]) to the builder.
293292
#[must_use]
294-
pub fn rsdp_v2_tag(self, tag: RsdpV2Tag) -> Self {
295-
self.add_tag(&tag).unwrap()
293+
pub fn rsdp_v2_tag(self, tag: &RsdpV2Tag) -> Self {
294+
self.add_tag(tag).unwrap()
296295
}
297296

298297
/// Adds a 'SMBIOS tables' tag (represented by [`SmbiosTag`]) to the builder.
299298
#[must_use]
300-
pub fn smbios_tag(self, tag: Box<SmbiosTag>) -> Self {
301-
self.add_tag(&*tag).unwrap()
299+
pub fn smbios_tag(self, tag: &SmbiosTag) -> Self {
300+
self.add_tag(tag).unwrap()
302301
}
303302

304303
const fn tag_is_allowed_multiple_times(tag_type: TagType) -> bool {
@@ -322,18 +321,18 @@ mod tests {
322321
assert_eq!(builder.expected_len(), expected_len);
323322

324323
// the most simple tag
325-
builder = builder.basic_memory_info_tag(BasicMemoryInfoTag::new(640, 7 * 1024));
324+
builder = builder.basic_memory_info_tag(&BasicMemoryInfoTag::new(640, 7 * 1024));
326325
expected_len += 16;
327326
assert_eq!(builder.expected_len(), expected_len);
328327
// a tag that has a dynamic size
329-
builder = builder.command_line_tag(CommandLineTag::new("test"));
328+
builder = builder.command_line_tag(&CommandLineTag::new("test"));
330329
expected_len += 8 + 5 + 3; // padding
331330
assert_eq!(builder.expected_len(), expected_len);
332331
// many modules
333-
builder = builder.add_module_tag(ModuleTag::new(0, 1234, "module1"));
332+
builder = builder.add_module_tag(&ModuleTag::new(0, 1234, "module1"));
334333
expected_len += 16 + 8;
335334
assert_eq!(builder.expected_len(), expected_len);
336-
builder = builder.add_module_tag(ModuleTag::new(5678, 6789, "module2"));
335+
builder = builder.add_module_tag(&ModuleTag::new(5678, 6789, "module2"));
337336
expected_len += 16 + 8;
338337
assert_eq!(builder.expected_len(), expected_len);
339338

multiboot2/src/command_line.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use core::fmt::{Debug, Formatter};
66
use core::mem;
77
use core::str;
88
#[cfg(feature = "builder")]
9-
use {crate::new_boxed, alloc::boxed::Box, alloc::vec::Vec};
9+
use {crate::new_boxed, alloc::boxed::Box};
1010

1111
const METADATA_SIZE: usize = mem::size_of::<TagHeader>();
1212

multiboot2/src/elf_sections.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::{TagHeader, TagTrait, TagType};
44
use core::fmt::{Debug, Formatter};
5-
use core::marker::{PhantomData, PhantomPinned};
5+
use core::marker::PhantomData;
66
use core::mem;
77
use core::str::Utf8Error;
88
#[cfg(feature = "builder")]
@@ -35,7 +35,7 @@ impl ElfSectionsTag {
3535
}
3636

3737
/// Get an iterator of loaded ELF sections.
38-
pub(crate) fn sections(&self) -> ElfSectionIter {
38+
pub(crate) const fn sections(&self) -> ElfSectionIter {
3939
let string_section_offset = (self.shndx * self.entry_size) as isize;
4040
let string_section_ptr =
4141
unsafe { self.sections.as_ptr().offset(string_section_offset) as *const _ };
@@ -44,7 +44,7 @@ impl ElfSectionsTag {
4444
remaining_sections: self.number_of_sections,
4545
entry_size: self.entry_size,
4646
string_section: string_section_ptr,
47-
_phantom_data: PhantomData::default(),
47+
_phantom_data: PhantomData,
4848
}
4949
}
5050
}
@@ -90,7 +90,7 @@ impl<'a> Iterator for ElfSectionIter<'a> {
9090
inner: self.current_section,
9191
string_section: self.string_section,
9292
entry_size: self.entry_size,
93-
_phantom: PhantomData::default(),
93+
_phantom: PhantomData,
9494
};
9595

9696
self.current_section = unsafe { self.current_section.offset(self.entry_size as isize) };

multiboot2/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// now allow a few rules which are denied by the above statement
1313
// --> They are either ridiculous, not necessary, or we can't fix them.
1414
#![allow(clippy::multiple_crate_versions)]
15-
// #![deny(missing_docs)]
15+
#![deny(missing_docs)]
1616
#![deny(missing_debug_implementations)]
1717
#![deny(rustdoc::all)]
1818
// --- END STYLE CHECKS ---
@@ -100,7 +100,9 @@ pub use smbios::SmbiosTag;
100100
pub use tag::TagHeader;
101101
pub use tag_trait::TagTrait;
102102
pub use tag_type::{TagType, TagTypeId};
103-
pub use util::{new_boxed, parse_slice_as_string, StringError};
103+
#[cfg(feature = "alloc")]
104+
pub use util::new_boxed;
105+
pub use util::{parse_slice_as_string, StringError};
104106
pub use vbe_info::{
105107
VBECapabilities, VBEControlInfo, VBEDirectColorAttributes, VBEField, VBEInfoTag,
106108
VBEMemoryModel, VBEModeAttributes, VBEModeInfo, VBEWindowAttributes,

multiboot2/src/memory_map.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl MemoryMapTag {
4343
let entry_version = 0_u32.to_ne_bytes();
4444
let areas = {
4545
let ptr = areas.as_ptr().cast::<u8>();
46-
let len = areas.len() * size_of::<MemoryArea>();
46+
let len = mem::size_of_val(areas);
4747
unsafe { slice::from_raw_parts(ptr, len) }
4848
};
4949
new_boxed(&[&entry_size, &entry_version, areas])
@@ -322,7 +322,7 @@ impl EFIMemoryMapTag {
322322
pub fn new_from_descs(descs: &[EFIMemoryDesc]) -> Box<Self> {
323323
let efi_mmap = {
324324
let ptr = descs.as_ptr().cast::<u8>();
325-
let len = descs.len() * size_of::<EFIMemoryDesc>();
325+
let len = mem::size_of_val(descs);
326326
unsafe { slice::from_raw_parts(ptr, len) }
327327
};
328328

multiboot2/src/module.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{parse_slice_as_string, StringError, TagTrait, TagType};
55
use core::fmt::{Debug, Formatter};
66
use core::mem;
77
#[cfg(feature = "builder")]
8-
use {crate::new_boxed, alloc::boxed::Box, alloc::vec::Vec};
8+
use {crate::new_boxed, alloc::boxed::Box};
99

1010
const METADATA_SIZE: usize = mem::size_of::<TagHeader>() + 2 * mem::size_of::<u32>();
1111

multiboot2/src/tag.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl TagHeader {
5757
/// is [`ALIGNMENT`]-aligned
5858
#[derive(Clone, Debug, PartialEq, Eq)]
5959
#[repr(transparent)]
60-
pub(crate) struct TagBytesRef<'a>(&'a [u8]);
60+
pub struct TagBytesRef<'a>(&'a [u8]);
6161

6262
impl<'a> TryFrom<&'a [u8]> for TagBytesRef<'a> {
6363
type Error = MemoryError;
@@ -124,18 +124,16 @@ impl GenericTag {
124124
let dst_len = Self::dst_len(header);
125125
assert_eq!(header.size as usize, Self::BASE_SIZE + dst_len);
126126

127-
let generic_tag: *const GenericTag =
128-
ptr_meta::from_raw_parts(bytes.as_ptr().cast(), dst_len);
129-
let generic_tag = unsafe { &*generic_tag };
130-
131-
generic_tag
127+
let generic_tag: *const Self = ptr_meta::from_raw_parts(bytes.as_ptr().cast(), dst_len);
128+
unsafe { &*generic_tag }
132129
}
133130

134-
pub fn header(&self) -> &TagHeader {
131+
pub const fn header(&self) -> &TagHeader {
135132
&self.header
136133
}
137134

138-
pub fn payload(&self) -> &[u8] {
135+
#[cfg(all(test, feature = "builder"))]
136+
pub const fn payload(&self) -> &[u8] {
139137
&self.payload
140138
}
141139

@@ -346,7 +344,7 @@ mod tests {
346344
// Guaranteed wrong alignment
347345
let unaligned_slice = &slice[3..];
348346
assert_eq!(
349-
TagBytesRef::try_from(&unaligned_slice[..]),
347+
TagBytesRef::try_from(unaligned_slice),
350348
Err(MemoryError::WrongAlignment)
351349
);
352350

multiboot2/src/test_util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use core::ops::Deref;
99
/// information or just the bytes for simple tags, in a manual and raw approach.
1010
#[cfg(test)]
1111
#[repr(C, align(8))]
12-
pub(crate) struct AlignedBytes<const N: usize>(pub [u8; N]);
12+
pub struct AlignedBytes<const N: usize>(pub [u8; N]);
1313

1414
impl<const N: usize> AlignedBytes<N> {
1515
pub const fn new(bytes: [u8; N]) -> Self {

multiboot2/src/util.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
//! Various utilities.
22
3-
use crate::tag::GenericTag;
4-
use crate::{TagHeader, TagTrait, TagType, ALIGNMENT};
3+
use crate::ALIGNMENT;
54
use core::fmt;
65
use core::fmt::{Display, Formatter};
76
use core::str::Utf8Error;
8-
use core::{ptr, slice};
7+
#[cfg(feature = "alloc")]
8+
use {
9+
crate::{TagHeader, TagTrait},
10+
core::{mem, ptr},
11+
};
912
#[cfg(feature = "builder")]
1013
use {alloc::alloc::Layout, alloc::boxed::Box};
1114

@@ -45,13 +48,14 @@ impl core::error::Error for StringError {
4548
/// without additional padding in-between. You don't need to add the bytes
4649
/// for [`TagHeader`], but only additional ones.
4750
#[cfg(feature = "alloc")]
51+
#[must_use]
4852
pub fn new_boxed<T: TagTrait + ?Sized>(additional_bytes_slices: &[&[u8]]) -> Box<T> {
4953
let additional_size = additional_bytes_slices
5054
.iter()
5155
.map(|b| b.len())
5256
.sum::<usize>();
5357

54-
let size = size_of::<TagHeader>() + additional_size;
58+
let size = mem::size_of::<TagHeader>() + additional_size;
5559
let alloc_size = increase_to_alignment(size);
5660
let layout = Layout::from_size_align(alloc_size, ALIGNMENT).unwrap();
5761
let heap_ptr = unsafe { alloc::alloc::alloc(layout) };
@@ -62,7 +66,7 @@ pub fn new_boxed<T: TagTrait + ?Sized>(additional_bytes_slices: &[&[u8]]) -> Box
6266
heap_ptr.cast::<u32>().add(1).write(size as u32);
6367
}
6468

65-
let mut write_offset = size_of::<TagHeader>();
69+
let mut write_offset = mem::size_of::<TagHeader>();
6670
for &bytes in additional_bytes_slices {
6771
unsafe {
6872
let len = bytes.len();
@@ -101,8 +105,8 @@ pub const fn increase_to_alignment(size: usize) -> usize {
101105
#[cfg(test)]
102106
mod tests {
103107
use super::*;
104-
use crate::tag::GenericTag;
105-
use crate::CommandLineTag;
108+
#[cfg(feature = "alloc")]
109+
use {crate::tag::GenericTag, crate::CommandLineTag};
106110

107111
#[test]
108112
fn test_parse_slice_as_string() {
@@ -140,6 +144,7 @@ mod tests {
140144
}
141145

142146
#[test]
147+
#[cfg(feature = "alloc")]
143148
fn test_new_boxed() {
144149
let tag = new_boxed::<GenericTag>(&[&[0, 1, 2, 3]]);
145150
assert_eq!(tag.header().typ, GenericTag::ID);
@@ -150,7 +155,7 @@ mod tests {
150155
assert_eq!(tag.header().typ, GenericTag::ID);
151156
assert_eq!(tag.payload(), &[0, 1, 2, 3]);
152157

153-
let tag = new_boxed::<CommandLineTag>(&["hello\0".as_bytes()]);
158+
let tag = new_boxed::<CommandLineTag>(&[b"hello\0"]);
154159
assert_eq!(tag.cmdline(), Ok("hello"));
155160
}
156161
}

0 commit comments

Comments
 (0)