Skip to content

Commit b296673

Browse files
committed
multiboot2-header: improve code style
1 parent a9d93ee commit b296673

18 files changed

+419
-193
lines changed

multiboot2-header/Changelog.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
## Unreleased
44

5+
- **Breaking** All functions that returns something useful are now `#[must_use]`
56
- updated dependencies
6-
- MSRV is 1.75
7+
- documentation enhancements
78

89
## 0.4.0 (2024-05-01)
910

multiboot2-header/src/address.rs

+30-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{HeaderTagFlag, HeaderTagType};
1+
use crate::{HeaderTagFlag, HeaderTagHeader, HeaderTagType};
22
use core::mem::size_of;
33

44
/// This information does not need to be provided if the kernel image is in ELF
@@ -8,9 +8,7 @@ use core::mem::size_of;
88
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
99
#[repr(C)]
1010
pub struct AddressHeaderTag {
11-
typ: HeaderTagType,
12-
flags: HeaderTagFlag,
13-
size: u32,
11+
header: HeaderTagHeader,
1412
/// Contains the address corresponding to the beginning of the Multiboot2 header — the physical memory location at which the magic value is supposed to be loaded. This field serves to synchronize the mapping between OS image offsets and physical memory addresses.
1513
header_addr: u32,
1614
/// Contains the physical address of the beginning of the text segment. The offset in the OS image file at which to start loading is defined by the offset at which the header was found, minus (header_addr - load_addr). load_addr must be less than or equal to header_addr.
@@ -24,42 +22,63 @@ pub struct AddressHeaderTag {
2422
}
2523

2624
impl AddressHeaderTag {
25+
/// Constructs a new tag.
26+
#[must_use]
2727
pub const fn new(
2828
flags: HeaderTagFlag,
2929
header_addr: u32,
3030
load_addr: u32,
3131
load_end_addr: u32,
3232
bss_end_addr: u32,
3333
) -> Self {
34-
AddressHeaderTag {
35-
typ: HeaderTagType::Address,
36-
flags,
37-
size: size_of::<Self>() as u32,
34+
let header = HeaderTagHeader::new(HeaderTagType::Address, flags, size_of::<Self>() as u32);
35+
Self {
36+
header,
3837
header_addr,
3938
load_addr,
4039
load_end_addr,
4140
bss_end_addr,
4241
}
4342
}
4443

44+
/// Returns the [`HeaderTagType`].
45+
#[must_use]
4546
pub const fn typ(&self) -> HeaderTagType {
46-
self.typ
47+
self.header.typ()
4748
}
49+
50+
/// Returns the [`HeaderTagFlag`]s.
51+
#[must_use]
4852
pub const fn flags(&self) -> HeaderTagFlag {
49-
self.flags
53+
self.header.flags()
5054
}
55+
56+
/// Returns the size.
57+
#[must_use]
5158
pub const fn size(&self) -> u32 {
52-
self.size
59+
self.header.size()
5360
}
61+
62+
/// Returns the header address.
63+
#[must_use]
5464
pub const fn header_addr(&self) -> u32 {
5565
self.header_addr
5666
}
67+
68+
/// Returns the load begin address.
69+
#[must_use]
5770
pub const fn load_addr(&self) -> u32 {
5871
self.load_addr
5972
}
73+
74+
/// Returns the load end address.
75+
#[must_use]
6076
pub const fn load_end_addr(&self) -> u32 {
6177
self.load_end_addr
6278
}
79+
80+
/// Returns the bss end address.
81+
#[must_use]
6382
pub const fn bss_end_addr(&self) -> u32 {
6483
self.bss_end_addr
6584
}

multiboot2-header/src/builder/header.rs

+37-3
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ pub struct HeaderBuilder {
7272
}
7373

7474
impl HeaderBuilder {
75+
/// Creates a new builder.
76+
#[must_use]
7577
pub const fn new(arch: HeaderTagISA) -> Self {
7678
Self {
7779
arch,
@@ -98,6 +100,7 @@ impl HeaderBuilder {
98100

99101
/// Returns the expected length of the Multiboot2 header, when the
100102
/// [`Self::build`]-method gets called.
103+
#[must_use]
101104
pub fn expected_len(&self) -> usize {
102105
let base_len = size_of::<Multiboot2BasicHeader>();
103106
// size_or_up_aligned not required, because basic header length is 16 and the
@@ -159,7 +162,8 @@ impl HeaderBuilder {
159162
}
160163

161164
/// Constructs the bytes for a valid Multiboot2 header with the given properties.
162-
pub fn build(mut self) -> HeaderBytes {
165+
#[must_use]
166+
pub fn build(self) -> HeaderBytes {
163167
const ALIGN: usize = 8;
164168

165169
// PHASE 1/2: Prepare Vector
@@ -205,7 +209,7 @@ impl HeaderBuilder {
205209
}
206210

207211
/// Helper method that adds all the tags to the given vector.
208-
fn build_add_tags(&mut self, bytes: &mut Vec<u8>) {
212+
fn build_add_tags(&self, bytes: &mut Vec<u8>) {
209213
Self::build_add_bytes(
210214
bytes,
211215
// important that we write the correct expected length into the header!
@@ -247,46 +251,76 @@ impl HeaderBuilder {
247251
}
248252

249253
// clippy thinks this can be a const fn but the compiler denies it
250-
#[allow(clippy::missing_const_for_fn)]
254+
// #[allow(clippy::missing_const_for_fn)]
255+
/// Adds information requests from the
256+
/// [`InformationRequestHeaderTagBuilder`] to the builder.
257+
#[must_use]
251258
pub fn information_request_tag(
252259
mut self,
253260
information_request_tag: InformationRequestHeaderTagBuilder,
254261
) -> Self {
255262
self.information_request_tag = Some(information_request_tag);
256263
self
257264
}
265+
266+
/// Adds a [`AddressHeaderTag`] to the builder.
267+
#[must_use]
258268
pub const fn address_tag(mut self, address_tag: AddressHeaderTag) -> Self {
259269
self.address_tag = Some(address_tag);
260270
self
261271
}
272+
273+
/// Adds a [`EntryAddressHeaderTag`] to the builder.
274+
#[must_use]
262275
pub const fn entry_tag(mut self, entry_tag: EntryAddressHeaderTag) -> Self {
263276
self.entry_tag = Some(entry_tag);
264277
self
265278
}
279+
280+
/// Adds a [`ConsoleHeaderTag`] to the builder.
281+
#[must_use]
266282
pub const fn console_tag(mut self, console_tag: ConsoleHeaderTag) -> Self {
267283
self.console_tag = Some(console_tag);
268284
self
269285
}
286+
287+
/// Adds a [`FramebufferHeaderTag`] to the builder.
288+
#[must_use]
270289
pub const fn framebuffer_tag(mut self, framebuffer_tag: FramebufferHeaderTag) -> Self {
271290
self.framebuffer_tag = Some(framebuffer_tag);
272291
self
273292
}
293+
294+
/// Adds a [`ModuleAlignHeaderTag`] to the builder.
295+
#[must_use]
274296
pub const fn module_align_tag(mut self, module_align_tag: ModuleAlignHeaderTag) -> Self {
275297
self.module_align_tag = Some(module_align_tag);
276298
self
277299
}
300+
301+
/// Adds a [`EfiBootServiceHeaderTag`] to the builder.
302+
#[must_use]
278303
pub const fn efi_bs_tag(mut self, efi_bs_tag: EfiBootServiceHeaderTag) -> Self {
279304
self.efi_bs_tag = Some(efi_bs_tag);
280305
self
281306
}
307+
308+
/// Adds a [`EntryEfi32HeaderTag`] to the builder.
309+
#[must_use]
282310
pub const fn efi_32_tag(mut self, efi_32_tag: EntryEfi32HeaderTag) -> Self {
283311
self.efi_32_tag = Some(efi_32_tag);
284312
self
285313
}
314+
315+
/// Adds a [`EntryEfi64HeaderTag`] to the builder.
316+
#[must_use]
286317
pub const fn efi_64_tag(mut self, efi_64_tag: EntryEfi64HeaderTag) -> Self {
287318
self.efi_64_tag = Some(efi_64_tag);
288319
self
289320
}
321+
322+
/// Adds a [`RelocatableHeaderTag`] to the builder.
323+
#[must_use]
290324
pub const fn relocatable_tag(mut self, relocatable_tag: RelocatableHeaderTag) -> Self {
291325
self.relocatable_tag = Some(relocatable_tag);
292326
self

multiboot2-header/src/builder/information_request.rs

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub struct InformationRequestHeaderTagBuilder {
2222
#[cfg(feature = "builder")]
2323
impl InformationRequestHeaderTagBuilder {
2424
/// New builder.
25+
#[must_use]
2526
pub const fn new(flag: HeaderTagFlag) -> Self {
2627
Self {
2728
irs: BTreeSet::new(),
@@ -31,19 +32,22 @@ impl InformationRequestHeaderTagBuilder {
3132

3233
/// Returns the expected length of the information request tag,
3334
/// when the `build`-method gets called.
35+
#[must_use]
3436
pub fn expected_len(&self) -> usize {
3537
let basic_header_size = size_of::<InformationRequestHeaderTag<0>>();
3638
let req_tags_size = self.irs.len() * size_of::<MbiTagTypeId>();
3739
basic_header_size + req_tags_size
3840
}
3941

4042
/// Adds an [`MbiTagType`] to the information request.
43+
#[must_use]
4144
pub fn add_ir(mut self, tag: MbiTagType) -> Self {
4245
self.irs.insert(tag);
4346
self
4447
}
4548

4649
/// Adds multiple [`MbiTagType`] to the information request.
50+
#[must_use]
4751
pub fn add_irs(mut self, tags: &[MbiTagType]) -> Self {
4852
self.irs.extend(tags);
4953
self

multiboot2-header/src/builder/traits.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use core::mem::size_of;
1111
/// Trait for all tags that helps to create a byte array from the tag.
1212
/// Useful in builders to construct a byte vector that
1313
/// represents the Multiboot2 header with all its tags.
14-
pub(crate) trait StructAsBytes: Sized {
14+
pub trait StructAsBytes: Sized {
1515
/// Returns the size in bytes of the struct, as known during compile
1616
/// time. This doesn't use read the "size" field of tags.
1717
fn byte_size(&self) -> usize {

multiboot2-header/src/console.rs

+22-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{HeaderTagFlag, HeaderTagType};
1+
use crate::{HeaderTagFlag, HeaderTagHeader, HeaderTagType};
22
use core::mem::size_of;
33

44
/// Possible flags for [`ConsoleHeaderTag`].
@@ -16,31 +16,42 @@ pub enum ConsoleHeaderTagFlags {
1616
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
1717
#[repr(C)]
1818
pub struct ConsoleHeaderTag {
19-
typ: HeaderTagType,
20-
flags: HeaderTagFlag,
21-
size: u32,
19+
header: HeaderTagHeader,
2220
console_flags: ConsoleHeaderTagFlags,
2321
}
2422

2523
impl ConsoleHeaderTag {
24+
/// Constructs a new tag.
25+
#[must_use]
2626
pub const fn new(flags: HeaderTagFlag, console_flags: ConsoleHeaderTagFlags) -> Self {
27-
ConsoleHeaderTag {
28-
typ: HeaderTagType::ConsoleFlags,
29-
flags,
30-
size: size_of::<Self>() as u32,
27+
let header =
28+
HeaderTagHeader::new(HeaderTagType::ConsoleFlags, flags, size_of::<Self>() as u32);
29+
Self {
30+
header,
3131
console_flags,
3232
}
3333
}
3434

35+
/// Returns the [`HeaderTagType`].
36+
#[must_use]
3537
pub const fn typ(&self) -> HeaderTagType {
36-
self.typ
38+
self.header.typ()
3739
}
40+
41+
/// Returns the [`HeaderTagFlag`]s.
42+
#[must_use]
3843
pub const fn flags(&self) -> HeaderTagFlag {
39-
self.flags
44+
self.header.flags()
4045
}
46+
47+
/// Returns the size.
48+
#[must_use]
4149
pub const fn size(&self) -> u32 {
42-
self.size
50+
self.header.size()
4351
}
52+
53+
/// Returns the [`ConsoleHeaderTagFlags`].
54+
#[must_use]
4455
pub const fn console_flags(&self) -> ConsoleHeaderTagFlags {
4556
self.console_flags
4657
}

multiboot2-header/src/end.rs

+21-14
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
use crate::{HeaderTagFlag, HeaderTagType};
1+
use crate::{HeaderTagFlag, HeaderTagHeader, HeaderTagType};
22
use core::mem::size_of;
33

44
/// Terminates a list of optional tags in a Multiboot2 header.
55
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
66
#[repr(C)]
77
pub struct EndHeaderTag {
8-
// u16 value
9-
typ: HeaderTagType,
10-
// u16 value
11-
flags: HeaderTagFlag,
12-
size: u32,
8+
header: HeaderTagHeader,
139
}
1410

1511
impl Default for EndHeaderTag {
@@ -19,22 +15,33 @@ impl Default for EndHeaderTag {
1915
}
2016

2117
impl EndHeaderTag {
18+
/// Constructs a new tag.
19+
#[must_use]
2220
pub const fn new() -> Self {
23-
EndHeaderTag {
24-
typ: HeaderTagType::End,
25-
flags: HeaderTagFlag::Required,
26-
size: size_of::<Self>() as u32,
27-
}
21+
let header = HeaderTagHeader::new(
22+
HeaderTagType::EntryAddress,
23+
HeaderTagFlag::Required,
24+
size_of::<Self>() as u32,
25+
);
26+
Self { header }
2827
}
2928

29+
/// Returns the [`HeaderTagType`].
30+
#[must_use]
3031
pub const fn typ(&self) -> HeaderTagType {
31-
self.typ
32+
self.header.typ()
3233
}
34+
35+
/// Returns the [`HeaderTagFlag`]s.
36+
#[must_use]
3337
pub const fn flags(&self) -> HeaderTagFlag {
34-
self.flags
38+
self.header.flags()
3539
}
40+
41+
/// Returns the size.
42+
#[must_use]
3643
pub const fn size(&self) -> u32 {
37-
self.size
44+
self.header.size()
3845
}
3946
}
4047

0 commit comments

Comments
 (0)