Skip to content

Commit 39578f8

Browse files
committed
treewide: use thiserror
I think this makes the code a little nicer.
1 parent 1e8cf13 commit 39578f8

File tree

10 files changed

+62
-92
lines changed

10 files changed

+62
-92
lines changed

Cargo.lock

Lines changed: 24 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@ package.license = "MIT/Apache-2.0"
1414

1515
[workspace.dependencies]
1616
bitflags = "2.9.1"
17-
derive_more = { version = "2.0.1", default-features = false, features = ["display"] }
1817
log = { version = "~0.4", default-features = false }
18+
ptr_meta = { version = "~0.3", default-features = false, features = ["derive"] }
19+
thiserror = { version = "2.0.12", default-features = false }
20+
21+
# Intra-workspace dependencies of upstream crates: always point to the latest
22+
# stable version
1923
multiboot2 = { version = "0.23.1", default-features = false }
2024
multiboot2-common = { version = "0.2.1", default-features = false }
21-
ptr_meta = { version = "~0.3", default-features = false, features = ["derive"] }
2225

2326
# This way, the corresponding crate dependency can be normalley referenced by
2427
# version, while still the repository version is used transparently during local

multiboot2-common/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ builder = ["alloc"]
2929

3030

3131
[dependencies]
32-
derive_more.workspace = true
3332
ptr_meta.workspace = true
33+
thiserror.workspace = true
3434

3535
[package.metadata.docs.rs]
3636
all-features = true

multiboot2-common/src/lib.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ use core::mem;
260260
use core::ptr;
261261
use core::ptr::NonNull;
262262
use core::slice;
263+
use thiserror::Error;
263264

264265
/// The alignment of all Multiboot2 data structures.
265266
pub const ALIGNMENT: usize = 8;
@@ -423,26 +424,29 @@ impl<H: Header> DynSizedStructure<H> {
423424
}
424425

425426
/// Errors that may occur when working with memory.
426-
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash, derive_more::Display)]
427+
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash, Error)]
427428
pub enum MemoryError {
428429
/// The memory points to null.
430+
#[error("memory points to null")]
429431
Null,
430432
/// The memory must be at least [`ALIGNMENT`]-aligned.
433+
#[error("memory is not properly aligned")]
431434
WrongAlignment,
432435
/// The memory must cover at least the length of the sized structure header
433436
/// type.
437+
#[error("memory range is shorter than the size of the header structure")]
434438
ShorterThanHeader,
435439
/// The buffer misses the terminating padding to the next alignment
436440
/// boundary. The padding is relevant to satisfy Rustc/Miri, but also the
437441
/// spec mandates that the padding is added.
442+
#[error("memory is missing required padding")]
438443
MissingPadding,
439444
/// The size-property has an illegal value that can't be fulfilled with the
440445
/// given bytes.
446+
#[error("the header reports an invalid total size")]
441447
InvalidReportedTotalSize,
442448
}
443449

444-
impl core::error::Error for MemoryError {}
445-
446450
/// Increases the given size to the next alignment boundary, if it is not a
447451
/// multiple of the alignment yet.
448452
///

multiboot2-header/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ alloc = ["multiboot2-common/alloc"]
4141
builder = ["alloc", "multiboot2-common/builder"]
4242

4343
[dependencies]
44-
derive_more.workspace = true
4544
log.workspace = true
4645
multiboot2-common.workspace = true
4746
multiboot2.workspace = true
4847
ptr_meta.workspace = true
48+
thiserror.workspace = true
4949

5050
[package.metadata.docs.rs]
5151
all-features = true

multiboot2-header/src/header.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ use crate::{
77
use core::fmt::{Debug, Formatter};
88
use core::mem::size_of;
99
use core::ptr::NonNull;
10-
use multiboot2_common::{DynSizedStructure, Header, MemoryError, Tag, ALIGNMENT};
10+
use multiboot2_common::{ALIGNMENT, DynSizedStructure, Header, MemoryError, Tag};
11+
use thiserror::Error;
1112

1213
/// Magic value for a [`Multiboot2Header`], as defined by the spec.
1314
pub const MAGIC: u32 = 0xe85250d6;
@@ -224,24 +225,18 @@ impl Debug for Multiboot2Header<'_> {
224225

225226
/// Errors that occur when a chunk of memory can't be parsed as
226227
/// [`Multiboot2Header`].
227-
#[derive(Copy, Clone, Debug, derive_more::Display, PartialEq, Eq, PartialOrd, Ord, Hash)]
228+
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Error)]
228229
pub enum LoadError {
229230
/// The provided checksum does not match the expected value.
231+
#[error("checksum does not match expected value")]
230232
ChecksumMismatch,
231233
/// The header does not contain the correct magic number.
234+
#[error("header does not contain expected magic value")]
232235
MagicNotFound,
233236
/// The provided memory can't be parsed as [`Multiboot2Header`].
234237
/// See [`MemoryError`].
235-
Memory(MemoryError),
236-
}
237-
238-
impl Error for LoadError {
239-
fn source(&self) -> Option<&(dyn Error + 'static)> {
240-
match self {
241-
Self::Memory(inner) => Some(inner),
242-
_ => None,
243-
}
244-
}
238+
#[error("memory can't be parsed as multiboot2 header")]
239+
Memory(#[source] MemoryError),
245240
}
246241

247242
/// The "basic" Multiboot2 header. This means only the properties, that are known during

multiboot2/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ builder = ["alloc", "multiboot2-common/builder"]
4141

4242
[dependencies]
4343
bitflags.workspace = true
44-
derive_more.workspace = true
4544
log.workspace = true
46-
ptr_meta.workspace = true
4745
multiboot2-common.workspace = true
46+
ptr_meta.workspace = true
47+
thiserror.workspace = true
4848
uefi-raw = { version = "~0.8.0", default-features = false }
4949

5050
[package.metadata.docs.rs]

multiboot2/src/boot_information.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,31 @@
33
use crate::framebuffer::UnknownFramebufferType;
44
use crate::tag::TagHeader;
55
use crate::{
6-
module, ApmTag, BasicMemoryInfoTag, BootLoaderNameTag, BootdevTag, CommandLineTag,
6+
ApmTag, BasicMemoryInfoTag, BootLoaderNameTag, BootdevTag, CommandLineTag,
77
EFIBootServicesNotExitedTag, EFIImageHandle32Tag, EFIImageHandle64Tag, EFIMemoryMapTag,
88
EFISdt32Tag, EFISdt64Tag, ElfSectionIter, ElfSectionsTag, EndTag, FramebufferTag,
99
ImageLoadPhysAddrTag, MemoryMapTag, ModuleIter, NetworkTag, RsdpV1Tag, RsdpV2Tag, SmbiosTag,
10-
TagIter, TagType, VBEInfoTag,
10+
TagIter, TagType, VBEInfoTag, module,
1111
};
12-
use core::error::Error;
1312
use core::fmt;
1413
use core::mem;
1514
use core::ptr::NonNull;
16-
use derive_more::Display;
1715
use multiboot2_common::{DynSizedStructure, Header, MaybeDynSized, MemoryError, Tag};
16+
use thiserror::Error;
1817

1918
/// Errors that occur when a chunk of memory can't be parsed as
2019
/// [`BootInformation`].
21-
#[derive(Display, Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
20+
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Error)]
2221
pub enum LoadError {
2322
/// The provided memory can't be parsed as [`BootInformation`].
2423
/// See [`MemoryError`].
25-
Memory(MemoryError),
24+
#[error("memory can't be parsed as boot information")]
25+
Memory(#[source] MemoryError),
2626
/// Missing mandatory end tag.
27+
#[error("missing mandatory end tag")]
2728
NoEndTag,
2829
}
2930

30-
impl Error for LoadError {
31-
fn source(&self) -> Option<&(dyn Error + 'static)> {
32-
match self {
33-
Self::Memory(inner) => Some(inner),
34-
Self::NoEndTag => None,
35-
}
36-
}
37-
}
38-
3931
/// The basic header of a [`BootInformation`] as sized Rust type.
4032
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
4133
#[repr(C, align(8))]

multiboot2/src/framebuffer.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
//! Module for [`FramebufferTag`].
22
3-
use crate::tag::TagHeader;
43
use crate::TagType;
4+
use crate::tag::TagHeader;
55
use core::fmt::Debug;
66
use core::mem;
77
use core::slice;
8-
use derive_more::Display;
98
use multiboot2_common::{MaybeDynSized, Tag};
9+
use thiserror::Error;
1010
#[cfg(feature = "builder")]
1111
use {alloc::boxed::Box, multiboot2_common::new_boxed};
1212

@@ -402,12 +402,10 @@ pub struct FramebufferColor {
402402
}
403403

404404
/// Error when an unknown [`FramebufferTypeId`] is found.
405-
#[derive(Debug, Copy, Clone, Display, PartialEq, Eq)]
406-
#[display("Unknown framebuffer type {}", _0)]
405+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Error)]
406+
#[error("Unknown framebuffer type {0}")]
407407
pub struct UnknownFramebufferType(u8);
408408

409-
impl core::error::Error for UnknownFramebufferType {}
410-
411409
#[cfg(test)]
412410
mod tests {
413411
use super::*;

multiboot2/src/util.rs

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
//! Various utilities.
22
3-
use core::fmt;
4-
use core::fmt::{Display, Formatter};
53
use core::str::Utf8Error;
64
use thiserror::Error;
75

@@ -10,24 +8,11 @@ use thiserror::Error;
108
pub enum StringError {
119
/// There is no terminating NUL character, although the specification
1210
/// requires one.
13-
MissingNul(core::ffi::FromBytesUntilNulError),
11+
#[error("string is not null terminated")]
12+
MissingNul(#[source] core::ffi::FromBytesUntilNulError),
1413
/// The sequence until the first NUL character is not valid UTF-8.
15-
Utf8(Utf8Error),
16-
}
17-
18-
impl Display for StringError {
19-
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
20-
write!(f, "{self:?}")
21-
}
22-
}
23-
24-
impl core::error::Error for StringError {
25-
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
26-
match self {
27-
Self::MissingNul(e) => Some(e),
28-
Self::Utf8(e) => Some(e),
29-
}
30-
}
14+
#[error("string is not valid UTF-8")]
15+
Utf8(#[source] Utf8Error),
3116
}
3217

3318
/// Parses the provided byte sequence as Multiboot string, which maps to a

0 commit comments

Comments
 (0)