diff options
author | Alex Crichton <alex@alexcrichton.com> | 2017-07-06 07:34:45 -0700 |
---|---|---|
committer | Alex Crichton <alex@alexcrichton.com> | 2017-07-06 07:34:45 -0700 |
commit | ad5bd8758e60fc918423bbbf3dedf04037487d62 (patch) | |
tree | c2ac2070b99883d64ee6207839090cc1db4c77c9 | |
parent | f6354998a5083fdda2f34560e7f64b64ecf61f4a (diff) | |
download | milf-rs-ad5bd8758e60fc918423bbbf3dedf04037487d62.tar.gz milf-rs-ad5bd8758e60fc918423bbbf3dedf04037487d62.zip |
Support deserializing newtypes
Closes #196
-rw-r--r-- | src/de.rs | 13 | ||||
-rw-r--r-- | src/value.rs | 12 | ||||
-rw-r--r-- | tests/serde.rs | 16 |
3 files changed, 38 insertions, 3 deletions
@@ -248,7 +248,6 @@ impl<'de, 'b> de::Deserializer<'de> for &'b mut Deserializer<'de> { } } - forward_to_deserialize_any! { bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq bytes byte_buf map struct unit newtype_struct @@ -560,9 +559,19 @@ impl<'de> de::Deserializer<'de> for ValueDeserializer<'de> { } } + fn deserialize_newtype_struct<V>( + self, + _name: &'static str, + visitor: V + ) -> Result<V::Value, Error> + where V: de::Visitor<'de> + { + visitor.visit_newtype_struct(self) + } + forward_to_deserialize_any! { bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq - bytes byte_buf map unit newtype_struct identifier + bytes byte_buf map unit identifier ignored_any unit_struct tuple_struct tuple } } diff --git a/src/value.rs b/src/value.rs index 5efdb92..056eac4 100644 --- a/src/value.rs +++ b/src/value.rs @@ -544,10 +544,20 @@ impl<'de> de::Deserializer<'de> for Value { visitor.visit_some(self) } + fn deserialize_newtype_struct<V>( + self, + _name: &'static str, + visitor: V + ) -> Result<V::Value, ::de::Error> + where V: de::Visitor<'de> + { + visitor.visit_newtype_struct(self) + } + forward_to_deserialize_any! { bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit seq bytes byte_buf map unit_struct tuple_struct struct - tuple ignored_any newtype_struct identifier + tuple ignored_any identifier } } diff --git a/tests/serde.rs b/tests/serde.rs index 2f8d871..3ae2bbd 100644 --- a/tests/serde.rs +++ b/tests/serde.rs @@ -509,3 +509,19 @@ fn extra_keys() { assert!(toml.clone().try_into::<Foo>().is_ok()); assert!(toml::from_str::<Foo>(&toml.to_string()).is_ok()); } + +#[test] +fn newtypes() { + #[derive(Deserialize, Serialize, PartialEq, Debug, Clone)] + struct A { + b: B + } + + #[derive(Deserialize, Serialize, PartialEq, Debug, Clone)] + struct B(u32); + + equivalent! { + A { b: B(2) }, + Table(map! { b: Integer(2) }), + } +} |