cbor icon indicating copy to clipboard operation
cbor copied to clipboard

Incorrect behavior for Option<()>

Open syntacticsugarglider opened this issue 5 years ago • 0 comments

assert_eq!(
        Some(()),
        serde_cbor::from_slice::<Option<()>>(serde_cbor::to_vec(&Some(())).unwrap().as_slice()).unwrap()
);

fails, as serialize_unit is implemented by simply calling serialize_none and serialize_some does not prepend a determinant (per CBOR specification). This leads to this incorrect behavior where a type implementing Serialize + Deserialize is not correctly isomorphic to its wire representation. I'm not fully familiar with the CBOR spec but presumably one could choose to serialize an empty tuple or other sequential structure or maybe a single-variant enum to represent the unit type. This is distinct to the failure to round-trip observed in #5 as the fundamental cause here is the use of 0xF6 as a marker value for both None and () leading to None::<()> and Some(()) having the same representation.

syntacticsugarglider avatar Mar 25 '20 23:03 syntacticsugarglider