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

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

Closed
exzachlyvv opened this issue Dec 24, 2023 · 1 comment · Fixed by serde-rs/serde-rs.github.io#179
Closed

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

exzachlyvv opened this issue Dec 24, 2023 · 1 comment · Fixed by serde-rs/serde-rs.github.io#179
Labels

Comments

@exzachlyvv
Copy link

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
Copy link
Member

dtolnay commented May 15, 2024

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.

@dtolnay dtolnay added the docs label May 15, 2024
zeenix added a commit to zeenix/serde-rs.github.io that referenced this issue Mar 5, 2025
…tures

I was happily writing some code that needs to support no_std and no_alloc
only to find out that it's not possible through the compilation simply
failing:

```rust
error[E0433]: failed to resolve: could not find `ContentRefDeserializer` in `de`
   --> zarlink/src/connection/mod.rs:136:36
    |
136 |         #[derive(Debug, Serialize, Deserialize)]
    |                                    ^^^^^^^^^^^ could not find `ContentRefDeserializer` in `de`
    |
note: found an item that was configured out
   --> /home/zeenix/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/serde-1.0.218/src/private/de.rs:14:35
    |
14  |     Content, ContentDeserializer, ContentRefDeserializer, EnumDeserializer,
    |                                   ^^^^^^^^^^^^^^^^^^^^^^
note: the item is gated here
   --> /home/zeenix/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/serde-1.0.218/src/private/de.rs:12:1
    |
12  | #[cfg(any(feature = "std", feature = "alloc"))]
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    = note: this error originates in the derive macro `Deserialize` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0412]: cannot find type `Content` in module `_serde::__private::de`
   --> zarlink/src/connection/mod.rs:136:36
    |
136 |         #[derive(Debug, Serialize, Deserialize)]
    |                                    ^^^^^^^^^^^ not found in `_serde::__private::de`
    |
note: found an item that was configured out
   --> /home/zeenix/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/serde-1.0.218/src/private/de.rs:14:5
    |
14  |     Content, ContentDeserializer, ContentRefDeserializer, EnumDeserializer,
    |     ^^^^^^^
note: the item is gated here
   --> /home/zeenix/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/serde-1.0.218/src/private/de.rs:12:1
    |
12  | #[cfg(any(feature = "std", feature = "alloc"))]
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    = note: this error originates in the derive macro `Deserialize` (in Nightly builds, run with -Z macro-backtrace for more info)
```

Fixes serde-rs/serde#2668.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2 participants