Skip to content
Myr

Fix roundtrip inconsistency:

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

- deserialization of flatten unit variant is possible - serialization of such variant gives Err("can only flatten structs and maps (got an enum)")

authored by Mingun · committed by Oli Scherer · parent b6f339c · Browse files · 2 files+39 added−2 removed
This commit

State

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

Diff stats

2 files+39 added−2 removed

  • tests:+35 added−0 removed
  • other:+4 added−2 removed

2 files+39 added−2 removed

modified:serde/src/private/ser.rs+4 added-2 removed
52 unmodified lines
53 Optional,54 Sequence,55 Tuple,56 TupleStruct,57 #[cfg(not(any(feature = "std", feature = "alloc")))]58 Enum,59}6061impl Display for Unsupported {
8 unmodified lines
70 Unsupported::Optional => formatter.write_str("an optional"),71 Unsupported::Sequence => formatter.write_str("a sequence"),72 Unsupported::Tuple => formatter.write_str("a tuple"),73 Unsupported::TupleStruct => formatter.write_str("a tuple struct"),74 #[cfg(not(any(feature = "std", feature = "alloc")))]75 Unsupported::Enum => formatter.write_str("an enum"),76 }77 }78}
1017 unmodified lines
1096 fn serialize_unit_variant(1097 self,1098 _: &'static str,1099 _: u32,1098 _: &'static str,1100 variant: &'static str,1101 ) -> Result<Self::Ok, Self::Error> {1100 Err(Self::bad_type(Unsupported::Enum))1102 self.0.serialize_entry(variant, &())1103 }11041105 fn serialize_newtype_struct<T>(1106 self,
256 unmodified lines
modified:test_suite/tests/test_annotations.rs+35 added
2653 unmodified lines
2654 }26552656 #[derive(Debug, PartialEq, Serialize, Deserialize)]2657 enum Enum {2658 Unit,2659 Newtype(HashMap<String, String>),2660 Tuple(u32, u32),2661 Struct { index: u32, value: u32 },2662 }26632664 #[test]2665 fn unit() {2666 let value = Flatten {2667 data: Enum::Unit,2668 extra: HashMap::from_iter([("extra_key".into(), "extra value".into())]),2669 };2670 assert_tokens(2671 &value,2672 &[2673 Token::Map { len: None },2674 // data2675 Token::Str("Unit"), // variant2676 Token::Unit,2677 // extra2678 Token::Str("extra_key"),2679 Token::Str("extra value"),2680 Token::MapEnd,2681 ],2682 );2683 assert_de_tokens(2684 &value,2685 &[2686 Token::Map { len: None },2687 // extra2688 Token::Str("extra_key"),2689 Token::Str("extra value"),2690 // data2691 Token::Str("Unit"), // variant2692 Token::Unit,2693 Token::MapEnd,2694 ],2695 );2696 }26972698 #[test]2699 fn newtype() {2700 assert_tokens(2701 &Flatten {2702 data: Enum::Newtype(HashMap::from_iter([("key".into(), "value".into())])),
310 unmodified lines