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

Cannot derive remote serialize for #[non_exhaustive] enum #1991

Closed
abonander opened this issue Feb 26, 2021 · 6 comments · Fixed by #2570
Closed

Cannot derive remote serialize for #[non_exhaustive] enum #1991

abonander opened this issue Feb 26, 2021 · 6 comments · Fixed by #2570
Labels

Comments

@abonander
Copy link

Attempting to use a remote derive for gstreamer_webrtc::WebRTCSDPType:

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "snake_case", remote = "WebRTCSDPType")]
#[non_exhaustive]
pub enum SdpType {
    /// offer
    Offer,
    /// pranswer
    Pranswer,
    /// answer
    Answer,
    /// rollback
    Rollback,
}

produces the following error:

error[E0004]: non-exhaustive patterns: `_` not covered
  --> src/foo.rs:96:10
   |
96 | #[derive(Serialize, Deserialize)]
   |          ^^^^^^^^^ pattern `_` not covered
   |
   = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
   = note: the matched value is of type `WebRTCSDPType`
   = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

This can't be reproduced in a single-crate example, however, because #[non_exhaustive] isn't enforced for crate-local enums: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=084b2a4a7888fc0069d5b368e0bc6d0f

@soywod
Copy link

soywod commented Oct 22, 2021

I got the same issue, how to deal with it?

@anden3
Copy link

anden3 commented Nov 5, 2021

I have the same issue, so I resorted to patching the remote crate for now.

@rhysd
Copy link

rhysd commented Aug 21, 2023

Compile error is different, but I see a similar issue on serde v1.0.184.

I created a small reproduction.

#[non_exhaustive]
#[derive(Serialize)]
enum E {
  S(String),
}

Compiling this code with rustc v1.71.1 caused the following error:

error[E0507]: cannot move out of `*self` which is behind a shared reference
    --> src\keyboard.rs:1601:17
     |
1601 | #[derive(Debug, Serialize)]
     |                 ^^^^^^^^^
     |                 |
     |                 data moved here
     |                 move occurs because `unrecognized` has type `E`, which does not implement the `Copy` trait
     |
     = note: this error originates in the derive macro `Serialize` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider borrowing here
     |
1601 | #[derive(Debug, &Serialize)]
     |                 +

For more information about this error, try `rustc --explain E0507`.

@dtolnay
Copy link
Member

dtolnay commented Aug 21, 2023

Fixed in serde_derive 1.0.185.

I wasn't able to build gstreamer-webrtc on my machine, but I tested with std::io::ErrorKind which should be effectively equivalent; please let me know if there is still any issue with the original use case.

use serde::{Deserialize, Serialize};

#[derive(Deserialize, Serialize)]
#[serde(remote = "std::io::ErrorKind")]
#[non_exhaustive]
pub enum ErrorKind {
    NotFound,
    PermissionDenied,
}

@dtolnay dtolnay closed this as completed Aug 21, 2023
@rhysd
Copy link

rhysd commented Aug 21, 2023

I confirmed the fix. Thanks for the quick release.

@Dominilk
Copy link

This needs to be documented more clearly. It's not mentioned in https://serde.rs/remote-derive.html. Error message is misleading.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging a pull request may close this issue.

6 participants