Skip to content
Myr

Support (de-)serializing flattened unit struct

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Fixes #2801.

authored by Jon Gjengset · committed by Jon Gjengset · parent 1b4da41 · Browse files · 3 files+38 added−6 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+38 added−6 removed

  • tests:+26 added−0 removed
  • other:+12 added−6 removed

3 files+38 added−6 removed

modified:serde/src/private/de.rs+11 added-1 removed
2708 unmodified lines
2709 {2710 visitor.visit_unit()2711 }27122713 fn deserialize_unit_struct<V>(2714 self,2715 _name: &'static str,2716 visitor: V,2717 ) -> Result<V::Value, Self::Error>2718 where2719 V: Visitor<'de>,2720 {2721 visitor.visit_unit()2722 }27232724 fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>2725 where2726 V: Visitor<'de>,2727 {
16 unmodified lines
2744 deserialize_str()2745 deserialize_string()2746 deserialize_bytes()2747 deserialize_byte_buf()2737 deserialize_unit_struct(&'static str)2748 deserialize_seq()2749 deserialize_tuple(usize)2750 deserialize_tuple_struct(&'static str, usize)2751 deserialize_identifier()
158 unmodified lines
modified:serde/src/private/ser.rs+1 added-5 removed
49 unmodified lines
50 Char,51 String,52 ByteArray,53 Optional,54 #[cfg(any(feature = "std", feature = "alloc"))]55 UnitStruct,54 Sequence,55 Tuple,56 TupleStruct,57 Enum,
8 unmodified lines
66 Unsupported::Char => formatter.write_str("a char"),67 Unsupported::String => formatter.write_str("a string"),68 Unsupported::ByteArray => formatter.write_str("a byte array"),69 Unsupported::Optional => formatter.write_str("an optional"),72 #[cfg(any(feature = "std", feature = "alloc"))]73 Unsupported::UnitStruct => formatter.write_str("unit struct"),70 Unsupported::Sequence => formatter.write_str("a sequence"),71 Unsupported::Tuple => formatter.write_str("a tuple"),72 Unsupported::TupleStruct => formatter.write_str("a tuple struct"),73 Unsupported::Enum => formatter.write_str("an enum"),
1013 unmodified lines
1087 Ok(())1088 }10891090 fn serialize_unit_struct(self, _: &'static str) -> Result<Self::Ok, Self::Error> {1095 Err(Self::bad_type(Unsupported::UnitStruct))1091 Ok(())1092 }10931094 fn serialize_unit_variant(1095 self,
265 unmodified lines
modified:test_suite/tests/test_annotations.rs+26 added
1814 unmodified lines
1815 );1816}18171818#[test]1819fn test_flatten_unit_struct() {1820 #[derive(Debug, PartialEq, Serialize, Deserialize)]1821 struct Response<T> {1822 #[serde(flatten)]1823 data: T,1824 status: usize,1825 }18261827 #[derive(Debug, PartialEq, Serialize, Deserialize)]1828 struct Unit;18291830 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}18431844#[test]1845fn test_flatten_unsupported_type() {1846 #[derive(Debug, PartialEq, Serialize, Deserialize)]1847 struct Outer {1848 outer: String,
1462 unmodified lines