Merge pull request #2802 from jonhoo/flatten-unit-struct
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Support (de-)serializing flattened unit struct
authored by David Tolnay · committed by GitHub · parents 1b4da41, 4036ff8 · 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
Provenance
- Commit
bfbedac- Tree
03a4a2f- Author
David Tolnay <dtolnay@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
1b4da41- Parent 2
4036ff8
3 files changed+38 added−6 removed
modified:serde/src/private/de.rs+11 added-1 removed
2708 unmodified lines2709 {2710 visitor.visit_unit()2711 }2712 2713+ 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+ }2723+2724 fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>2725 where2726 V: Visitor<'de>,2727 {16 unmodified lines2744 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 linesmodified:serde/src/private/ser.rs+1 added-5 removed
49 unmodified lines50 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 lines66 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 lines1087 Ok(())1088 }1089 1090 fn serialize_unit_struct(self, _: &'static str) -> Result<Self::Ok, Self::Error> {1095- Err(Self::bad_type(Unsupported::UnitStruct))1091+ Ok(())1092 }1093 1094 fn serialize_unit_variant(1095 self,265 unmodified linesmodified:test_suite/tests/test_annotations.rs+26 added
1814 unmodified lines1815 );1816 }1817 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+1844+#[test]1845 fn test_flatten_unsupported_type() {1846 #[derive(Debug, PartialEq, Serialize, Deserialize)]1847 struct Outer {1848 outer: String,1462 unmodified lines