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

Feature request: Allow #[serde(rename = "...")] on tuple struct / tuple variant member fields #1510

Closed
jplatte opened this issue Apr 20, 2019 · 4 comments

Comments

@jplatte
Copy link
Contributor

jplatte commented Apr 20, 2019

Here is some working code that I would love to be able to condense:

/// The authentication mechanism.
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(tag = "type")]
pub enum LoginType {
    /// A password is supplied to authenticate.
    #[serde(rename = "m.login.password")]
    Password {
        password: String
    },
    /// Token-based login.
    #[serde(rename = "m.login.token")]
    Token {
        token: String
    },
}

I tried like this, but got Error("invalid type: map, expected a string") on deserialization:

/// The authentication mechanism.
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(tag = "type")]
pub enum LoginType {
    /// A password is supplied to authenticate.
    #[serde(rename = "m.login.password")]
    Password(#[serde(rename = "password")] String),
    /// Token-based login.
    #[serde(rename = "m.login.token")]
    Token(#[serde(rename = "token")] String),
}

This looks like something that could be supported to me. Or if it is deemed too niche + cryptic the additional rename should probably compile-error since it doesn't have any effect, right?

@jplatte
Copy link
Contributor Author

jplatte commented Dec 15, 2019

@dtolnay Would you accept a PR that implements this?

@dtolnay
Copy link
Member

dtolnay commented Dec 16, 2019

I would prefer not to support this. I agree it's something that we could support, but I don't think the benefit will be big enough to justify adding logic to serde_derive.

One thing I would gladly accept though is any simplifications to serde_derive, to make the implementation more straightforward or compile faster.

@jplatte
Copy link
Contributor Author

jplatte commented Dec 16, 2019

That's unfortunate. So should this issue be closed then?

Re. simplifications: Are you talking about the raw compile time of serde_derive, or about the compile time of the code it generates (or both)? I haven't previously done any compile time profiling, but I've heard Rust has pretty decent tooling for this, so I might give it a try.

@dtolnay
Copy link
Member

dtolnay commented Jan 21, 2020

Are you talking about the raw compile time of serde_derive, or about the compile time of the code it generates (or both)?

Both of those are good things!

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

2 participants