From 6c1b0cd2c755612849c69846fd710a5e4016b223 Mon Sep 17 00:00:00 2001 From: Ross Lannen Date: Thu, 8 May 2025 23:40:37 -0600 Subject: [PATCH] Implement core::error::Error for error types. (#780) --- src/error.rs | 21 +++++++++++++++++++++ src/features/impl_std.rs | 19 ------------------- src/features/serde/mod.rs | 6 ------ 3 files changed, 21 insertions(+), 25 deletions(-) diff --git a/src/error.rs b/src/error.rs index 7fb9c5c..b3dc565 100644 --- a/src/error.rs +++ b/src/error.rs @@ -63,6 +63,27 @@ impl core::fmt::Display for EncodeError { } } +impl core::error::Error for EncodeError { + fn source(&self) -> Option<&(dyn core::error::Error + 'static)> { + match self { + Self::RefCellAlreadyBorrowed { inner, .. } => Some(inner), + #[cfg(feature = "std")] + Self::Io { inner, .. } => Some(inner), + #[cfg(feature = "std")] + Self::InvalidSystemTime { inner, .. } => Some(inner), + _ => None, + } + } +} +impl core::error::Error for DecodeError { + fn source(&self) -> Option<&(dyn core::error::Error + 'static)> { + match self { + Self::Utf8 { inner } => Some(inner), + _ => None, + } + } +} + /// Errors that can be encountered by decoding a type #[non_exhaustive] #[derive(Debug)] diff --git a/src/features/impl_std.rs b/src/features/impl_std.rs index ef1f328..b3be41b 100644 --- a/src/features/impl_std.rs +++ b/src/features/impl_std.rs @@ -412,25 +412,6 @@ impl Decode for SocketAddrV6 { } impl_borrow_decode!(SocketAddrV6); -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), - _ => None, - } - } -} - impl Encode for HashMap where K: Encode, diff --git a/src/features/serde/mod.rs b/src/features/serde/mod.rs index 5874cca..7b1ad22 100644 --- a/src/features/serde/mod.rs +++ b/src/features/serde/mod.rs @@ -114,9 +114,6 @@ impl serde::de::Error for crate::error::DecodeError { } } -#[cfg(not(feature = "std"))] -impl serde::de::StdError for crate::error::DecodeError {} - #[cfg(not(feature = "alloc"))] impl serde::de::Error for crate::error::DecodeError { fn custom(_: T) -> Self @@ -169,9 +166,6 @@ impl serde::ser::Error for crate::error::EncodeError { } } -#[cfg(not(feature = "std"))] -impl serde::de::StdError for crate::error::EncodeError {} - #[cfg(not(feature = "alloc"))] impl serde::ser::Error for crate::error::EncodeError { fn custom(_: T) -> Self