mirror of https://git.sr.ht/~stygianentity/bincode
Added `std::error::Error::source` (#530)
This commit is contained in:
parent
49c8d1148f
commit
36e45d26eb
|
|
@ -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 })
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
},
|
||||
|
|
@ -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]),
|
||||
|
|
|
|||
|
|
@ -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(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue