Skip to content

Commit 0f6c6a8

Browse files
committed
multiboot2-header: no_std works + builder-feature
1 parent 8adc3e0 commit 0f6c6a8

14 files changed

+59
-30
lines changed

multiboot2-header/src/address.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{HeaderTagFlag, HeaderTagType, StructAsBytes};
1+
use crate::{HeaderTagFlag, HeaderTagType};
22
use core::mem::size_of;
33

44
/// This information does not need to be provided if the kernel image is in ELF
@@ -65,4 +65,5 @@ impl AddressHeaderTag {
6565
}
6666
}
6767

68-
impl StructAsBytes for AddressHeaderTag {}
68+
#[cfg(feature = "builder")]
69+
impl crate::StructAsBytes for AddressHeaderTag {}

multiboot2-header/src/console.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{HeaderTagFlag, HeaderTagType, StructAsBytes};
1+
use crate::{HeaderTagFlag, HeaderTagType};
22
use core::mem::size_of;
33

44
/// Possible flags for [`ConsoleHeaderTag`].
@@ -46,7 +46,8 @@ impl ConsoleHeaderTag {
4646
}
4747
}
4848

49-
impl StructAsBytes for ConsoleHeaderTag {}
49+
#[cfg(feature = "builder")]
50+
impl crate::StructAsBytes for ConsoleHeaderTag {}
5051

5152
#[cfg(test)]
5253
mod tests {

multiboot2-header/src/end.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{HeaderTagFlag, HeaderTagType, StructAsBytes};
1+
use crate::{HeaderTagFlag, HeaderTagType};
22
use core::mem::size_of;
33

44
/// Terminates a list of optional tags
@@ -33,4 +33,5 @@ impl EndHeaderTag {
3333
}
3434
}
3535

36-
impl StructAsBytes for EndHeaderTag {}
36+
#[cfg(feature = "builder")]
37+
impl crate::StructAsBytes for EndHeaderTag {}

multiboot2-header/src/entry_efi_32.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{HeaderTagFlag, HeaderTagType, StructAsBytes};
1+
use crate::{HeaderTagFlag, HeaderTagType};
22
use core::fmt;
33
use core::fmt::{Debug, Formatter};
44
use core::mem::size_of;
@@ -50,4 +50,5 @@ impl Debug for EntryEfi32HeaderTag {
5050
}
5151
}
5252

53-
impl StructAsBytes for EntryEfi32HeaderTag {}
53+
#[cfg(feature = "builder")]
54+
impl crate::StructAsBytes for EntryEfi32HeaderTag {}

multiboot2-header/src/entry_efi_64.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{HeaderTagFlag, HeaderTagType, StructAsBytes};
1+
use crate::{HeaderTagFlag, HeaderTagType};
22
use core::fmt;
33
use core::fmt::{Debug, Formatter};
44
use core::mem::size_of;
@@ -50,4 +50,5 @@ impl Debug for EntryEfi64HeaderTag {
5050
}
5151
}
5252

53-
impl StructAsBytes for EntryEfi64HeaderTag {}
53+
#[cfg(feature = "builder")]
54+
impl crate::StructAsBytes for EntryEfi64HeaderTag {}

multiboot2-header/src/entry_header.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{HeaderTagFlag, HeaderTagType, StructAsBytes};
1+
use crate::{HeaderTagFlag, HeaderTagType};
22
use core::fmt;
33
use core::fmt::{Debug, Formatter};
44
use core::mem::size_of;
@@ -50,4 +50,5 @@ impl Debug for EntryHeaderTag {
5050
}
5151
}
5252

53-
impl StructAsBytes for EntryHeaderTag {}
53+
#[cfg(feature = "builder")]
54+
impl crate::StructAsBytes for EntryHeaderTag {}

multiboot2-header/src/framebuffer.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{HeaderTagFlag, HeaderTagType, StructAsBytes};
1+
use crate::{HeaderTagFlag, HeaderTagType};
22
use core::mem::size_of;
33

44
/// Specifies the preferred graphics mode. If this tag
@@ -48,4 +48,5 @@ impl FramebufferHeaderTag {
4848
}
4949
}
5050

51-
impl StructAsBytes for FramebufferHeaderTag {}
51+
#[cfg(feature = "builder")]
52+
impl crate::StructAsBytes for FramebufferHeaderTag {}

multiboot2-header/src/header/builder.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::{
77
ModuleAlignHeaderTag, Multiboot2BasicHeader, RelocatableHeaderTag, StructAsBytes,
88
};
99
use core::mem::size_of;
10+
use alloc::vec::Vec;
1011

1112
/// Builder to construct a valid Multiboot2 header dynamically at runtime.
1213
/// The tags will appear in the order of their corresponding enumeration,

multiboot2-header/src/header/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
pub mod builder;
55

66
pub use self::builder::*;
7-
use crate::{AddressHeaderTag, InformationRequestHeaderTag, RelocatableHeaderTag, StructAsBytes};
7+
use crate::{AddressHeaderTag, InformationRequestHeaderTag, RelocatableHeaderTag};
88
use crate::{ConsoleHeaderTag, EntryHeaderTag};
99
use crate::{EfiBootServiceHeaderTag, FramebufferHeaderTag};
1010
use crate::{EndHeaderTag, HeaderTagType};
@@ -194,7 +194,8 @@ impl Debug for Multiboot2BasicHeader {
194194
}
195195
}
196196

197-
impl StructAsBytes for Multiboot2BasicHeader {}
197+
#[cfg(feature = "builder")]
198+
impl crate::StructAsBytes for Multiboot2BasicHeader {}
198199

199200
/// Iterator over all tags of a Multiboot2 header. The number of items is derived
200201
/// by the size/length of the header.

multiboot2-header/src/information_request.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
use crate::{HeaderTagFlag, MbiTagType};
2-
use crate::{HeaderTagType, StructAsBytes};
2+
use crate::{HeaderTagType};
33
use core::fmt;
44
use core::fmt::{Debug, Formatter};
55
use core::marker::PhantomData;
66
use core::mem::size_of;
7-
use std::collections::HashSet;
7+
#[cfg(feature = "builder")]
8+
use alloc::vec::Vec;
9+
#[cfg(feature = "builder")]
10+
use alloc::collections::BTreeSet;
11+
#[cfg(feature = "builder")]
12+
use crate::StructAsBytes;
813

914
/// Specifies what specific tag types the bootloader should provide
1015
/// inside the mbi.
@@ -87,21 +92,24 @@ impl<const N: usize> Debug for InformationRequestHeaderTag<N> {
8792
}
8893
}
8994

90-
impl<const N: usize> StructAsBytes for InformationRequestHeaderTag<N> {}
95+
#[cfg(feature = "builder")]
96+
impl<const N: usize> crate::StructAsBytes for InformationRequestHeaderTag<N> {}
9197

9298
/// Helper to build the dynamically sized [`InformationRequestHeaderTag`]
9399
/// at runtime.
94100
#[derive(Debug)]
101+
#[cfg(feature = "builder")]
95102
pub struct InformationRequestHeaderTagBuilder {
96103
flag: HeaderTagFlag,
97-
irs: HashSet<MbiTagType>,
104+
irs: BTreeSet<MbiTagType>,
98105
}
99106

107+
#[cfg(feature = "builder")]
100108
impl InformationRequestHeaderTagBuilder {
101109
/// New builder.
102110
pub fn new(flag: HeaderTagFlag) -> Self {
103111
Self {
104-
irs: HashSet::new(),
112+
irs: BTreeSet::new(),
105113
flag,
106114
}
107115
}

multiboot2-header/src/lib.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,20 @@
3030
//!
3131
//! ```
3232
33+
#![no_std]
3334
#![deny(rustdoc::all)]
3435
#![allow(rustdoc::missing_doc_code_examples)]
3536
#![deny(clippy::all)]
3637
#![deny(clippy::missing_const_for_fn)]
3738
#![deny(missing_debug_implementations)]
3839

40+
#[cfg(feature = "builder")]
41+
extern crate alloc;
42+
43+
#[cfg_attr(test, macro_use)]
44+
#[cfg(test)]
45+
extern crate std;
46+
3947
#[cfg_attr(test, macro_use)]
4048
#[cfg(test)]
4149
pub(crate) mod test_utils;
@@ -71,11 +79,12 @@ pub use self::uefi_bs::*;
7179
/// Re-export of [`multiboot2::TagType`] from `multiboot2`-crate as `MbiTagType`, i.e. tags that
7280
/// describe the entries in the Multiboot2 Information Structure (MBI).
7381
pub use multiboot2::TagType as MbiTagType;
74-
use std::mem::size_of;
82+
use core::mem::size_of;
7583

7684
/// Trait for all tags that creates a byte array from the tag.
7785
/// Useful in builders to construct a byte vector that
7886
/// represents the Multiboot2 header with all its tags.
87+
#[cfg(feature = "builder")]
7988
pub(crate) trait StructAsBytes: Sized {
8089
/// Returns the size in bytes of the struct, as known during compile
8190
/// time. This doesn't use read the "size" field of tags.
@@ -90,9 +99,9 @@ pub(crate) trait StructAsBytes: Sized {
9099

91100
/// Returns the structure as a vector of its bytes.
92101
/// The length is determined by [`size`].
93-
fn struct_as_bytes(&self) -> Vec<u8> {
102+
fn struct_as_bytes(&self) -> alloc::vec::Vec<u8> {
94103
let ptr = self.as_ptr();
95-
let mut vec = Vec::with_capacity(self.byte_size());
104+
let mut vec = alloc::vec::Vec::with_capacity(self.byte_size());
96105
for i in 0..self.byte_size() {
97106
vec.push(unsafe { *ptr.add(i) })
98107
}

multiboot2-header/src/module_alignment.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{HeaderTagFlag, HeaderTagType, StructAsBytes};
1+
use crate::{HeaderTagFlag, HeaderTagType};
22
use core::mem::size_of;
33

44
/// If this tag is present, provided boot modules must be page aligned.
@@ -30,4 +30,5 @@ impl ModuleAlignHeaderTag {
3030
}
3131
}
3232

33-
impl StructAsBytes for ModuleAlignHeaderTag {}
33+
#[cfg(feature = "builder")]
34+
impl crate::StructAsBytes for ModuleAlignHeaderTag {}

multiboot2-header/src/relocatable.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{HeaderTagFlag, HeaderTagType, StructAsBytes};
1+
use crate::{HeaderTagFlag, HeaderTagType};
22
use core::fmt;
33
use core::fmt::{Debug, Formatter};
44
use core::mem::size_of;
@@ -91,4 +91,5 @@ impl Debug for RelocatableHeaderTag {
9191
}
9292
}
9393

94-
impl StructAsBytes for RelocatableHeaderTag {}
94+
#[cfg(feature = "builder")]
95+
impl crate::StructAsBytes for RelocatableHeaderTag {}

multiboot2-header/src/uefi_bs.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{HeaderTagFlag, HeaderTagType, StructAsBytes};
1+
use crate::{HeaderTagFlag, HeaderTagType};
22
use core::mem::size_of;
33

44
/// This tag indicates that payload supports starting without terminating UEFI boot services.
@@ -31,4 +31,5 @@ impl EfiBootServiceHeaderTag {
3131
}
3232
}
3333

34-
impl StructAsBytes for EfiBootServiceHeaderTag {}
34+
#[cfg(feature = "builder")]
35+
impl crate::StructAsBytes for EfiBootServiceHeaderTag {}

0 commit comments

Comments
 (0)