diff --git a/serde/Cargo.toml b/serde/Cargo.toml index 58d15249..151e6fdf 100644 --- a/serde/Cargo.toml +++ b/serde/Cargo.toml @@ -29,7 +29,7 @@ unstable-testing = ["unstable", "std"] playground = ["serde_derive"] [dependencies] -serde_derive = { version = "0.9", optional = true } +serde_derive = { version = "0.9", optional = true, path = "../serde_derive" } [dev-dependencies] -serde_derive = "0.9" +serde_derive = { version = "0.9", path = "../serde_derive" } diff --git a/serde/src/bytes.rs b/serde/src/bytes.rs index d1ea0189..b6b6d02a 100644 --- a/serde/src/bytes.rs +++ b/serde/src/bytes.rs @@ -237,7 +237,7 @@ mod bytebuf { /// This type implements the `serde::de::Visitor` trait for a `ByteBuf`. pub struct ByteBufVisitor; - impl de::Visitor for ByteBufVisitor { + impl<'de> de::Visitor<'de> for ByteBufVisitor { type Value = ByteBuf; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { @@ -253,7 +253,7 @@ mod bytebuf { #[inline] fn visit_seq(self, mut visitor: V) -> Result - where V: de::SeqVisitor + where V: de::SeqVisitor<'de> { let len = cmp::min(visitor.size_hint().0, 4096); let mut values = Vec::with_capacity(len); @@ -292,10 +292,10 @@ mod bytebuf { } } - impl de::Deserialize for ByteBuf { + impl<'de> de::Deserialize<'de> for ByteBuf { #[inline] fn deserialize(deserializer: D) -> Result - where D: de::Deserializer + where D: de::Deserializer<'de> { deserializer.deserialize_byte_buf(ByteBufVisitor) } diff --git a/serde/src/de/content.rs b/serde/src/de/content.rs index e784b4ec..62f71d10 100644 --- a/serde/src/de/content.rs +++ b/serde/src/de/content.rs @@ -83,8 +83,10 @@ impl Content { } } -impl Deserialize for Content { - fn deserialize(deserializer: D) -> Result { +impl<'de> Deserialize<'de> for Content { + fn deserialize(deserializer: D) -> Result + where D: Deserializer<'de> + { // Untagged and internally tagged enums are only supported in // self-describing formats. deserializer.deserialize(ContentVisitor) @@ -93,7 +95,7 @@ impl Deserialize for Content { struct ContentVisitor; -impl Visitor for ContentVisitor { +impl<'de> Visitor<'de> for ContentVisitor { type Value = Content; fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result { @@ -209,19 +211,19 @@ impl Visitor for ContentVisitor { } fn visit_some(self, deserializer: D) -> Result - where D: Deserializer + where D: Deserializer<'de> { Deserialize::deserialize(deserializer).map(|v| Content::Some(Box::new(v))) } fn visit_newtype_struct(self, deserializer: D) -> Result - where D: Deserializer + where D: Deserializer<'de> { Deserialize::deserialize(deserializer).map(|v| Content::Newtype(Box::new(v))) } fn visit_seq(self, mut visitor: V) -> Result - where V: SeqVisitor + where V: SeqVisitor<'de> { let mut vec = Vec::with_capacity(cmp::min(visitor.size_hint().0, 4096)); while let Some(e) = try!(visitor.visit()) { @@ -231,7 +233,7 @@ impl Visitor for ContentVisitor { } fn visit_map(self, mut visitor: V) -> Result - where V: MapVisitor + where V: MapVisitor<'de> { let mut vec = Vec::with_capacity(cmp::min(visitor.size_hint().0, 4096)); while let Some(kv) = try!(visitor.visit()) { @@ -241,7 +243,7 @@ impl Visitor for ContentVisitor { } fn visit_enum(self, _visitor: V) -> Result - where V: EnumVisitor + where V: EnumVisitor<'de> { Err(de::Error::custom("untagged and internally tagged enums do not support enum input")) } @@ -265,11 +267,11 @@ impl TagOrContentVisitor { } } -impl DeserializeSeed for TagOrContentVisitor { +impl<'de> DeserializeSeed<'de> for TagOrContentVisitor { type Value = TagOrContent; fn deserialize(self, deserializer: D) -> Result - where D: Deserializer + where D: Deserializer<'de> { // Internally tagged enums are only supported in self-describing // formats. @@ -277,7 +279,7 @@ impl DeserializeSeed for TagOrContentVisitor { } } -impl Visitor for TagOrContentVisitor { +impl<'de> Visitor<'de> for TagOrContentVisitor { type Value = TagOrContent; fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result { @@ -409,31 +411,31 @@ impl Visitor for TagOrContentVisitor { } fn visit_some(self, deserializer: D) -> Result - where D: Deserializer + where D: Deserializer<'de> { ContentVisitor.visit_some(deserializer).map(TagOrContent::Content) } fn visit_newtype_struct(self, deserializer: D) -> Result - where D: Deserializer + where D: Deserializer<'de> { ContentVisitor.visit_newtype_struct(deserializer).map(TagOrContent::Content) } fn visit_seq(self, visitor: V) -> Result - where V: SeqVisitor + where V: SeqVisitor<'de> { ContentVisitor.visit_seq(visitor).map(TagOrContent::Content) } fn visit_map(self, visitor: V) -> Result - where V: MapVisitor + where V: MapVisitor<'de> { ContentVisitor.visit_map(visitor).map(TagOrContent::Content) } fn visit_enum(self, visitor: V) -> Result - where V: EnumVisitor + where V: EnumVisitor<'de> { ContentVisitor.visit_enum(visitor).map(TagOrContent::Content) } @@ -464,13 +466,13 @@ impl TaggedContentVisitor { } } -impl DeserializeSeed for TaggedContentVisitor - where T: Deserialize +impl<'de, T> DeserializeSeed<'de> for TaggedContentVisitor + where T: Deserialize<'de> { type Value = TaggedContent; fn deserialize(self, deserializer: D) -> Result - where D: Deserializer + where D: Deserializer<'de> { // Internally tagged enums are only supported in self-describing // formats. @@ -478,8 +480,8 @@ impl DeserializeSeed for TaggedContentVisitor } } -impl Visitor for TaggedContentVisitor - where T: Deserialize +impl<'de, T> Visitor<'de> for TaggedContentVisitor + where T: Deserialize<'de> { type Value = TaggedContent; @@ -488,7 +490,7 @@ impl Visitor for TaggedContentVisitor } fn visit_map(self, mut visitor: V) -> Result - where V: MapVisitor + where V: MapVisitor<'de> { let mut tag = None; let mut vec = Vec::with_capacity(cmp::min(visitor.size_hint().0, 4096)); @@ -532,17 +534,17 @@ pub struct TagOrContentFieldVisitor { pub content: &'static str, } -impl DeserializeSeed for TagOrContentFieldVisitor { +impl<'de> DeserializeSeed<'de> for TagOrContentFieldVisitor { type Value = TagOrContentField; fn deserialize(self, deserializer: D) -> Result - where D: Deserializer + where D: Deserializer<'de> { deserializer.deserialize_str(self) } } -impl Visitor for TagOrContentFieldVisitor { +impl<'de> Visitor<'de> for TagOrContentFieldVisitor { type Value = TagOrContentField; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { @@ -570,13 +572,13 @@ pub struct ContentDeserializer { /// Used when deserializing an internally tagged enum because the content will /// be used exactly once. -impl Deserializer for ContentDeserializer +impl<'de, E> Deserializer<'de> for ContentDeserializer where E: de::Error { type Error = E; fn deserialize(self, visitor: V) -> Result - where V: Visitor + where V: Visitor<'de> { match self.content { Content::Bool(v) => visitor.visit_bool(v), @@ -618,7 +620,7 @@ impl Deserializer for ContentDeserializer } fn deserialize_option(self, visitor: V) -> Result - where V: Visitor + where V: Visitor<'de> { match self.content { Content::None => visitor.visit_none(), @@ -629,7 +631,7 @@ impl Deserializer for ContentDeserializer } fn deserialize_newtype_struct(self, _name: &str, visitor: V) -> Result - where V: Visitor + where V: Visitor<'de> { visitor.visit_newtype_struct(self) } @@ -639,7 +641,7 @@ impl Deserializer for ContentDeserializer _variants: &'static [&'static str], visitor: V) -> Result - where V: Visitor + where V: Visitor<'de> { let (variant, value) = match self.content { Content::Map(value) => { @@ -696,7 +698,7 @@ struct EnumDeserializer err: PhantomData, } -impl de::EnumVisitor for EnumDeserializer +impl<'de, E> de::EnumVisitor<'de> for EnumDeserializer where E: de::Error { type Error = E; @@ -705,7 +707,7 @@ impl de::EnumVisitor for EnumDeserializer fn visit_variant_seed(self, seed: V) -> Result<(V::Value, VariantDeserializer), Self::Error> - where V: de::DeserializeSeed + where V: de::DeserializeSeed<'de> { let visitor = VariantDeserializer { value: self.value, @@ -722,7 +724,7 @@ struct VariantDeserializer err: PhantomData, } -impl de::VariantVisitor for VariantDeserializer +impl<'de, E> de::VariantVisitor<'de> for VariantDeserializer where E: de::Error { type Error = E; @@ -735,7 +737,7 @@ impl de::VariantVisitor for VariantDeserializer } fn visit_newtype_seed(self, seed: T) -> Result - where T: de::DeserializeSeed + where T: de::DeserializeSeed<'de> { match self.value { Some(value) => seed.deserialize(ContentDeserializer::new(value)), @@ -744,7 +746,7 @@ impl de::VariantVisitor for VariantDeserializer } fn visit_tuple(self, _len: usize, visitor: V) -> Result - where V: de::Visitor + where V: de::Visitor<'de> { match self.value { Some(Content::Seq(v)) => { @@ -759,7 +761,7 @@ impl de::VariantVisitor for VariantDeserializer _fields: &'static [&'static str], visitor: V) -> Result - where V: de::Visitor + where V: de::Visitor<'de> { match self.value { Some(Content::Map(v)) => { @@ -789,14 +791,14 @@ impl SeqDeserializer } } -impl de::Deserializer for SeqDeserializer +impl<'de, E> de::Deserializer<'de> for SeqDeserializer where E: de::Error { type Error = E; #[inline] fn deserialize(mut self, visitor: V) -> Result - where V: de::Visitor + where V: de::Visitor<'de> { let len = self.iter.len(); if len == 0 { @@ -819,13 +821,13 @@ impl de::Deserializer for SeqDeserializer } } -impl de::SeqVisitor for SeqDeserializer +impl<'de, E> de::SeqVisitor<'de> for SeqDeserializer where E: de::Error { type Error = E; fn visit_seed(&mut self, seed: T) -> Result, Self::Error> - where T: de::DeserializeSeed + where T: de::DeserializeSeed<'de> { match self.iter.next() { Some(value) => seed.deserialize(ContentDeserializer::new(value)).map(Some), @@ -858,13 +860,13 @@ impl MapDeserializer } } -impl de::MapVisitor for MapDeserializer +impl<'de, E> de::MapVisitor<'de> for MapDeserializer where E: de::Error { type Error = E; fn visit_key_seed(&mut self, seed: T) -> Result, Self::Error> - where T: de::DeserializeSeed + where T: de::DeserializeSeed<'de> { match self.iter.next() { Some((key, value)) => { @@ -876,7 +878,7 @@ impl de::MapVisitor for MapDeserializer } fn visit_value_seed(&mut self, seed: T) -> Result - where T: de::DeserializeSeed + where T: de::DeserializeSeed<'de> { match self.value.take() { Some(value) => seed.deserialize(ContentDeserializer::new(value)), @@ -889,14 +891,14 @@ impl de::MapVisitor for MapDeserializer } } -impl de::Deserializer for MapDeserializer +impl<'de, E> de::Deserializer<'de> for MapDeserializer where E: de::Error { type Error = E; #[inline] fn deserialize(self, visitor: V) -> Result - where V: de::Visitor + where V: de::Visitor<'de> { visitor.visit_map(self) } @@ -917,13 +919,13 @@ pub struct ContentRefDeserializer<'a, E> { /// Used when deserializing an untagged enum because the content may need to be /// used more than once. -impl<'a, E> Deserializer for ContentRefDeserializer<'a, E> +impl<'de, 'a, E> Deserializer<'de> for ContentRefDeserializer<'a, E> where E: de::Error { type Error = E; fn deserialize(self, visitor: V) -> Result - where V: Visitor + where V: Visitor<'de> { match *self.content { Content::Bool(v) => visitor.visit_bool(v), @@ -965,7 +967,7 @@ impl<'a, E> Deserializer for ContentRefDeserializer<'a, E> } fn deserialize_option(self, visitor: V) -> Result - where V: Visitor + where V: Visitor<'de> { match *self.content { Content::None => visitor.visit_none(), @@ -976,7 +978,7 @@ impl<'a, E> Deserializer for ContentRefDeserializer<'a, E> } fn deserialize_newtype_struct(self, _name: &str, visitor: V) -> Result - where V: Visitor + where V: Visitor<'de> { visitor.visit_newtype_struct(self) } @@ -986,7 +988,7 @@ impl<'a, E> Deserializer for ContentRefDeserializer<'a, E> _variants: &'static [&'static str], visitor: V) -> Result - where V: Visitor + where V: Visitor<'de> { let (variant, value) = match *self.content { Content::Map(ref value) => { @@ -1043,14 +1045,14 @@ struct EnumRefDeserializer<'a, E> err: PhantomData, } -impl<'a, E> de::EnumVisitor for EnumRefDeserializer<'a, E> +impl<'de, 'a, E> de::EnumVisitor<'de> for EnumRefDeserializer<'a, E> where E: de::Error { type Error = E; type Variant = VariantRefDeserializer<'a, Self::Error>; fn visit_variant_seed(self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error> - where V: de::DeserializeSeed + where V: de::DeserializeSeed<'de> { let visitor = VariantRefDeserializer { value: self.value, @@ -1067,7 +1069,7 @@ struct VariantRefDeserializer<'a, E> err: PhantomData, } -impl<'a, E> de::VariantVisitor for VariantRefDeserializer<'a, E> +impl<'de, 'a, E> de::VariantVisitor<'de> for VariantRefDeserializer<'a, E> where E: de::Error { type Error = E; @@ -1080,7 +1082,7 @@ impl<'a, E> de::VariantVisitor for VariantRefDeserializer<'a, E> } fn visit_newtype_seed(self, seed: T) -> Result - where T: de::DeserializeSeed + where T: de::DeserializeSeed<'de> { match self.value { Some(value) => seed.deserialize(ContentRefDeserializer::new(value)), @@ -1089,7 +1091,7 @@ impl<'a, E> de::VariantVisitor for VariantRefDeserializer<'a, E> } fn visit_tuple(self, _len: usize, visitor: V) -> Result - where V: de::Visitor + where V: de::Visitor<'de> { match self.value { Some(&Content::Seq(ref v)) => { @@ -1104,7 +1106,7 @@ impl<'a, E> de::VariantVisitor for VariantRefDeserializer<'a, E> _fields: &'static [&'static str], visitor: V) -> Result - where V: de::Visitor + where V: de::Visitor<'de> { match self.value { Some(&Content::Map(ref v)) => { @@ -1134,14 +1136,14 @@ impl<'a, E> SeqRefDeserializer<'a, E> } } -impl<'a, E> de::Deserializer for SeqRefDeserializer<'a, E> +impl<'de, 'a, E> de::Deserializer<'de> for SeqRefDeserializer<'a, E> where E: de::Error { type Error = E; #[inline] fn deserialize(mut self, visitor: V) -> Result - where V: de::Visitor + where V: de::Visitor<'de> { let len = self.iter.len(); if len == 0 { @@ -1164,13 +1166,13 @@ impl<'a, E> de::Deserializer for SeqRefDeserializer<'a, E> } } -impl<'a, E> de::SeqVisitor for SeqRefDeserializer<'a, E> +impl<'de, 'a, E> de::SeqVisitor<'de> for SeqRefDeserializer<'a, E> where E: de::Error { type Error = E; fn visit_seed(&mut self, seed: T) -> Result, Self::Error> - where T: de::DeserializeSeed + where T: de::DeserializeSeed<'de> { match self.iter.next() { Some(value) => seed.deserialize(ContentRefDeserializer::new(value)).map(Some), @@ -1203,13 +1205,13 @@ impl<'a, E> MapRefDeserializer<'a, E> } } -impl<'a, E> de::MapVisitor for MapRefDeserializer<'a, E> +impl<'de, 'a, E> de::MapVisitor<'de> for MapRefDeserializer<'a, E> where E: de::Error { type Error = E; fn visit_key_seed(&mut self, seed: T) -> Result, Self::Error> - where T: de::DeserializeSeed + where T: de::DeserializeSeed<'de> { match self.iter.next() { Some(&(ref key, ref value)) => { @@ -1221,7 +1223,7 @@ impl<'a, E> de::MapVisitor for MapRefDeserializer<'a, E> } fn visit_value_seed(&mut self, seed: T) -> Result - where T: de::DeserializeSeed + where T: de::DeserializeSeed<'de> { match self.value.take() { Some(value) => seed.deserialize(ContentRefDeserializer::new(value)), @@ -1234,14 +1236,14 @@ impl<'a, E> de::MapVisitor for MapRefDeserializer<'a, E> } } -impl<'a, E> de::Deserializer for MapRefDeserializer<'a, E> +impl<'de, 'a, E> de::Deserializer<'de> for MapRefDeserializer<'a, E> where E: de::Error { type Error = E; #[inline] fn deserialize(self, visitor: V) -> Result - where V: de::Visitor + where V: de::Visitor<'de> { visitor.visit_map(self) } @@ -1253,7 +1255,7 @@ impl<'a, E> de::Deserializer for MapRefDeserializer<'a, E> } } -impl de::value::ValueDeserializer for ContentDeserializer +impl<'de, E> de::value::ValueDeserializer<'de, E> for ContentDeserializer where E: de::Error { type Deserializer = Self; @@ -1263,7 +1265,7 @@ impl de::value::ValueDeserializer for ContentDeserializer } } -impl<'a, E> de::value::ValueDeserializer for ContentRefDeserializer<'a, E> +impl<'de, 'a, E> de::value::ValueDeserializer<'de, E> for ContentRefDeserializer<'a, E> where E: de::Error { type Deserializer = Self; @@ -1291,7 +1293,7 @@ impl<'a> InternallyTaggedUnitVisitor<'a> { } } -impl<'a> Visitor for InternallyTaggedUnitVisitor<'a> { +impl<'de, 'a> Visitor<'de> for InternallyTaggedUnitVisitor<'a> { type Value = (); fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { @@ -1299,7 +1301,7 @@ impl<'a> Visitor for InternallyTaggedUnitVisitor<'a> { } fn visit_map(self, _: V) -> Result<(), V::Error> - where V: MapVisitor + where V: MapVisitor<'de> { Ok(()) } @@ -1323,7 +1325,7 @@ impl<'a> UntaggedUnitVisitor<'a> { } } -impl<'a> Visitor for UntaggedUnitVisitor<'a> { +impl<'de, 'a> Visitor<'de> for UntaggedUnitVisitor<'a> { type Value = (); fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { diff --git a/serde/src/de/impls.rs b/serde/src/de/impls.rs index 7bffab32..fc61dac1 100644 --- a/serde/src/de/impls.rs +++ b/serde/src/de/impls.rs @@ -68,7 +68,7 @@ use bytes::ByteBuf; /// A visitor that produces a `()`. pub struct UnitVisitor; -impl Visitor for UnitVisitor { +impl<'de> Visitor<'de> for UnitVisitor { type Value = (); fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { @@ -82,15 +82,15 @@ impl Visitor for UnitVisitor { } fn visit_seq(self, _: V) -> Result<(), V::Error> - where V: SeqVisitor + where V: SeqVisitor<'de> { Ok(()) } } -impl Deserialize for () { +impl<'de> Deserialize<'de> for () { fn deserialize(deserializer: D) -> Result<(), D::Error> - where D: Deserializer + where D: Deserializer<'de> { deserializer.deserialize_unit(UnitVisitor) } @@ -101,7 +101,7 @@ impl Deserialize for () { /// A visitor that produces a `bool`. pub struct BoolVisitor; -impl Visitor for BoolVisitor { +impl<'de> Visitor<'de> for BoolVisitor { type Value = bool; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { @@ -125,9 +125,9 @@ impl Visitor for BoolVisitor { } } -impl Deserialize for bool { +impl<'de> Deserialize<'de> for bool { fn deserialize(deserializer: D) -> Result - where D: Deserializer + where D: Deserializer<'de> { deserializer.deserialize_bool(BoolVisitor) } @@ -151,14 +151,14 @@ macro_rules! impl_deserialize_num_method { macro_rules! impl_deserialize_num { ($ty:ident, $method:ident) => { - impl Deserialize for $ty { + impl<'de> Deserialize<'de> for $ty { #[inline] fn deserialize(deserializer: D) -> Result<$ty, D::Error> - where D: Deserializer, + where D: Deserializer<'de>, { struct PrimitiveVisitor; - impl Visitor for PrimitiveVisitor { + impl<'de> Visitor<'de> for PrimitiveVisitor { type Value = $ty; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { @@ -209,7 +209,7 @@ impl_deserialize_num!(f64, deserialize_f64); struct CharVisitor; -impl Visitor for CharVisitor { +impl<'de> Visitor<'de> for CharVisitor { type Value = char; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { @@ -235,10 +235,10 @@ impl Visitor for CharVisitor { } } -impl Deserialize for char { +impl<'de> Deserialize<'de> for char { #[inline] fn deserialize(deserializer: D) -> Result - where D: Deserializer + where D: Deserializer<'de> { deserializer.deserialize_char(CharVisitor) } @@ -250,7 +250,7 @@ impl Deserialize for char { struct StringVisitor; #[cfg(any(feature = "std", feature = "collections"))] -impl Visitor for StringVisitor { +impl<'de> Visitor<'de> for StringVisitor { type Value = String; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { @@ -295,9 +295,9 @@ impl Visitor for StringVisitor { } #[cfg(any(feature = "std", feature = "collections"))] -impl Deserialize for String { +impl<'de> Deserialize<'de> for String { fn deserialize(deserializer: D) -> Result - where D: Deserializer + where D: Deserializer<'de> { deserializer.deserialize_string(StringVisitor) } @@ -306,9 +306,9 @@ impl Deserialize for String { /////////////////////////////////////////////////////////////////////////////// #[cfg(all(feature = "std", feature="unstable"))] -impl Deserialize for Box { +impl<'de> Deserialize<'de> for Box { fn deserialize(deserializer: D) -> Result - where D: Deserializer + where D: Deserializer<'de> { let s = try!(CString::deserialize(deserializer)); Ok(s.into_boxed_c_str()) @@ -316,9 +316,9 @@ impl Deserialize for Box { } #[cfg(feature = "std")] -impl Deserialize for CString { +impl<'de> Deserialize<'de> for CString { fn deserialize(deserializer: D) -> Result - where D: Deserializer + where D: Deserializer<'de> { let bytes = try!(ByteBuf::deserialize(deserializer)); CString::new(bytes).map_err(Error::custom) @@ -331,7 +331,9 @@ struct OptionVisitor { marker: PhantomData, } -impl Visitor for OptionVisitor { +impl<'de, T> Visitor<'de> for OptionVisitor + where T: Deserialize<'de> +{ type Value = Option; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { @@ -354,17 +356,17 @@ impl Visitor for OptionVisitor { #[inline] fn visit_some(self, deserializer: D) -> Result, D::Error> - where D: Deserializer + where D: Deserializer<'de> { Ok(Some(try!(Deserialize::deserialize(deserializer)))) } } -impl Deserialize for Option - where T: Deserialize +impl<'de, T> Deserialize<'de> for Option + where T: Deserialize<'de> { fn deserialize(deserializer: D) -> Result, D::Error> - where D: Deserializer + where D: Deserializer<'de> { deserializer.deserialize_option(OptionVisitor { marker: PhantomData }) } @@ -377,7 +379,7 @@ pub struct PhantomDataVisitor { marker: PhantomData, } -impl Visitor for PhantomDataVisitor { +impl<'de, T> Visitor<'de> for PhantomDataVisitor { type Value = PhantomData; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { @@ -392,9 +394,9 @@ impl Visitor for PhantomDataVisitor { } } -impl Deserialize for PhantomData { +impl<'de, T> Deserialize<'de> for PhantomData { fn deserialize(deserializer: D) -> Result, D::Error> - where D: Deserializer + where D: Deserializer<'de> { let visitor = PhantomDataVisitor { marker: PhantomData }; deserializer.deserialize_unit_struct("PhantomData", visitor) @@ -405,21 +407,19 @@ impl Deserialize for PhantomData { macro_rules! seq_impl { ( - $ty:ty, - $visitor_ty:ident < $($typaram:ident : $bound1:ident $(+ $bound2:ident)*),* >, + $ty:ident < T $(, $typaram:ident)* >, + $visitor_ty:ident $( < $($boundparam:ident : $bound1:ident $(+ $bound2:ident)*),* > )*, $visitor:ident, $ctor:expr, $with_capacity:expr, $insert:expr ) => { /// A visitor that produces a sequence. - pub struct $visitor_ty<$($typaram),*> { - marker: PhantomData<$ty>, + pub struct $visitor_ty { + marker: PhantomData<$ty>, } - impl<$($typaram),*> $visitor_ty<$($typaram),*> - where $($typaram: $bound1 $(+ $bound2)*),* - { + impl $visitor_ty { /// Construct a new sequence visitor. pub fn new() -> Self { $visitor_ty { @@ -428,25 +428,26 @@ macro_rules! seq_impl { } } - impl<$($typaram),*> Visitor for $visitor_ty<$($typaram),*> - where $($typaram: $bound1 $(+ $bound2)*),* + impl<'de, T $(, $typaram)*> Visitor<'de> for $visitor_ty + where T: Deserialize<'de>, + $($($boundparam: $bound1 $(+ $bound2)*),*)* { - type Value = $ty; + type Value = $ty; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("a sequence") } #[inline] - fn visit_unit(self) -> Result<$ty, E> + fn visit_unit(self) -> Result where E: Error, { Ok($ctor) } #[inline] - fn visit_seq(self, mut $visitor: V) -> Result<$ty, V::Error> - where V: SeqVisitor, + fn visit_seq(self, mut $visitor: V) -> Result + where V: SeqVisitor<'de>, { let mut values = $with_capacity; @@ -458,11 +459,12 @@ macro_rules! seq_impl { } } - impl<$($typaram),*> Deserialize for $ty - where $($typaram: $bound1 $(+ $bound2)*),* + impl<'de, T $(, $typaram)*> Deserialize<'de> for $ty + where T: Deserialize<'de>, + $($($boundparam: $bound1 $(+ $bound2)*),*)* { - fn deserialize(deserializer: D) -> Result<$ty, D::Error> - where D: Deserializer, + fn deserialize(deserializer: D) -> Result + where D: Deserializer<'de>, { deserializer.deserialize_seq($visitor_ty::new()) } @@ -473,7 +475,7 @@ macro_rules! seq_impl { #[cfg(any(feature = "std", feature = "collections"))] seq_impl!( BinaryHeap, - BinaryHeapVisitor, + BinaryHeapVisitor, visitor, BinaryHeap::new(), BinaryHeap::with_capacity(cmp::min(visitor.size_hint().0, 4096)), @@ -482,7 +484,7 @@ seq_impl!( #[cfg(any(feature = "std", feature = "collections"))] seq_impl!( BTreeSet, - BTreeSetVisitor, + BTreeSetVisitor, visitor, BTreeSet::new(), BTreeSet::new(), @@ -491,7 +493,7 @@ seq_impl!( #[cfg(any(feature = "std", feature = "collections"))] seq_impl!( LinkedList, - LinkedListVisitor, + LinkedListVisitor, visitor, LinkedList::new(), LinkedList::new(), @@ -500,7 +502,7 @@ seq_impl!( #[cfg(feature = "std")] seq_impl!( HashSet, - HashSetVisitor, visitor, HashSet::with_hasher(S::default()), @@ -510,7 +512,7 @@ seq_impl!( #[cfg(any(feature = "std", feature = "collections"))] seq_impl!( Vec, - VecVisitor, + VecVisitor, visitor, Vec::new(), Vec::with_capacity(cmp::min(visitor.size_hint().0, 4096)), @@ -519,7 +521,7 @@ seq_impl!( #[cfg(any(feature = "std", feature = "collections"))] seq_impl!( VecDeque, - VecDequeVisitor, + VecDequeVisitor, visitor, VecDeque::new(), VecDeque::with_capacity(cmp::min(visitor.size_hint().0, 4096)), @@ -537,8 +539,8 @@ impl ArrayVisitor { } } -impl Visitor for ArrayVisitor<[T; 0]> - where T: Deserialize +impl<'de, T> Visitor<'de> for ArrayVisitor<[T; 0]> + where T: Deserialize<'de> { type Value = [T; 0]; @@ -555,17 +557,17 @@ impl Visitor for ArrayVisitor<[T; 0]> #[inline] fn visit_seq(self, _: V) -> Result<[T; 0], V::Error> - where V: SeqVisitor + where V: SeqVisitor<'de> { Ok([]) } } -impl Deserialize for [T; 0] - where T: Deserialize +impl<'de, T> Deserialize<'de> for [T; 0] + where T: Deserialize<'de> { fn deserialize(deserializer: D) -> Result<[T; 0], D::Error> - where D: Deserializer + where D: Deserializer<'de> { deserializer.deserialize_seq_fixed_size(0, ArrayVisitor::<[T; 0]>::new()) } @@ -574,7 +576,9 @@ impl Deserialize for [T; 0] macro_rules! array_impls { ($($len:expr => ($($n:tt $name:ident)+))+) => { $( - impl Visitor for ArrayVisitor<[T; $len]> where T: Deserialize { + impl<'de, T> Visitor<'de> for ArrayVisitor<[T; $len]> + where T: Deserialize<'de> + { type Value = [T; $len]; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { @@ -583,7 +587,7 @@ macro_rules! array_impls { #[inline] fn visit_seq(self, mut visitor: V) -> Result<[T; $len], V::Error> - where V: SeqVisitor, + where V: SeqVisitor<'de>, { $( let $name = match try!(visitor.visit()) { @@ -596,11 +600,11 @@ macro_rules! array_impls { } } - impl Deserialize for [T; $len] - where T: Deserialize, + impl<'de, T> Deserialize<'de> for [T; $len] + where T: Deserialize<'de>, { fn deserialize(deserializer: D) -> Result<[T; $len], D::Error> - where D: Deserializer, + where D: Deserializer<'de>, { deserializer.deserialize_seq_fixed_size($len, ArrayVisitor::<[T; $len]>::new()) } @@ -654,14 +658,14 @@ macro_rules! tuple_impls { marker: PhantomData<($($name,)+)>, } - impl<$($name: Deserialize,)+> $visitor<$($name,)+> { + impl<$($name,)+> $visitor<$($name,)+> { /// Construct a `TupleVisitor*`. pub fn new() -> Self { $visitor { marker: PhantomData } } } - impl<$($name: Deserialize),+> Visitor for $visitor<$($name,)+> { + impl<'de, $($name: Deserialize<'de>),+> Visitor<'de> for $visitor<$($name,)+> { type Value = ($($name,)+); fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { @@ -671,7 +675,7 @@ macro_rules! tuple_impls { #[inline] #[allow(non_snake_case)] fn visit_seq(self, mut visitor: V) -> Result<($($name,)+), V::Error> - where V: SeqVisitor, + where V: SeqVisitor<'de>, { $( let $name = match try!(visitor.visit()) { @@ -684,10 +688,10 @@ macro_rules! tuple_impls { } } - impl<$($name: Deserialize),+> Deserialize for ($($name,)+) { + impl<'de, $($name: Deserialize<'de>),+> Deserialize<'de> for ($($name,)+) { #[inline] fn deserialize(deserializer: D) -> Result<($($name,)+), D::Error> - where D: Deserializer, + where D: Deserializer<'de>, { deserializer.deserialize_tuple($len, $visitor::new()) } @@ -719,20 +723,18 @@ tuple_impls! { macro_rules! map_impl { ( - $ty:ty, - $visitor_ty:ident < $($typaram:ident : $bound1:ident $(+ $bound2:ident)*),* >, + $ty:ident < K, V $(, $typaram:ident)* >, + $visitor_ty:ident < $($boundparam:ident : $bound1:ident $(+ $bound2:ident)*),* >, $visitor:ident, $ctor:expr, $with_capacity:expr ) => { /// A visitor that produces a map. - pub struct $visitor_ty<$($typaram),*> { - marker: PhantomData<$ty>, + pub struct $visitor_ty { + marker: PhantomData<$ty>, } - impl<$($typaram),*> $visitor_ty<$($typaram),*> - where $($typaram: $bound1 $(+ $bound2)*),* - { + impl $visitor_ty { /// Construct a `MapVisitor*`. pub fn new() -> Self { $visitor_ty { @@ -741,25 +743,27 @@ macro_rules! map_impl { } } - impl<$($typaram),*> Visitor for $visitor_ty<$($typaram),*> - where $($typaram: $bound1 $(+ $bound2)*),* + impl<'de, K, V $(, $typaram)*> Visitor<'de> for $visitor_ty + where K: Deserialize<'de>, + V: Deserialize<'de>, + $($boundparam: $bound1 $(+ $bound2)*),* { - type Value = $ty; + type Value = $ty; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("a map") } #[inline] - fn visit_unit(self) -> Result<$ty, E> + fn visit_unit(self) -> Result where E: Error, { Ok($ctor) } #[inline] - fn visit_map(self, mut $visitor: Visitor) -> Result<$ty, Visitor::Error> - where Visitor: MapVisitor, + fn visit_map(self, mut $visitor: Visitor) -> Result + where Visitor: MapVisitor<'de>, { let mut values = $with_capacity; @@ -771,11 +775,13 @@ macro_rules! map_impl { } } - impl<$($typaram),*> Deserialize for $ty - where $($typaram: $bound1 $(+ $bound2)*),* + impl<'de, K, V $(, $typaram)*> Deserialize<'de> for $ty + where K: Deserialize<'de>, + V: Deserialize<'de>, + $($boundparam: $bound1 $(+ $bound2)*),* { - fn deserialize(deserializer: D) -> Result<$ty, D::Error> - where D: Deserializer, + fn deserialize(deserializer: D) -> Result + where D: Deserializer<'de>, { deserializer.deserialize_map($visitor_ty::new()) } @@ -786,8 +792,7 @@ macro_rules! map_impl { #[cfg(any(feature = "std", feature = "collections"))] map_impl!( BTreeMap, - BTreeMapVisitor, + BTreeMapVisitor, visitor, BTreeMap::new(), BTreeMap::new()); @@ -795,8 +800,7 @@ map_impl!( #[cfg(feature = "std")] map_impl!( HashMap, - HashMapVisitor, visitor, HashMap::with_hasher(S::default()), @@ -805,9 +809,9 @@ map_impl!( /////////////////////////////////////////////////////////////////////////////// #[cfg(feature = "std")] -impl Deserialize for net::IpAddr { +impl<'de> Deserialize<'de> for net::IpAddr { fn deserialize(deserializer: D) -> Result - where D: Deserializer + where D: Deserializer<'de> { let s = try!(String::deserialize(deserializer)); match s.parse() { @@ -818,9 +822,9 @@ impl Deserialize for net::IpAddr { } #[cfg(feature = "std")] -impl Deserialize for net::Ipv4Addr { +impl<'de> Deserialize<'de> for net::Ipv4Addr { fn deserialize(deserializer: D) -> Result - where D: Deserializer + where D: Deserializer<'de> { let s = try!(String::deserialize(deserializer)); match s.parse() { @@ -831,9 +835,9 @@ impl Deserialize for net::Ipv4Addr { } #[cfg(feature = "std")] -impl Deserialize for net::Ipv6Addr { +impl<'de> Deserialize<'de> for net::Ipv6Addr { fn deserialize(deserializer: D) -> Result - where D: Deserializer + where D: Deserializer<'de> { let s = try!(String::deserialize(deserializer)); match s.parse() { @@ -846,9 +850,9 @@ impl Deserialize for net::Ipv6Addr { /////////////////////////////////////////////////////////////////////////////// #[cfg(feature = "std")] -impl Deserialize for net::SocketAddr { +impl<'de> Deserialize<'de> for net::SocketAddr { fn deserialize(deserializer: D) -> Result - where D: Deserializer + where D: Deserializer<'de> { let s = try!(String::deserialize(deserializer)); match s.parse() { @@ -859,9 +863,9 @@ impl Deserialize for net::SocketAddr { } #[cfg(feature = "std")] -impl Deserialize for net::SocketAddrV4 { +impl<'de> Deserialize<'de> for net::SocketAddrV4 { fn deserialize(deserializer: D) -> Result - where D: Deserializer + where D: Deserializer<'de> { let s = try!(String::deserialize(deserializer)); match s.parse() { @@ -872,9 +876,9 @@ impl Deserialize for net::SocketAddrV4 { } #[cfg(feature = "std")] -impl Deserialize for net::SocketAddrV6 { +impl<'de> Deserialize<'de> for net::SocketAddrV6 { fn deserialize(deserializer: D) -> Result - where D: Deserializer + where D: Deserializer<'de> { let s = try!(String::deserialize(deserializer)); match s.parse() { @@ -890,7 +894,7 @@ impl Deserialize for net::SocketAddrV6 { struct PathBufVisitor; #[cfg(feature = "std")] -impl Visitor for PathBufVisitor { +impl<'de> Visitor<'de> for PathBufVisitor { type Value = path::PathBuf; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { @@ -912,9 +916,9 @@ impl Visitor for PathBufVisitor { #[cfg(feature = "std")] -impl Deserialize for path::PathBuf { +impl<'de> Deserialize<'de> for path::PathBuf { fn deserialize(deserializer: D) -> Result - where D: Deserializer + where D: Deserializer<'de> { deserializer.deserialize_string(PathBufVisitor) } @@ -932,13 +936,13 @@ enum OsStringKind { static OSSTR_VARIANTS: &'static [&'static str] = &["Unix", "Windows"]; #[cfg(all(feature = "std", any(unix, windows)))] -impl Deserialize for OsStringKind { +impl<'de> Deserialize<'de> for OsStringKind { fn deserialize(deserializer: D) -> Result - where D: Deserializer + where D: Deserializer<'de> { struct KindVisitor; - impl Visitor for KindVisitor { + impl<'de> Visitor<'de> for KindVisitor { type Value = OsStringKind; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { @@ -991,7 +995,7 @@ impl Deserialize for OsStringKind { struct OsStringVisitor; #[cfg(all(feature = "std", any(unix, windows)))] -impl Visitor for OsStringVisitor { +impl<'de> Visitor<'de> for OsStringVisitor { type Value = OsString; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { @@ -1000,7 +1004,7 @@ impl Visitor for OsStringVisitor { #[cfg(unix)] fn visit_enum(self, visitor: V) -> Result - where V: EnumVisitor, + where V: EnumVisitor<'de>, { use std::os::unix::ffi::OsStringExt; @@ -1016,9 +1020,9 @@ impl Visitor for OsStringVisitor { } #[cfg(all(feature = "std", any(unix, windows)))] -impl Deserialize for OsString { +impl<'de> Deserialize<'de> for OsString { fn deserialize(deserializer: D) -> Result - where D: Deserializer + where D: Deserializer<'de> { deserializer.deserialize_enum("OsString", OSSTR_VARIANTS, OsStringVisitor) } @@ -1027,9 +1031,9 @@ impl Deserialize for OsString { /////////////////////////////////////////////////////////////////////////////// #[cfg(any(feature = "std", feature = "alloc"))] -impl Deserialize for Box { +impl<'de, T: Deserialize<'de>> Deserialize<'de> for Box { fn deserialize(deserializer: D) -> Result, D::Error> - where D: Deserializer + where D: Deserializer<'de> { let val = try!(Deserialize::deserialize(deserializer)); Ok(Box::new(val)) @@ -1037,9 +1041,9 @@ impl Deserialize for Box { } #[cfg(any(feature = "std", feature = "collections"))] -impl Deserialize for Box<[T]> { +impl<'de, T: Deserialize<'de>> Deserialize<'de> for Box<[T]> { fn deserialize(deserializer: D) -> Result, D::Error> - where D: Deserializer + where D: Deserializer<'de> { let v: Vec = try!(Deserialize::deserialize(deserializer)); Ok(v.into_boxed_slice()) @@ -1047,9 +1051,9 @@ impl Deserialize for Box<[T]> { } #[cfg(any(feature = "std", feature = "collections"))] -impl Deserialize for Box { +impl<'de> Deserialize<'de> for Box { fn deserialize(deserializer: D) -> Result - where D: Deserializer + where D: Deserializer<'de> { let s = try!(String::deserialize(deserializer)); Ok(s.into_boxed_str()) @@ -1057,9 +1061,9 @@ impl Deserialize for Box { } #[cfg(any(feature = "std", feature = "alloc"))] -impl Deserialize for Arc { +impl<'de, T: Deserialize<'de>> Deserialize<'de> for Arc { fn deserialize(deserializer: D) -> Result, D::Error> - where D: Deserializer + where D: Deserializer<'de> { let val = try!(Deserialize::deserialize(deserializer)); Ok(Arc::new(val)) @@ -1067,9 +1071,9 @@ impl Deserialize for Arc { } #[cfg(any(feature = "std", feature = "alloc"))] -impl Deserialize for Rc { +impl<'de, T: Deserialize<'de>> Deserialize<'de> for Rc { fn deserialize(deserializer: D) -> Result, D::Error> - where D: Deserializer + where D: Deserializer<'de> { let val = try!(Deserialize::deserialize(deserializer)); Ok(Rc::new(val)) @@ -1077,13 +1081,13 @@ impl Deserialize for Rc { } #[cfg(any(feature = "std", feature = "collections"))] -impl<'a, T: ?Sized> Deserialize for Cow<'a, T> +impl<'de, 'a, T: ?Sized> Deserialize<'de> for Cow<'a, T> where T: ToOwned, - T::Owned: Deserialize + T::Owned: Deserialize<'de> { #[inline] fn deserialize(deserializer: D) -> Result, D::Error> - where D: Deserializer + where D: Deserializer<'de> { let val = try!(Deserialize::deserialize(deserializer)); Ok(Cow::Owned(val)) @@ -1101,22 +1105,22 @@ impl<'a, T: ?Sized> Deserialize for Cow<'a, T> // nanos: u32, // } #[cfg(feature = "std")] -impl Deserialize for Duration { +impl<'de> Deserialize<'de> for Duration { fn deserialize(deserializer: D) -> Result - where D: Deserializer + where D: Deserializer<'de> { enum Field { Secs, Nanos, }; - impl Deserialize for Field { + impl<'de> Deserialize<'de> for Field { fn deserialize(deserializer: D) -> Result - where D: Deserializer + where D: Deserializer<'de> { struct FieldVisitor; - impl Visitor for FieldVisitor { + impl<'de> Visitor<'de> for FieldVisitor { type Value = Field; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { @@ -1153,7 +1157,7 @@ impl Deserialize for Duration { struct DurationVisitor; - impl Visitor for DurationVisitor { + impl<'de> Visitor<'de> for DurationVisitor { type Value = Duration; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { @@ -1161,7 +1165,7 @@ impl Deserialize for Duration { } fn visit_seq(self, mut visitor: V) -> Result - where V: SeqVisitor + where V: SeqVisitor<'de> { let secs: u64 = match try!(visitor.visit()) { Some(value) => value, @@ -1179,7 +1183,7 @@ impl Deserialize for Duration { } fn visit_map(self, mut visitor: V) -> Result - where V: MapVisitor + where V: MapVisitor<'de> { let mut secs: Option = None; let mut nanos: Option = None; @@ -1227,22 +1231,22 @@ impl Deserialize for Duration { // end: u32, // } #[cfg(feature = "std")] -impl Deserialize for std::ops::Range { +impl<'de, Idx: Deserialize<'de>> Deserialize<'de> for std::ops::Range { fn deserialize(deserializer: D) -> Result - where D: Deserializer + where D: Deserializer<'de> { enum Field { Start, End, }; - impl Deserialize for Field { + impl<'de> Deserialize<'de> for Field { fn deserialize(deserializer: D) -> Result - where D: Deserializer + where D: Deserializer<'de> { struct FieldVisitor; - impl Visitor for FieldVisitor { + impl<'de> Visitor<'de> for FieldVisitor { type Value = Field; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { @@ -1281,7 +1285,7 @@ impl Deserialize for std::ops::Range { phantom: PhantomData, } - impl Visitor for RangeVisitor { + impl<'de, Idx: Deserialize<'de>> Visitor<'de> for RangeVisitor { type Value = std::ops::Range; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { @@ -1289,7 +1293,7 @@ impl Deserialize for std::ops::Range { } fn visit_seq(self, mut visitor: V) -> Result, V::Error> - where V: SeqVisitor + where V: SeqVisitor<'de> { let start: Idx = match try!(visitor.visit()) { Some(value) => value, @@ -1307,7 +1311,7 @@ impl Deserialize for std::ops::Range { } fn visit_map(self, mut visitor: V) -> Result, V::Error> - where V: MapVisitor + where V: MapVisitor<'de> { let mut start: Option = None; let mut end: Option = None; @@ -1351,11 +1355,11 @@ impl Deserialize for std::ops::Range { #[cfg(feature = "unstable")] #[allow(deprecated)] // num::Zero is deprecated but there is no replacement -impl Deserialize for NonZero - where T: Deserialize + PartialEq + Zeroable + Zero +impl<'de, T> Deserialize<'de> for NonZero + where T: Deserialize<'de> + PartialEq + Zeroable + Zero { fn deserialize(deserializer: D) -> Result, D::Error> - where D: Deserializer + where D: Deserializer<'de> { let value = try!(Deserialize::deserialize(deserializer)); if value == Zero::zero() { @@ -1368,26 +1372,26 @@ impl Deserialize for NonZero /////////////////////////////////////////////////////////////////////////////// -impl Deserialize for Result - where T: Deserialize, - E: Deserialize +impl<'de, T, E> Deserialize<'de> for Result + where T: Deserialize<'de>, + E: Deserialize<'de> { fn deserialize(deserializer: D) -> Result, D::Error> - where D: Deserializer + where D: Deserializer<'de> { enum Field { Ok, Err, } - impl Deserialize for Field { + impl<'de> Deserialize<'de> for Field { #[inline] fn deserialize(deserializer: D) -> Result - where D: Deserializer + where D: Deserializer<'de> { struct FieldVisitor; - impl Visitor for FieldVisitor { + impl<'de> Visitor<'de> for FieldVisitor { type Value = Field; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { @@ -1440,9 +1444,9 @@ impl Deserialize for Result struct ResultVisitor(PhantomData>); - impl Visitor for ResultVisitor - where T: Deserialize, - E: Deserialize + impl<'de, T, E> Visitor<'de> for ResultVisitor + where T: Deserialize<'de>, + E: Deserialize<'de> { type Value = Result; @@ -1451,7 +1455,7 @@ impl Deserialize for Result } fn visit_enum(self, visitor: V) -> Result, V::Error> - where V: EnumVisitor + where V: EnumVisitor<'de> { match try!(visitor.visit_variant()) { (Field::Ok, variant) => variant.visit_newtype().map(Ok), @@ -1472,14 +1476,14 @@ impl Deserialize for Result /// Deserialize and silently eats data given to it. pub struct IgnoredAny; -impl Deserialize for IgnoredAny { +impl<'de> Deserialize<'de> for IgnoredAny { #[inline] fn deserialize(deserializer: D) -> Result - where D: Deserializer + where D: Deserializer<'de> { struct IgnoredAnyVisitor; - impl Visitor for IgnoredAnyVisitor { + impl<'de> Visitor<'de> for IgnoredAnyVisitor { type Value = IgnoredAny; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { @@ -1520,14 +1524,14 @@ impl Deserialize for IgnoredAny { #[inline] fn visit_some(self, _: D) -> Result - where D: Deserializer + where D: Deserializer<'de> { Ok(IgnoredAny) } #[inline] fn visit_newtype_struct(self, _: D) -> Result - where D: Deserializer + where D: Deserializer<'de> { Ok(IgnoredAny) } @@ -1539,7 +1543,7 @@ impl Deserialize for IgnoredAny { #[inline] fn visit_seq(self, mut visitor: V) -> Result - where V: SeqVisitor + where V: SeqVisitor<'de> { while let Some(_) = try!(visitor.visit::()) { // Gobble @@ -1549,7 +1553,7 @@ impl Deserialize for IgnoredAny { #[inline] fn visit_map(self, mut visitor: V) -> Result - where V: MapVisitor + where V: MapVisitor<'de> { while let Some((_, _)) = try!(visitor.visit::()) { // Gobble diff --git a/serde/src/de/mod.rs b/serde/src/de/mod.rs index 334ef2ab..ad6944ba 100644 --- a/serde/src/de/mod.rs +++ b/serde/src/de/mod.rs @@ -147,9 +147,9 @@ pub trait Error: Sized + error::Error { /// # type Err = String; /// # fn from_str(_: &str) -> Result { unimplemented!() } /// # } - /// impl Deserialize for IpAddr { + /// impl<'de> Deserialize<'de> for IpAddr { /// fn deserialize(deserializer: D) -> Result - /// where D: Deserializer + /// where D: Deserializer<'de> /// { /// let s = try!(String::deserialize(deserializer)); /// s.parse().map_err(Error::custom) @@ -256,7 +256,7 @@ pub trait Error: Sized + error::Error { /// # use std::fmt; /// # #[allow(dead_code)] /// # struct Example; -/// # impl Visitor for Example { +/// # impl<'de> Visitor<'de> for Example { /// # type Value = (); /// fn visit_bool(self, v: bool) -> Result /// where E: Error @@ -376,7 +376,7 @@ impl<'a> fmt::Display for Unexpected<'a> { /// # use std::fmt; /// # #[allow(dead_code)] /// # struct Example; -/// # impl Visitor for Example { +/// # impl<'de> Visitor<'de> for Example { /// # type Value = (); /// fn visit_bool(self, v: bool) -> Result /// where E: Error @@ -405,8 +405,8 @@ pub trait Expected { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result; } -impl Expected for T - where T: Visitor +impl<'de, T> Expected for T + where T: Visitor<'de> { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { self.expecting(formatter) @@ -451,16 +451,36 @@ impl<'a> Display for Expected + 'a { /// [de]: https://docs.serde.rs/serde/de/index.html /// [codegen]: https://serde.rs/codegen.html /// [impl-deserialize]: https://serde.rs/impl-deserialize.html -pub trait Deserialize: Sized { +pub trait Deserialize<'de>: Sized { /// Deserialize this value from the given Serde deserializer. /// /// See the [Implementing `Deserialize`][impl-deserialize] section of the /// manual for more information about how to implement this method. /// /// [impl-deserialize]: https://serde.rs/impl-deserialize.html - fn deserialize(deserializer: D) -> Result where D: Deserializer; + fn deserialize(deserializer: D) -> Result + where D: Deserializer<'de>; } +/// A data structure that can be deserialized without borrowing any data from +/// the deserializer. +/// +/// This is primarily useful for trait bounds on functions. For example a +/// `from_str` function may be able to deserialize a data structure that borrows +/// from the input string, but a `from_reader` function may only deserialize +/// owned data. +/// +/// ```rust,ignore +/// pub fn from_str<'a, T>(s: &'a str) -> Result +/// where T: Deserialize<'a>; +/// +/// pub fn from_reader(rdr: R) -> Result +/// where R: Read, +/// T: DeserializeOwned; +/// ``` +pub trait DeserializeOwned: for<'de> Deserialize<'de> {} +impl DeserializeOwned for T where T: for<'de> Deserialize<'de> {} + /// `DeserializeSeed` is the stateful form of the `Deserialize` trait. If you /// ever find yourself looking for a way to pass data into a `Deserialize` impl, /// this trait is the way to do it. @@ -479,7 +499,7 @@ pub trait Deserialize: Sized { /// # #[allow(dead_code)] /// # enum Error {} /// # #[allow(dead_code)] -/// fn func() -> Result +/// fn func<'de, T: Deserialize<'de>>() -> Result /// # { unimplemented!() } /// ``` /// @@ -491,7 +511,7 @@ pub trait Deserialize: Sized { /// # #[allow(dead_code)] /// # enum Error {} /// # #[allow(dead_code)] -/// fn func_seed(seed: T) -> Result +/// fn func_seed<'de, T: DeserializeSeed<'de>>(seed: T) -> Result /// # { /// # let _ = seed; /// # unimplemented!() @@ -523,8 +543,8 @@ pub trait Deserialize: Sized { /// // appending each integer into the existing Vec. /// struct ExtendVec<'a, T: 'a>(&'a mut Vec); /// -/// impl<'a, T> DeserializeSeed for ExtendVec<'a, T> -/// where T: Deserialize +/// impl<'de, 'a, T> DeserializeSeed<'de> for ExtendVec<'a, T> +/// where T: Deserialize<'de> /// { /// // The return type of the `deserialize` method. This implementation /// // appends onto an existing vector but does not create any new data @@ -532,19 +552,19 @@ pub trait Deserialize: Sized { /// type Value = (); /// /// fn deserialize(self, deserializer: D) -> Result -/// where D: Deserializer +/// where D: Deserializer<'de> /// { /// // Visitor implementation that will walk an inner array of the JSON /// // input. /// struct ExtendVecVisitor<'a, T: 'a>(&'a mut Vec); /// -/// impl<'a, T> Visitor for ExtendVecVisitor<'a, T> -/// where T: Deserialize +/// impl<'de, 'a, T> Visitor<'de> for ExtendVecVisitor<'a, T> +/// where T: Deserialize<'de> /// { /// type Value = (); /// /// fn visit_seq(self, mut visitor: V) -> Result<(), V::Error> -/// where V: SeqVisitor +/// where V: SeqVisitor<'de> /// { /// // Visit each element in the inner array and push it onto /// // the existing vector. @@ -566,15 +586,15 @@ pub trait Deserialize: Sized { /// // Visitor implementation that will walk the outer array of the JSON input. /// struct FlattenedVecVisitor(PhantomData); /// -/// impl Visitor for FlattenedVecVisitor -/// where T: Deserialize +/// impl<'de, T> Visitor<'de> for FlattenedVecVisitor +/// where T: Deserialize<'de> /// { /// // This Visitor constructs a single Vec to hold the flattened /// // contents of the inner arrays. /// type Value = Vec; /// /// fn visit_seq(self, mut visitor: V) -> Result, V::Error> -/// where V: SeqVisitor +/// where V: SeqVisitor<'de> /// { /// // Create a single Vec to hold the flattened contents. /// let mut vec = Vec::new(); @@ -594,29 +614,30 @@ pub trait Deserialize: Sized { /// } /// /// # #[allow(dead_code)] -/// # fn example(deserializer: D) -> Result<(), D::Error> { +/// # fn example<'de, D: Deserializer<'de>>(deserializer: D) -> Result<(), D::Error> { /// let visitor = FlattenedVecVisitor(PhantomData); /// let flattened: Vec = deserializer.deserialize_seq(visitor)?; /// # let _ = flattened; /// # Ok(()) } /// ``` -pub trait DeserializeSeed: Sized { +pub trait DeserializeSeed<'de>: Sized { /// The type produced by using this seed. type Value; /// Equivalent to the more common `Deserialize::deserialize` method, except /// with some initial piece of data (the seed) passed in. - fn deserialize(self, deserializer: D) -> Result where D: Deserializer; + fn deserialize(self, deserializer: D) -> Result + where D: Deserializer<'de>; } -impl DeserializeSeed for PhantomData - where T: Deserialize +impl<'de, T> DeserializeSeed<'de> for PhantomData + where T: Deserialize<'de> { type Value = T; #[inline] fn deserialize(self, deserializer: D) -> Result - where D: Deserializer + where D: Deserializer<'de> { T::deserialize(deserializer) } @@ -705,7 +726,7 @@ impl DeserializeSeed for PhantomData /// what type is in the input. Know that relying on `Deserializer::deserialize` /// means your data type will be able to deserialize from self-describing /// formats only, ruling out Bincode and many others. -pub trait Deserializer: Sized { +pub trait Deserializer<'de>: Sized { /// The error type that can be returned if some error occurs during /// deserialization. type Error: Error; @@ -719,43 +740,43 @@ pub trait Deserializer: Sized { /// `Deserializer::deserialize` means your data type will be able to /// deserialize from self-describing formats only, ruling out Bincode and /// many others. - fn deserialize(self, visitor: V) -> Result where V: Visitor; + fn deserialize(self, visitor: V) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting a `bool` value. - fn deserialize_bool(self, visitor: V) -> Result where V: Visitor; + fn deserialize_bool(self, visitor: V) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting a `u8` value. - fn deserialize_u8(self, visitor: V) -> Result where V: Visitor; + fn deserialize_u8(self, visitor: V) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting a `u16` value. - fn deserialize_u16(self, visitor: V) -> Result where V: Visitor; + fn deserialize_u16(self, visitor: V) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting a `u32` value. - fn deserialize_u32(self, visitor: V) -> Result where V: Visitor; + fn deserialize_u32(self, visitor: V) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting a `u64` value. - fn deserialize_u64(self, visitor: V) -> Result where V: Visitor; + fn deserialize_u64(self, visitor: V) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting an `i8` value. - fn deserialize_i8(self, visitor: V) -> Result where V: Visitor; + fn deserialize_i8(self, visitor: V) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting an `i16` value. - fn deserialize_i16(self, visitor: V) -> Result where V: Visitor; + fn deserialize_i16(self, visitor: V) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting an `i32` value. - fn deserialize_i32(self, visitor: V) -> Result where V: Visitor; + fn deserialize_i32(self, visitor: V) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting an `i64` value. - fn deserialize_i64(self, visitor: V) -> Result where V: Visitor; + fn deserialize_i64(self, visitor: V) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting a `f32` value. - fn deserialize_f32(self, visitor: V) -> Result where V: Visitor; + fn deserialize_f32(self, visitor: V) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting a `f64` value. - fn deserialize_f64(self, visitor: V) -> Result where V: Visitor; + fn deserialize_f64(self, visitor: V) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting a `char` value. - fn deserialize_char(self, visitor: V) -> Result where V: Visitor; + fn deserialize_char(self, visitor: V) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting a string value and does /// not benefit from taking ownership of buffered data owned by the @@ -764,7 +785,7 @@ pub trait Deserializer: Sized { /// If the `Visitor` would benefit from taking ownership of `String` data, /// indiciate this to the `Deserializer` by using `deserialize_string` /// instead. - fn deserialize_str(self, visitor: V) -> Result where V: Visitor; + fn deserialize_str(self, visitor: V) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting a string value and would /// benefit from taking ownership of buffered data owned by the @@ -773,7 +794,7 @@ pub trait Deserializer: Sized { /// If the `Visitor` would not benefit from taking ownership of `String` /// data, indicate that to the `Deserializer` by using `deserialize_str` /// instead. - fn deserialize_string(self, visitor: V) -> Result where V: Visitor; + fn deserialize_string(self, visitor: V) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting a byte array and does not /// benefit from taking ownership of buffered data owned by the @@ -782,7 +803,7 @@ pub trait Deserializer: Sized { /// If the `Visitor` would benefit from taking ownership of `Vec` data, /// indicate this to the `Deserializer` by using `deserialize_byte_buf` /// instead. - fn deserialize_bytes(self, visitor: V) -> Result where V: Visitor; + fn deserialize_bytes(self, visitor: V) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting a byte array and would /// benefit from taking ownership of buffered data owned by the @@ -791,17 +812,17 @@ pub trait Deserializer: Sized { /// If the `Visitor` would not benefit from taking ownership of `Vec` /// data, indicate that to the `Deserializer` by using `deserialize_bytes` /// instead. - fn deserialize_byte_buf(self, visitor: V) -> Result where V: Visitor; + fn deserialize_byte_buf(self, visitor: V) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting an optional value. /// /// This allows deserializers that encode an optional value as a nullable /// value to convert the null value into `None` and a regular value into /// `Some(value)`. - fn deserialize_option(self, visitor: V) -> Result where V: Visitor; + fn deserialize_option(self, visitor: V) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting a unit value. - fn deserialize_unit(self, visitor: V) -> Result where V: Visitor; + fn deserialize_unit(self, visitor: V) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting a unit struct with a /// particular name. @@ -809,7 +830,7 @@ pub trait Deserializer: Sized { name: &'static str, visitor: V) -> Result - where V: Visitor; + where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting a newtype struct with a /// particular name. @@ -817,10 +838,10 @@ pub trait Deserializer: Sized { name: &'static str, visitor: V) -> Result - where V: Visitor; + where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting a sequence of values. - fn deserialize_seq(self, visitor: V) -> Result where V: Visitor; + fn deserialize_seq(self, visitor: V) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting a sequence of values and /// knows how many values there are without looking at the serialized data. @@ -828,12 +849,12 @@ pub trait Deserializer: Sized { len: usize, visitor: V) -> Result - where V: Visitor; + where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting a tuple value with a /// particular number of elements. fn deserialize_tuple(self, len: usize, visitor: V) -> Result - where V: Visitor; + where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting a tuple struct with a /// particular name and number of fields. @@ -842,10 +863,10 @@ pub trait Deserializer: Sized { len: usize, visitor: V) -> Result - where V: Visitor; + where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting a map of key-value pairs. - fn deserialize_map(self, visitor: V) -> Result where V: Visitor; + fn deserialize_map(self, visitor: V) -> Result where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting a struct with a particular /// name and fields. @@ -854,12 +875,12 @@ pub trait Deserializer: Sized { fields: &'static [&'static str], visitor: V) -> Result - where V: Visitor; + where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting the name of a struct /// field. fn deserialize_struct_field(self, visitor: V) -> Result - where V: Visitor; + where V: Visitor<'de>; /// Hint that the `Deserialize` type is expecting an enum value with a /// particular name and possible variants. @@ -868,14 +889,14 @@ pub trait Deserializer: Sized { variants: &'static [&'static str], visitor: V) -> Result - where V: Visitor; + where V: Visitor<'de>; /// Hint that the `Deserialize` type needs to deserialize a value whose type /// doesn't matter because it is ignored. /// /// Deserializers for non-self-describing formats may not support this mode. fn deserialize_ignored_any(self, visitor: V) -> Result - where V: Visitor; + where V: Visitor<'de>; } /////////////////////////////////////////////////////////////////////////////// @@ -892,7 +913,7 @@ pub trait Deserializer: Sized { /// min: usize, /// } /// -/// impl Visitor for LongString { +/// impl<'de> Visitor<'de> for LongString { /// type Value = String; /// /// fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { @@ -910,7 +931,7 @@ pub trait Deserializer: Sized { /// } /// } /// ``` -pub trait Visitor: Sized { +pub trait Visitor<'de>: Sized { /// The value produced by this visitor. type Value; @@ -925,7 +946,7 @@ pub trait Visitor: Sized { /// # use std::fmt; /// # #[allow(dead_code)] /// # struct S { max: usize } - /// # impl serde::de::Visitor for S { + /// # impl<'de> serde::de::Visitor<'de> for S { /// # type Value = (); /// fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { /// write!(formatter, "an integer between 0 and {}", self.max) @@ -1035,6 +1056,21 @@ pub trait Visitor: Sized { Err(Error::invalid_type(Unexpected::Str(v), &self)) } + /// Deserialize a `&str` that is borrowed from the input data. + /// + /// This enables zero-copy deserialization of strings in some formats. For + /// example JSON input containing the JSON string `"borrowed"` can be + /// deserialized with zero copying into a `&'a str` as long as the input + /// data outlives `'a`. + /// + /// The default implementation forwards to `visit_str`. + #[inline] + fn visit_borrowed_str(self, v: &'de str) -> Result + where E: Error + { + self.visit_str(v) + } + /// Deserialize a `String` into a `Value`. /// /// This method allows the `Visitor` to avoid a copy by taking ownership of @@ -1073,7 +1109,7 @@ pub trait Visitor: Sized { /// Deserialize a present optional `Value`. fn visit_some(self, deserializer: D) -> Result - where D: Deserializer + where D: Deserializer<'de> { let _ = deserializer; Err(Error::invalid_type(Unexpected::Option, &self)) @@ -1081,7 +1117,7 @@ pub trait Visitor: Sized { /// Deserialize `Value` as a newtype struct. fn visit_newtype_struct(self, deserializer: D) -> Result - where D: Deserializer + where D: Deserializer<'de> { let _ = deserializer; Err(Error::invalid_type(Unexpected::NewtypeStruct, &self)) @@ -1089,7 +1125,7 @@ pub trait Visitor: Sized { /// Deserialize `Value` as a sequence of elements. fn visit_seq(self, visitor: V) -> Result - where V: SeqVisitor + where V: SeqVisitor<'de> { let _ = visitor; Err(Error::invalid_type(Unexpected::Seq, &self)) @@ -1097,7 +1133,7 @@ pub trait Visitor: Sized { /// Deserialize `Value` as a key-value map. fn visit_map(self, visitor: V) -> Result - where V: MapVisitor + where V: MapVisitor<'de> { let _ = visitor; Err(Error::invalid_type(Unexpected::Map, &self)) @@ -1105,7 +1141,7 @@ pub trait Visitor: Sized { /// Deserialize `Value` as an enum. fn visit_enum(self, visitor: V) -> Result - where V: EnumVisitor + where V: EnumVisitor<'de> { let _ = visitor; Err(Error::invalid_type(Unexpected::Enum, &self)) @@ -1128,6 +1164,20 @@ pub trait Visitor: Sized { Err(Error::invalid_type(Unexpected::Bytes(v), &self)) } + /// Deserialize a `&[u8]` that is borrowed from the input data. + /// + /// This enables zero-copy deserialization of bytes in some formats. For + /// example Bincode data containing bytes can be deserialized with zero + /// copying into a `&'a [u8]` as long as the input data outlives `'a`. + /// + /// The default implementation forwards to `visit_bytes`. + #[inline] + fn visit_borrowed_bytes(self, v: &'de [u8]) -> Result + where E: Error + { + self.visit_bytes(v) + } + /// Deserialize a `Vec` into a `Value`. /// /// This method allows the `Visitor` to avoid a copy by taking ownership of @@ -1157,7 +1207,7 @@ pub trait Visitor: Sized { /// /// This is a trait that a `Deserializer` passes to a `Visitor` implementation, /// which deserializes each item in a sequence. -pub trait SeqVisitor { +pub trait SeqVisitor<'de> { /// The error type that can be returned if some error occurs during /// deserialization. type Error: Error; @@ -1168,7 +1218,7 @@ pub trait SeqVisitor { /// `Deserialize` implementations should typically use `SeqVisitor::visit` /// instead. fn visit_seed(&mut self, seed: T) -> Result, Self::Error> - where T: DeserializeSeed; + where T: DeserializeSeed<'de>; /// This returns `Ok(Some(value))` for the next value in the sequence, or /// `Ok(None)` if there are no more remaining items. @@ -1177,7 +1227,7 @@ pub trait SeqVisitor { /// `SeqVisitor` implementations should not override the default behavior. #[inline] fn visit(&mut self) -> Result, Self::Error> - where T: Deserialize + where T: Deserialize<'de> { self.visit_seed(PhantomData) } @@ -1189,21 +1239,21 @@ pub trait SeqVisitor { } } -impl<'a, V> SeqVisitor for &'a mut V - where V: SeqVisitor +impl<'de, 'a, V> SeqVisitor<'de> for &'a mut V + where V: SeqVisitor<'de> { type Error = V::Error; #[inline] fn visit_seed(&mut self, seed: T) -> Result, V::Error> - where T: DeserializeSeed + where T: DeserializeSeed<'de> { (**self).visit_seed(seed) } #[inline] fn visit(&mut self) -> Result, V::Error> - where T: Deserialize + where T: Deserialize<'de> { (**self).visit() } @@ -1219,7 +1269,7 @@ impl<'a, V> SeqVisitor for &'a mut V /// `MapVisitor` visits each item in a sequence. /// /// This is a trait that a `Deserializer` passes to a `Visitor` implementation. -pub trait MapVisitor { +pub trait MapVisitor<'de> { /// The error type that can be returned if some error occurs during /// deserialization. type Error: Error; @@ -1230,14 +1280,14 @@ pub trait MapVisitor { /// `Deserialize` implementations should typically use /// `MapVisitor::visit_key` or `MapVisitor::visit` instead. fn visit_key_seed(&mut self, seed: K) -> Result, Self::Error> - where K: DeserializeSeed; + where K: DeserializeSeed<'de>; /// This returns a `Ok(value)` for the next value in the map. /// /// `Deserialize` implementations should typically use /// `MapVisitor::visit_value` instead. fn visit_value_seed(&mut self, seed: V) -> Result - where V: DeserializeSeed; + where V: DeserializeSeed<'de>; /// This returns `Ok(Some((key, value)))` for the next (key-value) pair in /// the map, or `Ok(None)` if there are no more remaining items. @@ -1252,8 +1302,8 @@ pub trait MapVisitor { kseed: K, vseed: V) -> Result, Self::Error> - where K: DeserializeSeed, - V: DeserializeSeed + where K: DeserializeSeed<'de>, + V: DeserializeSeed<'de> { match try!(self.visit_key_seed(kseed)) { Some(key) => { @@ -1271,7 +1321,7 @@ pub trait MapVisitor { /// `MapVisitor` implementations should not override the default behavior. #[inline] fn visit_key(&mut self) -> Result, Self::Error> - where K: Deserialize + where K: Deserialize<'de> { self.visit_key_seed(PhantomData) } @@ -1282,7 +1332,7 @@ pub trait MapVisitor { /// `MapVisitor` implementations should not override the default behavior. #[inline] fn visit_value(&mut self) -> Result - where V: Deserialize + where V: Deserialize<'de> { self.visit_value_seed(PhantomData) } @@ -1294,8 +1344,8 @@ pub trait MapVisitor { /// `MapVisitor` implementations should not override the default behavior. #[inline] fn visit(&mut self) -> Result, Self::Error> - where K: Deserialize, - V: Deserialize + where K: Deserialize<'de>, + V: Deserialize<'de> { self.visit_seed(PhantomData, PhantomData) } @@ -1307,21 +1357,21 @@ pub trait MapVisitor { } } -impl<'a, V_> MapVisitor for &'a mut V_ - where V_: MapVisitor +impl<'de, 'a, V_> MapVisitor<'de> for &'a mut V_ + where V_: MapVisitor<'de> { type Error = V_::Error; #[inline] fn visit_key_seed(&mut self, seed: K) -> Result, Self::Error> - where K: DeserializeSeed + where K: DeserializeSeed<'de> { (**self).visit_key_seed(seed) } #[inline] fn visit_value_seed(&mut self, seed: V) -> Result - where V: DeserializeSeed + where V: DeserializeSeed<'de> { (**self).visit_value_seed(seed) } @@ -1331,30 +1381,30 @@ impl<'a, V_> MapVisitor for &'a mut V_ kseed: K, vseed: V) -> Result, Self::Error> - where K: DeserializeSeed, - V: DeserializeSeed + where K: DeserializeSeed<'de>, + V: DeserializeSeed<'de> { (**self).visit_seed(kseed, vseed) } #[inline] fn visit(&mut self) -> Result, V_::Error> - where K: Deserialize, - V: Deserialize + where K: Deserialize<'de>, + V: Deserialize<'de> { (**self).visit() } #[inline] fn visit_key(&mut self) -> Result, V_::Error> - where K: Deserialize + where K: Deserialize<'de> { (**self).visit_key() } #[inline] fn visit_value(&mut self) -> Result - where V: Deserialize + where V: Deserialize<'de> { (**self).visit_value() } @@ -1370,20 +1420,20 @@ impl<'a, V_> MapVisitor for &'a mut V_ /// `EnumVisitor` is a visitor that is created by the `Deserializer` and passed /// to the `Deserialize` in order to identify which variant of an enum to /// deserialize. -pub trait EnumVisitor: Sized { +pub trait EnumVisitor<'de>: Sized { /// The error type that can be returned if some error occurs during /// deserialization. type Error: Error; /// The `Visitor` that will be used to deserialize the content of the enum /// variant. - type Variant: VariantVisitor; + type Variant: VariantVisitor<'de, Error = Self::Error>; /// `visit_variant` is called to identify which variant to deserialize. /// /// `Deserialize` implementations should typically use /// `EnumVisitor::visit_variant` instead. fn visit_variant_seed(self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error> - where V: DeserializeSeed; + where V: DeserializeSeed<'de>; /// `visit_variant` is called to identify which variant to deserialize. /// @@ -1391,7 +1441,7 @@ pub trait EnumVisitor: Sized { /// `EnumVisitor` implementations should not override the default behavior. #[inline] fn visit_variant(self) -> Result<(V, Self::Variant), Self::Error> - where V: Deserialize + where V: Deserialize<'de> { self.visit_variant_seed(PhantomData) } @@ -1400,7 +1450,7 @@ pub trait EnumVisitor: Sized { /// `VariantVisitor` is a visitor that is created by the `Deserializer` and /// passed to the `Deserialize` to deserialize the content of a particular enum /// variant. -pub trait VariantVisitor: Sized { +pub trait VariantVisitor<'de>: Sized { /// The error type that can be returned if some error occurs during /// deserialization. Must match the error type of our `EnumVisitor`. type Error: Error; @@ -1437,7 +1487,7 @@ pub trait VariantVisitor: Sized { /// } /// ``` fn visit_newtype_seed(self, seed: T) -> Result - where T: DeserializeSeed; + where T: DeserializeSeed<'de>; /// Called when deserializing a variant with a single value. /// @@ -1446,7 +1496,7 @@ pub trait VariantVisitor: Sized { /// behavior. #[inline] fn visit_newtype(self) -> Result - where T: Deserialize + where T: Deserialize<'de> { self.visit_newtype_seed(PhantomData) } @@ -1470,7 +1520,7 @@ pub trait VariantVisitor: Sized { /// } /// ``` fn visit_tuple(self, len: usize, visitor: V) -> Result - where V: Visitor; + where V: Visitor<'de>; /// Called when deserializing a struct-like variant. /// @@ -1494,7 +1544,7 @@ pub trait VariantVisitor: Sized { fields: &'static [&'static str], visitor: V) -> Result - where V: Visitor; + where V: Visitor<'de>; } /////////////////////////////////////////////////////////////////////////////// diff --git a/serde/src/de/private.rs b/serde/src/de/private.rs index 092d66a6..0e001962 100644 --- a/serde/src/de/private.rs +++ b/serde/src/de/private.rs @@ -9,25 +9,25 @@ pub use de::content::{Content, ContentRefDeserializer, ContentDeserializer, Tagg /// If the missing field is of type `Option` then treat is as `None`, /// otherwise it is an error. -pub fn missing_field(field: &'static str) -> Result - where V: Deserialize, +pub fn missing_field<'de, V, E>(field: &'static str) -> Result + where V: Deserialize<'de>, E: Error { struct MissingFieldDeserializer(&'static str, PhantomData); - impl Deserializer for MissingFieldDeserializer + impl<'de, E> Deserializer<'de> for MissingFieldDeserializer where E: Error { type Error = E; fn deserialize(self, _visitor: V) -> Result - where V: Visitor + where V: Visitor<'de> { Err(Error::missing_field(self.0)) } fn deserialize_option(self, visitor: V) -> Result - where V: Visitor + where V: Visitor<'de> { visitor.visit_none() } diff --git a/serde/src/de/value.rs b/serde/src/de/value.rs index 00f32d68..13aaaba7 100644 --- a/serde/src/de/value.rs +++ b/serde/src/de/value.rs @@ -83,9 +83,9 @@ impl error::Error for Error { /////////////////////////////////////////////////////////////////////////////// /// This trait converts primitive types into a deserializer. -pub trait ValueDeserializer { +pub trait ValueDeserializer<'de, E: de::Error = Error> { /// The actual deserializer type. - type Deserializer: de::Deserializer; + type Deserializer: de::Deserializer<'de, Error = E>; /// Convert this value into a deserializer. fn into_deserializer(self) -> Self::Deserializer; @@ -93,7 +93,7 @@ pub trait ValueDeserializer { /////////////////////////////////////////////////////////////////////////////// -impl ValueDeserializer for () +impl<'de, E> ValueDeserializer<'de, E> for () where E: de::Error { type Deserializer = UnitDeserializer; @@ -108,7 +108,7 @@ pub struct UnitDeserializer { marker: PhantomData, } -impl de::Deserializer for UnitDeserializer +impl<'de, E> de::Deserializer<'de> for UnitDeserializer where E: de::Error { type Error = E; @@ -120,13 +120,13 @@ impl de::Deserializer for UnitDeserializer } fn deserialize(self, visitor: V) -> Result - where V: de::Visitor + where V: de::Visitor<'de> { visitor.visit_unit() } fn deserialize_option(self, visitor: V) -> Result - where V: de::Visitor + where V: de::Visitor<'de> { visitor.visit_none() } @@ -142,7 +142,7 @@ macro_rules! primitive_deserializer { marker: PhantomData } - impl ValueDeserializer for $ty + impl<'de, E> ValueDeserializer<'de, E> for $ty where E: de::Error, { type Deserializer = $name; @@ -155,7 +155,7 @@ macro_rules! primitive_deserializer { } } - impl de::Deserializer for $name + impl<'de, E> de::Deserializer<'de> for $name where E: de::Error, { type Error = E; @@ -167,7 +167,7 @@ macro_rules! primitive_deserializer { } fn deserialize(self, visitor: V) -> Result - where V: de::Visitor, + where V: de::Visitor<'de>, { visitor.$method(self.value $($cast)*) } @@ -195,7 +195,7 @@ pub struct U32Deserializer { marker: PhantomData, } -impl ValueDeserializer for u32 +impl<'de, E> ValueDeserializer<'de, E> for u32 where E: de::Error { type Deserializer = U32Deserializer; @@ -208,7 +208,7 @@ impl ValueDeserializer for u32 } } -impl de::Deserializer for U32Deserializer +impl<'de, E> de::Deserializer<'de> for U32Deserializer where E: de::Error { type Error = E; @@ -220,7 +220,7 @@ impl de::Deserializer for U32Deserializer } fn deserialize(self, visitor: V) -> Result - where V: de::Visitor + where V: de::Visitor<'de> { visitor.visit_u32(self.value) } @@ -230,20 +230,20 @@ impl de::Deserializer for U32Deserializer _variants: &'static [&'static str], visitor: V) -> Result - where V: de::Visitor + where V: de::Visitor<'de> { visitor.visit_enum(self) } } -impl de::EnumVisitor for U32Deserializer +impl<'de, E> de::EnumVisitor<'de> for U32Deserializer where E: de::Error { type Error = E; type Variant = private::UnitOnly; fn visit_variant_seed(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error> - where T: de::DeserializeSeed + where T: de::DeserializeSeed<'de> { seed.deserialize(self).map(private::unit_only) } @@ -257,7 +257,7 @@ pub struct StrDeserializer<'a, E> { marker: PhantomData, } -impl<'a, E> ValueDeserializer for &'a str +impl<'de, 'a, E> ValueDeserializer<'de, E> for &'a str where E: de::Error { type Deserializer = StrDeserializer<'a, E>; @@ -270,13 +270,13 @@ impl<'a, E> ValueDeserializer for &'a str } } -impl<'a, E> de::Deserializer for StrDeserializer<'a, E> +impl<'de, 'a, E> de::Deserializer<'de> for StrDeserializer<'a, E> where E: de::Error { type Error = E; fn deserialize(self, visitor: V) -> Result - where V: de::Visitor + where V: de::Visitor<'de> { visitor.visit_str(self.value) } @@ -286,7 +286,7 @@ impl<'a, E> de::Deserializer for StrDeserializer<'a, E> _variants: &'static [&'static str], visitor: V) -> Result - where V: de::Visitor + where V: de::Visitor<'de> { visitor.visit_enum(self) } @@ -298,14 +298,14 @@ impl<'a, E> de::Deserializer for StrDeserializer<'a, E> } } -impl<'a, E> de::EnumVisitor for StrDeserializer<'a, E> +impl<'de, 'a, E> de::EnumVisitor<'de> for StrDeserializer<'a, E> where E: de::Error { type Error = E; type Variant = private::UnitOnly; fn visit_variant_seed(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error> - where T: de::DeserializeSeed + where T: de::DeserializeSeed<'de> { seed.deserialize(self).map(private::unit_only) } @@ -321,7 +321,7 @@ pub struct StringDeserializer { } #[cfg(any(feature = "std", feature = "collections"))] -impl ValueDeserializer for String +impl<'de, E> ValueDeserializer<'de, E> for String where E: de::Error { type Deserializer = StringDeserializer; @@ -335,13 +335,13 @@ impl ValueDeserializer for String } #[cfg(any(feature = "std", feature = "collections"))] -impl de::Deserializer for StringDeserializer +impl<'de, E> de::Deserializer<'de> for StringDeserializer where E: de::Error { type Error = E; fn deserialize(self, visitor: V) -> Result - where V: de::Visitor + where V: de::Visitor<'de> { visitor.visit_string(self.value) } @@ -351,7 +351,7 @@ impl de::Deserializer for StringDeserializer _variants: &'static [&'static str], visitor: V) -> Result - where V: de::Visitor + where V: de::Visitor<'de> { visitor.visit_enum(self) } @@ -364,14 +364,14 @@ impl de::Deserializer for StringDeserializer } #[cfg(any(feature = "std", feature = "collections"))] -impl<'a, E> de::EnumVisitor for StringDeserializer +impl<'de, 'a, E> de::EnumVisitor<'de> for StringDeserializer where E: de::Error { type Error = E; type Variant = private::UnitOnly; fn visit_variant_seed(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error> - where T: de::DeserializeSeed + where T: de::DeserializeSeed<'de> { seed.deserialize(self).map(private::unit_only) } @@ -387,7 +387,7 @@ pub struct CowStrDeserializer<'a, E> { } #[cfg(any(feature = "std", feature = "collections"))] -impl<'a, E> ValueDeserializer for Cow<'a, str> +impl<'de, 'a, E> ValueDeserializer<'de, E> for Cow<'a, str> where E: de::Error { type Deserializer = CowStrDeserializer<'a, E>; @@ -401,13 +401,13 @@ impl<'a, E> ValueDeserializer for Cow<'a, str> } #[cfg(any(feature = "std", feature = "collections"))] -impl<'a, E> de::Deserializer for CowStrDeserializer<'a, E> +impl<'de, 'a, E> de::Deserializer<'de> for CowStrDeserializer<'a, E> where E: de::Error { type Error = E; fn deserialize(self, visitor: V) -> Result - where V: de::Visitor + where V: de::Visitor<'de> { match self.value { Cow::Borrowed(string) => visitor.visit_str(string), @@ -420,7 +420,7 @@ impl<'a, E> de::Deserializer for CowStrDeserializer<'a, E> _variants: &'static [&'static str], visitor: V) -> Result - where V: de::Visitor + where V: de::Visitor<'de> { visitor.visit_enum(self) } @@ -433,14 +433,14 @@ impl<'a, E> de::Deserializer for CowStrDeserializer<'a, E> } #[cfg(any(feature = "std", feature = "collections"))] -impl<'a, E> de::EnumVisitor for CowStrDeserializer<'a, E> +impl<'de, 'a, E> de::EnumVisitor<'de> for CowStrDeserializer<'a, E> where E: de::Error { type Error = E; type Variant = private::UnitOnly; fn visit_variant_seed(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error> - where T: de::DeserializeSeed + where T: de::DeserializeSeed<'de> { seed.deserialize(self).map(private::unit_only) } @@ -485,15 +485,15 @@ impl SeqDeserializer } } -impl de::Deserializer for SeqDeserializer +impl<'de, I, T, E> de::Deserializer<'de> for SeqDeserializer where I: Iterator, - T: ValueDeserializer, + T: ValueDeserializer<'de, E>, E: de::Error { type Error = E; fn deserialize(mut self, visitor: V) -> Result - where V: de::Visitor + where V: de::Visitor<'de> { let v = try!(visitor.visit_seq(&mut self)); try!(self.end()); @@ -507,15 +507,15 @@ impl de::Deserializer for SeqDeserializer } } -impl de::SeqVisitor for SeqDeserializer +impl<'de, I, T, E> de::SeqVisitor<'de> for SeqDeserializer where I: Iterator, - T: ValueDeserializer, + T: ValueDeserializer<'de, E>, E: de::Error { type Error = E; fn visit_seed(&mut self, seed: V) -> Result, Self::Error> - where V: de::DeserializeSeed + where V: de::DeserializeSeed<'de> { match self.iter.next() { Some(value) => { @@ -546,8 +546,8 @@ impl Expected for ExpectedInSeq { /////////////////////////////////////////////////////////////////////////////// #[cfg(any(feature = "std", feature = "collections"))] -impl ValueDeserializer for Vec - where T: ValueDeserializer, +impl<'de, T, E> ValueDeserializer<'de, E> for Vec + where T: ValueDeserializer<'de, E>, E: de::Error { type Deserializer = SeqDeserializer, E>; @@ -558,8 +558,8 @@ impl ValueDeserializer for Vec } #[cfg(any(feature = "std", feature = "collections"))] -impl ValueDeserializer for BTreeSet - where T: ValueDeserializer + Eq + Ord, +impl<'de, T, E> ValueDeserializer<'de, E> for BTreeSet + where T: ValueDeserializer<'de, E> + Eq + Ord, E: de::Error { type Deserializer = SeqDeserializer, E>; @@ -570,8 +570,8 @@ impl ValueDeserializer for BTreeSet } #[cfg(feature = "std")] -impl ValueDeserializer for HashSet - where T: ValueDeserializer + Eq + Hash, +impl<'de, T, E> ValueDeserializer<'de, E> for HashSet + where T: ValueDeserializer<'de, E> + Eq + Hash, E: de::Error { type Deserializer = SeqDeserializer, E>; @@ -584,31 +584,27 @@ impl ValueDeserializer for HashSet /////////////////////////////////////////////////////////////////////////////// /// A helper deserializer that deserializes a sequence using a `SeqVisitor`. -pub struct SeqVisitorDeserializer { +pub struct SeqVisitorDeserializer { visitor: V_, - marker: PhantomData, } -impl SeqVisitorDeserializer - where V_: de::SeqVisitor, - E: de::Error -{ +impl SeqVisitorDeserializer { /// Construct a new `SeqVisitorDeserializer`. pub fn new(visitor: V_) -> Self { SeqVisitorDeserializer { visitor: visitor, - marker: PhantomData, } } } -impl de::Deserializer for SeqVisitorDeserializer - where V_: de::SeqVisitor, - E: de::Error +impl<'de, V_> de::Deserializer<'de> for SeqVisitorDeserializer + where V_: de::SeqVisitor<'de> { - type Error = E; + type Error = V_::Error; - fn deserialize(self, visitor: V) -> Result { + fn deserialize(self, visitor: V) -> Result + where V: de::Visitor<'de> + { visitor.visit_seq(self.visitor) } @@ -622,24 +618,25 @@ impl de::Deserializer for SeqVisitorDeserializer /////////////////////////////////////////////////////////////////////////////// /// A helper deserializer that deserializes a map. -pub struct MapDeserializer +pub struct MapDeserializer<'de, I, E> where I: Iterator, I::Item: private::Pair, - ::First: ValueDeserializer, - ::Second: ValueDeserializer, + ::First: ValueDeserializer<'de, E>, + ::Second: ValueDeserializer<'de, E>, E: de::Error { iter: iter::Fuse, value: Option<::Second>, count: usize, - marker: PhantomData, + lifetime: PhantomData<&'de ()>, + error: PhantomData, } -impl MapDeserializer +impl<'de, I, E> MapDeserializer<'de, I, E> where I: Iterator, I::Item: private::Pair, - ::First: ValueDeserializer, - ::Second: ValueDeserializer, + ::First: ValueDeserializer<'de, E>, + ::Second: ValueDeserializer<'de, E>, E: de::Error { /// Construct a new `MapDeserializer`. @@ -648,7 +645,8 @@ impl MapDeserializer iter: iter.fuse(), value: None, count: 0, - marker: PhantomData, + lifetime: PhantomData, + error: PhantomData, } } @@ -681,17 +679,17 @@ impl MapDeserializer } } -impl de::Deserializer for MapDeserializer +impl<'de, I, E> de::Deserializer<'de> for MapDeserializer<'de, I, E> where I: Iterator, I::Item: private::Pair, - ::First: ValueDeserializer, - ::Second: ValueDeserializer, + ::First: ValueDeserializer<'de, E>, + ::Second: ValueDeserializer<'de, E>, E: de::Error { type Error = E; fn deserialize(mut self, visitor: V_) -> Result - where V_: de::Visitor + where V_: de::Visitor<'de> { let value = try!(visitor.visit_map(&mut self)); try!(self.end()); @@ -699,7 +697,7 @@ impl de::Deserializer for MapDeserializer } fn deserialize_seq(mut self, visitor: V_) -> Result - where V_: de::Visitor + where V_: de::Visitor<'de> { let value = try!(visitor.visit_seq(&mut self)); try!(self.end()); @@ -710,7 +708,7 @@ impl de::Deserializer for MapDeserializer _len: usize, visitor: V_) -> Result - where V_: de::Visitor + where V_: de::Visitor<'de> { self.deserialize_seq(visitor) } @@ -722,17 +720,17 @@ impl de::Deserializer for MapDeserializer } } -impl de::MapVisitor for MapDeserializer +impl<'de, I, E> de::MapVisitor<'de> for MapDeserializer<'de, I, E> where I: Iterator, I::Item: private::Pair, - ::First: ValueDeserializer, - ::Second: ValueDeserializer, + ::First: ValueDeserializer<'de, E>, + ::Second: ValueDeserializer<'de, E>, E: de::Error { type Error = E; fn visit_key_seed(&mut self, seed: T) -> Result, Self::Error> - where T: de::DeserializeSeed + where T: de::DeserializeSeed<'de> { match self.next_pair() { Some((key, value)) => { @@ -744,7 +742,7 @@ impl de::MapVisitor for MapDeserializer } fn visit_value_seed(&mut self, seed: T) -> Result - where T: de::DeserializeSeed + where T: de::DeserializeSeed<'de> { let value = self.value.take(); // Panic because this indicates a bug in the program rather than an @@ -757,8 +755,8 @@ impl de::MapVisitor for MapDeserializer kseed: TK, vseed: TV) -> Result, Self::Error> - where TK: de::DeserializeSeed, - TV: de::DeserializeSeed + where TK: de::DeserializeSeed<'de>, + TV: de::DeserializeSeed<'de> { match self.next_pair() { Some((key, value)) => { @@ -775,17 +773,17 @@ impl de::MapVisitor for MapDeserializer } } -impl de::SeqVisitor for MapDeserializer +impl<'de, I, E> de::SeqVisitor<'de> for MapDeserializer<'de, I, E> where I: Iterator, I::Item: private::Pair, - ::First: ValueDeserializer, - ::Second: ValueDeserializer, + ::First: ValueDeserializer<'de, E>, + ::Second: ValueDeserializer<'de, E>, E: de::Error { type Error = E; fn visit_seed(&mut self, seed: T) -> Result, Self::Error> - where T: de::DeserializeSeed + where T: de::DeserializeSeed<'de> { match self.next_pair() { Some((k, v)) => { @@ -805,9 +803,9 @@ impl de::SeqVisitor for MapDeserializer // sequence of pairs. struct PairDeserializer(A, B, PhantomData); -impl de::Deserializer for PairDeserializer - where A: ValueDeserializer, - B: ValueDeserializer, +impl<'de, A, B, E> de::Deserializer<'de> for PairDeserializer + where A: ValueDeserializer<'de, E>, + B: ValueDeserializer<'de, E>, E: de::Error { type Error = E; @@ -819,13 +817,13 @@ impl de::Deserializer for PairDeserializer } fn deserialize(self, visitor: V) -> Result - where V: de::Visitor + where V: de::Visitor<'de> { self.deserialize_seq(visitor) } fn deserialize_seq(self, visitor: V) -> Result - where V: de::Visitor + where V: de::Visitor<'de> { let mut pair_visitor = PairVisitor(Some(self.0), Some(self.1), PhantomData); let pair = try!(visitor.visit_seq(&mut pair_visitor)); @@ -840,7 +838,7 @@ impl de::Deserializer for PairDeserializer } fn deserialize_seq_fixed_size(self, len: usize, visitor: V) -> Result - where V: de::Visitor + where V: de::Visitor<'de> { if len == 2 { self.deserialize_seq(visitor) @@ -854,15 +852,15 @@ impl de::Deserializer for PairDeserializer struct PairVisitor(Option, Option, PhantomData); -impl de::SeqVisitor for PairVisitor - where A: ValueDeserializer, - B: ValueDeserializer, +impl<'de, A, B, E> de::SeqVisitor<'de> for PairVisitor + where A: ValueDeserializer<'de, E>, + B: ValueDeserializer<'de, E>, E: de::Error { type Error = E; fn visit_seed(&mut self, seed: T) -> Result, Self::Error> - where T: de::DeserializeSeed + where T: de::DeserializeSeed<'de> { if let Some(k) = self.0.take() { seed.deserialize(k.into_deserializer()).map(Some) @@ -900,12 +898,12 @@ impl Expected for ExpectedInMap { /////////////////////////////////////////////////////////////////////////////// #[cfg(any(feature = "std", feature = "collections"))] -impl ValueDeserializer for BTreeMap - where K: ValueDeserializer + Eq + Ord, - V: ValueDeserializer, +impl<'de, K, V, E> ValueDeserializer<'de, E> for BTreeMap + where K: ValueDeserializer<'de, E> + Eq + Ord, + V: ValueDeserializer<'de, E>, E: de::Error { - type Deserializer = MapDeserializer, E>; + type Deserializer = MapDeserializer<'de, btree_map::IntoIter, E>; fn into_deserializer(self) -> Self::Deserializer { MapDeserializer::new(self.into_iter()) @@ -913,12 +911,12 @@ impl ValueDeserializer for BTreeMap } #[cfg(feature = "std")] -impl ValueDeserializer for HashMap - where K: ValueDeserializer + Eq + Hash, - V: ValueDeserializer, +impl<'de, K, V, E> ValueDeserializer<'de, E> for HashMap + where K: ValueDeserializer<'de, E> + Eq + Hash, + V: ValueDeserializer<'de, E>, E: de::Error { - type Deserializer = MapDeserializer, E>; + type Deserializer = MapDeserializer<'de, hash_map::IntoIter, E>; fn into_deserializer(self) -> Self::Deserializer { MapDeserializer::new(self.into_iter()) @@ -928,31 +926,27 @@ impl ValueDeserializer for HashMap /////////////////////////////////////////////////////////////////////////////// /// A helper deserializer that deserializes a map using a `MapVisitor`. -pub struct MapVisitorDeserializer { +pub struct MapVisitorDeserializer { visitor: V_, - marker: PhantomData, } -impl MapVisitorDeserializer - where V_: de::MapVisitor, - E: de::Error -{ +impl MapVisitorDeserializer { /// Construct a new `MapVisitorDeserializer`. pub fn new(visitor: V_) -> Self { MapVisitorDeserializer { visitor: visitor, - marker: PhantomData, } } } -impl de::Deserializer for MapVisitorDeserializer - where V_: de::MapVisitor, - E: de::Error +impl<'de, V_> de::Deserializer<'de> for MapVisitorDeserializer + where V_: de::MapVisitor<'de> { - type Error = E; + type Error = V_::Error; - fn deserialize(self, visitor: V) -> Result { + fn deserialize(self, visitor: V) -> Result + where V: de::Visitor<'de> + { visitor.visit_map(self.visitor) } @@ -965,7 +959,7 @@ impl de::Deserializer for MapVisitorDeserializer /////////////////////////////////////////////////////////////////////////////// -impl<'a, E> ValueDeserializer for bytes::Bytes<'a> +impl<'de, 'a, E> ValueDeserializer<'de, E> for bytes::Bytes<'a> where E: de::Error { type Deserializer = BytesDeserializer<'a, E>; @@ -984,13 +978,13 @@ pub struct BytesDeserializer<'a, E> { marker: PhantomData, } -impl<'a, E> de::Deserializer for BytesDeserializer<'a, E> +impl<'de, 'a, E> de::Deserializer<'de> for BytesDeserializer<'a, E> where E: de::Error { type Error = E; fn deserialize(self, visitor: V) -> Result - where V: de::Visitor + where V: de::Visitor<'de> { visitor.visit_bytes(self.value) } @@ -1005,7 +999,7 @@ impl<'a, E> de::Deserializer for BytesDeserializer<'a, E> /////////////////////////////////////////////////////////////////////////////// #[cfg(any(feature = "std", feature = "collections"))] -impl ValueDeserializer for bytes::ByteBuf +impl<'de, E> ValueDeserializer<'de, E> for bytes::ByteBuf where E: de::Error { type Deserializer = ByteBufDeserializer; @@ -1026,13 +1020,13 @@ pub struct ByteBufDeserializer { } #[cfg(any(feature = "std", feature = "collections"))] -impl de::Deserializer for ByteBufDeserializer +impl<'de, E> de::Deserializer<'de> for ByteBufDeserializer where E: de::Error { type Error = E; fn deserialize(self, visitor: V) -> Result - where V: de::Visitor + where V: de::Visitor<'de> { visitor.visit_byte_buf(self.value) } @@ -1058,7 +1052,7 @@ mod private { (t, UnitOnly { marker: PhantomData }) } - impl de::VariantVisitor for UnitOnly + impl<'de, E> de::VariantVisitor<'de> for UnitOnly where E: de::Error { type Error = E; @@ -1068,13 +1062,13 @@ mod private { } fn visit_newtype_seed(self, _seed: T) -> Result - where T: de::DeserializeSeed + where T: de::DeserializeSeed<'de> { Err(de::Error::invalid_type(Unexpected::UnitVariant, &"newtype variant")) } fn visit_tuple(self, _len: usize, _visitor: V) -> Result - where V: de::Visitor + where V: de::Visitor<'de> { Err(de::Error::invalid_type(Unexpected::UnitVariant, &"tuple variant")) } @@ -1083,7 +1077,7 @@ mod private { _fields: &'static [&'static str], _visitor: V) -> Result - where V: de::Visitor + where V: de::Visitor<'de> { Err(de::Error::invalid_type(Unexpected::UnitVariant, &"struct variant")) } diff --git a/serde/src/macros.rs b/serde/src/macros.rs index 126c7ec1..b547f472 100644 --- a/serde/src/macros.rs +++ b/serde/src/macros.rs @@ -5,7 +5,7 @@ macro_rules! forward_to_deserialize_method { ($func:ident($($arg:ty),*)) => { #[inline] fn $func<__V>(self, $(_: $arg,)* visitor: __V) -> ::std::result::Result<__V::Value, Self::Error> - where __V: $crate::de::Visitor + where __V: $crate::de::Visitor<'de> { self.deserialize(visitor) } @@ -19,7 +19,7 @@ macro_rules! forward_to_deserialize_method { ($func:ident($($arg:ty),*)) => { #[inline] fn $func<__V>(self, $(_: $arg,)* visitor: __V) -> ::core::result::Result<__V::Value, Self::Error> - where __V: $crate::de::Visitor + where __V: $crate::de::Visitor<'de> { self.deserialize(visitor) } @@ -135,15 +135,15 @@ macro_rules! forward_to_deserialize_helper { /// # #[macro_use] extern crate serde; /// # use serde::de::{value, Deserializer, Visitor}; /// # pub struct MyDeserializer; -/// # impl Deserializer for MyDeserializer { +/// # impl<'de> Deserializer<'de> for MyDeserializer { /// # type Error = value::Error; /// # fn deserialize(self, _: V) -> Result -/// # where V: Visitor +/// # where V: Visitor<'de> /// # { unimplemented!() } /// # /// #[inline] /// fn deserialize_bool(self, visitor: V) -> Result -/// where V: Visitor +/// where V: Visitor<'de> /// { /// self.deserialize(visitor) /// } @@ -164,10 +164,10 @@ macro_rules! forward_to_deserialize_helper { /// # #[macro_use] extern crate serde; /// # use serde::de::{value, Deserializer, Visitor}; /// # pub struct MyDeserializer; -/// impl Deserializer for MyDeserializer { +/// impl<'de> Deserializer<'de> for MyDeserializer { /// # type Error = value::Error; /// fn deserialize(self, visitor: V) -> Result -/// where V: Visitor +/// where V: Visitor<'de> /// { /// /* ... */ /// # let _ = visitor; @@ -182,6 +182,10 @@ macro_rules! forward_to_deserialize_helper { /// } /// # fn main() {} /// ``` +/// +/// The macro assumes the convention that your `Deserializer` lifetime parameter +/// is called `'de`. It will not work if the `Deserializer` lifetime parameter +/// is called something different. #[macro_export] macro_rules! forward_to_deserialize { ($($func:ident)*) => { diff --git a/serde_derive/src/bound.rs b/serde_derive/src/bound.rs index 2fe797a1..fe45344f 100644 --- a/serde_derive/src/bound.rs +++ b/serde_derive/src/bound.rs @@ -6,28 +6,8 @@ use internals::ast::Item; use internals::attr; macro_rules! path { - ($first:ident $(:: $rest:ident)*) => { - syn::Path { - global: false, - segments: vec![ - stringify!($first).into(), - $( - stringify!($rest).into(), - )* - ], - } - }; - - (::$first:ident $(:: $rest:ident)*) => { - syn::Path { - global: true, - segments: vec![ - stringify!($first).into(), - $( - stringify!($rest).into(), - )* - ], - } + ($($path:tt)+) => { + syn::parse_path(stringify!($($path)+)).unwrap() }; } diff --git a/serde_derive/src/de.rs b/serde_derive/src/de.rs index 637efb51..6ab9f95e 100644 --- a/serde_derive/src/de.rs +++ b/serde_derive/src/de.rs @@ -1,5 +1,5 @@ use syn::{self, Ident}; -use quote::{self, Tokens}; +use quote::{self, Tokens, ToTokens}; use bound; use fragment::{Fragment, Expr, Stmts, Match}; @@ -17,7 +17,7 @@ pub fn expand_derive_deserialize(item: &syn::DeriveInput) -> Result Result for #ident #ty_generics #where_clause { fn deserialize<__D>(__deserializer: __D) -> _serde::export::Result - where __D: _serde::Deserializer + where __D: _serde::Deserializer<'de> { #body } @@ -60,7 +60,7 @@ fn build_generics(item: &Item) -> syn::Generics { bound::with_bound(item, &generics, needs_deserialize_bound, - &path!(_serde::Deserialize)); + &path!(_serde::Deserialize<'de>)); bound::with_bound(item, &generics, @@ -136,7 +136,7 @@ fn deserialize_unit_struct(ident: &syn::Ident, item_attrs: &attr::Item) -> Fragm quote_block! { struct __Visitor; - impl _serde::de::Visitor for __Visitor { + impl<'de> _serde::de::Visitor<'de> for __Visitor { type Value = #ident; fn expecting(&self, formatter: &mut _serde::export::fmt::Formatter) -> _serde::export::fmt::Result { @@ -152,7 +152,7 @@ fn deserialize_unit_struct(ident: &syn::Ident, item_attrs: &attr::Item) -> Fragm #[inline] fn visit_seq<__V>(self, _: __V) -> _serde::export::Result<#ident, __V::Error> - where __V: _serde::de::SeqVisitor + where __V: _serde::de::SeqVisitor<'de> { _serde::export::Ok(#ident) } @@ -169,7 +169,7 @@ fn deserialize_tuple(ident: &syn::Ident, item_attrs: &attr::Item, deserializer: Option) -> Fragment { - let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); + let (de_impl_generics, de_ty_generics, ty_generics, where_clause) = split_with_de_lifetime(generics); let is_enum = variant_ident.is_some(); let type_path = match variant_ident { @@ -192,7 +192,10 @@ fn deserialize_tuple(ident: &syn::Ident, let visit_seq = Stmts(deserialize_seq(ident, &type_path, generics, fields, false, item_attrs)); let visitor_expr = quote! { - __Visitor { marker: _serde::export::PhantomData::<#ident #ty_generics> } + __Visitor { + marker: _serde::export::PhantomData::<#ident #ty_generics>, + lifetime: _serde::export::PhantomData, + } }; let dispatch = if let Some(deserializer) = deserializer { quote!(_serde::Deserializer::deserialize_tuple(#deserializer, #nfields, #visitor_expr)) @@ -214,11 +217,12 @@ fn deserialize_tuple(ident: &syn::Ident, }; quote_block! { - struct __Visitor #impl_generics #where_clause { + struct __Visitor #de_impl_generics #where_clause { marker: _serde::export::PhantomData<#ident #ty_generics>, + lifetime: _serde::export::PhantomData<&'de ()>, } - impl #impl_generics _serde::de::Visitor for __Visitor #ty_generics #where_clause { + impl #de_impl_generics _serde::de::Visitor<'de> for __Visitor #de_ty_generics #where_clause { type Value = #ident #ty_generics; fn expecting(&self, formatter: &mut _serde::export::fmt::Formatter) -> _serde::export::fmt::Result { @@ -229,7 +233,7 @@ fn deserialize_tuple(ident: &syn::Ident, #[inline] fn visit_seq<__V>(self, #visitor_var: __V) -> _serde::export::Result - where __V: _serde::de::SeqVisitor + where __V: _serde::de::SeqVisitor<'de> { #visit_seq } @@ -332,7 +336,7 @@ fn deserialize_newtype_struct(ident: &syn::Ident, quote! { #[inline] fn visit_newtype_struct<__E>(self, __e: __E) -> _serde::export::Result - where __E: _serde::Deserializer + where __E: _serde::Deserializer<'de> { _serde::export::Ok(#type_path(#value)) } @@ -349,7 +353,7 @@ fn deserialize_struct(ident: &syn::Ident, let is_enum = variant_ident.is_some(); let is_untagged = deserializer.is_some(); - let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); + let (de_impl_generics, de_ty_generics, ty_generics, where_clause) = split_with_de_lifetime(generics); let type_path = match variant_ident { Some(variant_ident) => quote!(#ident::#variant_ident), @@ -369,7 +373,10 @@ fn deserialize_struct(ident: &syn::Ident, let visit_map = Stmts(visit_map); let visitor_expr = quote! { - __Visitor { marker: _serde::export::PhantomData::<#ident #ty_generics> } + __Visitor { + marker: _serde::export::PhantomData::<#ident #ty_generics>, + lifetime: _serde::export::PhantomData, + } }; let dispatch = if let Some(deserializer) = deserializer { quote! { @@ -400,7 +407,7 @@ fn deserialize_struct(ident: &syn::Ident, Some(quote! { #[inline] fn visit_seq<__V>(self, #visitor_var: __V) -> _serde::export::Result - where __V: _serde::de::SeqVisitor + where __V: _serde::de::SeqVisitor<'de> { #visit_seq } @@ -410,11 +417,12 @@ fn deserialize_struct(ident: &syn::Ident, quote_block! { #field_visitor - struct __Visitor #impl_generics #where_clause { + struct __Visitor #de_impl_generics #where_clause { marker: _serde::export::PhantomData<#ident #ty_generics>, + lifetime: _serde::export::PhantomData<&'de ()>, } - impl #generics _serde::de::Visitor for __Visitor #ty_generics #where_clause { + impl #de_impl_generics _serde::de::Visitor<'de> for __Visitor #de_ty_generics #where_clause { type Value = #ident #ty_generics; fn expecting(&self, formatter: &mut _serde::export::fmt::Formatter) -> _serde::export::fmt::Result { @@ -425,7 +433,7 @@ fn deserialize_struct(ident: &syn::Ident, #[inline] fn visit_map<__V>(self, mut __visitor: __V) -> _serde::export::Result - where __V: _serde::de::MapVisitor + where __V: _serde::de::MapVisitor<'de> { #visit_map } @@ -472,7 +480,7 @@ fn deserialize_externally_tagged_enum(ident: &syn::Ident, variants: &[Variant], item_attrs: &attr::Item) -> Fragment { - let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); + let (de_impl_generics, de_ty_generics, ty_generics, where_clause) = split_with_de_lifetime(generics); let type_name = item_attrs.name().deserialize_name(); @@ -533,11 +541,12 @@ fn deserialize_externally_tagged_enum(ident: &syn::Ident, quote_block! { #variant_visitor - struct __Visitor #impl_generics #where_clause { + struct __Visitor #de_impl_generics #where_clause { marker: _serde::export::PhantomData<#ident #ty_generics>, + lifetime: _serde::export::PhantomData<&'de ()>, } - impl #generics _serde::de::Visitor for __Visitor #ty_generics #where_clause { + impl #de_impl_generics _serde::de::Visitor<'de> for __Visitor #de_ty_generics #where_clause { type Value = #ident #ty_generics; fn expecting(&self, formatter: &mut _serde::export::fmt::Formatter) -> _serde::export::fmt::Result { @@ -545,7 +554,7 @@ fn deserialize_externally_tagged_enum(ident: &syn::Ident, } fn visit_enum<__V>(self, __visitor: __V) -> _serde::export::Result - where __V: _serde::de::EnumVisitor + where __V: _serde::de::EnumVisitor<'de> { #match_variant } @@ -556,6 +565,7 @@ fn deserialize_externally_tagged_enum(ident: &syn::Ident, _serde::Deserializer::deserialize_enum(__deserializer, #type_name, VARIANTS, __Visitor { marker: _serde::export::PhantomData::<#ident #ty_generics>, + lifetime: _serde::export::PhantomData, }) } } @@ -623,7 +633,7 @@ fn deserialize_adjacently_tagged_enum(ident: &syn::Ident, tag: &str, content: &str) -> Fragment { - let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); + let (de_impl_generics, de_ty_generics, ty_generics, where_clause) = split_with_de_lifetime(generics); let variant_names_idents: Vec<_> = variants.iter() .enumerate() @@ -724,16 +734,17 @@ fn deserialize_adjacently_tagged_enum(ident: &syn::Ident, #variants_stmt - struct __Seed #impl_generics #where_clause { + struct __Seed #de_impl_generics #where_clause { field: __Field, marker: _serde::export::PhantomData<#ident #ty_generics>, + lifetime: _serde::export::PhantomData<&'de ()>, } - impl #impl_generics _serde::de::DeserializeSeed for __Seed #ty_generics #where_clause { + impl #de_impl_generics _serde::de::DeserializeSeed<'de> for __Seed #de_ty_generics #where_clause { type Value = #ident #ty_generics; fn deserialize<__D>(self, __deserializer: __D) -> _serde::export::Result - where __D: _serde::Deserializer + where __D: _serde::Deserializer<'de> { match self.field { #(#variant_arms)* @@ -741,11 +752,12 @@ fn deserialize_adjacently_tagged_enum(ident: &syn::Ident, } } - struct __Visitor #impl_generics #where_clause { + struct __Visitor #de_impl_generics #where_clause { marker: _serde::export::PhantomData<#ident #ty_generics>, + lifetime: _serde::export::PhantomData<&'de ()>, } - impl #impl_generics _serde::de::Visitor for __Visitor #ty_generics #where_clause { + impl #de_impl_generics _serde::de::Visitor<'de> for __Visitor #de_ty_generics #where_clause { type Value = #ident #ty_generics; fn expecting(&self, formatter: &mut _serde::export::fmt::Formatter) -> _serde::export::fmt::Result { @@ -753,7 +765,7 @@ fn deserialize_adjacently_tagged_enum(ident: &syn::Ident, } fn visit_map<__V>(self, mut __visitor: __V) -> _serde::export::Result - where __V: _serde::de::MapVisitor + where __V: _serde::de::MapVisitor<'de> { // Visit the first key. match try!(_serde::de::MapVisitor::visit_key_seed(&mut __visitor, #tag_or_content)) { @@ -769,7 +781,12 @@ fn deserialize_adjacently_tagged_enum(ident: &syn::Ident, } // Second key is the content. _serde::export::Some(_serde::de::private::TagOrContentField::Content) => { - let __ret = try!(_serde::de::MapVisitor::visit_value_seed(&mut __visitor, __Seed { field: __field, marker: _serde::export::PhantomData })); + let __ret = try!(_serde::de::MapVisitor::visit_value_seed(&mut __visitor, + __Seed { + field: __field, + marker: _serde::export::PhantomData, + lifetime: _serde::export::PhantomData, + })); // Visit the third key, hopefully there isn't one. #visit_third_key } @@ -812,13 +829,18 @@ fn deserialize_adjacently_tagged_enum(ident: &syn::Ident, } fn visit_seq<__V>(self, mut __visitor: __V) -> _serde::export::Result - where __V: _serde::de::SeqVisitor + where __V: _serde::de::SeqVisitor<'de> { // Visit the first element - the tag. match try!(_serde::de::SeqVisitor::visit(&mut __visitor)) { _serde::export::Some(__field) => { // Visit the second element - the content. - match try!(_serde::de::SeqVisitor::visit_seed(&mut __visitor, __Seed { field: __field, marker: _serde::export::PhantomData })) { + match try!(_serde::de::SeqVisitor::visit_seed(&mut __visitor, + __Seed { + field: __field, + marker: _serde::export::PhantomData, + lifetime: _serde::export::PhantomData, + })) { _serde::export::Some(__ret) => _serde::export::Ok(__ret), // There is no second element. _serde::export::None => { @@ -836,7 +858,10 @@ fn deserialize_adjacently_tagged_enum(ident: &syn::Ident, const FIELDS: &'static [&'static str] = &[#tag, #content]; _serde::Deserializer::deserialize_struct(__deserializer, #type_name, FIELDS, - __Visitor { marker: _serde::export::PhantomData::<#ident #ty_generics> }) + __Visitor { + marker: _serde::export::PhantomData::<#ident #ty_generics>, + lifetime: _serde::export::PhantomData, + }) } } @@ -1111,14 +1136,14 @@ fn deserialize_field_visitor(fields: Vec<(String, Ident)>, #ignore_variant } - impl _serde::Deserialize for __Field { + impl<'de> _serde::Deserialize<'de> for __Field { #[inline] fn deserialize<__D>(__deserializer: __D) -> _serde::export::Result<__Field, __D::Error> - where __D: _serde::Deserializer + where __D: _serde::Deserializer<'de> { struct __FieldVisitor; - impl _serde::de::Visitor for __FieldVisitor { + impl<'de> _serde::de::Visitor<'de> for __FieldVisitor { type Value = __Field; fn expecting(&self, formatter: &mut _serde::export::fmt::Formatter) -> _serde::export::fmt::Result { @@ -1334,27 +1359,29 @@ fn wrap_deserialize_with(ident: &syn::Ident, field_ty: &syn::Ty, deserialize_with: &syn::Path) -> (Tokens, Tokens) { - let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); + let (de_impl_generics, de_ty_generics, ty_generics, where_clause) = split_with_de_lifetime(generics); let wrapper = quote! { - struct __DeserializeWith #impl_generics #where_clause { + struct __DeserializeWith #de_impl_generics #where_clause { value: #field_ty, phantom: _serde::export::PhantomData<#ident #ty_generics>, + lifetime: _serde::export::PhantomData<&'de ()>, } - impl #impl_generics _serde::Deserialize for __DeserializeWith #ty_generics #where_clause { + impl #de_impl_generics _serde::Deserialize<'de> for __DeserializeWith #de_ty_generics #where_clause { fn deserialize<__D>(__deserializer: __D) -> _serde::export::Result - where __D: _serde::Deserializer + where __D: _serde::Deserializer<'de> { _serde::export::Ok(__DeserializeWith { value: try!(#deserialize_with(__deserializer)), phantom: _serde::export::PhantomData, + lifetime: _serde::export::PhantomData, }) } } }; - let wrapper_ty = quote!(__DeserializeWith #ty_generics); + let wrapper_ty = quote!(__DeserializeWith #de_ty_generics); (wrapper, wrapper_ty) } @@ -1414,3 +1441,32 @@ fn check_no_str(cx: &internals::Ctxt, item: &Item) { } } } + +struct DeImplGenerics<'a>(&'a syn::Generics); + +impl<'a> ToTokens for DeImplGenerics<'a> { + fn to_tokens(&self, tokens: &mut Tokens) { + let mut generics = self.0.clone(); + generics.lifetimes.insert(0, syn::LifetimeDef::new("'de")); + let (impl_generics, _, _) = generics.split_for_impl(); + impl_generics.to_tokens(tokens); + } +} + +struct DeTyGenerics<'a>(&'a syn::Generics); + +impl<'a> ToTokens for DeTyGenerics<'a> { + fn to_tokens(&self, tokens: &mut Tokens) { + let mut generics = self.0.clone(); + generics.lifetimes.insert(0, syn::LifetimeDef::new("'de")); + let (_, ty_generics, _) = generics.split_for_impl(); + ty_generics.to_tokens(tokens); + } +} + +fn split_with_de_lifetime(generics: &syn::Generics) -> (DeImplGenerics, DeTyGenerics, syn::TyGenerics, &syn::WhereClause) { + let de_impl_generics = DeImplGenerics(generics); + let de_ty_generics = DeTyGenerics(generics); + let (_, ty_generics, where_clause) = generics.split_for_impl(); + (de_impl_generics, de_ty_generics, ty_generics, where_clause) +} diff --git a/serde_test/src/assert.rs b/serde_test/src/assert.rs index 9d281faa..fc33f282 100644 --- a/serde_test/src/assert.rs +++ b/serde_test/src/assert.rs @@ -8,8 +8,8 @@ use token::Token; use std::fmt::Debug; /// Runs both `assert_ser_tokens` and `assert_de_tokens`. -pub fn assert_tokens(value: &T, tokens: &[Token<'static>]) - where T: Serialize + Deserialize + PartialEq + Debug +pub fn assert_tokens<'de, T>(value: &T, tokens: &[Token<'static>]) + where T: Serialize + Deserialize<'de> + PartialEq + Debug { assert_ser_tokens(value, tokens); assert_de_tokens(value, tokens); @@ -35,8 +35,8 @@ pub fn assert_ser_tokens_error(value: &T, tokens: &[Token], error: Error) } /// Asserts that the given `tokens` deserialize into `value`. -pub fn assert_de_tokens(value: &T, tokens: &[Token<'static>]) - where T: Deserialize + PartialEq + Debug +pub fn assert_de_tokens<'de, T>(value: &T, tokens: &[Token<'static>]) + where T: Deserialize<'de> + PartialEq + Debug { let mut de = Deserializer::new(tokens.to_vec().into_iter()); let v: Result = Deserialize::deserialize(&mut de); @@ -45,8 +45,8 @@ pub fn assert_de_tokens(value: &T, tokens: &[Token<'static>]) } /// Asserts that the given `tokens` yield `error` when deserializing. -pub fn assert_de_tokens_error(tokens: &[Token<'static>], error: Error) - where T: Deserialize + PartialEq + Debug +pub fn assert_de_tokens_error<'de, T>(tokens: &[Token<'static>], error: Error) + where T: Deserialize<'de> + PartialEq + Debug { let mut de = Deserializer::new(tokens.to_vec().into_iter()); let v: Result = Deserialize::deserialize(&mut de); diff --git a/serde_test/src/de.rs b/serde_test/src/de.rs index e975186d..b2d6dd12 100644 --- a/serde_test/src/de.rs +++ b/serde_test/src/de.rs @@ -41,13 +41,13 @@ impl Deserializer } } - fn visit_seq(&mut self, + fn visit_seq<'de, V>(&mut self, len: Option, sep: Token<'static>, end: Token<'static>, visitor: V) -> Result - where V: Visitor + where V: Visitor<'de> { let value = try!(visitor.visit_seq(DeserializerSeqVisitor { de: self, @@ -59,13 +59,13 @@ impl Deserializer Ok(value) } - fn visit_map(&mut self, + fn visit_map<'de, V>(&mut self, len: Option, sep: Token<'static>, end: Token<'static>, visitor: V) -> Result - where V: Visitor + where V: Visitor<'de> { let value = try!(visitor.visit_map(DeserializerMapVisitor { de: self, @@ -78,7 +78,7 @@ impl Deserializer } } -impl<'a, I> de::Deserializer for &'a mut Deserializer +impl<'de, 'a, I> de::Deserializer<'de> for &'a mut Deserializer where I: Iterator> { type Error = Error; @@ -89,7 +89,7 @@ impl<'a, I> de::Deserializer for &'a mut Deserializer } fn deserialize(self, visitor: V) -> Result - where V: Visitor + where V: Visitor<'de> { match self.tokens.next() { Some(Token::Bool(v)) => visitor.visit_bool(v), @@ -148,7 +148,7 @@ impl<'a, I> de::Deserializer for &'a mut Deserializer /// Hook into `Option` deserializing so we can treat `Unit` as a /// `None`, or a regular value as `Some(value)`. fn deserialize_option(self, visitor: V) -> Result - where V: Visitor + where V: Visitor<'de> { match self.tokens.peek() { Some(&Token::Unit) | @@ -170,7 +170,7 @@ impl<'a, I> de::Deserializer for &'a mut Deserializer _variants: &'static [&'static str], visitor: V) -> Result - where V: Visitor + where V: Visitor<'de> { match self.tokens.peek() { Some(&Token::EnumStart(n)) if name == n => { @@ -193,7 +193,7 @@ impl<'a, I> de::Deserializer for &'a mut Deserializer } fn deserialize_unit_struct(self, name: &str, visitor: V) -> Result - where V: Visitor + where V: Visitor<'de> { match self.tokens.peek() { Some(&Token::UnitStruct(n)) => { @@ -210,7 +210,7 @@ impl<'a, I> de::Deserializer for &'a mut Deserializer } fn deserialize_newtype_struct(self, name: &str, visitor: V) -> Result - where V: Visitor + where V: Visitor<'de> { match self.tokens.peek() { Some(&Token::StructNewType(n)) => { @@ -227,7 +227,7 @@ impl<'a, I> de::Deserializer for &'a mut Deserializer } fn deserialize_seq_fixed_size(self, len: usize, visitor: V) -> Result - where V: Visitor + where V: Visitor<'de> { match self.tokens.peek() { Some(&Token::SeqArrayStart(_)) => { @@ -240,7 +240,7 @@ impl<'a, I> de::Deserializer for &'a mut Deserializer } fn deserialize_tuple(self, len: usize, visitor: V) -> Result - where V: Visitor + where V: Visitor<'de> { match self.tokens.peek() { Some(&Token::Unit) | @@ -277,7 +277,7 @@ impl<'a, I> de::Deserializer for &'a mut Deserializer len: usize, visitor: V) -> Result - where V: Visitor + where V: Visitor<'de> { match self.tokens.peek() { Some(&Token::Unit) => { @@ -325,7 +325,7 @@ impl<'a, I> de::Deserializer for &'a mut Deserializer fields: &'static [&'static str], visitor: V) -> Result - where V: Visitor + where V: Visitor<'de> { match self.tokens.peek() { Some(&Token::StructStart(n, _)) => { @@ -360,13 +360,13 @@ struct DeserializerSeqVisitor<'a, I: 'a> end: Token<'static>, } -impl<'a, I> SeqVisitor for DeserializerSeqVisitor<'a, I> +impl<'de, 'a, I> SeqVisitor<'de> for DeserializerSeqVisitor<'a, I> where I: Iterator> { type Error = Error; fn visit_seed(&mut self, seed: T) -> Result, Error> - where T: DeserializeSeed + where T: DeserializeSeed<'de> { if self.de.tokens.peek() == Some(&self.end) { return Ok(None); @@ -398,13 +398,13 @@ struct DeserializerMapVisitor<'a, I: 'a> end: Token<'static>, } -impl<'a, I> MapVisitor for DeserializerMapVisitor<'a, I> +impl<'de, 'a, I> MapVisitor<'de> for DeserializerMapVisitor<'a, I> where I: Iterator> { type Error = Error; fn visit_key_seed(&mut self, seed: K) -> Result, Error> - where K: DeserializeSeed + where K: DeserializeSeed<'de> { if self.de.tokens.peek() == Some(&self.end) { return Ok(None); @@ -420,7 +420,7 @@ impl<'a, I> MapVisitor for DeserializerMapVisitor<'a, I> } fn visit_value_seed(&mut self, seed: V) -> Result - where V: DeserializeSeed + where V: DeserializeSeed<'de> { seed.deserialize(&mut *self.de) } @@ -439,14 +439,14 @@ struct DeserializerEnumVisitor<'a, I: 'a> de: &'a mut Deserializer, } -impl<'a, I> EnumVisitor for DeserializerEnumVisitor<'a, I> +impl<'de, 'a, I> EnumVisitor<'de> for DeserializerEnumVisitor<'a, I> where I: Iterator> { type Error = Error; type Variant = Self; fn visit_variant_seed(self, seed: V) -> Result<(V::Value, Self), Error> - where V: DeserializeSeed + where V: DeserializeSeed<'de> { match self.de.tokens.peek() { Some(&Token::EnumUnit(_, v)) | @@ -466,7 +466,7 @@ impl<'a, I> EnumVisitor for DeserializerEnumVisitor<'a, I> } } -impl<'a, I> VariantVisitor for DeserializerEnumVisitor<'a, I> +impl<'de, 'a, I> VariantVisitor<'de> for DeserializerEnumVisitor<'a, I> where I: Iterator> { type Error = Error; @@ -483,7 +483,7 @@ impl<'a, I> VariantVisitor for DeserializerEnumVisitor<'a, I> } fn visit_newtype_seed(self, seed: T) -> Result - where T: DeserializeSeed + where T: DeserializeSeed<'de> { match self.de.tokens.peek() { Some(&Token::EnumNewType(_, _)) => { @@ -496,7 +496,7 @@ impl<'a, I> VariantVisitor for DeserializerEnumVisitor<'a, I> } fn visit_tuple(self, len: usize, visitor: V) -> Result - where V: Visitor + where V: Visitor<'de> { match self.de.tokens.peek() { Some(&Token::EnumSeqStart(_, _, enum_len)) => { @@ -523,7 +523,7 @@ impl<'a, I> VariantVisitor for DeserializerEnumVisitor<'a, I> } fn visit_struct(self, fields: &'static [&'static str], visitor: V) -> Result - where V: Visitor + where V: Visitor<'de> { match self.de.tokens.peek() { Some(&Token::EnumMapStart(_, _, enum_len)) => { @@ -573,13 +573,13 @@ impl<'a, I: 'a> EnumMapVisitor<'a, I> } } -impl<'a, I: 'a> MapVisitor for EnumMapVisitor<'a, I> +impl<'de, 'a, I: 'a> MapVisitor<'de> for EnumMapVisitor<'a, I> where I: Iterator> { type Error = Error; fn visit_key_seed(&mut self, seed: K) -> Result, Error> - where K: DeserializeSeed + where K: DeserializeSeed<'de> { match self.variant.take() { Some(variant) => seed.deserialize(variant.into_deserializer()).map(Some), @@ -588,7 +588,7 @@ impl<'a, I: 'a> MapVisitor for EnumMapVisitor<'a, I> } fn visit_value_seed(&mut self, seed: V) -> Result - where V: DeserializeSeed + where V: DeserializeSeed<'de> { match self.de.tokens.peek() { Some(&Token::EnumSeqSep) => { diff --git a/test_suite/tests/test_annotations.rs b/test_suite/tests/test_annotations.rs index af49203b..3bcedd77 100644 --- a/test_suite/tests/test_annotations.rs +++ b/test_suite/tests/test_annotations.rs @@ -28,8 +28,8 @@ trait SerializeWith: Sized { } trait DeserializeWith: Sized { - fn deserialize_with(de: D) -> Result - where D: Deserializer; + fn deserialize_with<'de, D>(de: D) -> Result + where D: Deserializer<'de>; } impl MyDefault for i32 { @@ -53,8 +53,8 @@ impl SerializeWith for i32 { } impl DeserializeWith for i32 { - fn deserialize_with(de: D) -> Result - where D: Deserializer + fn deserialize_with<'de, D>(de: D) -> Result + where D: Deserializer<'de> { if try!(Deserialize::deserialize(de)) { Ok(123) @@ -242,8 +242,8 @@ impl Default for NotDeserializeStruct { } impl DeserializeWith for NotDeserializeStruct { - fn deserialize_with(_: D) -> Result - where D: Deserializer + fn deserialize_with<'de, D>(_: D) -> Result + where D: Deserializer<'de> { panic!() } diff --git a/test_suite/tests/test_gen.rs b/test_suite/tests/test_gen.rs index 8cdb254e..68e63505 100644 --- a/test_suite/tests/test_gen.rs +++ b/test_suite/tests/test_gen.rs @@ -9,7 +9,7 @@ extern crate serde_derive; extern crate serde; use self::serde::ser::{Serialize, Serializer}; -use self::serde::de::{Deserialize, Deserializer}; +use self::serde::de::{DeserializeOwned, Deserializer}; use std::borrow::Cow; use std::marker::PhantomData; @@ -64,15 +64,6 @@ fn test_gen() { } assert::>(); - #[derive(Serialize, Deserialize)] - struct Bounds { - t: T, - option: Option, - boxed: Box, - option_boxed: Option>, - } - assert::>(); - #[derive(Serialize, Deserialize)] struct NoBounds { t: T, @@ -206,7 +197,7 @@ fn test_gen() { assert::(); #[derive(Serialize, Deserialize)] - #[serde(bound(deserialize = "T::Owned: Deserialize"))] + #[serde(bound(deserialize = "T::Owned: DeserializeOwned"))] struct CowT<'a, T: ?Sized + 'a + ToOwned>(Cow<'a, T>); assert::>(); @@ -308,7 +299,7 @@ fn test_gen() { ////////////////////////////////////////////////////////////////////////// -fn assert() {} +fn assert() {} fn assert_ser() {} trait SerializeWith { @@ -316,7 +307,7 @@ trait SerializeWith { } trait DeserializeWith: Sized { - fn deserialize_with(_: D) -> StdResult; + fn deserialize_with<'de, D: Deserializer<'de>>(_: D) -> StdResult; } // Implements neither Serialize nor Deserialize @@ -326,7 +317,7 @@ pub fn ser_x(_: &X, _: S) -> StdResult { unimplemented!() } -pub fn de_x(_: D) -> StdResult { +pub fn de_x<'de, D: Deserializer<'de>>(_: D) -> StdResult { unimplemented!() } @@ -341,7 +332,7 @@ impl SerializeWith for X { } impl DeserializeWith for X { - fn deserialize_with(_: D) -> StdResult { + fn deserialize_with<'de, D: Deserializer<'de>>(_: D) -> StdResult { unimplemented!() } }