Support serde 1.0.0

This commit is contained in:
Alex Crichton 2017-04-20 10:16:00 -07:00
parent 8683f1d22b
commit dc5ff1ef90
6 changed files with 156 additions and 170 deletions

View file

@ -19,8 +19,8 @@ categories = ["config", "encoding", "parser-implementations"]
travis-ci = { repository = "alexcrichton/toml-rs" } travis-ci = { repository = "alexcrichton/toml-rs" }
[dependencies] [dependencies]
serde = "0.9.6" serde = "1.0"
[dev-dependencies] [dev-dependencies]
serde_derive = "0.9" serde_derive = "1.0"
serde_json = "0.9" serde_json = "1.0"

View file

@ -315,13 +315,13 @@ impl ser::Serialize for Datetime {
} }
} }
impl de::Deserialize for Datetime { impl<'de> de::Deserialize<'de> for Datetime {
fn deserialize<D>(deserializer: D) -> Result<Datetime, D::Error> fn deserialize<D>(deserializer: D) -> Result<Datetime, D::Error>
where D: de::Deserializer where D: de::Deserializer<'de>
{ {
struct DatetimeVisitor; struct DatetimeVisitor;
impl de::Visitor for DatetimeVisitor { impl<'de> de::Visitor<'de> for DatetimeVisitor {
type Value = Datetime; type Value = Datetime;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
@ -329,13 +329,13 @@ impl de::Deserialize for Datetime {
} }
fn visit_map<V>(self, mut visitor: V) -> Result<Datetime, V::Error> fn visit_map<V>(self, mut visitor: V) -> Result<Datetime, V::Error>
where V: de::MapVisitor where V: de::MapAccess<'de>
{ {
let value = visitor.visit_key::<DatetimeKey>()?; let value = visitor.next_key::<DatetimeKey>()?;
if value.is_none() { if value.is_none() {
return Err(de::Error::custom("datetime key not found")) return Err(de::Error::custom("datetime key not found"))
} }
let v: DatetimeFromString = visitor.visit_value()?; let v: DatetimeFromString = visitor.next_value()?;
Ok(v.value) Ok(v.value)
} }
@ -350,13 +350,13 @@ impl de::Deserialize for Datetime {
struct DatetimeKey; struct DatetimeKey;
impl de::Deserialize for DatetimeKey { impl<'de> de::Deserialize<'de> for DatetimeKey {
fn deserialize<D>(deserializer: D) -> Result<DatetimeKey, D::Error> fn deserialize<D>(deserializer: D) -> Result<DatetimeKey, D::Error>
where D: de::Deserializer where D: de::Deserializer<'de>
{ {
struct FieldVisitor; struct FieldVisitor;
impl de::Visitor for FieldVisitor { impl<'de> de::Visitor<'de> for FieldVisitor {
type Value = (); type Value = ();
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
@ -374,7 +374,7 @@ impl de::Deserialize for DatetimeKey {
} }
} }
deserializer.deserialize_struct_field(FieldVisitor)?; deserializer.deserialize_identifier(FieldVisitor)?;
Ok(DatetimeKey) Ok(DatetimeKey)
} }
} }
@ -383,13 +383,13 @@ pub struct DatetimeFromString {
pub value: Datetime, pub value: Datetime,
} }
impl de::Deserialize for DatetimeFromString { impl<'de> de::Deserialize<'de> for DatetimeFromString {
fn deserialize<D>(deserializer: D) -> Result<DatetimeFromString, D::Error> fn deserialize<D>(deserializer: D) -> Result<DatetimeFromString, D::Error>
where D: de::Deserializer where D: de::Deserializer<'de>
{ {
struct Visitor; struct Visitor;
impl de::Visitor for Visitor { impl<'de> de::Visitor<'de> for Visitor {
type Value = DatetimeFromString; type Value = DatetimeFromString;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {

136
src/de.rs
View file

@ -19,8 +19,8 @@ use datetime::{SERDE_STRUCT_FIELD_NAME, SERDE_STRUCT_NAME};
/// ///
/// This function will attempt to interpret `bytes` as UTF-8 data and then /// This function will attempt to interpret `bytes` as UTF-8 data and then
/// deserialize `T` from the TOML document provided. /// deserialize `T` from the TOML document provided.
pub fn from_slice<T>(bytes: &[u8]) -> Result<T, Error> pub fn from_slice<'de, T>(bytes: &'de [u8]) -> Result<T, Error>
where T: de::Deserialize, where T: de::Deserialize<'de>,
{ {
match str::from_utf8(bytes) { match str::from_utf8(bytes) {
Ok(s) => from_str(s), Ok(s) => from_str(s),
@ -32,8 +32,8 @@ pub fn from_slice<T>(bytes: &[u8]) -> Result<T, Error>
/// ///
/// This function will attempt to interpret `s` as a TOML document and /// This function will attempt to interpret `s` as a TOML document and
/// deserialize `T` from the document. /// deserialize `T` from the document.
pub fn from_str<T>(s: &str) -> Result<T, Error> pub fn from_str<'de, T>(s: &'de str) -> Result<T, Error>
where T: de::Deserialize, where T: de::Deserialize<'de>,
{ {
let mut d = Deserializer::new(s); let mut d = Deserializer::new(s);
let ret = T::deserialize(&mut d)?; let ret = T::deserialize(&mut d)?;
@ -132,11 +132,11 @@ pub struct Deserializer<'a> {
tokens: Tokenizer<'a>, tokens: Tokenizer<'a>,
} }
impl<'a, 'b> de::Deserializer for &'b mut Deserializer<'a> { impl<'de, 'b> de::Deserializer<'de> for &'b mut Deserializer<'de> {
type Error = Error; type Error = Error;
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Error> fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error>
where V: de::Visitor, where V: de::Visitor<'de>,
{ {
let mut tables = Vec::new(); let mut tables = Vec::new();
let mut cur_table = Table { let mut cur_table = Table {
@ -192,10 +192,10 @@ impl<'a, 'b> de::Deserializer for &'b mut Deserializer<'a> {
}) })
} }
forward_to_deserialize! { forward_to_deserialize_any! {
bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq
seq_fixed_size bytes byte_buf map struct unit enum newtype_struct bytes byte_buf map struct unit enum newtype_struct
struct_field ignored_any unit_struct tuple_struct tuple option ignored_any unit_struct tuple_struct tuple option identifier
} }
} }
@ -207,23 +207,23 @@ struct Table<'a> {
} }
#[doc(hidden)] #[doc(hidden)]
pub struct MapVisitor<'a: 'b, 'b> { pub struct MapVisitor<'de: 'b, 'b> {
values: vec::IntoIter<(Cow<'a, str>, Value<'a>)>, values: vec::IntoIter<(Cow<'de, str>, Value<'de>)>,
next_value: Option<(Cow<'a, str>, Value<'a>)>, next_value: Option<(Cow<'de, str>, Value<'de>)>,
depth: usize, depth: usize,
cur: usize, cur: usize,
cur_parent: usize, cur_parent: usize,
max: usize, max: usize,
tables: &'b mut [Table<'a>], tables: &'b mut [Table<'de>],
array: bool, array: bool,
de: &'b mut Deserializer<'a>, de: &'b mut Deserializer<'de>,
} }
impl<'a, 'b> de::MapVisitor for MapVisitor<'a, 'b> { impl<'de, 'b> de::MapAccess<'de> for MapVisitor<'de, 'b> {
type Error = Error; type Error = Error;
fn visit_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Error> fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Error>
where K: de::DeserializeSeed, where K: de::DeserializeSeed<'de>,
{ {
if self.cur_parent == self.max || self.cur == self.max { if self.cur_parent == self.max || self.cur == self.max {
return Ok(None) return Ok(None)
@ -268,11 +268,11 @@ impl<'a, 'b> de::MapVisitor for MapVisitor<'a, 'b> {
let table = &mut self.tables[pos]; let table = &mut self.tables[pos];
// If we're not yet at the appropriate depth for this table then we // If we're not yet at the appropriate depth for this table then we
// just visit the next portion of its header and then continue // just next the next portion of its header and then continue
// decoding. // decoding.
if self.depth != table.header.len() { if self.depth != table.header.len() {
let key = &table.header[self.depth]; let key = &table.header[self.depth];
let key = seed.deserialize(StrDeserializer::new(key[..].into()))?; let key = seed.deserialize(StrDeserializer::new(key.clone()))?;
return Ok(Some(key)) return Ok(Some(key))
} }
@ -289,8 +289,8 @@ impl<'a, 'b> de::MapVisitor for MapVisitor<'a, 'b> {
} }
} }
fn visit_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Error> fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Error>
where V: de::DeserializeSeed, where V: de::DeserializeSeed<'de>,
{ {
if let Some((k, v)) = self.next_value.take() { if let Some((k, v)) = self.next_value.take() {
match seed.deserialize(ValueDeserializer::new(v)) { match seed.deserialize(ValueDeserializer::new(v)) {
@ -323,11 +323,11 @@ impl<'a, 'b> de::MapVisitor for MapVisitor<'a, 'b> {
} }
} }
impl<'a, 'b> de::SeqVisitor for MapVisitor<'a, 'b> { impl<'de, 'b> de::SeqAccess<'de> for MapVisitor<'de, 'b> {
type Error = Error; type Error = Error;
fn visit_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Error> fn next_element_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Error>
where K: de::DeserializeSeed, where K: de::DeserializeSeed<'de>,
{ {
assert!(self.next_value.is_none()); assert!(self.next_value.is_none());
assert!(self.values.next().is_none()); assert!(self.values.next().is_none());
@ -361,11 +361,11 @@ impl<'a, 'b> de::SeqVisitor for MapVisitor<'a, 'b> {
} }
} }
impl<'a, 'b> de::Deserializer for MapVisitor<'a, 'b> { impl<'de, 'b> de::Deserializer<'de> for MapVisitor<'de, 'b> {
type Error = Error; type Error = Error;
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Error> fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error>
where V: de::Visitor, where V: de::Visitor<'de>,
{ {
if self.array { if self.array {
visitor.visit_seq(self) visitor.visit_seq(self)
@ -377,15 +377,15 @@ impl<'a, 'b> de::Deserializer for MapVisitor<'a, 'b> {
// `None` is interpreted as a missing field so be sure to implement `Some` // `None` is interpreted as a missing field so be sure to implement `Some`
// as a present field. // as a present field.
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Error> fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Error>
where V: de::Visitor where V: de::Visitor<'de>,
{ {
visitor.visit_some(self) visitor.visit_some(self)
} }
forward_to_deserialize! { forward_to_deserialize_any! {
bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq
seq_fixed_size bytes byte_buf map struct unit newtype_struct bytes byte_buf map struct unit newtype_struct identifier
struct_field ignored_any unit_struct tuple_struct tuple enum ignored_any unit_struct tuple_struct tuple enum
} }
} }
@ -401,22 +401,22 @@ impl<'a> StrDeserializer<'a> {
} }
} }
impl<'a> de::Deserializer for StrDeserializer<'a> { impl<'de> de::Deserializer<'de> for StrDeserializer<'de> {
type Error = Error; type Error = Error;
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Error> fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error>
where V: de::Visitor, where V: de::Visitor<'de>,
{ {
match self.key { match self.key {
Cow::Borrowed(s) => visitor.visit_str(s), Cow::Borrowed(s) => visitor.visit_borrowed_str(s),
Cow::Owned(s) => visitor.visit_string(s), Cow::Owned(s) => visitor.visit_string(s),
} }
} }
forward_to_deserialize! { forward_to_deserialize_any! {
bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq
seq_fixed_size bytes byte_buf map struct option unit newtype_struct bytes byte_buf map struct option unit newtype_struct
struct_field ignored_any unit_struct tuple_struct tuple enum ignored_any unit_struct tuple_struct tuple enum identifier
} }
} }
@ -432,17 +432,17 @@ impl<'a> ValueDeserializer<'a> {
} }
} }
impl<'a> de::Deserializer for ValueDeserializer<'a> { impl<'de> de::Deserializer<'de> for ValueDeserializer<'de> {
type Error = Error; type Error = Error;
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Error> fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error>
where V: de::Visitor, where V: de::Visitor<'de>,
{ {
match self.value { match self.value {
Value::Integer(i) => visitor.visit_i64(i), Value::Integer(i) => visitor.visit_i64(i),
Value::Boolean(b) => visitor.visit_bool(b), Value::Boolean(b) => visitor.visit_bool(b),
Value::Float(f) => visitor.visit_f64(f), Value::Float(f) => visitor.visit_f64(f),
Value::String(Cow::Borrowed(s)) => visitor.visit_str(s), Value::String(Cow::Borrowed(s)) => visitor.visit_borrowed_str(s),
Value::String(Cow::Owned(s)) => visitor.visit_string(s), Value::String(Cow::Owned(s)) => visitor.visit_string(s),
Value::Datetime(s) => visitor.visit_map(DatetimeDeserializer { Value::Datetime(s) => visitor.visit_map(DatetimeDeserializer {
date: s, date: s,
@ -467,7 +467,7 @@ impl<'a> de::Deserializer for ValueDeserializer<'a> {
name: &'static str, name: &'static str,
fields: &'static [&'static str], fields: &'static [&'static str],
visitor: V) -> Result<V::Value, Error> visitor: V) -> Result<V::Value, Error>
where V: de::Visitor, where V: de::Visitor<'de>,
{ {
if name == SERDE_STRUCT_NAME && fields == &[SERDE_STRUCT_FIELD_NAME] { if name == SERDE_STRUCT_NAME && fields == &[SERDE_STRUCT_FIELD_NAME] {
if let Value::Datetime(s) = self.value { if let Value::Datetime(s) = self.value {
@ -478,26 +478,26 @@ impl<'a> de::Deserializer for ValueDeserializer<'a> {
} }
} }
self.deserialize(visitor) self.deserialize_any(visitor)
} }
// `None` is interpreted as a missing field so be sure to implement `Some` // `None` is interpreted as a missing field so be sure to implement `Some`
// as a present field. // as a present field.
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Error> fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Error>
where V: de::Visitor where V: de::Visitor<'de>,
{ {
visitor.visit_some(self) visitor.visit_some(self)
} }
forward_to_deserialize! { forward_to_deserialize_any! {
bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq
seq_fixed_size bytes byte_buf map unit newtype_struct bytes byte_buf map unit newtype_struct identifier
struct_field ignored_any unit_struct tuple_struct tuple enum ignored_any unit_struct tuple_struct tuple enum
} }
} }
impl<'a> de::value::ValueDeserializer<Error> for Value<'a> { impl<'de> de::IntoDeserializer<'de, Error> for Value<'de> {
type Deserializer = ValueDeserializer<'a>; type Deserializer = ValueDeserializer<'de>;
fn into_deserializer(self) -> Self::Deserializer { fn into_deserializer(self) -> Self::Deserializer {
ValueDeserializer::new(self) ValueDeserializer::new(self)
@ -509,11 +509,11 @@ struct DatetimeDeserializer<'a> {
date: &'a str, date: &'a str,
} }
impl<'a> de::MapVisitor for DatetimeDeserializer<'a> { impl<'de> de::MapAccess<'de> for DatetimeDeserializer<'de> {
type Error = Error; type Error = Error;
fn visit_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Error> fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Error>
where K: de::DeserializeSeed, where K: de::DeserializeSeed<'de>,
{ {
if self.visited { if self.visited {
return Ok(None) return Ok(None)
@ -522,8 +522,8 @@ impl<'a> de::MapVisitor for DatetimeDeserializer<'a> {
seed.deserialize(DatetimeFieldDeserializer).map(Some) seed.deserialize(DatetimeFieldDeserializer).map(Some)
} }
fn visit_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Error> fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Error>
where V: de::DeserializeSeed, where V: de::DeserializeSeed<'de>,
{ {
seed.deserialize(StrDeserializer::new(self.date.into())) seed.deserialize(StrDeserializer::new(self.date.into()))
} }
@ -531,19 +531,19 @@ impl<'a> de::MapVisitor for DatetimeDeserializer<'a> {
struct DatetimeFieldDeserializer; struct DatetimeFieldDeserializer;
impl de::Deserializer for DatetimeFieldDeserializer { impl<'de> de::Deserializer<'de> for DatetimeFieldDeserializer {
type Error = Error; type Error = Error;
fn deserialize<V>(self, visitor: V) -> Result<V::Value, Error> fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error>
where V: de::Visitor, where V: de::Visitor<'de>,
{ {
visitor.visit_str(SERDE_STRUCT_FIELD_NAME) visitor.visit_borrowed_str(SERDE_STRUCT_FIELD_NAME)
} }
forward_to_deserialize! { forward_to_deserialize_any! {
bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq
seq_fixed_size bytes byte_buf map struct option unit newtype_struct bytes byte_buf map struct option unit newtype_struct
struct_field ignored_any unit_struct tuple_struct tuple enum ignored_any unit_struct tuple_struct tuple enum identifier
} }
} }
@ -552,11 +552,11 @@ struct InlineTableDeserializer<'a> {
next_value: Option<Value<'a>>, next_value: Option<Value<'a>>,
} }
impl<'a> de::MapVisitor for InlineTableDeserializer<'a> { impl<'de> de::MapAccess<'de> for InlineTableDeserializer<'de> {
type Error = Error; type Error = Error;
fn visit_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Error> fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Error>
where K: de::DeserializeSeed, where K: de::DeserializeSeed<'de>,
{ {
let (key, value) = match self.values.next() { let (key, value) = match self.values.next() {
Some(pair) => pair, Some(pair) => pair,
@ -566,8 +566,8 @@ impl<'a> de::MapVisitor for InlineTableDeserializer<'a> {
seed.deserialize(StrDeserializer::new(key)).map(Some) seed.deserialize(StrDeserializer::new(key)).map(Some)
} }
fn visit_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Error> fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Error>
where V: de::DeserializeSeed, where V: de::DeserializeSeed<'de>,
{ {
let value = self.next_value.take().expect("Unable to read table values"); let value = self.next_value.take().expect("Unable to read table values");
seed.deserialize(ValueDeserializer::new(value)) seed.deserialize(ValueDeserializer::new(value))

View file

@ -420,7 +420,7 @@ impl<'a, 'b> ser::Serializer for &'b mut Serializer<'a> {
fn serialize_unit_variant(self, fn serialize_unit_variant(self,
_name: &'static str, _name: &'static str,
_variant_index: usize, _variant_index: u32,
_variant: &'static str) _variant: &'static str)
-> Result<(), Self::Error> { -> Result<(), Self::Error> {
Err(Error::UnsupportedType) Err(Error::UnsupportedType)
@ -435,7 +435,7 @@ impl<'a, 'b> ser::Serializer for &'b mut Serializer<'a> {
fn serialize_newtype_variant<T: ?Sized>(self, fn serialize_newtype_variant<T: ?Sized>(self,
_name: &'static str, _name: &'static str,
_variant_index: usize, _variant_index: u32,
_variant: &'static str, _variant: &'static str,
_value: &T) _value: &T)
-> Result<(), Self::Error> -> Result<(), Self::Error>
@ -454,11 +454,6 @@ impl<'a, 'b> ser::Serializer for &'b mut Serializer<'a> {
}) })
} }
fn serialize_seq_fixed_size(self, size: usize)
-> Result<Self::SerializeSeq, Self::Error> {
self.serialize_seq(Some(size))
}
fn serialize_tuple(self, _len: usize) fn serialize_tuple(self, _len: usize)
-> Result<Self::SerializeTuple, Self::Error> { -> Result<Self::SerializeTuple, Self::Error> {
Err(Error::UnsupportedType) Err(Error::UnsupportedType)
@ -471,7 +466,7 @@ impl<'a, 'b> ser::Serializer for &'b mut Serializer<'a> {
fn serialize_tuple_variant(self, fn serialize_tuple_variant(self,
_name: &'static str, _name: &'static str,
_variant_index: usize, _variant_index: u32,
_variant: &'static str, _variant: &'static str,
_len: usize) _len: usize)
-> Result<Self::SerializeTupleVariant, Self::Error> { -> Result<Self::SerializeTupleVariant, Self::Error> {
@ -507,7 +502,7 @@ impl<'a, 'b> ser::Serializer for &'b mut Serializer<'a> {
fn serialize_struct_variant(self, fn serialize_struct_variant(self,
_name: &'static str, _name: &'static str,
_variant_index: usize, _variant_index: u32,
_variant: &'static str, _variant: &'static str,
_len: usize) _len: usize)
-> Result<Self::SerializeStructVariant, Self::Error> { -> Result<Self::SerializeStructVariant, Self::Error> {
@ -754,7 +749,7 @@ impl<'a, 'b> ser::Serializer for DateStrEmitter<'a, 'b> {
fn serialize_unit_variant(self, fn serialize_unit_variant(self,
_name: &'static str, _name: &'static str,
_variant_index: usize, _variant_index: u32,
_variant: &'static str) _variant: &'static str)
-> Result<(), Self::Error> { -> Result<(), Self::Error> {
Err(Error::DateInvalid) Err(Error::DateInvalid)
@ -769,7 +764,7 @@ impl<'a, 'b> ser::Serializer for DateStrEmitter<'a, 'b> {
fn serialize_newtype_variant<T: ?Sized>(self, fn serialize_newtype_variant<T: ?Sized>(self,
_name: &'static str, _name: &'static str,
_variant_index: usize, _variant_index: u32,
_variant: &'static str, _variant: &'static str,
_value: &T) _value: &T)
-> Result<(), Self::Error> -> Result<(), Self::Error>
@ -783,11 +778,6 @@ impl<'a, 'b> ser::Serializer for DateStrEmitter<'a, 'b> {
Err(Error::DateInvalid) Err(Error::DateInvalid)
} }
fn serialize_seq_fixed_size(self, _size: usize)
-> Result<Self::SerializeSeq, Self::Error> {
Err(Error::DateInvalid)
}
fn serialize_tuple(self, _len: usize) fn serialize_tuple(self, _len: usize)
-> Result<Self::SerializeTuple, Self::Error> { -> Result<Self::SerializeTuple, Self::Error> {
Err(Error::DateInvalid) Err(Error::DateInvalid)
@ -800,7 +790,7 @@ impl<'a, 'b> ser::Serializer for DateStrEmitter<'a, 'b> {
fn serialize_tuple_variant(self, fn serialize_tuple_variant(self,
_name: &'static str, _name: &'static str,
_variant_index: usize, _variant_index: u32,
_variant: &'static str, _variant: &'static str,
_len: usize) _len: usize)
-> Result<Self::SerializeTupleVariant, Self::Error> { -> Result<Self::SerializeTupleVariant, Self::Error> {
@ -819,7 +809,7 @@ impl<'a, 'b> ser::Serializer for DateStrEmitter<'a, 'b> {
fn serialize_struct_variant(self, fn serialize_struct_variant(self,
_name: &'static str, _name: &'static str,
_variant_index: usize, _variant_index: u32,
_variant: &'static str, _variant: &'static str,
_len: usize) _len: usize)
-> Result<Self::SerializeStructVariant, Self::Error> { -> Result<Self::SerializeStructVariant, Self::Error> {
@ -918,7 +908,7 @@ impl ser::Serializer for StringExtractor {
fn serialize_unit_variant(self, fn serialize_unit_variant(self,
_name: &'static str, _name: &'static str,
_variant_index: usize, _variant_index: u32,
_variant: &'static str) _variant: &'static str)
-> Result<String, Self::Error> { -> Result<String, Self::Error> {
Err(Error::KeyNotString) Err(Error::KeyNotString)
@ -933,7 +923,7 @@ impl ser::Serializer for StringExtractor {
fn serialize_newtype_variant<T: ?Sized>(self, fn serialize_newtype_variant<T: ?Sized>(self,
_name: &'static str, _name: &'static str,
_variant_index: usize, _variant_index: u32,
_variant: &'static str, _variant: &'static str,
_value: &T) _value: &T)
-> Result<String, Self::Error> -> Result<String, Self::Error>
@ -947,11 +937,6 @@ impl ser::Serializer for StringExtractor {
Err(Error::KeyNotString) Err(Error::KeyNotString)
} }
fn serialize_seq_fixed_size(self, _size: usize)
-> Result<Self::SerializeSeq, Self::Error> {
Err(Error::KeyNotString)
}
fn serialize_tuple(self, _len: usize) fn serialize_tuple(self, _len: usize)
-> Result<Self::SerializeTuple, Self::Error> { -> Result<Self::SerializeTuple, Self::Error> {
Err(Error::KeyNotString) Err(Error::KeyNotString)
@ -964,7 +949,7 @@ impl ser::Serializer for StringExtractor {
fn serialize_tuple_variant(self, fn serialize_tuple_variant(self,
_name: &'static str, _name: &'static str,
_variant_index: usize, _variant_index: u32,
_variant: &'static str, _variant: &'static str,
_len: usize) _len: usize)
-> Result<Self::SerializeTupleVariant, Self::Error> { -> Result<Self::SerializeTupleVariant, Self::Error> {
@ -983,7 +968,7 @@ impl ser::Serializer for StringExtractor {
fn serialize_struct_variant(self, fn serialize_struct_variant(self,
_name: &'static str, _name: &'static str,
_variant_index: usize, _variant_index: u32,
_variant: &'static str, _variant: &'static str,
_len: usize) _len: usize)
-> Result<Self::SerializeStructVariant, Self::Error> { -> Result<Self::SerializeStructVariant, Self::Error> {
@ -1175,7 +1160,7 @@ impl<E: ser::Error> ser::Serializer for Categorize<E> {
Err(ser::Error::custom("unsupported")) Err(ser::Error::custom("unsupported"))
} }
fn serialize_unit_variant(self, _: &'static str, _: usize, _: &'static str) -> Result<Self::Ok, Self::Error> { fn serialize_unit_variant(self, _: &'static str, _: u32, _: &'static str) -> Result<Self::Ok, Self::Error> {
Err(ser::Error::custom("unsupported")) Err(ser::Error::custom("unsupported"))
} }
@ -1183,7 +1168,7 @@ impl<E: ser::Error> ser::Serializer for Categorize<E> {
v.serialize(self) v.serialize(self)
} }
fn serialize_newtype_variant<T: ?Sized + ser::Serialize>(self, _: &'static str, _: usize, _: &'static str, _: &T) -> Result<Self::Ok, Self::Error> { fn serialize_newtype_variant<T: ?Sized + ser::Serialize>(self, _: &'static str, _: u32, _: &'static str, _: &T) -> Result<Self::Ok, Self::Error> {
Err(ser::Error::custom("unsupported")) Err(ser::Error::custom("unsupported"))
} }
@ -1191,10 +1176,6 @@ impl<E: ser::Error> ser::Serializer for Categorize<E> {
Ok(self) Ok(self)
} }
fn serialize_seq_fixed_size(self, _: usize) -> Result<Self, Self::Error> {
Ok(self)
}
fn serialize_tuple(self, _: usize) -> Result<Self::SerializeTuple, Self::Error> { fn serialize_tuple(self, _: usize) -> Result<Self::SerializeTuple, Self::Error> {
Err(ser::Error::custom("unsupported")) Err(ser::Error::custom("unsupported"))
} }
@ -1203,7 +1184,7 @@ impl<E: ser::Error> ser::Serializer for Categorize<E> {
Err(ser::Error::custom("unsupported")) Err(ser::Error::custom("unsupported"))
} }
fn serialize_tuple_variant(self, _: &'static str, _: usize, _: &'static str, _: usize) -> Result<Self::SerializeTupleVariant, Self::Error> { fn serialize_tuple_variant(self, _: &'static str, _: u32, _: &'static str, _: usize) -> Result<Self::SerializeTupleVariant, Self::Error> {
Err(ser::Error::custom("unsupported")) Err(ser::Error::custom("unsupported"))
} }
@ -1215,7 +1196,7 @@ impl<E: ser::Error> ser::Serializer for Categorize<E> {
Ok(self) Ok(self)
} }
fn serialize_struct_variant(self, _: &'static str, _: usize, _: &'static str, _: usize) -> Result<Self::SerializeStructVariant, Self::Error> { fn serialize_struct_variant(self, _: &'static str, _: u32, _: &'static str, _: usize) -> Result<Self::SerializeStructVariant, Self::Error> {
Err(ser::Error::custom("unsupported")) Err(ser::Error::custom("unsupported"))
} }
} }

View file

@ -58,8 +58,8 @@ impl Value {
/// something is wrong with the data, for example required struct fields are /// something is wrong with the data, for example required struct fields are
/// missing from the TOML map or some number is too big to fit in the expected /// missing from the TOML map or some number is too big to fit in the expected
/// primitive type. /// primitive type.
pub fn try_into<T>(self) -> Result<T, ::de::Error> pub fn try_into<'de, T>(self) -> Result<T, ::de::Error>
where T: de::Deserialize, where T: de::Deserialize<'de>,
{ {
de::Deserialize::deserialize(self) de::Deserialize::deserialize(self)
} }
@ -387,13 +387,13 @@ impl ser::Serialize for Value {
} }
} }
impl de::Deserialize for Value { impl<'de> de::Deserialize<'de> for Value {
fn deserialize<D>(deserializer: D) -> Result<Value, D::Error> fn deserialize<D>(deserializer: D) -> Result<Value, D::Error>
where D: de::Deserializer where D: de::Deserializer<'de>,
{ {
struct ValueVisitor; struct ValueVisitor;
impl de::Visitor for ValueVisitor { impl<'de> de::Visitor<'de> for ValueVisitor {
type Value = Value; type Value = Value;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
@ -421,55 +421,58 @@ impl de::Deserialize for Value {
} }
fn visit_some<D>(self, deserializer: D) -> Result<Value, D::Error> fn visit_some<D>(self, deserializer: D) -> Result<Value, D::Error>
where D: de::Deserializer, where D: de::Deserializer<'de>,
{ {
de::Deserialize::deserialize(deserializer) de::Deserialize::deserialize(deserializer)
} }
fn visit_seq<V>(self, visitor: V) -> Result<Value, V::Error> fn visit_seq<V>(self, mut visitor: V) -> Result<Value, V::Error>
where V: de::SeqVisitor where V: de::SeqAccess<'de>,
{ {
let values = de::impls::VecVisitor::new().visit_seq(visitor)?; let mut vec = Vec::new();
Ok(Value::Array(values)) while let Some(elem) = try!(visitor.next_element()) {
vec.push(elem);
}
Ok(Value::Array(vec))
} }
fn visit_map<V>(self, mut visitor: V) -> Result<Value, V::Error> fn visit_map<V>(self, mut visitor: V) -> Result<Value, V::Error>
where V: de::MapVisitor where V: de::MapAccess<'de>,
{ {
let mut key = String::new(); let mut key = String::new();
let datetime = visitor.visit_key_seed(DatetimeOrTable { let datetime = visitor.next_key_seed(DatetimeOrTable {
key: &mut key, key: &mut key,
})?; })?;
match datetime { match datetime {
Some(true) => { Some(true) => {
let date: DatetimeFromString = visitor.visit_value()?; let date: DatetimeFromString = visitor.next_value()?;
return Ok(Value::Datetime(date.value)) return Ok(Value::Datetime(date.value))
} }
None => return Ok(Value::Table(BTreeMap::new())), None => return Ok(Value::Table(BTreeMap::new())),
Some(false) => {} Some(false) => {}
} }
let mut map = BTreeMap::new(); let mut map = BTreeMap::new();
map.insert(key, visitor.visit_value()?); map.insert(key, visitor.next_value()?);
while let Some(key) = visitor.visit_key()? { while let Some(key) = visitor.next_key()? {
if map.contains_key(&key) { if map.contains_key(&key) {
let msg = format!("duplicate key: `{}`", key); let msg = format!("duplicate key: `{}`", key);
return Err(de::Error::custom(msg)) return Err(de::Error::custom(msg))
} }
map.insert(key, visitor.visit_value()?); map.insert(key, visitor.next_value()?);
} }
Ok(Value::Table(map)) Ok(Value::Table(map))
} }
} }
deserializer.deserialize(ValueVisitor) deserializer.deserialize_any(ValueVisitor)
} }
} }
impl de::Deserializer for Value { impl<'de> de::Deserializer<'de> for Value {
type Error = ::de::Error; type Error = ::de::Error;
fn deserialize<V>(self, visitor: V) -> Result<V::Value, ::de::Error> fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, ::de::Error>
where V: de::Visitor, where V: de::Visitor<'de>,
{ {
match self { match self {
Value::Boolean(v) => visitor.visit_bool(v), Value::Boolean(v) => visitor.visit_bool(v),
@ -505,15 +508,15 @@ impl de::Deserializer for Value {
// `None` is interpreted as a missing field so be sure to implement `Some` // `None` is interpreted as a missing field so be sure to implement `Some`
// as a present field. // as a present field.
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, ::de::Error> fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, ::de::Error>
where V: de::Visitor where V: de::Visitor<'de>,
{ {
visitor.visit_some(self) visitor.visit_some(self)
} }
forward_to_deserialize! { forward_to_deserialize_any! {
bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit seq bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit seq
seq_fixed_size bytes byte_buf map unit_struct tuple_struct struct bytes byte_buf map unit_struct tuple_struct struct
struct_field tuple ignored_any enum newtype_struct tuple ignored_any enum newtype_struct identifier
} }
} }
@ -529,11 +532,12 @@ impl SeqDeserializer {
} }
} }
impl de::SeqVisitor for SeqDeserializer { impl<'de> de::SeqAccess<'de> for SeqDeserializer {
type Error = ::de::Error; type Error = ::de::Error;
fn visit_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, ::de::Error> fn next_element_seed<T>(&mut self, seed: T)
where T: de::DeserializeSeed, -> Result<Option<T::Value>, ::de::Error>
where T: de::DeserializeSeed<'de>,
{ {
match self.iter.next() { match self.iter.next() {
Some(value) => seed.deserialize(value).map(Some), Some(value) => seed.deserialize(value).map(Some),
@ -541,8 +545,11 @@ impl de::SeqVisitor for SeqDeserializer {
} }
} }
fn size_hint(&self) -> (usize, Option<usize>) { fn size_hint(&self) -> Option<usize> {
self.iter.size_hint() match self.iter.size_hint() {
(lower, Some(upper)) if lower == upper => Some(upper),
_ => None,
}
} }
} }
@ -560,11 +567,11 @@ impl MapDeserializer {
} }
} }
impl de::MapVisitor for MapDeserializer { impl<'de> de::MapAccess<'de> for MapDeserializer {
type Error = ::de::Error; type Error = ::de::Error;
fn visit_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, ::de::Error> fn next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, ::de::Error>
where T: de::DeserializeSeed, where T: de::DeserializeSeed<'de>,
{ {
match self.iter.next() { match self.iter.next() {
Some((key, value)) => { Some((key, value)) => {
@ -575,8 +582,8 @@ impl de::MapVisitor for MapDeserializer {
} }
} }
fn visit_value_seed<T>(&mut self, seed: T) -> Result<T::Value, ::de::Error> fn next_value_seed<T>(&mut self, seed: T) -> Result<T::Value, ::de::Error>
where T: de::DeserializeSeed, where T: de::DeserializeSeed<'de>,
{ {
let (key, res) = match self.value.take() { let (key, res) = match self.value.take() {
Some((key, value)) => (key, seed.deserialize(value)), Some((key, value)) => (key, seed.deserialize(value)),
@ -588,8 +595,11 @@ impl de::MapVisitor for MapDeserializer {
}) })
} }
fn size_hint(&self) -> (usize, Option<usize>) { fn size_hint(&self) -> Option<usize> {
self.iter.size_hint() match self.iter.size_hint() {
(lower, Some(upper)) if lower == upper => Some(upper),
_ => None,
}
} }
} }
@ -681,7 +691,7 @@ impl ser::Serializer for Serializer {
fn serialize_unit_variant(self, fn serialize_unit_variant(self,
_name: &'static str, _name: &'static str,
_variant_index: usize, _variant_index: u32,
_variant: &'static str) _variant: &'static str)
-> Result<Value, ::ser::Error> { -> Result<Value, ::ser::Error> {
Err(::ser::Error::UnsupportedType) Err(::ser::Error::UnsupportedType)
@ -698,7 +708,7 @@ impl ser::Serializer for Serializer {
fn serialize_newtype_variant<T: ?Sized>(self, fn serialize_newtype_variant<T: ?Sized>(self,
_name: &'static str, _name: &'static str,
_variant_index: usize, _variant_index: u32,
_variant: &'static str, _variant: &'static str,
_value: &T) _value: &T)
-> Result<Value, ::ser::Error> -> Result<Value, ::ser::Error>
@ -725,11 +735,6 @@ impl ser::Serializer for Serializer {
}) })
} }
fn serialize_seq_fixed_size(self, size: usize)
-> Result<Self::SerializeSeq, ::ser::Error> {
self.serialize_seq(Some(size))
}
fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, ::ser::Error> { fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, ::ser::Error> {
Err(::ser::Error::UnsupportedType) Err(::ser::Error::UnsupportedType)
} }
@ -741,7 +746,7 @@ impl ser::Serializer for Serializer {
fn serialize_tuple_variant(self, fn serialize_tuple_variant(self,
_name: &'static str, _name: &'static str,
_variant_index: usize, _variant_index: u32,
_variant: &'static str, _variant: &'static str,
_len: usize) _len: usize)
-> Result<Self::SerializeTupleVariant, ::ser::Error> -> Result<Self::SerializeTupleVariant, ::ser::Error>
@ -765,7 +770,7 @@ impl ser::Serializer for Serializer {
fn serialize_struct_variant(self, fn serialize_struct_variant(self,
_name: &'static str, _name: &'static str,
_variant_index: usize, _variant_index: u32,
_variant: &'static str, _variant: &'static str,
_len: usize) _len: usize)
-> Result<Self::SerializeStructVariant, ::ser::Error> -> Result<Self::SerializeStructVariant, ::ser::Error>
@ -851,17 +856,17 @@ struct DatetimeOrTable<'a> {
key: &'a mut String, key: &'a mut String,
} }
impl<'a> de::DeserializeSeed for DatetimeOrTable<'a> { impl<'a, 'de> de::DeserializeSeed<'de> for DatetimeOrTable<'a> {
type Value = bool; type Value = bool;
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error> fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where D: de::Deserializer where D: de::Deserializer<'de>
{ {
deserializer.deserialize(self) deserializer.deserialize_any(self)
} }
} }
impl<'a> de::Visitor for DatetimeOrTable<'a> { impl<'a, 'de> de::Visitor<'de> for DatetimeOrTable<'a> {
type Value = bool; type Value = bool;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {

View file

@ -130,8 +130,8 @@ fn nested() {
fn application_decode_error() { fn application_decode_error() {
#[derive(PartialEq, Debug)] #[derive(PartialEq, Debug)]
struct Range10(usize); struct Range10(usize);
impl Deserialize for Range10 { impl<'de> Deserialize<'de> for Range10 {
fn deserialize<D: Deserializer>(d: D) -> Result<Range10, D::Error> { fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Range10, D::Error> {
let x: usize = try!(Deserialize::deserialize(d)); let x: usize = try!(Deserialize::deserialize(d));
if x > 10 { if x > 10 {
Err(serde::de::Error::custom("more than 10")) Err(serde::de::Error::custom("more than 10"))