Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export std error type so no_std data formats don't need a "std" feature #1620

Merged
merged 1 commit into from Sep 8, 2019
Merged

Conversation

dtolnay
Copy link
Member

@dtolnay dtolnay commented Sep 5, 2019

This PR adds serde::ser::StdError and serde::de::StdError which are either a re-export of std::error::Error (if "std" is enabled) or a new identical trait (otherwise).

#[cfg(feature = "std")]
pub use std::error::Error as StdError;

#[cfg(not(feature = "std"))]
pub trait StdError: Debug + Display {
    fn source(&self) -> Option<&(Error + 'static)> { None }
}

Serde's error traits serde::ser::Error and serde::de::Error require std::error::Error as a supertrait, but only when Serde is built with "std" enabled. Data formats that don't care about no_std support should generally provide their error types with a std::error::Error impl directly:

#[derive(Debug)]
struct MySerError {...}

impl serde::ser::Error for MySerError {...}

impl std::fmt::Display for MySerError {...}

// We don't support no_std!
impl std::error::Error for MySerError {}

Data formats that do support no_std may either have a "std" feature of their own:

[features]
std = ["serde/std"]
#[cfg(feature = "std")]
impl std::error::Error for MySerError {}

... or else provide the std Error impl unconditionally via Serde's re-export:

impl serde::ser::StdError for MySerError {}

FYI @oli-obk, @cbeck88

@dtolnay
Copy link
Member Author

dtolnay commented Sep 5, 2019

It looks like a re-export like this used to exist but was lost to time. This is from 0.7.15:

serde/serde/src/error.rs

Lines 28 to 44 in 78e7488

pub trait Error: Debug + Display {
/// A short description of the error.
///
/// The description should not contain newlines or sentence-ending
/// punctuation, to facilitate embedding in larger user-facing
/// strings.
fn description(&self) -> &str;
/// The lower-level cause of this error, if any.
fn cause(&self) -> Option<&Error> { None }
/// Stubbed! Returns type_id of `()`
#[doc(hidden)]
fn type_id(&self) -> TypeId where Self: 'static {
TypeId::of::<()>()
}
}

#[cfg(feature = "std")]
use std::error;
#[cfg(not(feature = "std"))]
use error;

pub trait Error: Sized + error::Error {

@dtolnay dtolnay merged commit 3343885 into serde-rs:master Sep 8, 2019
@dtolnay dtolnay deleted the error branch September 8, 2019 01:54
Xanewok added a commit to Xanewok/json that referenced this pull request Feb 7, 2020
Relevant Serde PR: serde-rs/serde#1620

To support both no-/std builds without using somewhat noisy
conditional compilation directives, we implement the re-exported
`serde::de::StdError` trait in serde-rs#606.

However, this was only introduced in >= 1.0.100, so we need to bump
the version requirement of serde.

On the off chance of someone pulling in incompatible 1.0.4{5,6} versions
of serde_json, I believe it'd be good to yank those and cut a new
release with this patch.

Sorry for the omission in the original PR.

Fixes serde-rs#621.
Xanewok added a commit to Xanewok/json that referenced this pull request Feb 7, 2020
Relevant Serde PR: serde-rs/serde#1620

To support both no-/std builds without using somewhat noisy
conditional compilation directives, we implement the re-exported
`serde::de::StdError` trait in serde-rs#606.

However, this was only introduced in >= 1.0.100, so we need to bump
the version requirement of serde.

On the off chance of someone pulling in incompatible 1.0.4{5,6} versions
of serde_json, I believe it'd be good to yank those and cut a new
release with this patch.

Sorry for the omission in the original PR.

Fixes serde-rs#621.
Xanewok added a commit to Xanewok/json that referenced this pull request Feb 7, 2020
Relevant Serde PR: serde-rs/serde#1620

To support both no-/std builds without using somewhat noisy
conditional compilation directives, we implement the re-exported
`serde::de::StdError` trait in serde-rs#606.

However, this was only introduced in >= 1.0.100, so we need to bump
the version requirement of serde.

On the off chance of someone pulling in incompatible 1.0.4{5,6} versions
of serde_json, I believe it'd be good to yank those and cut a new
release with this patch.

Sorry for the omission in the original PR.

Fixes serde-rs#612.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant