Skip to content

Commit 909ed28

Browse files
committed
multiboot2: add unstable feature + core::error::Error
1 parent e9d8ce4 commit 909ed28

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
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: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
// this crate can use `std` in tests only
2-
#![cfg_attr(not(test), no_std)]
1+
#![no_std]
2+
3+
#![cfg_attr(feature = "unstable", feature(error_in_core))]
4+
35
#![deny(missing_debug_implementations)]
46
// --- BEGIN STYLE CHECKS ---
57
// These checks are optional in CI for PRs, as discussed in
68
// https://github.com/rust-osdev/multiboot2/pull/92
79
#![deny(clippy::all)]
810
#![deny(rustdoc::all)]
9-
// Forcing this would be a little bit ridiculous, because it would require code examples for
10-
// each getter and each trivial trait implementation (like Debug).
11-
#![allow(rustdoc::missing_doc_code_examples)]
1211
// --- END STYLE CHECKS ---
1312

1413
//! Library that helps you to parse the multiboot information structure (mbi) from
@@ -40,6 +39,7 @@
4039
extern crate std;
4140

4241
use core::fmt;
42+
use derive_more::Display;
4343

4444
pub use boot_loader_name::BootLoaderNameTag;
4545
pub use command_line::CommandLineTag;
@@ -161,19 +161,25 @@ pub unsafe fn load_with_offset(
161161

162162
/// Error type that describes errors while loading/parsing a multiboot2 information structure
163163
/// from a given address.
164-
#[derive(Debug)]
164+
#[derive(Debug, Display)]
165165
pub enum MbiLoadError {
166166
/// The address is invalid. Make sure that the address is 8-byte aligned,
167167
/// according to the spec.
168+
#[display(fmt = "The address is invalid")]
168169
IllegalAddress,
169170
/// The total size of the multiboot2 information structure must be a multiple of 8.
170171
/// (Not in spec, but it is implicitly the case, because the begin of MBI
171172
/// 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")]
172174
IllegalTotalSize(u32),
173175
/// End tag missing. Each multiboot2 header requires to have an end tag.
176+
#[display(fmt = "There is no end tag")]
174177
NoEndTag,
175178
}
176179

180+
#[cfg(feature = "unstable")]
181+
impl core::error::Error for MbiLoadError {}
182+
177183
/// A Multiboot 2 Boot Information struct.
178184
pub struct BootInformation {
179185
inner: *const BootInformationInner,
@@ -1435,4 +1441,13 @@ mod tests {
14351441
core::mem::transmute::<[u8; 16], EFIMemoryMapTag>([0u8; 16]);
14361442
}
14371443
}
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+
}
1451+
consumer(MbiLoadError::IllegalAddress)
1452+
}
14381453
}

0 commit comments

Comments
 (0)