Skip to content

Commit 37c8d2a

Browse files
committed
refactor: use core error
It doesn't require std. This feature is introduced since Rust 1.81.
1 parent f24752b commit 37c8d2a

File tree

2 files changed

+6
-54
lines changed

2 files changed

+6
-54
lines changed

src/core/error.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,8 @@ impl<E: fmt::Debug> fmt::Display for DecodeError<E> {
149149
}
150150
}
151151

152-
#[cfg(feature = "use_std")]
153-
impl<E: std::error::Error + 'static> std::error::Error for DecodeError<E> {
154-
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
152+
impl<E: core::error::Error + 'static> core::error::Error for DecodeError<E> {
153+
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
155154
match self {
156155
DecodeError::Read(err) => Some(err),
157156
_ => None
@@ -179,9 +178,8 @@ impl<E: fmt::Debug> fmt::Display for EncodeError<E> {
179178
}
180179
}
181180

182-
#[cfg(feature = "use_std")]
183-
impl<E: std::error::Error + 'static> std::error::Error for EncodeError<E> {
184-
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
181+
impl<E: core::error::Error + 'static> core::error::Error for EncodeError<E> {
182+
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
185183
match self {
186184
EncodeError::Write(err) => Some(err),
187185
}

src/serde/error.rs

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -25,44 +25,21 @@ impl<E> From<E> for DecodeError<E> {
2525
}
2626

2727
#[cfg(feature = "serde1")]
28-
#[cfg(feature = "use_std")]
29-
impl<E: std::error::Error + 'static> serde::de::Error for DecodeError<E> {
28+
impl<E: core::error::Error + 'static> serde::de::Error for DecodeError<E> {
3029
fn custom<T: fmt::Display>(msg: T) -> Self {
3130
DecodeError::Custom(msg.to_string().into_boxed_str())
3231
}
3332
}
3433

35-
#[cfg(feature = "serde1")]
36-
#[cfg(not(feature = "use_std"))]
37-
impl<E: fmt::Debug> serde::de::Error for DecodeError<E> {
38-
fn custom<T: fmt::Display>(msg: T) -> Self {
39-
#[cfg(not(feature = "use_std"))]
40-
use crate::alloc::string::ToString;
41-
42-
DecodeError::Custom(msg.to_string().into_boxed_str())
43-
}
44-
}
45-
4634
impl<E: fmt::Debug> fmt::Display for DecodeError<E> {
4735
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4836
fmt::Debug::fmt(self, f)
4937
}
5038
}
5139

5240
#[cfg(feature = "serde1")]
53-
#[cfg(not(feature = "use_std"))]
5441
impl<E: fmt::Debug> serde::ser::StdError for DecodeError<E> {}
5542

56-
#[cfg(feature = "use_std")]
57-
impl<E: std::error::Error + 'static> std::error::Error for DecodeError<E> {
58-
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
59-
match self {
60-
DecodeError::Core(err) => Some(err),
61-
_ => None
62-
}
63-
}
64-
}
65-
6643
#[derive(Debug)]
6744
pub enum EncodeError<E> {
6845
Core(enc::Error<E>),
@@ -86,40 +63,17 @@ impl<E> From<E> for EncodeError<E> {
8663
}
8764

8865
#[cfg(feature = "serde1")]
89-
#[cfg(feature = "use_std")]
90-
impl<E: std::error::Error + 'static> serde::ser::Error for EncodeError<E> {
66+
impl<E: core::error::Error + 'static> serde::ser::Error for EncodeError<E> {
9167
fn custom<T: fmt::Display>(msg: T) -> Self {
9268
EncodeError::Custom(msg.to_string().into_boxed_str())
9369
}
9470
}
9571

96-
#[cfg(feature = "serde1")]
97-
#[cfg(not(feature = "use_std"))]
98-
impl<E: fmt::Debug> serde::ser::Error for EncodeError<E> {
99-
fn custom<T: fmt::Display>(msg: T) -> Self {
100-
#[cfg(not(feature = "use_std"))]
101-
use crate::alloc::string::ToString;
102-
103-
EncodeError::Custom(msg.to_string().into_boxed_str())
104-
}
105-
}
106-
10772
impl<E: fmt::Debug> fmt::Display for EncodeError<E> {
10873
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
10974
fmt::Debug::fmt(self, f)
11075
}
11176
}
11277

11378
#[cfg(feature = "serde1")]
114-
#[cfg(not(feature = "use_std"))]
11579
impl<E: fmt::Debug> serde::ser::StdError for EncodeError<E> {}
116-
117-
#[cfg(feature = "use_std")]
118-
impl<E: std::error::Error + 'static> std::error::Error for EncodeError<E> {
119-
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
120-
match self {
121-
EncodeError::Core(err) => Some(err),
122-
_ => None
123-
}
124-
}
125-
}

0 commit comments

Comments
 (0)