Skip to content

Commit cf40c3f

Browse files
authored
Implements From<DekuError> for no_std_io::io::Error in no_std context (sharksforarms#578)
1 parent 740916f commit cf40c3f

2 files changed

Lines changed: 19 additions & 4 deletions

File tree

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ std = ["deku_derive/std", "bitvec?/std", "alloc", "no_std_io/std"]
2525
alloc = ["bitvec?/alloc"]
2626
logging = ["deku_derive/logging", "log"]
2727
no-assert-string = ["deku_derive/no-assert-string"]
28-
error_in_core = []
2928
bits = ["dep:bitvec", "deku_derive/bits"]
3029

3130
[dependencies]

src/error.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,25 @@ impl core::fmt::Display for DekuError {
9090
}
9191
}
9292

93+
#[cfg(not(feature = "std"))]
94+
impl core::error::Error for DekuError {}
95+
96+
#[cfg(not(feature = "std"))]
97+
impl From<DekuError> for no_std_io::io::Error {
98+
fn from(error: DekuError) -> Self {
99+
use no_std_io::io;
100+
match error {
101+
DekuError::Incomplete(_) => io::Error::new(io::ErrorKind::UnexpectedEof, error),
102+
DekuError::Parse(_) => io::Error::new(io::ErrorKind::InvalidData, error),
103+
DekuError::InvalidParam(_) => io::Error::new(io::ErrorKind::InvalidInput, error),
104+
DekuError::Assertion(_) => io::Error::new(io::ErrorKind::InvalidData, error),
105+
DekuError::AssertionNoStr => io::Error::from(io::ErrorKind::InvalidData),
106+
DekuError::IdVariantNotFound => io::Error::new(io::ErrorKind::NotFound, error),
107+
DekuError::Io(e) => io::Error::new(e, error),
108+
}
109+
}
110+
}
111+
93112
#[cfg(feature = "std")]
94113
impl std::error::Error for DekuError {
95114
fn cause(&self) -> Option<&dyn std::error::Error> {
@@ -112,6 +131,3 @@ impl From<DekuError> for std::io::Error {
112131
}
113132
}
114133
}
115-
116-
#[cfg(feature = "error_in_core")]
117-
impl core::error::Error for DekuError {}

0 commit comments

Comments
 (0)