Skip to content

Commit

Permalink
Merge pull request #2436 from Mingun/flatten-ignored-any
Browse files Browse the repository at this point in the history
Allow to flatten `IgnoredAny`
  • Loading branch information
dtolnay committed Jul 6, 2023
2 parents 807bd20 + 51799dd commit 3686277
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion serde/src/de/ignored_any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ use de::{
/// # Ok(())
/// # }
/// ```
#[derive(Copy, Clone, Debug, Default)]
#[derive(Copy, Clone, Debug, Default, PartialEq)]
pub struct IgnoredAny;

impl<'de> Visitor<'de> for IgnoredAny {
Expand Down
8 changes: 7 additions & 1 deletion serde/src/private/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2794,6 +2794,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()
Expand All @@ -2816,7 +2823,6 @@ where
deserialize_tuple(usize)
deserialize_tuple_struct(&'static str, usize)
deserialize_identifier()
deserialize_ignored_any()
}
}

Expand Down
27 changes: 26 additions & 1 deletion test_suite/tests/test_annotations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -2697,6 +2697,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)]
Expand Down

0 comments on commit 3686277

Please sign in to comment.