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

Allow multiple flattened maps to see the same fields #1299

Merged
merged 1 commit into from Jun 1, 2018
Merged

Allow multiple flattened maps to see the same fields #1299

merged 1 commit into from Jun 1, 2018

Conversation

dtolnay
Copy link
Member

@dtolnay dtolnay commented Jun 1, 2018

Before this change, flattening anything after a flattened map was nonsensical because the later flattened field would always observe no input fields.

#[derive(Deserialize)]
struct S {
    #[serde(flatten)]
    map: Map<K, V>,
    #[serde(flatten)]
    other: Other, // always empty
}

This change makes a flattened map not consume any of the input fields, leaving them available to later flattened fields in the same struct. The new behavior is useful when two flattened fields that both use deserialize_map care about disjoint subsets of the fields in the input.

#[derive(Deserialize)]
struct S {
    // Looks at fields with a "player1_" prefix.
    #[serde(flatten, with = "prefix_player1")]
    player1: Player,
    // Looks at fields with a "player2_" prefix.
    #[serde(flatten, with = "prefix_player2")]
    player2: Player,
}

Before this change, flattening anything after a flattened map was
nonsensical because the later flattened field would always observe no
input fields.

    #[derive(Deserialize)]
    struct S {
        #[serde(flatten)]
        map: Map<K, V>,
        #[serde(flatten)]
        other: Other, // always empty
    }

This change makes a flattened map not consume any of the input fields,
leaving them available to later flattened fields in the same struct. The
new behavior is useful when two flattened fields that both use
deserialize_map care about disjoint subsets of the fields in the input.

    #[derive(Deserialize)]
    struct S {
        // Looks at fields with a "player1_" prefix.
        #[serde(flatten, with = "prefix_player1")]
        player1: Player,
        // Looks at fields with a "player2_" prefix.
        #[serde(flatten, with = "prefix_player2")]
        player2: Player,
    }
@dtolnay dtolnay merged commit d5ec3ef into serde-rs:master Jun 1, 2018
@dtolnay dtolnay deleted the flattenmap branch June 1, 2018 20:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant