Skip to content

Commit 62346d2

Browse files
bors[bot]Johnathan Van Why
and
Johnathan Van Why
authored
Merge #223
223: Add the ErrorCode type to libtock_platform. r=hudson-ayers a=jrvanwhy ErrorCode is a wrapper around an isize that is useful for type safety. Its intended use case is to wrap a non-successful ReturnCode returned from a system call. Co-authored-by: Johnathan Van Why <[email protected]>
2 parents 86c3690 + 244b68e commit 62346d2

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

core/platform/src/error_code.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/// An error code returned by the kernel. Tock's system calls return errors as a
2+
/// negative `isize`. This wraps the isize, and is useful for adding type safety
3+
/// to APIs.
4+
5+
#[derive(Clone, Copy, PartialEq, Eq)]
6+
pub struct ErrorCode {
7+
// Note: value *should* always be negative, but we do not *verify* that so
8+
// unsafe code cannot rely on value being negative.
9+
value: isize,
10+
}
11+
12+
impl ErrorCode {
13+
// Converts the given isize into an ErrorCode. Note that the isize should be
14+
// negative, although that is not verified to reduce code size. We don't
15+
// implement From because not every isize converts sensibly to an ErrorCode.
16+
pub fn new(value: isize) -> ErrorCode {
17+
ErrorCode { value }
18+
}
19+
}
20+
21+
impl Into<isize> for ErrorCode {
22+
fn into(self) -> isize {
23+
self.value
24+
}
25+
}

core/platform/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@
77
// 2. The PlatformApi trait and Platform implementation.
88
// 3. A system call trait so that Platform works in both real Tock apps and
99
// unit test environments.
10+
11+
mod error_code;
12+
13+
pub use error_code::ErrorCode;

0 commit comments

Comments
 (0)