Skip to content

Commit 708343b

Browse files
Merge pull request #916 from cmoylan/implement-error-trait
Implement core::error::Error for all error types
2 parents ce99742 + 4dc61a4 commit 708343b

File tree

10 files changed

+159
-9
lines changed

10 files changed

+159
-9
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- `FileSystem::copy` is now more efficient for large files.
1616
- `MpService::startup_all_aps` and `MpService::startup_this_ap` now accept an
1717
optional `event` parameter to allow non-blocking operation.
18+
- Added `core::error::Error` implementations to all error types.
1819

1920
### Removed
2021
- `BootServices::memmove` and `BootServices::set_mem` have been removed, use

uefi/src/data_types/chars.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,21 @@
33
//! UEFI uses both Latin-1 and UCS-2 character encoding, this module implements
44
//! support for the associated character types.
55
6-
use core::fmt;
6+
use core::fmt::{self, Display, Formatter};
77

88
/// Character conversion error
99
#[derive(Clone, Copy, Debug)]
1010
pub struct CharConversionError;
1111

12+
impl Display for CharConversionError {
13+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
14+
write!(f, "{self:?}")
15+
}
16+
}
17+
18+
#[cfg(feature = "unstable")]
19+
impl core::error::Error for CharConversionError {}
20+
1221
/// A Latin-1 character
1322
#[derive(Clone, Copy, Default, Eq, PartialEq, PartialOrd, Ord)]
1423
#[repr(transparent)]

uefi/src/data_types/owned_strs.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ use alloc::borrow::{Borrow, ToOwned};
77
use alloc::string::String;
88
use alloc::vec;
99
use alloc::vec::Vec;
10-
use core::{fmt, ops};
10+
use core::fmt::{self, Display, Formatter};
11+
use core::ops;
1112

1213
/// Error returned by [`CString16::try_from::<&str>`].
1314
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
@@ -18,8 +19,8 @@ pub enum FromStrError {
1819
InteriorNul,
1920
}
2021

21-
impl fmt::Display for FromStrError {
22-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22+
impl Display for FromStrError {
23+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
2324
write!(
2425
f,
2526
"UCS-2 Conversion Error: {}",

uefi/src/data_types/strs.rs

+42-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ use super::UnalignedSlice;
33
use crate::polyfill::maybe_uninit_slice_assume_init_ref;
44
use core::borrow::Borrow;
55
use core::ffi::CStr;
6+
use core::fmt::{self, Display, Formatter};
67
use core::iter::Iterator;
78
use core::mem::MaybeUninit;
89
use core::result::Result;
9-
use core::{fmt, slice};
10+
use core::slice;
1011

1112
#[cfg(feature = "alloc")]
1213
use super::CString16;
@@ -24,6 +25,19 @@ pub enum FromSliceWithNulError {
2425
NotNulTerminated,
2526
}
2627

28+
impl Display for FromSliceWithNulError {
29+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
30+
match self {
31+
Self::InvalidChar(usize) => write!(f, "invalid character at index {}", usize),
32+
Self::InteriorNul(usize) => write!(f, "interior null character at index {}", usize),
33+
Self::NotNulTerminated => write!(f, "not null-terminated"),
34+
}
35+
}
36+
}
37+
38+
#[cfg(feature = "unstable")]
39+
impl core::error::Error for FromSliceWithNulError {}
40+
2741
/// Error returned by [`CStr16::from_unaligned_slice`].
2842
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
2943
pub enum UnalignedCStr16Error {
@@ -41,6 +55,20 @@ pub enum UnalignedCStr16Error {
4155
BufferTooSmall,
4256
}
4357

58+
impl Display for UnalignedCStr16Error {
59+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
60+
match self {
61+
Self::InvalidChar(usize) => write!(f, "invalid character at index {}", usize),
62+
Self::InteriorNul(usize) => write!(f, "interior null character at index {}", usize),
63+
Self::NotNulTerminated => write!(f, "not null-terminated"),
64+
Self::BufferTooSmall => write!(f, "buffer too small"),
65+
}
66+
}
67+
}
68+
69+
#[cfg(feature = "unstable")]
70+
impl core::error::Error for UnalignedCStr16Error {}
71+
4472
/// Error returned by [`CStr16::from_str_with_buf`].
4573
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
4674
pub enum FromStrWithBufError {
@@ -55,6 +83,19 @@ pub enum FromStrWithBufError {
5583
BufferTooSmall,
5684
}
5785

86+
impl Display for FromStrWithBufError {
87+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
88+
match self {
89+
Self::InvalidChar(usize) => write!(f, "invalid character at index {}", usize),
90+
Self::InteriorNul(usize) => write!(f, "interior null character at index {}", usize),
91+
Self::BufferTooSmall => write!(f, "buffer too small"),
92+
}
93+
}
94+
}
95+
96+
#[cfg(feature = "unstable")]
97+
impl core::error::Error for FromStrWithBufError {}
98+
5899
/// A null-terminated Latin-1 string.
59100
///
60101
/// This type is largely inspired by [`core::ffi::CStr`] with the exception that all characters are

uefi/src/proto/device_path/build.rs

+18
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub use crate::proto::device_path::device_path_gen::build::*;
99

1010
use crate::polyfill::{maybe_uninit_slice_as_mut_ptr, maybe_uninit_slice_assume_init_ref};
1111
use crate::proto::device_path::{DevicePath, DevicePathNode};
12+
use core::fmt::{self, Display, Formatter};
1213
use core::mem::MaybeUninit;
1314

1415
#[cfg(feature = "alloc")]
@@ -174,6 +175,23 @@ pub enum BuildError {
174175
UnexpectedEndEntire,
175176
}
176177

178+
impl Display for BuildError {
179+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
180+
write!(
181+
f,
182+
"{}",
183+
match self {
184+
Self::BufferTooSmall => "a node was too big to fit in remaining buffer space",
185+
Self::NodeTooBig => "a node was too big",
186+
Self::UnexpectedEndEntire => "unexpected END_ENTIRE",
187+
}
188+
)
189+
}
190+
}
191+
192+
#[cfg(feature = "unstable")]
193+
impl core::error::Error for BuildError {}
194+
177195
/// Trait for types that can be used to build a node via
178196
/// [`DevicePathBuilder::push`].
179197
///

uefi/src/proto/driver/component_name.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use crate::proto::unsafe_protocol;
88
use crate::table::boot::{BootServices, ScopedProtocol};
99
use crate::{CStr16, Error, Handle, Result, Status, StatusExt};
10-
use core::fmt::{Debug, Formatter};
10+
use core::fmt::{self, Debug, Display, Formatter};
1111
use core::{ptr, slice};
1212
use uefi_raw::protocol::driver::ComponentName2Protocol;
1313

@@ -238,6 +238,17 @@ pub enum LanguageError {
238238
},
239239
}
240240

241+
impl Display for LanguageError {
242+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
243+
match self {
244+
Self::Ascii { index } => write!(f, "invalid character at index: {}", index),
245+
}
246+
}
247+
}
248+
249+
#[cfg(feature = "unstable")]
250+
impl core::error::Error for LanguageError {}
251+
241252
#[derive(Debug, PartialEq)]
242253
enum LanguageIterKind {
243254
V1,

uefi/src/proto/media/file/info.rs

+16
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::data_types::Align;
33
use crate::table::runtime::Time;
44
use crate::{guid, CStr16, Char16, Guid, Identify};
55
use core::ffi::c_void;
6+
use core::fmt::{self, Display, Formatter};
67
use core::{mem, ptr};
78
use ptr_meta::Pointee;
89

@@ -124,6 +125,21 @@ pub enum FileInfoCreationError {
124125
InsufficientStorage(usize),
125126
}
126127

128+
impl Display for FileInfoCreationError {
129+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
130+
match self {
131+
Self::InsufficientStorage(bytes) => write!(
132+
f,
133+
"provided buffer was too small. need at least {} bytes",
134+
bytes
135+
),
136+
}
137+
}
138+
}
139+
140+
#[cfg(feature = "unstable")]
141+
impl core::error::Error for FileInfoCreationError {}
142+
127143
/// Generic file information
128144
///
129145
/// The following rules apply when using this struct with `set_info()`:

uefi/src/proto/network/pxe.rs

+28-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! PXE Base Code protocol.
22
33
use core::ffi::c_void;
4-
use core::fmt::{Debug, Formatter};
4+
use core::fmt::{self, Debug, Display, Formatter};
55
use core::iter::from_fn;
66
use core::mem::MaybeUninit;
77
use core::ptr::{null, null_mut};
@@ -1240,6 +1240,15 @@ pub struct IcmpError {
12401240
pub data: [u8; 494],
12411241
}
12421242

1243+
impl Display for IcmpError {
1244+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
1245+
write!(f, "{self:?}")
1246+
}
1247+
}
1248+
1249+
#[cfg(feature = "unstable")]
1250+
impl core::error::Error for IcmpError {}
1251+
12431252
/// Corresponds to the anonymous union inside
12441253
/// `EFI_PXE_BASE_CODE_ICMP_ERROR` in the C API.
12451254
#[repr(C)]
@@ -1278,6 +1287,15 @@ pub struct TftpError {
12781287
pub error_string: [u8; 127],
12791288
}
12801289

1290+
impl Display for TftpError {
1291+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
1292+
write!(f, "{self:?}")
1293+
}
1294+
}
1295+
1296+
#[cfg(feature = "unstable")]
1297+
impl core::error::Error for TftpError {}
1298+
12811299
/// Returned by [`BaseCode::tftp_read_dir`].
12821300
#[allow(missing_docs)]
12831301
#[derive(Debug)]
@@ -1311,3 +1329,12 @@ pub struct MtftpFileInfo<'a> {
13111329
/// [`BaseCode::tftp_read_dir`] or [`BaseCode::mtftp_read_dir`].
13121330
#[derive(Clone, Copy, Debug)]
13131331
pub struct ReadDirParseError;
1332+
1333+
impl Display for ReadDirParseError {
1334+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
1335+
write!(f, "{self:?}")
1336+
}
1337+
}
1338+
1339+
#[cfg(feature = "unstable")]
1340+
impl core::error::Error for ReadDirParseError {}

uefi/src/proto/string/unicode_collation.rs

+17
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use crate::data_types::{CStr16, CStr8, Char16, Char8};
77
use crate::proto::unsafe_protocol;
88
use core::cmp::Ordering;
9+
use core::fmt::{self, Display, Formatter};
910

1011
/// The Unicode Collation Protocol.
1112
///
@@ -159,3 +160,19 @@ pub enum StrConversionError {
159160
/// The buffer given is too small to hold the string.
160161
BufferTooSmall,
161162
}
163+
164+
impl Display for StrConversionError {
165+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
166+
write!(
167+
f,
168+
"{}",
169+
match self {
170+
Self::ConversionFailed => "conversion failed",
171+
Self::BufferTooSmall => "buffer too small",
172+
}
173+
)
174+
}
175+
}
176+
177+
#[cfg(feature = "unstable")]
178+
impl core::error::Error for StrConversionError {}

uefi/src/table/runtime.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
use super::Revision;
44
use crate::table::boot::MemoryDescriptor;
55
use crate::{CStr16, Error, Result, Status, StatusExt};
6-
use core::fmt::{Debug, Formatter};
6+
use core::fmt::{self, Debug, Display, Formatter};
77
use core::mem::MaybeUninit;
8-
use core::{fmt, ptr};
8+
use core::ptr;
99

1010
pub use uefi_raw::table::runtime::{
1111
ResetType, TimeCapabilities, VariableAttributes, VariableVendor,
@@ -352,6 +352,15 @@ pub struct TimeParams {
352352
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
353353
pub struct TimeError;
354354

355+
impl Display for TimeError {
356+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
357+
write!(f, "{self:?}")
358+
}
359+
}
360+
361+
#[cfg(feature = "unstable")]
362+
impl core::error::Error for TimeError {}
363+
355364
impl Time {
356365
/// Unspecified Timezone/local time.
357366
const UNSPECIFIED_TIMEZONE: i16 = uefi_raw::time::Time::UNSPECIFIED_TIMEZONE;

0 commit comments

Comments
 (0)