diff --git a/serde/src/de/value.rs b/serde/src/de/value.rs index ae5c93af..32b69d5f 100644 --- a/serde/src/de/value.rs +++ b/serde/src/de/value.rs @@ -51,7 +51,9 @@ use bytes; /// This represents all the possible errors that can occur using the `ValueDeserializer`. #[derive(Clone, Debug, PartialEq)] -pub struct Error(ErrorImpl); +pub struct Error { + err: ErrorImpl, +} #[cfg(any(feature = "std", feature = "collections"))] type ErrorImpl = Box; @@ -61,19 +63,23 @@ type ErrorImpl = (); impl de::Error for Error { #[cfg(any(feature = "std", feature = "collections"))] fn custom(msg: T) -> Self { - Error(msg.to_string().into_boxed_str()) + Error { + err: msg.to_string().into_boxed_str(), + } } #[cfg(not(any(feature = "std", feature = "collections")))] fn custom(_msg: T) -> Self { - Error(()) + Error { + err: (), + } } } impl Display for Error { #[cfg(any(feature = "std", feature = "collections"))] fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> { - formatter.write_str(&self.0) + formatter.write_str(&self.err) } #[cfg(not(any(feature = "std", feature = "collections")))] @@ -85,7 +91,7 @@ impl Display for Error { impl error::Error for Error { #[cfg(any(feature = "std", feature = "collections"))] fn description(&self) -> &str { - &self.0 + &self.err } #[cfg(not(any(feature = "std", feature = "collections")))] @@ -113,12 +119,16 @@ impl ValueDeserializer for () type Deserializer = UnitDeserializer; fn into_deserializer(self) -> UnitDeserializer { - UnitDeserializer(PhantomData) + UnitDeserializer { + marker: PhantomData, + } } } /// A helper deserializer that deserializes a `()`. -pub struct UnitDeserializer(PhantomData); +pub struct UnitDeserializer { + marker: PhantomData, +} impl de::Deserializer for UnitDeserializer where E: de::Error @@ -149,7 +159,10 @@ impl de::Deserializer for UnitDeserializer macro_rules! primitive_deserializer { ($ty:ty, $name:ident, $method:ident $($cast:tt)*) => { /// A helper deserializer that deserializes a number. - pub struct $name($ty, PhantomData); + pub struct $name { + value: $ty, + marker: PhantomData + } impl ValueDeserializer for $ty where E: de::Error, @@ -157,7 +170,10 @@ macro_rules! primitive_deserializer { type Deserializer = $name; fn into_deserializer(self) -> $name { - $name(self, PhantomData) + $name { + value: self, + marker: PhantomData, + } } } @@ -175,7 +191,7 @@ macro_rules! primitive_deserializer { fn deserialize(self, visitor: V) -> Result where V: de::Visitor, { - visitor.$method(self.0 $($cast)*) + visitor.$method(self.value $($cast)*) } } } @@ -199,7 +215,10 @@ primitive_deserializer!(char, CharDeserializer, visit_char); /////////////////////////////////////////////////////////////////////////////// /// A helper deserializer that deserializes a `&str`. -pub struct StrDeserializer<'a, E>(&'a str, PhantomData); +pub struct StrDeserializer<'a, E> { + value: &'a str, + marker: PhantomData, +} impl<'a, E> ValueDeserializer for &'a str where E: de::Error, @@ -207,7 +226,10 @@ impl<'a, E> ValueDeserializer for &'a str type Deserializer = StrDeserializer<'a, E>; fn into_deserializer(self) -> StrDeserializer<'a, E> { - StrDeserializer(self, PhantomData) + StrDeserializer { + value: self, + marker: PhantomData, + } } } @@ -219,7 +241,7 @@ impl<'a, E> de::Deserializer for StrDeserializer<'a, E> fn deserialize(self, visitor: V) -> Result where V: de::Visitor, { - visitor.visit_str(self.0) + visitor.visit_str(self.value) } fn deserialize_enum(self, @@ -255,7 +277,10 @@ impl<'a, E> de::EnumVisitor for StrDeserializer<'a, E> /// A helper deserializer that deserializes a `String`. #[cfg(any(feature = "std", feature = "collections"))] -pub struct StringDeserializer(String, PhantomData); +pub struct StringDeserializer { + value: String, + marker: PhantomData, +} #[cfg(any(feature = "std", feature = "collections"))] impl ValueDeserializer for String @@ -264,7 +289,10 @@ impl ValueDeserializer for String type Deserializer = StringDeserializer; fn into_deserializer(self) -> StringDeserializer { - StringDeserializer(self, PhantomData) + StringDeserializer { + value: self, + marker: PhantomData, + } } } @@ -277,7 +305,7 @@ impl de::Deserializer for StringDeserializer fn deserialize(self, visitor: V) -> Result where V: de::Visitor, { - visitor.visit_string(self.0) + visitor.visit_string(self.value) } fn deserialize_enum(self, @@ -314,7 +342,10 @@ impl<'a, E> de::EnumVisitor for StringDeserializer /// A helper deserializer that deserializes a `String`. #[cfg(any(feature = "std", feature = "collections"))] -pub struct CowStrDeserializer<'a, E>(Cow<'a, str>, PhantomData); +pub struct CowStrDeserializer<'a, E> { + value: Cow<'a, str>, + marker: PhantomData, +} #[cfg(any(feature = "std", feature = "collections"))] impl<'a, E> ValueDeserializer for Cow<'a, str> @@ -323,7 +354,10 @@ impl<'a, E> ValueDeserializer for Cow<'a, str> type Deserializer = CowStrDeserializer<'a, E>; fn into_deserializer(self) -> CowStrDeserializer<'a, E> { - CowStrDeserializer(self, PhantomData) + CowStrDeserializer { + value: self, + marker: PhantomData, + } } } @@ -336,7 +370,7 @@ impl<'a, E> de::Deserializer for CowStrDeserializer<'a, E> fn deserialize(self, visitor: V) -> Result where V: de::Visitor, { - match self.0 { + match self.value { Cow::Borrowed(string) => visitor.visit_str(string), Cow::Owned(string) => visitor.visit_string(string), } @@ -885,12 +919,18 @@ impl<'a, E> ValueDeserializer for bytes::Bytes<'a> type Deserializer = BytesDeserializer<'a, E>; fn into_deserializer(self) -> BytesDeserializer<'a, E> { - BytesDeserializer(self.into(), PhantomData) + BytesDeserializer { + value: self.into(), + marker: PhantomData, + } } } /// A helper deserializer that deserializes a `&[u8]`. -pub struct BytesDeserializer<'a, E>(&'a [u8], PhantomData); +pub struct BytesDeserializer<'a, E> { + value: &'a [u8], + marker: PhantomData, +} impl<'a, E> de::Deserializer for BytesDeserializer<'a, E> where E: de::Error @@ -900,7 +940,7 @@ impl<'a, E> de::Deserializer for BytesDeserializer<'a, E> fn deserialize(self, visitor: V) -> Result where V: de::Visitor, { - visitor.visit_bytes(self.0) + visitor.visit_bytes(self.value) } forward_to_deserialize! { @@ -919,13 +959,19 @@ impl ValueDeserializer for bytes::ByteBuf type Deserializer = ByteBufDeserializer; fn into_deserializer(self) -> Self::Deserializer { - ByteBufDeserializer(self.into(), PhantomData) + ByteBufDeserializer { + value: self.into(), + marker: PhantomData, + } } } /// A helper deserializer that deserializes a `Vec`. #[cfg(any(feature = "std", feature = "collections"))] -pub struct ByteBufDeserializer(Vec, PhantomData); +pub struct ByteBufDeserializer { + value: Vec, + marker: PhantomData, +} #[cfg(any(feature = "std", feature = "collections"))] impl de::Deserializer for ByteBufDeserializer @@ -936,7 +982,7 @@ impl de::Deserializer for ByteBufDeserializer fn deserialize(self, visitor: V) -> Result where V: de::Visitor, { - visitor.visit_byte_buf(self.0) + visitor.visit_byte_buf(self.value) } forward_to_deserialize! { @@ -952,10 +998,12 @@ mod private { use de::{self, Unexpected}; use core::marker::PhantomData; - pub struct UnitOnly(PhantomData); + pub struct UnitOnly { + marker: PhantomData, + } pub fn unit_only(t: T) -> (T, UnitOnly) { - (t, UnitOnly(PhantomData)) + (t, UnitOnly { marker: PhantomData }) } impl de::VariantVisitor for UnitOnly diff --git a/serde/src/ser/impls.rs b/serde/src/ser/impls.rs index bb3308e9..6496d860 100644 --- a/serde/src/ser/impls.rs +++ b/serde/src/ser/impls.rs @@ -231,7 +231,7 @@ impl<'a, I> Serialize for Iterator where S: Serializer, { // FIXME: use specialization to prevent invalidating the object in case of clonable iterators? - let iter = match self.0.borrow_mut().take() { + let iter = match self.data.borrow_mut().take() { Some(iter) => iter.into_iter(), None => return Err(Error::custom("Iterator used twice")), }; diff --git a/serde/src/ser/mod.rs b/serde/src/ser/mod.rs index 3e97bd20..8bdb437a 100644 --- a/serde/src/ser/mod.rs +++ b/serde/src/ser/mod.rs @@ -805,9 +805,12 @@ pub trait SerializeStructVariant { /// of this with the `serde::ser::iterator` function every time you want to /// serialize an iterator. #[cfg(feature = "unstable")] -pub struct Iterator(RefCell>) +pub struct Iterator where ::Item: Serialize, - I: IntoIterator; + I: IntoIterator +{ + data: RefCell>, +} /// Create a wrapper type that can be passed to any function expecting a /// `Serialize` and will serialize the given iterator as a sequence. @@ -816,5 +819,7 @@ pub fn iterator(iter: I) -> Iterator where ::Item: Serialize, I: IntoIterator { - Iterator(RefCell::new(Some(iter))) + Iterator { + data: RefCell::new(Some(iter)), + } }