diff --git a/src/ser.rs b/src/ser.rs index b8cec35..8f4e72b 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -741,9 +741,9 @@ impl<'a, 'b> ser::Serializer for &'b mut Serializer<'a> { type Ok = (); type Error = Error; type SerializeSeq = SerializeSeq<'a, 'b>; - type SerializeTuple = ser::Impossible<(), Error>; - type SerializeTupleStruct = ser::Impossible<(), Error>; - type SerializeTupleVariant = ser::Impossible<(), Error>; + type SerializeTuple = SerializeSeq<'a, 'b>; + type SerializeTupleStruct = SerializeSeq<'a, 'b>; + type SerializeTupleVariant = SerializeSeq<'a, 'b>; type SerializeMap = SerializeTable<'a, 'b>; type SerializeStruct = SerializeTable<'a, 'b>; type SerializeStructVariant = ser::Impossible<(), Error>; @@ -892,23 +892,23 @@ impl<'a, 'b> ser::Serializer for &'b mut Serializer<'a> { }) } - fn serialize_tuple(self, _len: usize) + fn serialize_tuple(self, len: usize) -> Result { - Err(Error::UnsupportedType) + self.serialize_seq(Some(len)) } - fn serialize_tuple_struct(self, _name: &'static str, _len: usize) + fn serialize_tuple_struct(self, _name: &'static str, len: usize) -> Result { - Err(Error::UnsupportedType) + self.serialize_seq(Some(len)) } fn serialize_tuple_variant(self, _name: &'static str, _variant_index: u32, _variant: &'static str, - _len: usize) + len: usize) -> Result { - Err(Error::UnsupportedType) + self.serialize_seq(Some(len)) } fn serialize_map(self, _len: Option) @@ -998,6 +998,51 @@ impl<'a, 'b> ser::SerializeSeq for SerializeSeq<'a, 'b> { } } +impl<'a, 'b> ser::SerializeTuple for SerializeSeq<'a, 'b> { + type Ok = (); + type Error = Error; + + fn serialize_element(&mut self, value: &T) -> Result<(), Error> + where T: ser::Serialize, + { + ser::SerializeSeq::serialize_element(self, value) + } + + fn end(self) -> Result<(), Error> { + ser::SerializeSeq::end(self) + } +} + +impl<'a, 'b> ser::SerializeTupleVariant for SerializeSeq<'a, 'b> { + type Ok = (); + type Error = Error; + + fn serialize_field(&mut self, value: &T) -> Result<(), Error> + where T: ser::Serialize, + { + ser::SerializeSeq::serialize_element(self, value) + } + + fn end(self) -> Result<(), Error> { + ser::SerializeSeq::end(self) + } +} + +impl<'a, 'b> ser::SerializeTupleStruct for SerializeSeq<'a, 'b> { + type Ok = (); + type Error = Error; + + fn serialize_field(&mut self, value: &T) -> Result<(), Error> + where T: ser::Serialize, + { + ser::SerializeSeq::serialize_element(self, value) + } + + fn end(self) -> Result<(), Error> { + ser::SerializeSeq::end(self) + } +} + impl<'a, 'b> ser::SerializeMap for SerializeTable<'a, 'b> { type Ok = (); type Error = Error; @@ -1543,9 +1588,9 @@ impl ser::Serializer for Categorize { type Ok = Category; type Error = E; type SerializeSeq = Self; - type SerializeTuple = ser::Impossible; - type SerializeTupleStruct = ser::Impossible; - type SerializeTupleVariant = ser::Impossible; + type SerializeTuple = Self; + type SerializeTupleStruct = Self; + type SerializeTupleVariant = Self; type SerializeMap = Self; type SerializeStruct = Self; type SerializeStructVariant = ser::Impossible; @@ -1639,15 +1684,15 @@ impl ser::Serializer for Categorize { } fn serialize_tuple(self, _: usize) -> Result { - Err(ser::Error::custom("unsupported")) + Ok(self) } fn serialize_tuple_struct(self, _: &'static str, _: usize) -> Result { - Err(ser::Error::custom("unsupported")) + Ok(self) } fn serialize_tuple_variant(self, _: &'static str, _: u32, _: &'static str, _: usize) -> Result { - Err(ser::Error::custom("unsupported")) + Ok(self) } fn serialize_map(self, _: Option) -> Result { @@ -1677,6 +1722,48 @@ impl ser::SerializeSeq for Categorize { } } +impl ser::SerializeTuple for Categorize { + type Ok = Category; + type Error = E; + + fn serialize_element(&mut self, _: &T) + -> Result<(), Self::Error> { + Ok(()) + } + + fn end(self) -> Result { + Ok(Category::Array) + } +} + +impl ser::SerializeTupleVariant for Categorize { + type Ok = Category; + type Error = E; + + fn serialize_field(&mut self, _: &T) + -> Result<(), Self::Error> { + Ok(()) + } + + fn end(self) -> Result { + Ok(Category::Array) + } +} + +impl ser::SerializeTupleStruct for Categorize { + type Ok = Category; + type Error = E; + + fn serialize_field(&mut self, _: &T) + -> Result<(), Self::Error> { + Ok(()) + } + + fn end(self) -> Result { + Ok(Category::Array) + } +} + impl ser::SerializeMap for Categorize { type Ok = Category; type Error = E; diff --git a/test-suite/tests/serde.rs b/test-suite/tests/serde.rs index 57fa5db..446cd7a 100644 --- a/test-suite/tests/serde.rs +++ b/test-suite/tests/serde.rs @@ -569,10 +569,23 @@ fn table_structs_empty() { expected.insert("bar".to_string(), CanBeEmpty::default()); expected.insert("baz".to_string(), CanBeEmpty::default()); expected.insert( - "bazv".to_string(), + "bazv".to_string(), CanBeEmpty {a: Some("foo".to_string()), b: None}, ); expected.insert("foo".to_string(), CanBeEmpty::default()); assert_eq!(value, expected); assert_eq!(toml::to_string(&value).unwrap(), text); } + +#[test] +fn fixed_size_array() { + #[derive(Serialize, PartialEq, Eq, Deserialize, Debug)] + struct Entity { + pos: [i32; 2] + } + let text = "pos = [1, 2]\n"; + let value: Entity = toml::from_str(text).unwrap(); + let expected = Entity { pos: [1, 2] }; + assert_eq!(value, expected); + assert_eq!(toml::to_string(&value).unwrap(), text); +}