rust-embedded-community / serde-json-core

`serde-json` for `no_std` programs
Apache License 2.0
161 stars 59 forks source link

Cannot be combined with serde std #63

Closed JarredAllen closed 2 years ago

JarredAllen commented 2 years ago

I'm writing a library which uses serde and which may be used with or without std present. I also need to do some JSON manipulation. The serde-json library depends on alloc, which I can't do, so I have to use this library. However, this library seems to not compile if I compile it with serde having the feature std enabled. Specifically, this is the compile error I get:

error[E0277]: the trait bound `de::Error: StdError` is not satisfied
   --> serde-json-core-0.4.0/src/de/mod.rs:661:6
    |
661 | impl de::Error for Error {
    |      ^^^^^^^^^ the trait `StdError` is not implemented for `de::Error`
    |
note: required by a bound in `serde::de::Error`
   --> serde-1.0.137/src/de/mod.rs:299:1
    |
299 | declare_error_trait!(Error: Sized + StdError);
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `serde::de::Error`
    = note: this error originates in the macro `declare_error_trait` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `ser::Error: StdError` is not satisfied
   --> serde-json-core-0.4.0/src/ser/mod.rs:458:6
    |
458 | impl ser::Error for Error {
    |      ^^^^^^^^^^ the trait `StdError` is not implemented for `ser::Error`
    |
note: required by a bound in `serde::ser::Error`
   --> serde-1.0.137/src/ser/mod.rs:183:1
    |
183 | declare_error_trait!(Error: Sized + StdError);
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `serde::ser::Error`
    = note: this error originates in the macro `declare_error_trait` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0277`.
error: could not compile `serde-json-core` due to 2 previous errors

I encounter this issue if I make a new package and put this crate and serde as dependencies. For reference, it compiles fine if I don't include the std feature on serde. Also, I'm using rust version 1.61.

Can this crate be changed to not break if it gets used with serde with std enabled?

ryan-summers commented 2 years ago

You need to explicitly enable the std feature for serde-json-core, not just serde, if you want this library to compile where serde's std feature is enabled.

JarredAllen commented 2 years ago

Oh, my bad, I missed that this feature exists. Thanks.