-
-
Notifications
You must be signed in to change notification settings - Fork 803
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
Derive permits internally tagged enum with tuple variants, errors at runtime #1307
Comments
In Serde we categorize #[derive(Serialize, Deserialize)]
#[serde(tag = "type")]
enum Foo {
Bar(Inner),
}
#[derive(Serialize, Deserialize)]
struct Inner {
q: String,
} The names of local variables in serde_derive like |
For a representation like #[derive(Serialize, Deserialize)]
#[serde(tag = "type", content = "__field0")]
enum Foo {
Bar(String),
Baz(String),
} |
It's still an error at runtime to try and serialize an internally tagged newtype variant that wraps a primitive datatype; shouldn't that be a compile-time error emitted by the derive instead? It would have saved me a lot of time and confusion. |
I don't know of a way to make that a compile time error. Macro expansion of |
I guess that is a problem, though you could do it with custom OIBITs if those ever become stable: pub trait InternallyTaggable {}
impl InternallyTaggable for ... {}
impl !InternallyTaggable for String {} // etc. Do what you want with this issue then, but I would advise maybe duplicating the definitions of the different kinds of variants to the Enum Representations page and maybe make it clear that Serde-primitive datatypes can't be used with internally tagged newtype variants. The error when attempting to deserialize is also quite unhelpful but I'm not sure what can be done about that either. |
Neat idea! Thanks. I filed #1308 to follow up on the custom OIBIT and we can track the documentation aspect in this issue. |
@dtolnay for the common case where the primitive datatype isn't aliased, can we have it error then? There could be false-positives if the user shadows the type name ( |
So what is the answer now? For example consider following enum #[derive(Serialize)]
#[serde(tag = "mytag")]
enum MyEnum {
NoMeaningfulNameForValue(i32),
Some { other: Struct }
} and i'd like it to be able to serialize into both Artificial Or at least make it compile time error |
The Enum Representations page specifies the following:
I take that to mean that the following will produce a compile-time error, because there's not really a good way to format internally tagged enums with tuple variants:
However, it turns out that this only becomes an error at runtime, and only when serializing the specific variant: http://play.rust-lang.org/?gist=9f5656641805cd794d79ef715bcf850e&version=stable&mode=debug
Deserialization, on the other hand, just produces a hugely unhelpful error if you try something like
{ "type": "Foo", "__field0": "some string" }
:The text was updated successfully, but these errors were encountered: