Skip to content

Commit 4f1f42d

Browse files
committed
Add returning io:ErrorKind from reader and writer
* Remove DekuError::Write and replace with DekuError::Io. Instead of ignoring error other then UnexpectedEof, return them to bubble up.
1 parent 295c164 commit 4f1f42d

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

src/error.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
33
#![cfg(feature = "alloc")]
44

5+
use no_std_io::io::ErrorKind;
6+
57
use alloc::format;
68
use alloc::string::String;
79

@@ -49,8 +51,8 @@ pub enum DekuError {
4951
AssertionNoStr,
5052
/// Could not resolve `id` for variant
5153
IdVariantNotFound,
52-
/// IO error while writing
53-
Write,
54+
/// IO error while reading or writing
55+
Io(ErrorKind),
5456
}
5557

5658
impl From<core::num::TryFromIntError> for DekuError {
@@ -86,7 +88,7 @@ impl core::fmt::Display for DekuError {
8688
DekuError::Assertion(ref err) => write!(f, "Assertion error: {err}"),
8789
DekuError::AssertionNoStr => write!(f, "Assertion error"),
8890
DekuError::IdVariantNotFound => write!(f, "Could not resolve `id` for variant"),
89-
DekuError::Write => write!(f, "write error"),
91+
DekuError::Io(ref e) => write!(f, "io errorr: {e}"),
9092
}
9193
}
9294
}
@@ -110,7 +112,7 @@ impl From<DekuError> for std::io::Error {
110112
DekuError::Assertion(_) => io::Error::new(io::ErrorKind::InvalidData, error),
111113
DekuError::AssertionNoStr => io::Error::from(io::ErrorKind::InvalidData),
112114
DekuError::IdVariantNotFound => io::Error::new(io::ErrorKind::NotFound, error),
113-
DekuError::Write => io::Error::new(io::ErrorKind::BrokenPipe, error),
115+
DekuError::Io(e) => io::Error::new(e, error),
114116
}
115117
}
116118
}

src/reader.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,7 @@ impl<'a, R: Read> Reader<'a, R> {
169169
if e.kind() == ErrorKind::UnexpectedEof {
170170
return Err(DekuError::Incomplete(NeedSize::new(amt)));
171171
}
172-
173-
// TODO: other errors?
172+
return Err(DekuError::Io(e.kind()));
174173
}
175174
let read_buf = &buf[..bytes_len];
176175

@@ -219,8 +218,7 @@ impl<'a, R: Read> Reader<'a, R> {
219218
if e.kind() == ErrorKind::UnexpectedEof {
220219
return Err(DekuError::Incomplete(NeedSize::new(amt * 8)));
221220
}
222-
223-
// TODO: other errors?
221+
return Err(DekuError::Io(e.kind()));
224222
}
225223

226224
self.bits_read += amt * 8;

src/writer.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ impl<W: Write> Writer<W> {
8181
bits = unsafe { bits.get_unchecked(count * bits_of::<u8>()..) };
8282

8383
self.leftover = bits.to_bitvec();
84-
if self.inner.write_all(&buf).is_err() {
85-
return Err(DekuError::Write);
84+
if let Err(e) = self.inner.write_all(&buf) {
85+
return Err(DekuError::Io(e.kind()));
8686
}
8787

8888
self.bits_written += buf.len() * 8;
@@ -110,8 +110,8 @@ impl<W: Write> Writer<W> {
110110
// instead of sending the entire thing. The rest would be through self.inner.write.
111111
self.write_bits(&BitVec::from_slice(buf))?;
112112
} else {
113-
if self.inner.write_all(buf).is_err() {
114-
return Err(DekuError::Write);
113+
if let Err(e) = self.inner.write_all(buf) {
114+
return Err(DekuError::Io(e.kind()));
115115
}
116116
self.bits_written += buf.len() * 8;
117117
}
@@ -141,8 +141,8 @@ impl<W: Write> Writer<W> {
141141
*slot = byte.load_be();
142142
});
143143

144-
if self.inner.write_all(&buf).is_err() {
145-
return Err(DekuError::Write);
144+
if let Err(e) = self.inner.write_all(&buf) {
145+
return Err(DekuError::Io(e.kind()));
146146
}
147147
self.bits_written += buf.len() * 8;
148148

0 commit comments

Comments
 (0)