Skip to content

Commit 508ec36

Browse files
committed
Unify handling of unit and unit structs in ContentDeserializer
1 parent 627d65c commit 508ec36

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

serde/src/private/de.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,9 +1318,24 @@ mod content {
13181318
where
13191319
V: Visitor<'de>,
13201320
{
1321+
// Covered by tests/test_enum_internally_tagged.rs
1322+
// newtype_unit
13211323
match self.content {
1322-
Content::Unit => visitor.visit_unit(),
1323-
1324+
// As a special case, allow deserializing untagged newtype
1325+
// variant containing unit struct.
1326+
//
1327+
// #[derive(Deserialize)]
1328+
// struct Info;
1329+
//
1330+
// #[derive(Deserialize)]
1331+
// #[serde(tag = "topic")]
1332+
// enum Message {
1333+
// Info(Info),
1334+
// }
1335+
//
1336+
// We want {"topic":"Info"} to deserialize even though
1337+
// ordinarily unit structs do not deserialize from empty map/seq.
1338+
//
13241339
// Allow deserializing newtype variant containing unit.
13251340
//
13261341
// #[derive(Deserialize)]
@@ -1331,7 +1346,8 @@ mod content {
13311346
//
13321347
// We want {"result":"Success"} to deserialize into Response<()>.
13331348
Content::Map(ref v) if v.is_empty() => visitor.visit_unit(),
1334-
_ => Err(self.invalid_type(&visitor)),
1349+
Content::Seq(ref v) if v.is_empty() => visitor.visit_unit(),
1350+
_ => self.deserialize_any(visitor),
13351351
}
13361352
}
13371353

@@ -1343,25 +1359,9 @@ mod content {
13431359
where
13441360
V: Visitor<'de>,
13451361
{
1346-
match self.content {
1347-
// As a special case, allow deserializing untagged newtype
1348-
// variant containing unit struct.
1349-
//
1350-
// #[derive(Deserialize)]
1351-
// struct Info;
1352-
//
1353-
// #[derive(Deserialize)]
1354-
// #[serde(tag = "topic")]
1355-
// enum Message {
1356-
// Info(Info),
1357-
// }
1358-
//
1359-
// We want {"topic":"Info"} to deserialize even though
1360-
// ordinarily unit structs do not deserialize from empty map/seq.
1361-
Content::Map(ref v) if v.is_empty() => visitor.visit_unit(),
1362-
Content::Seq(ref v) if v.is_empty() => visitor.visit_unit(),
1363-
_ => self.deserialize_any(visitor),
1364-
}
1362+
// Covered by tests/test_enum_internally_tagged.rs
1363+
// newtype_unit_struct
1364+
self.deserialize_unit(visitor)
13651365
}
13661366

13671367
fn deserialize_newtype_struct<V>(

0 commit comments

Comments
 (0)