Skip to content

Commit

Permalink
Merge pull request #1874 from dtolnay/flatunit
Browse files Browse the repository at this point in the history
Support flattening a Unit
  • Loading branch information
dtolnay authored Aug 10, 2020
2 parents 53b9871 + bf76f50 commit e6f086d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
8 changes: 7 additions & 1 deletion serde/src/private/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2763,6 +2763,13 @@ where
}
}

fn deserialize_unit<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 @@ -2780,7 +2787,6 @@ where
deserialize_string()
deserialize_bytes()
deserialize_byte_buf()
deserialize_unit()
deserialize_unit_struct(&'static str)
deserialize_seq()
deserialize_tuple(usize)
Expand Down
2 changes: 1 addition & 1 deletion serde/src/private/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1124,7 +1124,7 @@ where
}

fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
Err(Self::bad_type(Unsupported::Unit))
Ok(())
}

fn serialize_unit_struct(self, _: &'static str) -> Result<Self::Ok, Self::Error> {
Expand Down
23 changes: 23 additions & 0 deletions test_suite/tests/test_annotations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1967,6 +1967,29 @@ fn test_flatten_map_twice() {
);
}

#[test]
fn test_flatten_unit() {
#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct Response<T> {
#[serde(flatten)]
data: T,
status: usize,
}

assert_tokens(
&Response {
data: (),
status: 0,
},
&[
Token::Map { len: None },
Token::Str("status"),
Token::U64(0),
Token::MapEnd,
],
);
}

#[test]
fn test_flatten_unsupported_type() {
#[derive(Debug, PartialEq, Serialize, Deserialize)]
Expand Down

0 comments on commit e6f086d

Please sign in to comment.