Skip to content
New issue

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

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

Already on GitHub? # to your account

serde Error traits change super trait bounds based on cargo features #1567

Closed
KodrAus opened this issue Jul 10, 2019 · 1 comment
Closed

Comments

@KodrAus
Copy link

KodrAus commented Jul 10, 2019

Hi! 👋

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?

@dtolnay
Copy link
Member

dtolnay commented Nov 24, 2019

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 {}

@dtolnay dtolnay closed this as completed Nov 24, 2019
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Development

No branches or pull requests

2 participants