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

Explicit enum discriminants (variant_index) are not supported #2763

Open
ikhomyakov opened this issue Jun 26, 2024 · 0 comments
Open

Explicit enum discriminants (variant_index) are not supported #2763

ikhomyakov opened this issue Jun 26, 2024 · 0 comments

Comments

@ikhomyakov
Copy link

As described in the Rust reference, enum discriminant values (a.k.a. variant_indexes) can be assigned explicitly. For example:

#[repr(u8)]
#[derive(Serialize, Deserialize)]
enum Enum {
    Unit = 10,
    Tuple(u16) = 11,
    Struct {
        a: u8,
        b: u16,
    } = 12,
}

The problem is that these values do not get passed down to the serialization methods. For example, if we serialize Enum::Struct { a: 1, b: 2 }, it invokes serialize_struct_variant(self, name = "Enum", variant_index = 2, variant = "Struct", len = 2), whereas it would be reasonable to expect the true enum discriminant variant_index = 12.

Since changing this behavior would introduce a breaking change, it would be helpful to have a container-level attribute (e.g., #[serde(explicit_tags)]) that supports to the true discriminant values. Additionally, if you know of any current workarounds, we would appreciate it.

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

No branches or pull requests

1 participant