Skip to content
Myr

std_error.rs

This file

Path

serde_core/src/std_error.rs

48 lines · 1.3 KiB · Rust

Last changed by

Merge pull request #2608 from osiewicz/extract_serde_coreDavid Tolnaybefore adoption723fcac

History of this file

Take it

1use crate::lib::{Debug, Display};23/// Either a re-export of std::error::Error or a new identical trait, depending4/// on whether Serde's "std" feature is enabled.5///6/// Serde's error traits [`serde::ser::Error`] and [`serde::de::Error`] require7/// [`std::error::Error`] as a supertrait, but only when Serde is built with8/// "std" enabled. Data formats that don't care about no\_std support should9/// generally provide their error types with a `std::error::Error` impl10/// directly:11///12/// ```edition202113/// #[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 of25/// their own:26///27/// ```toml28/// [features]29/// std = ["serde/std"]30/// ```31///32/// ```edition202133/// #[cfg(feature = "std")]34/// impl std::error::Error for MySerError {}35/// ```36///37/// ... or else provide the std Error impl unconditionally via Serde's38/// re-export:39///40/// ```edition202141/// impl serde::ser::StdError for MySerError {}42/// ```43pub trait Error: Debug + Display {44    /// The underlying cause of this error, if any.45    fn source(&self) -> Option<&(dyn Error + 'static)> {46        None47    }48}