Skip to content

Commit 9b37f56

Browse files
committed
multiboot2: add unstable feature + core::error::Error
1 parent 36c27d9 commit 9b37f56

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

multiboot2/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,11 @@ homepage = "https://github.com/rust-osdev/multiboot2"
3131
repository = "https://github.com/rust-osdev/multiboot2"
3232
documentation = "https://docs.rs/multiboot2"
3333

34+
[features]
35+
default = []
36+
# Nightly-only features that will eventually be stabilized.
37+
unstable = []
38+
3439
[dependencies]
3540
bitflags = "1"
41+
derive_more = { version = "0.99.17", default-features = false, features = ["display"] }

multiboot2/Changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## 0.14.2
44
- documentation fixes
5+
- `MbiLoadError` now implements `Display`
6+
- Added the `unstable` feature, which enables nightly-only functionality.
7+
With this feature, `MbiLoadError` now implements `core::error::Error` and can
8+
be used with `anyhow::Result` for example.
59

610
## 0.14.1 (2023-03-09)
711
- fixed the calculation of the last area of the memory map tag ([#119](https://github.com/rust-osdev/multiboot2/pull/119))

multiboot2/src/lib.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// this crate can use `std` in tests only
2-
#![cfg_attr(not(test), no_std)]
1+
#![no_std]
2+
#![cfg_attr(feature = "unstable", feature(error_in_core))]
33
#![deny(missing_debug_implementations)]
44
// --- BEGIN STYLE CHECKS ---
55
// These checks are optional in CI for PRs, as discussed in
@@ -39,6 +39,7 @@
3939
extern crate std;
4040

4141
use core::fmt;
42+
use derive_more::Display;
4243

4344
pub use boot_loader_name::BootLoaderNameTag;
4445
pub use command_line::CommandLineTag;
@@ -160,19 +161,25 @@ pub unsafe fn load_with_offset(
160161

161162
/// Error type that describes errors while loading/parsing a multiboot2 information structure
162163
/// from a given address.
163-
#[derive(Debug)]
164+
#[derive(Debug, Display)]
164165
pub enum MbiLoadError {
165166
/// The address is invalid. Make sure that the address is 8-byte aligned,
166167
/// according to the spec.
168+
#[display(fmt = "The address is invalid")]
167169
IllegalAddress,
168170
/// The total size of the multiboot2 information structure must be a multiple of 8.
169171
/// (Not in spec, but it is implicitly the case, because the begin of MBI
170172
/// and all tags are 8-byte aligned and the end tag is exactly 8 byte long).
173+
#[display(fmt = "The size of the MBI is unexpected")]
171174
IllegalTotalSize(u32),
172175
/// End tag missing. Each multiboot2 header requires to have an end tag.
176+
#[display(fmt = "There is no end tag")]
173177
NoEndTag,
174178
}
175179

180+
#[cfg(feature = "unstable")]
181+
impl core::error::Error for MbiLoadError {}
182+
176183
/// A Multiboot 2 Boot Information struct.
177184
pub struct BootInformation {
178185
inner: *const BootInformationInner,
@@ -1434,4 +1441,12 @@ mod tests {
14341441
core::mem::transmute::<[u8; 16], EFIMemoryMapTag>([0u8; 16]);
14351442
}
14361443
}
1444+
1445+
#[test]
1446+
#[cfg(feature = "unstable")]
1447+
/// This test succeeds if it compiles.
1448+
fn mbi_load_error_implements_error() {
1449+
fn consumer<E: core::error::Error>(_e: E) {}
1450+
consumer(MbiLoadError::IllegalAddress)
1451+
}
14371452
}

0 commit comments

Comments
 (0)