Merge pull request #1874 from dtolnay/flatunit

Support flattening a Unit
This commit is contained in:
David Tolnay 2020-08-10 15:50:30 -07:00 committed by GitHub
commit e6f086d85e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 2 deletions

View File

@ -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()
@ -2780,7 +2787,6 @@ where
deserialize_string()
deserialize_bytes()
deserialize_byte_buf()
deserialize_unit()
deserialize_unit_struct(&'static str)
deserialize_seq()
deserialize_tuple(usize)

View File

@ -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> {

View File

@ -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)]