| 1 |
use crate::lib::{Debug, Display}; |
| 2 |
|
| 3 |
/// Either a re-export of std::error::Error or a new identical trait, depending |
| 4 |
/// on whether Serde's "std" feature is enabled. |
| 5 |
/// |
| 6 |
/// Serde's error traits [`serde::ser::Error`] and [`serde::de::Error`] require |
| 7 |
/// [`std::error::Error`] as a supertrait, but only when Serde is built with |
| 8 |
/// "std" enabled. Data formats that don't care about no\_std support should |
| 9 |
/// generally provide their error types with a `std::error::Error` impl |
| 10 |
/// directly: |
| 11 |
/// |
| 12 |
/// ```edition2021 |
| 13 |
/// #[derive(Debug)] |
| 14 |
/// struct MySerError {...} |
| 15 |
/// |
| 16 |
/// impl serde::ser::Error for MySerError {...} |
| 17 |
/// |
| 18 |
/// impl std::fmt::Display for MySerError {...} |
| 19 |
/// |
| 20 |
/// // We don't support no_std! |
| 21 |
/// impl std::error::Error for MySerError {} |
| 22 |
/// ``` |
| 23 |
/// |
| 24 |
/// Data formats that *do* support no\_std may either have a "std" feature of |
| 25 |
/// their own: |
| 26 |
/// |
| 27 |
/// ```toml |
| 28 |
/// [features] |
| 29 |
/// std = ["serde/std"] |
| 30 |
/// ``` |
| 31 |
/// |
| 32 |
/// ```edition2021 |
| 33 |
/// #[cfg(feature = "std")] |
| 34 |
/// impl std::error::Error for MySerError {} |
| 35 |
/// ``` |
| 36 |
/// |
| 37 |
/// ... or else provide the std Error impl unconditionally via Serde's |
| 38 |
/// re-export: |
| 39 |
/// |
| 40 |
/// ```edition2021 |
| 41 |
/// impl serde::ser::StdError for MySerError {} |
| 42 |
/// ``` |
| 43 |
pub trait Error: Debug + Display { |
| 44 |
/// The underlying cause of this error, if any. |
| 45 |
fn source(&self) -> Option<&(Error + 'static)> { |
| 46 |
None |
| 47 |
} |
| 48 |
} |