serde Error traits change super trait bounds based on cargo features

Created on 10 Jul 2019  路  1Comment  路  Source: serde-rs/serde

Hi! :wave:

The ser::Error and de::Error traits conditionally require a std::error::Error bound when serde is compiled with the std feature (enabled by default).

This can be problematic for crates offering Serializers or Deserializers that conditionally work in no-std environments, because they can't tell internally whether serde is pulled in with its std feature or not (this can also happen when including a library like serde-test as a dev dependency).

In a future with a unified std this might just be a non-issue, but is a bit of a head-scratcher at the moment. Some work-arounds I've thought of that might help crates in this position are:


Offer some newtype structs in serde that always implement the appropriate error trait, but require a consistent bound from the type they wrap:

pub struct ErrorMsg<E>(E);

impl<E> ser::Error for ErrorMsg<E>
where
    E: fmt::Display,
{
    ..
}

That internal bound could be something like fmt::Display, or some serde::ser::IntoError trait.


Offer a way to derive ser::Error or de::Error on caller structs that also handles std::error::Error if it's needed.


Offer a macro in serde like serde_if_std, so that crates could produce a nicer user-facing error message if their expectations of the error bounds don't align with serde's.

Note this one could be enough for an external crate to look at building derive support.


What do you think?

Most helpful comment

I believe this is fixed with the reexports in 1.0.100:
https://github.com/serde-rs/serde/releases/tag/v1.0.100

Data formats can provide the right Error impl whether or not Serde is built with "std" by writing:

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

impl serde::de::StdError for MyDeError {}

>All comments

I believe this is fixed with the reexports in 1.0.100:
https://github.com/serde-rs/serde/releases/tag/v1.0.100

Data formats can provide the right Error impl whether or not Serde is built with "std" by writing:

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

impl serde::de::StdError for MyDeError {}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

pwoolcoc picture pwoolcoc  路  3Comments

dtolnay picture dtolnay  路  3Comments

dtolnay picture dtolnay  路  3Comments

dtolnay picture dtolnay  路  3Comments

tikue picture tikue  路  4Comments