Allow to flatten IgnoredAny to ignore any additional data

Although any additional fields in struct by default are ignored, sometimes
this can be useful, if you use generic structures, for example
This commit is contained in:
Mingun 2023-04-30 01:18:42 +05:00
parent 732ac49321
commit 51799dd654
2 changed files with 33 additions and 2 deletions

View File

@ -2811,6 +2811,13 @@ where
visitor.visit_unit()
}
fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
visitor.visit_unit()
}
forward_to_deserialize_other! {
deserialize_bool()
deserialize_i8()
@ -2833,7 +2840,6 @@ where
deserialize_tuple(usize)
deserialize_tuple_struct(&'static str, usize)
deserialize_identifier()
deserialize_ignored_any()
}
}

View File

@ -10,7 +10,7 @@
clippy::uninlined_format_args,
)]
use serde::de::{self, MapAccess, Unexpected, Visitor};
use serde::de::{self, IgnoredAny, MapAccess, Unexpected, Visitor};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::collections::{BTreeMap, HashMap};
@ -2494,6 +2494,31 @@ fn test_flatten_option() {
);
}
#[test]
fn test_flatten_ignored_any() {
#[derive(Deserialize, PartialEq, Debug)]
struct Outer {
#[serde(flatten)]
inner: IgnoredAny,
}
assert_de_tokens(
&Outer { inner: IgnoredAny },
&[Token::Map { len: None }, Token::MapEnd],
);
assert_de_tokens(
&Outer { inner: IgnoredAny },
&[
Token::Struct {
name: "DoNotMatter",
len: 0,
},
Token::StructEnd,
],
);
}
#[test]
fn test_transparent_struct() {
#[derive(Serialize, Deserialize, PartialEq, Debug)]