Skip to content

Commit 0f3b9b2

Browse files
committed
riscv: add result module
Adds the `result` module to contain types for fallible functions. For discussion, see: <#212 (comment)>
1 parent 9d3fb05 commit 0f3b9b2

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

riscv/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub(crate) mod bits;
4040
pub mod delay;
4141
pub mod interrupt;
4242
pub mod register;
43+
pub mod result;
4344

4445
#[macro_use]
4546
mod macros;

riscv/src/result.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use core::fmt;
2+
3+
/// Convenience alias for the [Result](core::result::Result) type for the library.
4+
pub type Result<T> = core::result::Result<T, Error>;
5+
6+
/// Represents error variants for the library.
7+
#[repr(C)]
8+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
9+
pub enum Error {
10+
/// Attempted out-of-bounds access.
11+
OutOfBounds {
12+
index: usize,
13+
min: usize,
14+
max: usize,
15+
},
16+
/// Unimplemented function or type.
17+
Unimplemented,
18+
}
19+
20+
impl fmt::Display for Error {
21+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22+
match self {
23+
Self::OutOfBounds { index, min, max } => write!(
24+
f,
25+
"out-of-bounds access, index: {index}, min: {min}, max: {max}"
26+
),
27+
Self::Unimplemented => write!(f, "unimplemented"),
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)