From 8170ffef2e1f4570394f3238457824bc8d7cc0c5 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 10 Jul 2022 21:27:20 -0700 Subject: [PATCH] Add constructor function for all IntoDeserializer impls --- serde/src/de/value.rs | 60 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 52 insertions(+), 8 deletions(-) diff --git a/serde/src/de/value.rs b/serde/src/de/value.rs index a49e9358..e7afdd8d 100644 --- a/serde/src/de/value.rs +++ b/serde/src/de/value.rs @@ -128,9 +128,7 @@ where type Deserializer = UnitDeserializer; fn into_deserializer(self) -> UnitDeserializer { - UnitDeserializer { - marker: PhantomData, - } + UnitDeserializer::new() } } @@ -141,6 +139,15 @@ pub struct UnitDeserializer { impl_copy_clone!(UnitDeserializer); +impl UnitDeserializer { + #[allow(missing_docs)] + pub fn new() -> Self { + UnitDeserializer { + marker: PhantomData, + } + } +} + impl<'de, E> de::Deserializer<'de> for UnitDeserializer where E: de::Error, @@ -236,8 +243,15 @@ macro_rules! primitive_deserializer { type Deserializer = $name; fn into_deserializer(self) -> $name { + $name::new(self) + } + } + + impl $name { + #[allow(missing_docs)] + pub fn new(value: $ty) -> Self { $name { - value: self, + value: value, marker: PhantomData, } } @@ -308,8 +322,15 @@ where type Deserializer = U32Deserializer; fn into_deserializer(self) -> U32Deserializer { + U32Deserializer::new(self) + } +} + +impl U32Deserializer { + #[allow(missing_docs)] + pub fn new(value: u32) -> Self { U32Deserializer { - value: self, + value: value, marker: PhantomData, } } @@ -390,8 +411,15 @@ where type Deserializer = StrDeserializer<'a, E>; fn into_deserializer(self) -> StrDeserializer<'a, E> { + StrDeserializer::new(self) + } +} + +impl<'a, E> StrDeserializer<'a, E> { + #[allow(missing_docs)] + pub fn new(value: &'a str) -> Self { StrDeserializer { - value: self, + value: value, marker: PhantomData, } } @@ -561,8 +589,16 @@ where type Deserializer = StringDeserializer; fn into_deserializer(self) -> StringDeserializer { + StringDeserializer::new(self) + } +} + +#[cfg(any(feature = "std", feature = "alloc"))] +impl StringDeserializer { + #[allow(missing_docs)] + pub fn new(value: String) -> Self { StringDeserializer { - value: self, + value: value, marker: PhantomData, } } @@ -656,8 +692,16 @@ where type Deserializer = CowStrDeserializer<'a, E>; fn into_deserializer(self) -> CowStrDeserializer<'a, E> { + CowStrDeserializer::new(self) + } +} + +#[cfg(any(feature = "std", feature = "alloc"))] +impl<'a, E> CowStrDeserializer<'a, E> { + #[allow(missing_docs)] + pub fn new(value: Cow<'a, str>) -> Self { CowStrDeserializer { - value: self, + value: value, marker: PhantomData, } }