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

Maps don't consume inputs when used in flatten structs? #1909

Closed
Mingun opened this issue Oct 15, 2020 · 3 comments
Closed

Maps don't consume inputs when used in flatten structs? #1909

Mingun opened this issue Oct 15, 2020 · 3 comments

Comments

@Mingun
Copy link
Contributor

Mingun commented Oct 15, 2020

I'm wonder of existence of that test:

fn test_flatten_map_twice() {
#[derive(Debug, PartialEq, Deserialize)]
struct Outer {
#[serde(flatten)]
first: BTreeMap<String, String>,
#[serde(flatten)]
between: Inner,
#[serde(flatten)]
second: BTreeMap<String, String>,
}
#[derive(Debug, PartialEq, Deserialize)]
struct Inner {
y: String,
}
assert_de_tokens(
&Outer {
first: {
let mut first = BTreeMap::new();
first.insert("x".to_owned(), "X".to_owned());
first.insert("y".to_owned(), "Y".to_owned());
first
},
between: Inner { y: "Y".to_owned() },
second: {
let mut second = BTreeMap::new();
second.insert("x".to_owned(), "X".to_owned());
second
},
},
&[
Token::Map { len: None },
Token::Str("x"),
Token::Str("X"),
Token::Str("y"),
Token::Str("Y"),
Token::MapEnd,
],
);
}

Why first map don't consume all keys? It seems to me that it should. Current behavior is very unintuitive

@jonasbb
Copy link
Contributor

jonasbb commented Nov 22, 2020

Have you read the commit message of the commit introducing the code? It explains why. 5ee2fc0

@Mingun
Copy link
Contributor Author

Mingun commented Nov 23, 2020

No, I didn't read, thanks for pointing it. But anyway, this isn't explains why such behaviour considered as correct. Custom struct type interested only in some keys and consumes them from the input. Map<K, V> is a degenerate case of such struct that interested in all keys. I don't understand why it shouldn't consume them.

@dtolnay
Copy link
Member

dtolnay commented Jan 23, 2022

I think my preference would be to stick with the current behavior on this.

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

3 participants