serde-rs / serde

Serialization framework for Rust
https://serde.rs/
Apache License 2.0
8.82k stars 748 forks source link

Enum with `tag` does not work in `#[no_std]` environment #2668

Open exzachlyvv opened 6 months ago

exzachlyvv commented 6 months ago

I'm using a serde enum in a #[no_std] environment. When using tag, the code fails to compile with error:

  |
6 | #[derive(Deserialize, PartialEq, Debug)]
  |          ^^^^^^^^^^^ could not find `TaggedContentVisitor` in `de`
  |
  = note: this error originates in the derive macro `Deserialize` (in Nightly builds, run with -Z macro-backtrace for more info)

and

  |
6 | #[derive(Deserialize, PartialEq, Debug)]
  |          ^^^^^^^^^^^
  |          |
  |          could not find `ContentDeserializer` in `de`
  |          help: a struct with a similar name exists: `BytesDeserializer`
  |
  = note: this error originates in the derive macro `Deserialize` (in Nightly builds, run with -Z macro-backtrace for more info)

Example Code:

use serde::Deserialize;

#[derive(Deserialize, PartialEq, Debug)]
// #[serde(rename_all = "snake_case")] <-- compiles
#[serde(tag = "type", rename_all = "snake_case")] <-- doesn't compile
pub enum MyEnum {
    MyVariant
}

I have tried this same code in a std environment and it works as expected.

dtolnay commented 1 month ago

I don't know any way to not show those errors. I created #2742 which adds another error saying "Serde's `tag` attribute requires either "alloc" or "std" feature to be enabled on the serde crate", but I don't know if this is really better.

I think I would lean toward just adding documentation in whatever is the right place. This is already documented in https://serde.rs/no-std.html#derive, but we can add another note about it in https://serde.rs/container-attrs.html.