Skip to content

Commit 4036ff8

Browse files
committed
Support (de-)serializing flattened unit struct
Fixes #2801.
1 parent 1b4da41 commit 4036ff8

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

serde/src/private/de.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -2710,6 +2710,17 @@ where
27102710
visitor.visit_unit()
27112711
}
27122712

2713+
fn deserialize_unit_struct<V>(
2714+
self,
2715+
_name: &'static str,
2716+
visitor: V,
2717+
) -> Result<V::Value, Self::Error>
2718+
where
2719+
V: Visitor<'de>,
2720+
{
2721+
visitor.visit_unit()
2722+
}
2723+
27132724
fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
27142725
where
27152726
V: Visitor<'de>,
@@ -2734,7 +2745,6 @@ where
27342745
deserialize_string()
27352746
deserialize_bytes()
27362747
deserialize_byte_buf()
2737-
deserialize_unit_struct(&'static str)
27382748
deserialize_seq()
27392749
deserialize_tuple(usize)
27402750
deserialize_tuple_struct(&'static str, usize)

serde/src/private/ser.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ enum Unsupported {
5151
String,
5252
ByteArray,
5353
Optional,
54-
#[cfg(any(feature = "std", feature = "alloc"))]
55-
UnitStruct,
5654
Sequence,
5755
Tuple,
5856
TupleStruct,
@@ -69,8 +67,6 @@ impl Display for Unsupported {
6967
Unsupported::String => formatter.write_str("a string"),
7068
Unsupported::ByteArray => formatter.write_str("a byte array"),
7169
Unsupported::Optional => formatter.write_str("an optional"),
72-
#[cfg(any(feature = "std", feature = "alloc"))]
73-
Unsupported::UnitStruct => formatter.write_str("unit struct"),
7470
Unsupported::Sequence => formatter.write_str("a sequence"),
7571
Unsupported::Tuple => formatter.write_str("a tuple"),
7672
Unsupported::TupleStruct => formatter.write_str("a tuple struct"),
@@ -1092,7 +1088,7 @@ where
10921088
}
10931089

10941090
fn serialize_unit_struct(self, _: &'static str) -> Result<Self::Ok, Self::Error> {
1095-
Err(Self::bad_type(Unsupported::UnitStruct))
1091+
Ok(())
10961092
}
10971093

10981094
fn serialize_unit_variant(

test_suite/tests/test_annotations.rs

+26
Original file line numberDiff line numberDiff line change
@@ -1815,6 +1815,32 @@ fn test_flatten_unit() {
18151815
);
18161816
}
18171817

1818+
#[test]
1819+
fn test_flatten_unit_struct() {
1820+
#[derive(Debug, PartialEq, Serialize, Deserialize)]
1821+
struct Response<T> {
1822+
#[serde(flatten)]
1823+
data: T,
1824+
status: usize,
1825+
}
1826+
1827+
#[derive(Debug, PartialEq, Serialize, Deserialize)]
1828+
struct Unit;
1829+
1830+
assert_tokens(
1831+
&Response {
1832+
data: Unit,
1833+
status: 0,
1834+
},
1835+
&[
1836+
Token::Map { len: None },
1837+
Token::Str("status"),
1838+
Token::U64(0),
1839+
Token::MapEnd,
1840+
],
1841+
);
1842+
}
1843+
18181844
#[test]
18191845
fn test_flatten_unsupported_type() {
18201846
#[derive(Debug, PartialEq, Serialize, Deserialize)]

0 commit comments

Comments
 (0)