Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added std::error::Error::source #530

Merged
merged 1 commit into from
Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/de/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ impl<'a, 'de: 'a> BorrowDecode<'de> for Option<&'a [u8]> {
impl<'a, 'de: 'a> BorrowDecode<'de> for &'a str {
fn borrow_decode<D: BorrowDecoder<'de>>(decoder: &mut D) -> Result<Self, DecodeError> {
let slice = <&[u8]>::borrow_decode(decoder)?;
core::str::from_utf8(slice).map_err(DecodeError::Utf8)
core::str::from_utf8(slice).map_err(|inner| DecodeError::Utf8 { inner })
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub enum EncodeError {
#[cfg(feature = "std")]
Io {
/// The encountered error
error: std::io::Error,
inner: std::io::Error,
/// The amount of bytes that were written before the error occurred
index: usize,
},
Expand Down Expand Up @@ -107,7 +107,10 @@ pub enum DecodeError {
},

/// The decoder tried to decode a `str`, but an utf8 error was encountered.
Utf8(core::str::Utf8Error),
Utf8 {
/// The inner error
inner: core::str::Utf8Error,
},

/// The decoder tried to decode a `char` and failed. The given buffer contains the bytes that are read at the moment of failure.
InvalidCharEncoding([u8; 4]),
Expand Down
4 changes: 3 additions & 1 deletion src/features/impl_alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,9 @@ where
impl Decode for String {
fn decode<D: Decoder>(decoder: &mut D) -> Result<Self, DecodeError> {
let bytes = Vec::<u8>::decode(decoder)?;
String::from_utf8(bytes).map_err(|e| DecodeError::Utf8(e.utf8_error()))
String::from_utf8(bytes).map_err(|e| DecodeError::Utf8 {
inner: e.utf8_error(),
})
}
}

Expand Down
25 changes: 21 additions & 4 deletions src/features/impl_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ impl<'storage, W: std::io::Write> Writer for IoWriter<'storage, W> {
fn write(&mut self, bytes: &[u8]) -> Result<(), EncodeError> {
self.writer
.write_all(bytes)
.map_err(|error| EncodeError::Io {
error,
.map_err(|inner| EncodeError::Io {
inner,
index: self.bytes_written,
})?;
self.bytes_written += bytes.len();
Expand Down Expand Up @@ -358,8 +358,25 @@ impl Decode for SocketAddrV6 {
}
}

impl std::error::Error for EncodeError {}
impl std::error::Error for DecodeError {}
impl std::error::Error for EncodeError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::RefCellAlreadyBorrowed { inner, .. } => Some(inner),
Self::Io { inner, .. } => Some(inner),
Self::InvalidSystemTime { inner, .. } => Some(inner),
_ => None,
}
}
}
impl std::error::Error for DecodeError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Utf8 { inner } => Some(inner),
Self::CStringNulError { inner } => Some(inner),
_ => None,
}
}
}

impl<K, V, S> Encode for HashMap<K, V, S>
where
Expand Down