From 0a27e2475af657001592c2da925b6f9f705b9a31 Mon Sep 17 00:00:00 2001 From: Stephane Raux Date: Fri, 10 Mar 2023 16:07:52 -0600 Subject: [PATCH] Add test ensuring that length is provided to the serializer Some serializers (e.g. bincode) require a length to serialize a sequence. This test verifies that mapping a map to a sequence when serializing provides the length to the serializer. For #570 --- serde_with/tests/serde_as/map_tuple_list.rs | 333 ++++++++++++++++++++ 1 file changed, 333 insertions(+) diff --git a/serde_with/tests/serde_as/map_tuple_list.rs b/serde_with/tests/serde_as/map_tuple_list.rs index 202fa80c..59b00d61 100644 --- a/serde_with/tests/serde_as/map_tuple_list.rs +++ b/serde_with/tests/serde_as/map_tuple_list.rs @@ -352,3 +352,336 @@ fn test_map_as_tuple_with_nested_complex_type() { ]"#]], ); } + +#[test] +fn test_map_as_tuple_list_works_with_serializer_that_needs_length_to_serialize_sequence() { + use serde::{ + ser::{ + SerializeMap, SerializeSeq, SerializeStruct, SerializeStructVariant, SerializeTuple, + SerializeTupleStruct, SerializeTupleVariant, + }, + Serializer, + }; + use std::fmt::{self, Debug, Display}; + + #[derive(Debug)] + enum TestError { + LengthRequired, + Other, + } + + impl Display for TestError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + Debug::fmt(self, f) + } + } + + impl std::error::Error for TestError {} + + impl serde::ser::Error for TestError { + fn custom(_: T) -> Self + where + T: Display, + { + TestError::Other + } + } + + struct TestSerializer; + + impl<'a> Serializer for &'a mut TestSerializer { + type Ok = (); + type Error = TestError; + type SerializeSeq = Self; + type SerializeTuple = Self; + type SerializeTupleStruct = Self; + type SerializeTupleVariant = Self; + type SerializeMap = Self; + type SerializeStruct = Self; + type SerializeStructVariant = Self; + + fn serialize_bool(self, _: bool) -> Result { + Ok(()) + } + + fn serialize_i8(self, _: i8) -> Result { + Ok(()) + } + + fn serialize_i16(self, _: i16) -> Result { + Ok(()) + } + + fn serialize_i32(self, _: i32) -> Result { + Ok(()) + } + + fn serialize_i64(self, _: i64) -> Result { + Ok(()) + } + + fn serialize_u8(self, _: u8) -> Result { + Ok(()) + } + + fn serialize_u16(self, _: u16) -> Result { + Ok(()) + } + + fn serialize_u32(self, _: u32) -> Result { + Ok(()) + } + + fn serialize_u64(self, _: u64) -> Result { + Ok(()) + } + + fn serialize_f32(self, _: f32) -> Result { + Ok(()) + } + + fn serialize_f64(self, _: f64) -> Result { + Ok(()) + } + + fn serialize_char(self, _: char) -> Result { + Ok(()) + } + + fn serialize_str(self, _: &str) -> Result { + Ok(()) + } + + fn serialize_bytes(self, _: &[u8]) -> Result { + Ok(()) + } + + fn serialize_none(self) -> Result { + Ok(()) + } + + fn serialize_some(self, v: &T) -> Result + where + T: Serialize + ?Sized, + { + v.serialize(self) + } + + fn serialize_unit(self) -> Result { + Ok(()) + } + + fn serialize_unit_struct(self, _: &'static str) -> Result { + Ok(()) + } + + fn serialize_unit_variant( + self, + _: &'static str, + _: u32, + _: &'static str, + ) -> Result { + Ok(()) + } + + fn serialize_newtype_struct( + self, + _: &'static str, + value: &T, + ) -> Result + where + T: Serialize + ?Sized, + { + value.serialize(self) + } + + fn serialize_newtype_variant( + self, + _: &'static str, + _: u32, + _: &'static str, + value: &T, + ) -> Result + where + T: Serialize + ?Sized, + { + value.serialize(self) + } + + fn serialize_seq(self, len: Option) -> Result { + len.map(|_| self).ok_or(TestError::LengthRequired) + } + + fn serialize_tuple(self, _: usize) -> Result { + Ok(self) + } + + fn serialize_tuple_struct( + self, + _: &'static str, + _: usize, + ) -> Result { + Ok(self) + } + + fn serialize_tuple_variant( + self, + _: &'static str, + _: u32, + _: &'static str, + _: usize, + ) -> Result { + Ok(self) + } + + fn serialize_map(self, _: Option) -> Result { + Ok(self) + } + + fn serialize_struct( + self, + _: &'static str, + _: usize, + ) -> Result { + Ok(self) + } + + fn serialize_struct_variant( + self, + _: &'static str, + _: u32, + _: &'static str, + _: usize, + ) -> Result { + Ok(self) + } + } + + impl<'a> SerializeMap for &'a mut TestSerializer { + type Ok = (); + type Error = TestError; + + fn serialize_key(&mut self, key: &T) -> Result<(), Self::Error> + where + T: Serialize + ?Sized, + { + key.serialize(&mut **self) + } + + fn serialize_value(&mut self, value: &T) -> Result<(), Self::Error> + where + T: Serialize + ?Sized, + { + value.serialize(&mut **self) + } + + fn end(self) -> Result { + Ok(()) + } + } + + impl<'a> SerializeSeq for &'a mut TestSerializer { + type Ok = (); + type Error = TestError; + + fn serialize_element(&mut self, value: &T) -> Result<(), Self::Error> + where + T: Serialize + ?Sized, + { + value.serialize(&mut **self) + } + + fn end(self) -> Result { + Ok(()) + } + } + + impl<'a> SerializeStruct for &'a mut TestSerializer { + type Ok = (); + type Error = TestError; + + fn serialize_field(&mut self, _: &'static str, value: &T) -> Result<(), Self::Error> + where + T: Serialize + ?Sized, + { + value.serialize(&mut **self) + } + + fn end(self) -> Result { + Ok(()) + } + } + + impl<'a> SerializeStructVariant for &'a mut TestSerializer { + type Ok = (); + type Error = TestError; + + fn serialize_field(&mut self, _: &'static str, value: &T) -> Result<(), Self::Error> + where + T: Serialize + ?Sized, + { + value.serialize(&mut **self) + } + + fn end(self) -> Result { + Ok(()) + } + } + + impl<'a> SerializeTuple for &'a mut TestSerializer { + type Ok = (); + type Error = TestError; + + fn serialize_element(&mut self, value: &T) -> Result<(), Self::Error> + where + T: Serialize + ?Sized, + { + value.serialize(&mut **self) + } + + fn end(self) -> Result { + Ok(()) + } + } + + impl<'a> SerializeTupleStruct for &'a mut TestSerializer { + type Ok = (); + type Error = TestError; + + fn serialize_field(&mut self, value: &T) -> Result<(), Self::Error> + where + T: Serialize + ?Sized, + { + value.serialize(&mut **self) + } + + fn end(self) -> Result { + Ok(()) + } + } + + impl<'a> SerializeTupleVariant for &'a mut TestSerializer { + type Ok = (); + type Error = TestError; + + fn serialize_field(&mut self, value: &T) -> Result<(), Self::Error> + where + T: Serialize + ?Sized, + { + value.serialize(&mut **self) + } + + fn end(self) -> Result { + Ok(()) + } + } + + #[serde_as] + #[derive(Debug, Default, Serialize)] + struct Data { + #[serde_as(as = "Seq<(_, _)>")] + xs: HashMap, + } + + Data::default().serialize(&mut TestSerializer).unwrap(); +}