浏览代码

multiboot2: add unstable feature + core::error::Error

Philipp Schuster 2 年之前
父节点
当前提交
9b37f562ac
共有 3 个文件被更改,包括 28 次插入3 次删除
  1. 6 0
      multiboot2/Cargo.toml
  2. 4 0
      multiboot2/Changelog.md
  3. 18 3
      multiboot2/src/lib.rs

+ 6 - 0
multiboot2/Cargo.toml

@@ -31,5 +31,11 @@ homepage = "https://github.com/rust-osdev/multiboot2"
 repository = "https://github.com/rust-osdev/multiboot2"
 documentation = "https://docs.rs/multiboot2"
 
+[features]
+default = []
+# Nightly-only features that will eventually be stabilized.
+unstable = []
+
 [dependencies]
 bitflags = "1"
+derive_more = { version = "0.99.17", default-features = false, features = ["display"] }

+ 4 - 0
multiboot2/Changelog.md

@@ -2,6 +2,10 @@
 
 ## 0.14.2
 - documentation fixes
+- `MbiLoadError` now implements `Display`
+- Added the `unstable` feature, which enables nightly-only functionality.
+  With this feature, `MbiLoadError` now implements `core::error::Error` and can
+  be used with `anyhow::Result` for example.
 
 ## 0.14.1 (2023-03-09)
 - fixed the calculation of the last area of the memory map tag ([#119](https://github.com/rust-osdev/multiboot2/pull/119))

+ 18 - 3
multiboot2/src/lib.rs

@@ -1,5 +1,5 @@
-// this crate can use `std` in tests only
-#![cfg_attr(not(test), no_std)]
+#![no_std]
+#![cfg_attr(feature = "unstable", feature(error_in_core))]
 #![deny(missing_debug_implementations)]
 // --- BEGIN STYLE CHECKS ---
 // These checks are optional in CI for PRs, as discussed in
@@ -39,6 +39,7 @@
 extern crate std;
 
 use core::fmt;
+use derive_more::Display;
 
 pub use boot_loader_name::BootLoaderNameTag;
 pub use command_line::CommandLineTag;
@@ -160,19 +161,25 @@ pub unsafe fn load_with_offset(
 
 /// Error type that describes errors while loading/parsing a multiboot2 information structure
 /// from a given address.
-#[derive(Debug)]
+#[derive(Debug, Display)]
 pub enum MbiLoadError {
     /// The address is invalid. Make sure that the address is 8-byte aligned,
     /// according to the spec.
+    #[display(fmt = "The address is invalid")]
     IllegalAddress,
     /// The total size of the multiboot2 information structure must be a multiple of 8.
     /// (Not in spec, but it is implicitly the case, because the begin of MBI
     /// and all tags are 8-byte aligned and the end tag is exactly 8 byte long).
+    #[display(fmt = "The size of the MBI is unexpected")]
     IllegalTotalSize(u32),
     /// End tag missing. Each multiboot2 header requires to have an end tag.
+    #[display(fmt = "There is no end tag")]
     NoEndTag,
 }
 
+#[cfg(feature = "unstable")]
+impl core::error::Error for MbiLoadError {}
+
 /// A Multiboot 2 Boot Information struct.
 pub struct BootInformation {
     inner: *const BootInformationInner,
@@ -1434,4 +1441,12 @@ mod tests {
             core::mem::transmute::<[u8; 16], EFIMemoryMapTag>([0u8; 16]);
         }
     }
+
+    #[test]
+    #[cfg(feature = "unstable")]
+    /// This test succeeds if it compiles.
+    fn mbi_load_error_implements_error() {
+        fn consumer<E: core::error::Error>(_e: E) {}
+        consumer(MbiLoadError::IllegalAddress)
+    }
 }