Remove seq_fixed_size in favor of tuple

This commit is contained in:
David Tolnay 2017-04-17 12:07:49 -07:00
parent 739ad64c7c
commit 86deb8db79
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
15 changed files with 238 additions and 298 deletions

View File

@ -634,7 +634,7 @@ impl<'de, T> Deserialize<'de> for [T; 0] {
where where
D: Deserializer<'de>, D: Deserializer<'de>,
{ {
deserializer.deserialize_seq_fixed_size(0, ArrayVisitor::<[T; 0]>::new()) deserializer.deserialize_tuple(0, ArrayVisitor::<[T; 0]>::new())
} }
} }
@ -675,7 +675,7 @@ macro_rules! array_impls {
where where
D: Deserializer<'de>, D: Deserializer<'de>,
{ {
deserializer.deserialize_seq_fixed_size($len, ArrayVisitor::<[T; $len]>::new()) deserializer.deserialize_tuple($len, ArrayVisitor::<[T; $len]>::new())
} }
} }
)+ )+

View File

@ -704,7 +704,7 @@ where
/// Serde. /// Serde.
/// ///
/// The role of this trait is to define the deserialization half of the Serde /// The role of this trait is to define the deserialization half of the Serde
/// data model, which is a way to categorize every Rust data type into one of 28 /// data model, which is a way to categorize every Rust data type into one of 27
/// possible types. Each method of the `Serializer` trait corresponds to one of /// possible types. Each method of the `Serializer` trait corresponds to one of
/// the types of the data model. /// the types of the data model.
/// ///
@ -714,47 +714,54 @@ where
/// ///
/// The types that make up the Serde data model are: /// The types that make up the Serde data model are:
/// ///
/// - 12 primitive types: /// - **12 primitive types**
/// - bool /// - bool
/// - i8, i16, i32, i64 /// - i8, i16, i32, i64
/// - u8, u16, u32, u64 /// - u8, u16, u32, u64
/// - f32, f64 /// - f32, f64
/// - char /// - char
/// - string /// - **string**
/// - byte array - [u8] /// - UTF-8 bytes with a length and no null terminator.
/// - option /// - When serializing, all strings are handled equally. When deserializing,
/// - either none or some value /// there are three flavors of strings: transient, owned, and borrowed.
/// - unit /// - **byte array** - [u8]
/// - unit is the type of () in Rust /// - Similar to strings, during deserialization byte arrays can be transient,
/// - unit_struct /// owned, or borrowed.
/// - for example `struct Unit` or `PhantomData<T>` /// - **option**
/// - unit_variant /// - Either none or some value.
/// - the `E::A` and `E::B` in `enum E { A, B }` /// - **unit**
/// - newtype_struct /// - The type of `()` in Rust. It represents an anonymous value containing no
/// - for example `struct Millimeters(u8)` /// data.
/// - newtype_variant /// - **unit_struct**
/// - the `E::N` in `enum E { N(u8) }` /// - For example `struct Unit` or `PhantomData<T>`. It represents a named value
/// - seq /// containing no data.
/// - a variably sized sequence of values, for example `Vec<T>` or /// - **unit_variant**
/// `HashSet<T>` /// - For example the `E::A` and `E::B` in `enum E { A, B }`.
/// - seq_fixed_size /// - **newtype_struct**
/// - a statically sized sequence of values for which the size will be known /// - For example `struct Millimeters(u8)`.
/// at deserialization time without looking at the serialized data, for /// - **newtype_variant**
/// example `[u64; 10]` /// - For example the `E::N` in `enum E { N(u8) }`.
/// - tuple /// - **seq**
/// - for example `(u8,)` or `(String, u64, Vec<T>)` /// - A variably sized heterogeneous sequence of values, for example `Vec<T>` or
/// - tuple_struct /// `HashSet<T>`. When serializing, the length may or may not be known before
/// - for example `struct Rgb(u8, u8, u8)` /// iterating through all the data. When deserializing, the length is determined
/// - tuple_variant /// by looking at the serialized data.
/// - the `E::T` in `enum E { T(u8, u8) }` /// - **tuple**
/// - map /// - A statically sized heterogeneous sequence of values for which the length
/// - for example `BTreeMap<K, V>` /// will be known at deserialization time without looking at the serialized
/// - struct /// data, for example `(u8,)` or `(String, u64, Vec<T>)` or `[u64; 10]`.
/// - a key-value pairing in which the keys will be known at deserialization /// - **tuple_struct**
/// time without looking at the serialized data, for example `struct S { r: /// - A named tuple, for example `struct Rgb(u8, u8, u8)`.
/// u8, g: u8, b: u8 }` /// - **tuple_variant**
/// - struct_variant /// - For example the `E::T` in `enum E { T(u8, u8) }`.
/// - the `E::S` in `enum E { S { r: u8, g: u8, b: u8 } }` /// - **map**
/// - A heterogeneous key-value pairing, for example `BTreeMap<K, V>`.
/// - **struct**
/// - A heterogeneous key-value pairing in which the keys are strings and will be
/// known at deserialization time without looking at the serialized data, for
/// example `struct S { r: u8, g: u8, b: u8 }`.
/// - **struct_variant**
/// - For example the `E::S` in `enum E { S { r: u8, g: u8, b: u8 } }`.
/// ///
/// The `Deserializer` trait supports two entry point styles which enables /// The `Deserializer` trait supports two entry point styles which enables
/// different kinds of deserialization. /// different kinds of deserialization.
@ -944,16 +951,6 @@ pub trait Deserializer<'de>: Sized {
/// Hint that the `Deserialize` type is expecting a sequence of values and /// Hint that the `Deserialize` type is expecting a sequence of values and
/// knows how many values there are without looking at the serialized data. /// knows how many values there are without looking at the serialized data.
fn deserialize_seq_fixed_size<V>(
self,
len: usize,
visitor: V,
) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>;
/// Hint that the `Deserialize` type is expecting a tuple value with a
/// particular number of elements.
fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error> fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error>
where where
V: Visitor<'de>; V: Visitor<'de>;

View File

@ -130,8 +130,8 @@ where
forward_to_deserialize_any! { forward_to_deserialize_any! {
bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes
byte_buf unit unit_struct newtype_struct seq seq_fixed_size tuple byte_buf unit unit_struct newtype_struct seq tuple tuple_struct map
tuple_struct map struct enum identifier ignored_any struct enum identifier ignored_any
} }
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
@ -183,8 +183,8 @@ macro_rules! primitive_deserializer {
forward_to_deserialize_any! { forward_to_deserialize_any! {
bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes
byte_buf option unit unit_struct newtype_struct seq seq_fixed_size byte_buf option unit unit_struct newtype_struct seq tuple
tuple tuple_struct map struct enum identifier ignored_any tuple_struct map struct enum identifier ignored_any
} }
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
@ -240,8 +240,8 @@ where
forward_to_deserialize_any! { forward_to_deserialize_any! {
bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes
byte_buf option unit unit_struct newtype_struct seq seq_fixed_size byte_buf option unit unit_struct newtype_struct seq tuple tuple_struct
tuple tuple_struct map struct identifier ignored_any map struct identifier ignored_any
} }
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
@ -333,8 +333,8 @@ where
forward_to_deserialize_any! { forward_to_deserialize_any! {
bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes
byte_buf option unit unit_struct newtype_struct seq seq_fixed_size byte_buf option unit unit_struct newtype_struct seq tuple tuple_struct
tuple tuple_struct map struct identifier ignored_any map struct identifier ignored_any
} }
} }
@ -408,8 +408,8 @@ where
forward_to_deserialize_any! { forward_to_deserialize_any! {
bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes
byte_buf option unit unit_struct newtype_struct seq seq_fixed_size byte_buf option unit unit_struct newtype_struct seq tuple tuple_struct
tuple tuple_struct map struct identifier ignored_any map struct identifier ignored_any
} }
} }
@ -487,8 +487,8 @@ where
forward_to_deserialize_any! { forward_to_deserialize_any! {
bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes
byte_buf option unit unit_struct newtype_struct seq seq_fixed_size byte_buf option unit unit_struct newtype_struct seq tuple tuple_struct
tuple tuple_struct map struct identifier ignored_any map struct identifier ignored_any
} }
} }
@ -573,8 +573,8 @@ where
forward_to_deserialize_any! { forward_to_deserialize_any! {
bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes
byte_buf option unit unit_struct newtype_struct seq seq_fixed_size byte_buf option unit unit_struct newtype_struct seq tuple tuple_struct
tuple tuple_struct map struct enum identifier ignored_any map struct enum identifier ignored_any
} }
} }
@ -687,8 +687,8 @@ where
forward_to_deserialize_any! { forward_to_deserialize_any! {
bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes
byte_buf option unit unit_struct newtype_struct seq seq_fixed_size byte_buf option unit unit_struct newtype_struct seq tuple tuple_struct
tuple tuple_struct map struct enum identifier ignored_any map struct enum identifier ignored_any
} }
} }
@ -791,7 +791,7 @@ where
Ok(value) Ok(value)
} }
fn deserialize_seq_fixed_size<V>( fn deserialize_tuple<V>(
self, self,
len: usize, len: usize,
visitor: V, visitor: V,
@ -805,8 +805,8 @@ where
forward_to_deserialize_any! { forward_to_deserialize_any! {
bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes
byte_buf option unit unit_struct newtype_struct tuple tuple_struct byte_buf option unit unit_struct newtype_struct tuple_struct map struct
map struct enum identifier ignored_any enum identifier ignored_any
} }
} }
@ -946,8 +946,8 @@ where
forward_to_deserialize_any! { forward_to_deserialize_any! {
bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes
byte_buf option unit unit_struct newtype_struct tuple tuple_struct byte_buf option unit unit_struct newtype_struct tuple_struct map struct
map struct enum identifier ignored_any enum identifier ignored_any
} }
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
@ -973,7 +973,7 @@ where
} }
} }
fn deserialize_seq_fixed_size<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error> fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error>
where where
V: de::Visitor<'de>, V: de::Visitor<'de>,
{ {
@ -1093,8 +1093,8 @@ where
forward_to_deserialize_any! { forward_to_deserialize_any! {
bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes
byte_buf option unit unit_struct newtype_struct seq seq_fixed_size byte_buf option unit unit_struct newtype_struct seq tuple tuple_struct
tuple tuple_struct map struct enum identifier ignored_any map struct enum identifier ignored_any
} }
} }

View File

@ -44,9 +44,9 @@
/// } /// }
/// # /// #
/// # forward_to_deserialize_any! { /// # forward_to_deserialize_any! {
/// # u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit option /// # i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes
/// # seq seq_fixed_size bytes byte_buf map unit_struct newtype_struct /// # byte_buf option unit unit_struct newtype_struct seq tuple
/// # tuple_struct struct identifier tuple enum ignored_any /// # tuple_struct map struct enum identifier ignored_any
/// # } /// # }
/// # } /// # }
/// # /// #
@ -78,8 +78,8 @@
/// ///
/// forward_to_deserialize_any! { /// forward_to_deserialize_any! {
/// bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes /// bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes
/// byte_buf option unit unit_struct newtype_struct seq seq_fixed_size /// byte_buf option unit unit_struct newtype_struct seq tuple
/// tuple tuple_struct map struct enum identifier ignored_any /// tuple_struct map struct enum identifier ignored_any
/// } /// }
/// } /// }
/// # /// #
@ -113,8 +113,8 @@
/// forward_to_deserialize_any! { /// forward_to_deserialize_any! {
/// <W: Visitor<'q>> /// <W: Visitor<'q>>
/// bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes /// bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes
/// byte_buf option unit unit_struct newtype_struct seq seq_fixed_size /// byte_buf option unit unit_struct newtype_struct seq tuple tuple_struct
/// tuple tuple_struct map struct enum identifier ignored_any /// map struct enum identifier ignored_any
/// } /// }
/// # } /// # }
/// # /// #
@ -218,9 +218,6 @@ macro_rules! forward_to_deserialize_any_helper {
(seq<$l:tt, $v:ident>) => { (seq<$l:tt, $v:ident>) => {
forward_to_deserialize_any_method!{deserialize_seq<$l, $v>()} forward_to_deserialize_any_method!{deserialize_seq<$l, $v>()}
}; };
(seq_fixed_size<$l:tt, $v:ident>) => {
forward_to_deserialize_any_method!{deserialize_seq_fixed_size<$l, $v>(len: usize)}
};
(tuple<$l:tt, $v:ident>) => { (tuple<$l:tt, $v:ident>) => {
forward_to_deserialize_any_method!{deserialize_tuple<$l, $v>(len: usize)} forward_to_deserialize_any_method!{deserialize_tuple<$l, $v>(len: usize)}
}; };

View File

@ -49,8 +49,8 @@ where
forward_to_deserialize_any! { forward_to_deserialize_any! {
bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes
byte_buf unit unit_struct newtype_struct seq seq_fixed_size tuple byte_buf unit unit_struct newtype_struct seq tuple tuple_struct map
tuple_struct map struct enum identifier ignored_any struct enum identifier ignored_any
} }
} }
@ -995,8 +995,8 @@ mod content {
forward_to_deserialize_any! { forward_to_deserialize_any! {
bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes
byte_buf unit unit_struct seq seq_fixed_size tuple tuple_struct map byte_buf unit unit_struct seq tuple tuple_struct map struct
struct identifier ignored_any identifier ignored_any
} }
} }
@ -1153,8 +1153,8 @@ mod content {
forward_to_deserialize_any! { forward_to_deserialize_any! {
bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes
byte_buf option unit unit_struct newtype_struct seq seq_fixed_size byte_buf option unit unit_struct newtype_struct seq tuple
tuple tuple_struct map struct enum identifier ignored_any tuple_struct map struct enum identifier ignored_any
} }
} }
@ -1254,8 +1254,8 @@ mod content {
forward_to_deserialize_any! { forward_to_deserialize_any! {
bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes
byte_buf option unit unit_struct newtype_struct seq seq_fixed_size byte_buf option unit unit_struct newtype_struct seq tuple
tuple tuple_struct map struct enum identifier ignored_any tuple_struct map struct enum identifier ignored_any
} }
} }
@ -1390,8 +1390,8 @@ mod content {
forward_to_deserialize_any! { forward_to_deserialize_any! {
bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes
byte_buf unit unit_struct seq seq_fixed_size tuple tuple_struct map byte_buf unit unit_struct seq tuple tuple_struct map struct
struct identifier ignored_any identifier ignored_any
} }
} }
@ -1545,8 +1545,8 @@ mod content {
forward_to_deserialize_any! { forward_to_deserialize_any! {
bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes
byte_buf option unit unit_struct newtype_struct seq seq_fixed_size byte_buf option unit unit_struct newtype_struct seq tuple
tuple tuple_struct map struct enum identifier ignored_any tuple_struct map struct enum identifier ignored_any
} }
} }
@ -1647,8 +1647,8 @@ mod content {
forward_to_deserialize_any! { forward_to_deserialize_any! {
bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes
byte_buf option unit unit_struct newtype_struct seq seq_fixed_size byte_buf option unit unit_struct newtype_struct seq tuple
tuple tuple_struct map struct enum identifier ignored_any tuple_struct map struct enum identifier ignored_any
} }
} }
@ -1804,8 +1804,8 @@ where
forward_to_deserialize_any! { forward_to_deserialize_any! {
bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes
byte_buf option unit unit_struct newtype_struct seq seq_fixed_size byte_buf option unit unit_struct newtype_struct seq tuple tuple_struct
tuple tuple_struct map struct enum identifier ignored_any map struct enum identifier ignored_any
} }
} }
@ -1843,7 +1843,7 @@ where
forward_to_deserialize_any! { forward_to_deserialize_any! {
bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes
byte_buf option unit unit_struct newtype_struct seq seq_fixed_size byte_buf option unit unit_struct newtype_struct seq tuple tuple_struct
tuple tuple_struct map struct enum identifier ignored_any map struct enum identifier ignored_any
} }
} }

View File

@ -121,9 +121,6 @@ macro_rules! __serialize_unimplemented_helper {
type SerializeSeq = $crate::ser::Impossible<Self::Ok, Self::Error>; type SerializeSeq = $crate::ser::Impossible<Self::Ok, Self::Error>;
__serialize_unimplemented_method!(serialize_seq(Option<usize>) -> SerializeSeq); __serialize_unimplemented_method!(serialize_seq(Option<usize>) -> SerializeSeq);
}; };
(seq_fixed_size) => {
__serialize_unimplemented_method!(serialize_seq_fixed_size(usize) -> SerializeSeq);
};
(tuple) => { (tuple) => {
type SerializeTuple = $crate::ser::Impossible<Self::Ok, Self::Error>; type SerializeTuple = $crate::ser::Impossible<Self::Ok, Self::Error>;
__serialize_unimplemented_method!(serialize_tuple(usize) -> SerializeTuple); __serialize_unimplemented_method!(serialize_tuple(usize) -> SerializeTuple);

View File

@ -245,10 +245,6 @@ where
Err(self.bad_type(Unsupported::Sequence)) Err(self.bad_type(Unsupported::Sequence))
} }
fn serialize_seq_fixed_size(self, _: usize) -> Result<Self::SerializeSeq, Self::Error> {
Err(self.bad_type(Unsupported::Sequence))
}
fn serialize_tuple(self, _: usize) -> Result<Self::SerializeTuple, Self::Error> { fn serialize_tuple(self, _: usize) -> Result<Self::SerializeTuple, Self::Error> {
Err(self.bad_type(Unsupported::Tuple)) Err(self.bad_type(Unsupported::Tuple))
} }
@ -484,7 +480,6 @@ mod content {
NewtypeVariant(&'static str, u32, &'static str, Box<Content>), NewtypeVariant(&'static str, u32, &'static str, Box<Content>),
Seq(Vec<Content>), Seq(Vec<Content>),
SeqFixedSize(Vec<Content>),
Tuple(Vec<Content>), Tuple(Vec<Content>),
TupleStruct(&'static str, Vec<Content>), TupleStruct(&'static str, Vec<Content>),
TupleVariant(&'static str, u32, &'static str, Vec<Content>), TupleVariant(&'static str, u32, &'static str, Vec<Content>),
@ -523,14 +518,6 @@ mod content {
serializer.serialize_newtype_variant(n, i, v, &**c) serializer.serialize_newtype_variant(n, i, v, &**c)
} }
Content::Seq(ref elements) => elements.serialize(serializer), Content::Seq(ref elements) => elements.serialize(serializer),
Content::SeqFixedSize(ref elements) => {
use ser::SerializeSeq;
let mut seq = try!(serializer.serialize_seq_fixed_size(elements.len()));
for e in elements {
try!(seq.serialize_element(e));
}
seq.end()
}
Content::Tuple(ref elements) => { Content::Tuple(ref elements) => {
use ser::SerializeTuple; use ser::SerializeTuple;
let mut tuple = try!(serializer.serialize_tuple(elements.len())); let mut tuple = try!(serializer.serialize_tuple(elements.len()));
@ -726,23 +713,12 @@ mod content {
fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, E> { fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, E> {
Ok( Ok(
SerializeSeq { SerializeSeq {
fixed_size: false,
elements: Vec::with_capacity(len.unwrap_or(0)), elements: Vec::with_capacity(len.unwrap_or(0)),
error: PhantomData, error: PhantomData,
}, },
) )
} }
fn serialize_seq_fixed_size(self, size: usize) -> Result<Self::SerializeSeq, E> {
Ok(
SerializeSeq {
fixed_size: true,
elements: Vec::with_capacity(size),
error: PhantomData,
},
)
}
fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, E> { fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, E> {
Ok( Ok(
SerializeTuple { SerializeTuple {
@ -828,7 +804,6 @@ mod content {
} }
struct SerializeSeq<E> { struct SerializeSeq<E> {
fixed_size: bool,
elements: Vec<Content>, elements: Vec<Content>,
error: PhantomData<E>, error: PhantomData<E>,
} }
@ -850,13 +825,7 @@ mod content {
} }
fn end(self) -> Result<Content, E> { fn end(self) -> Result<Content, E> {
Ok( Ok(Content::Seq(self.elements))
if self.fixed_size {
Content::SeqFixedSize(self.elements)
} else {
Content::Seq(self.elements)
},
)
} }
} }

View File

@ -8,7 +8,7 @@
use lib::*; use lib::*;
use ser::{Serialize, SerializeSeq, SerializeTuple, Serializer}; use ser::{Serialize, SerializeTuple, Serializer};
#[cfg(feature = "std")] #[cfg(feature = "std")]
use ser::Error; use ser::Error;
@ -130,7 +130,7 @@ impl<T> Serialize for [T; 0] {
where where
S: Serializer, S: Serializer,
{ {
try!(serializer.serialize_seq_fixed_size(0)).end() try!(serializer.serialize_tuple(0)).end()
} }
} }
@ -146,7 +146,7 @@ macro_rules! array_impls {
where where
S: Serializer, S: Serializer,
{ {
let mut seq = try!(serializer.serialize_seq_fixed_size($len)); let mut seq = try!(serializer.serialize_tuple($len));
for e in self { for e in self {
try!(seq.serialize_element(e)); try!(seq.serialize_element(e));
} }

View File

@ -53,8 +53,7 @@ use ser::{self, Serialize, SerializeSeq, SerializeTuple, SerializeTupleStruct,
/// # __serialize_unimplemented! { /// # __serialize_unimplemented! {
/// # bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str bytes none some /// # bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str bytes none some
/// # unit unit_struct unit_variant newtype_struct newtype_variant /// # unit unit_struct unit_variant newtype_struct newtype_variant
/// # seq_fixed_size tuple tuple_struct tuple_variant map struct /// # tuple tuple_struct tuple_variant map struct struct_variant
/// # struct_variant
/// # } /// # }
/// } /// }
/// # /// #

View File

@ -239,7 +239,7 @@ pub trait Serialize {
/// A **data format** that can serialize any data structure supported by Serde. /// A **data format** that can serialize any data structure supported by Serde.
/// ///
/// The role of this trait is to define the serialization half of the Serde data /// The role of this trait is to define the serialization half of the Serde data
/// model, which is a way to categorize every Rust data structure into one of 28 /// model, which is a way to categorize every Rust data structure into one of 27
/// possible types. Each method of the `Serializer` trait corresponds to one of /// possible types. Each method of the `Serializer` trait corresponds to one of
/// the types of the data model. /// the types of the data model.
/// ///
@ -248,47 +248,54 @@ pub trait Serialize {
/// ///
/// The types that make up the Serde data model are: /// The types that make up the Serde data model are:
/// ///
/// - 12 primitive types: /// - **12 primitive types**
/// - bool /// - bool
/// - i8, i16, i32, i64 /// - i8, i16, i32, i64
/// - u8, u16, u32, u64 /// - u8, u16, u32, u64
/// - f32, f64 /// - f32, f64
/// - char /// - char
/// - string /// - **string**
/// - byte array - [u8] /// - UTF-8 bytes with a length and no null terminator.
/// - option /// - When serializing, all strings are handled equally. When deserializing,
/// - either none or some value /// there are three flavors of strings: transient, owned, and borrowed.
/// - unit /// - **byte array** - [u8]
/// - unit is the type of () in Rust /// - Similar to strings, during deserialization byte arrays can be transient,
/// - unit_struct /// owned, or borrowed.
/// - for example `struct Unit` or `PhantomData<T>` /// - **option**
/// - unit_variant /// - Either none or some value.
/// - the `E::A` and `E::B` in `enum E { A, B }` /// - **unit**
/// - newtype_struct /// - The type of `()` in Rust. It represents an anonymous value containing no
/// - for example `struct Millimeters(u8)` /// data.
/// - newtype_variant /// - **unit_struct**
/// - the `E::N` in `enum E { N(u8) }` /// - For example `struct Unit` or `PhantomData<T>`. It represents a named value
/// - seq /// containing no data.
/// - a variably sized sequence of values, for example `Vec<T>` or /// - **unit_variant**
/// `HashSet<T>` /// - For example the `E::A` and `E::B` in `enum E { A, B }`.
/// - seq_fixed_size /// - **newtype_struct**
/// - a statically sized sequence of values for which the size will be known /// - For example `struct Millimeters(u8)`.
/// at deserialization time without looking at the serialized data, for /// - **newtype_variant**
/// example `[u64; 10]` /// - For example the `E::N` in `enum E { N(u8) }`.
/// - tuple /// - **seq**
/// - for example `(u8,)` or `(String, u64, Vec<T>)` /// - A variably sized heterogeneous sequence of values, for example `Vec<T>` or
/// - tuple_struct /// `HashSet<T>`. When serializing, the length may or may not be known before
/// - for example `struct Rgb(u8, u8, u8)` /// iterating through all the data. When deserializing, the length is determined
/// - tuple_variant /// by looking at the serialized data.
/// - the `E::T` in `enum E { T(u8, u8) }` /// - **tuple**
/// - map /// - A statically sized heterogeneous sequence of values for which the length
/// - for example `BTreeMap<K, V>` /// will be known at deserialization time without looking at the serialized
/// - struct /// data, for example `(u8,)` or `(String, u64, Vec<T>)` or `[u64; 10]`.
/// - a key-value pairing in which the keys will be known at deserialization /// - **tuple_struct**
/// time without looking at the serialized data, for example `struct S { r: /// - A named tuple, for example `struct Rgb(u8, u8, u8)`.
/// u8, g: u8, b: u8 }` /// - **tuple_variant**
/// - struct_variant /// - For example the `E::T` in `enum E { T(u8, u8) }`.
/// - the `E::S` in `enum E { S { r: u8, g: u8, b: u8 } }` /// - **map**
/// - A heterogeneous key-value pairing, for example `BTreeMap<K, V>`.
/// - **struct**
/// - A heterogeneous key-value pairing in which the keys are strings and will be
/// known at deserialization time without looking at the serialized data, for
/// example `struct S { r: u8, g: u8, b: u8 }`.
/// - **struct_variant**
/// - For example the `E::S` in `enum E { S { r: u8, g: u8, b: u8 } }`.
/// ///
/// Many Serde serializers produce text or binary data as output, for example /// Many Serde serializers produce text or binary data as output, for example
/// JSON or Bincode. This is not a requirement of the `Serializer` trait, and /// JSON or Bincode. This is not a requirement of the `Serializer` trait, and
@ -310,11 +317,10 @@ pub trait Serializer: Sized {
/// The error type when some error occurs during serialization. /// The error type when some error occurs during serialization.
type Error: Error; type Error: Error;
/// Type returned from [`serialize_seq`] and [`serialize_seq_fixed_size`] /// Type returned from [`serialize_seq`] for serializing the content of the
/// for serializing the content of the sequence. /// sequence.
/// ///
/// [`serialize_seq`]: #tymethod.serialize_seq /// [`serialize_seq`]: #tymethod.serialize_seq
/// [`serialize_seq_fixed_size`]: #tymethod.serialize_seq_fixed_size
type SerializeSeq: SerializeSeq<Ok = Self::Ok, Error = Self::Error>; type SerializeSeq: SerializeSeq<Ok = Self::Ok, Error = Self::Error>;
/// Type returned from [`serialize_tuple`] for serializing the content of /// Type returned from [`serialize_tuple`] for serializing the content of
@ -702,8 +708,7 @@ pub trait Serializer: Sized {
/// # __serialize_unimplemented! { /// # __serialize_unimplemented! {
/// # bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str none some /// # bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str none some
/// # unit unit_struct unit_variant newtype_struct newtype_variant /// # unit unit_struct unit_variant newtype_struct newtype_variant
/// # seq seq_fixed_size tuple tuple_struct tuple_variant map struct /// # seq tuple tuple_struct tuple_variant map struct struct_variant
/// # struct_variant
/// # } /// # }
/// # } /// # }
/// # /// #
@ -966,29 +971,6 @@ pub trait Serializer: Sized {
/// then a call to `end`. /// then a call to `end`.
/// ///
/// ```rust /// ```rust
/// use serde::ser::{Serialize, Serializer, SerializeSeq};
///
/// const VRAM_SIZE: usize = 386;
/// struct Vram([u16; VRAM_SIZE]);
///
/// impl Serialize for Vram {
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
/// where S: Serializer
/// {
/// let mut seq = serializer.serialize_seq_fixed_size(VRAM_SIZE)?;
/// for element in &self.0[..] {
/// seq.serialize_element(element)?;
/// }
/// seq.end()
/// }
/// }
/// ```
fn serialize_seq_fixed_size(self, size: usize) -> Result<Self::SerializeSeq, Self::Error>;
/// Begin to serialize a tuple. This call must be followed by zero or more
/// calls to `serialize_element`, then a call to `end`.
///
/// ```rust
/// use serde::ser::{Serialize, Serializer, SerializeTuple}; /// use serde::ser::{Serialize, Serializer, SerializeTuple};
/// ///
/// # mod fool { /// # mod fool {
@ -1015,6 +997,25 @@ pub trait Serializer: Sized {
/// } /// }
/// } /// }
/// ``` /// ```
///
/// ```rust
/// use serde::ser::{Serialize, Serializer, SerializeTuple};
///
/// const VRAM_SIZE: usize = 386;
/// struct Vram([u16; VRAM_SIZE]);
///
/// impl Serialize for Vram {
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
/// where S: Serializer
/// {
/// let mut seq = serializer.serialize_tuple(VRAM_SIZE)?;
/// for element in &self.0[..] {
/// seq.serialize_element(element)?;
/// }
/// seq.end()
/// }
/// }
/// ```
fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Self::Error>; fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Self::Error>;
/// Begin to serialize a tuple struct like `struct Rgb(u8, u8, u8)`. This /// Begin to serialize a tuple struct like `struct Rgb(u8, u8, u8)`. This
@ -1359,34 +1360,26 @@ pub trait Serializer: Sized {
T: Display; T: Display;
} }
/// Returned from `Serializer::serialize_seq` and /// Returned from `Serializer::serialize_seq`.
/// `Serializer::serialize_seq_fixed_size`.
/// ///
/// ```rust /// ```rust
/// # use std::marker::PhantomData; /// # use std::marker::PhantomData;
/// # /// #
/// # macro_rules! unimplemented_vec { /// # struct Vec<T>(PhantomData<T>);
/// # ($name:ident) => {
/// # struct $name<T>(PhantomData<T>);
/// # /// #
/// # impl<T> $name<T> { /// # impl<T> Vec<T> {
/// # fn len(&self) -> usize { /// # fn len(&self) -> usize {
/// # unimplemented!() /// # unimplemented!()
/// # }
/// # }
/// #
/// # impl<'a, T> IntoIterator for &'a $name<T> {
/// # type Item = &'a T;
/// # type IntoIter = Box<Iterator<Item = &'a T>>;
/// # fn into_iter(self) -> Self::IntoIter {
/// # unimplemented!()
/// # }
/// # }
/// # } /// # }
/// # } /// # }
/// # /// #
/// # unimplemented_vec!(Vec); /// # impl<'a, T> IntoIterator for &'a Vec<T> {
/// # unimplemented_vec!(Array); /// # type Item = &'a T;
/// # type IntoIter = Box<Iterator<Item = &'a T>>;
/// # fn into_iter(self) -> Self::IntoIter {
/// # unimplemented!()
/// # }
/// # }
/// # /// #
/// use serde::ser::{Serialize, Serializer, SerializeSeq}; /// use serde::ser::{Serialize, Serializer, SerializeSeq};
/// ///
@ -1403,26 +1396,6 @@ pub trait Serializer: Sized {
/// seq.end() /// seq.end()
/// } /// }
/// } /// }
///
/// # mod fool {
/// # trait Serialize {}
/// impl<T> Serialize for [T; 16]
/// # {}
/// # }
/// #
/// # impl<T> Serialize for Array<T>
/// where T: Serialize
/// {
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
/// where S: Serializer
/// {
/// let mut seq = serializer.serialize_seq_fixed_size(16)?;
/// for element in self {
/// seq.serialize_element(element)?;
/// }
/// seq.end()
/// }
/// }
/// ``` /// ```
pub trait SerializeSeq { pub trait SerializeSeq {
/// Must match the `Ok` type of our `Serializer`. /// Must match the `Ok` type of our `Serializer`.
@ -1469,6 +1442,48 @@ pub trait SerializeSeq {
/// } /// }
/// } /// }
/// ``` /// ```
///
/// ```rust
/// # use std::marker::PhantomData;
/// #
/// # struct Array<T>(PhantomData<T>);
/// #
/// # impl<T> Array<T> {
/// # fn len(&self) -> usize {
/// # unimplemented!()
/// # }
/// # }
/// #
/// # impl<'a, T> IntoIterator for &'a Array<T> {
/// # type Item = &'a T;
/// # type IntoIter = Box<Iterator<Item = &'a T>>;
/// # fn into_iter(self) -> Self::IntoIter {
/// # unimplemented!()
/// # }
/// # }
/// #
/// use serde::ser::{Serialize, Serializer, SerializeTuple};
///
/// # mod fool {
/// # trait Serialize {}
/// impl<T> Serialize for [T; 16]
/// # {}
/// # }
/// #
/// # impl<T> Serialize for Array<T>
/// where T: Serialize
/// {
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
/// where S: Serializer
/// {
/// let mut seq = serializer.serialize_tuple(16)?;
/// for element in self {
/// seq.serialize_element(element)?;
/// }
/// seq.end()
/// }
/// }
/// ```
pub trait SerializeTuple { pub trait SerializeTuple {
/// Must match the `Ok` type of our `Serializer`. /// Must match the `Ok` type of our `Serializer`.
type Ok; type Ok;

View File

@ -132,7 +132,6 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
Token::UnitStruct(_name) => visitor.visit_unit(), Token::UnitStruct(_name) => visitor.visit_unit(),
Token::NewtypeStruct(_name) => visitor.visit_newtype_struct(self), Token::NewtypeStruct(_name) => visitor.visit_newtype_struct(self),
Token::Seq(len) => self.visit_seq(len, Token::SeqEnd, visitor), Token::Seq(len) => self.visit_seq(len, Token::SeqEnd, visitor),
Token::SeqFixedSize(len) => self.visit_seq(Some(len), Token::SeqEnd, visitor),
Token::Tuple(len) => self.visit_seq(Some(len), Token::TupleEnd, visitor), Token::Tuple(len) => self.visit_seq(Some(len), Token::TupleEnd, visitor),
Token::TupleStruct(_, len) => self.visit_seq(Some(len), Token::TupleStructEnd, visitor), Token::TupleStruct(_, len) => self.visit_seq(Some(len), Token::TupleStructEnd, visitor),
Token::Map(len) => self.visit_map(len, Token::MapEnd, visitor), Token::Map(len) => self.visit_map(len, Token::MapEnd, visitor),
@ -262,20 +261,6 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
} }
} }
fn deserialize_seq_fixed_size<V>(self, len: usize, visitor: V) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
match self.tokens.first() {
Some(&Token::SeqFixedSize(_)) => {
self.next_token();
self.visit_seq(Some(len), Token::SeqEnd, visitor)
}
Some(_) => self.deserialize_any(visitor),
None => Err(Error::EndOfTokens),
}
}
fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Error> fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Error>
where where
V: Visitor<'de>, V: Visitor<'de>,
@ -290,10 +275,6 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
self.next_token(); self.next_token();
self.visit_seq(Some(len), Token::SeqEnd, visitor) self.visit_seq(Some(len), Token::SeqEnd, visitor)
} }
Some(&Token::SeqFixedSize(_)) => {
self.next_token();
self.visit_seq(Some(len), Token::SeqEnd, visitor)
}
Some(&Token::Tuple(_)) => { Some(&Token::Tuple(_)) => {
self.next_token(); self.next_token();
self.visit_seq(Some(len), Token::TupleEnd, visitor) self.visit_seq(Some(len), Token::TupleEnd, visitor)
@ -333,10 +314,6 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
self.next_token(); self.next_token();
self.visit_seq(Some(len), Token::SeqEnd, visitor) self.visit_seq(Some(len), Token::SeqEnd, visitor)
} }
Some(&Token::SeqFixedSize(_)) => {
self.next_token();
self.visit_seq(Some(len), Token::SeqEnd, visitor)
}
Some(&Token::Tuple(_)) => { Some(&Token::Tuple(_)) => {
self.next_token(); self.next_token();
self.visit_seq(Some(len), Token::TupleEnd, visitor) self.visit_seq(Some(len), Token::TupleEnd, visitor)
@ -656,7 +633,7 @@ impl<'de> de::Deserializer<'de> for BytesDeserializer {
forward_to_deserialize_any! { forward_to_deserialize_any! {
bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes
byte_buf option unit unit_struct newtype_struct seq seq_fixed_size byte_buf option unit unit_struct newtype_struct seq tuple tuple_struct
tuple tuple_struct map struct enum identifier ignored_any map struct enum identifier ignored_any
} }
} }

View File

@ -212,11 +212,6 @@ impl<'s, 'a> ser::Serializer for &'s mut Serializer<'a> {
Ok(self) Ok(self)
} }
fn serialize_seq_fixed_size(self, len: usize) -> Result<Self, Error> {
assert_next_token!(self, SeqFixedSize(len));
Ok(self)
}
fn serialize_tuple(self, len: usize) -> Result<Self, Error> { fn serialize_tuple(self, len: usize) -> Result<Self, Error> {
assert_next_token!(self, Tuple(len)); assert_next_token!(self, Tuple(len));
Ok(self) Ok(self)

View File

@ -103,12 +103,6 @@ pub enum Token {
/// header is a list of elements, followed by `SeqEnd`. /// header is a list of elements, followed by `SeqEnd`.
Seq(Option<usize>), Seq(Option<usize>),
/// The header to an array of the given length.
///
/// These are serialized via `serialize_seq_fized_size`, which requires a length. After this
/// header is a list of elements, followed by `SeqEnd`.
SeqFixedSize(usize),
/// An indicator of the end of a sequence. /// An indicator of the end of a sequence.
SeqEnd, SeqEnd,

View File

@ -360,8 +360,8 @@ declare_tests! {
Token::SeqEnd, Token::SeqEnd,
], ],
[0; 0] => &[ [0; 0] => &[
Token::SeqFixedSize(0), Token::Tuple(0),
Token::SeqEnd, Token::TupleEnd,
], ],
([0; 0], [1], [2, 3]) => &[ ([0; 0], [1], [2, 3]) => &[
Token::Seq(Some(3)), Token::Seq(Some(3)),
@ -379,19 +379,19 @@ declare_tests! {
Token::SeqEnd, Token::SeqEnd,
], ],
([0; 0], [1], [2, 3]) => &[ ([0; 0], [1], [2, 3]) => &[
Token::SeqFixedSize(3), Token::Tuple(3),
Token::SeqFixedSize(0), Token::Tuple(0),
Token::SeqEnd, Token::TupleEnd,
Token::SeqFixedSize(1), Token::Tuple(1),
Token::I32(1), Token::I32(1),
Token::SeqEnd, Token::TupleEnd,
Token::SeqFixedSize(2), Token::Tuple(2),
Token::I32(2), Token::I32(2),
Token::I32(3), Token::I32(3),
Token::SeqEnd, Token::TupleEnd,
Token::SeqEnd, Token::TupleEnd,
], ],
[0; 0] => &[ [0; 0] => &[
Token::TupleStruct("Anything", 0), Token::TupleStruct("Anything", 0),

View File

@ -138,15 +138,15 @@ declare_tests! {
} }
test_array { test_array {
[0; 0] => &[ [0; 0] => &[
Token::SeqFixedSize(0), Token::Tuple(0),
Token::SeqEnd, Token::TupleEnd,
], ],
[1, 2, 3] => &[ [1, 2, 3] => &[
Token::SeqFixedSize(3), Token::Tuple(3),
Token::I32(1), Token::I32(1),
Token::I32(2), Token::I32(2),
Token::I32(3), Token::I32(3),
Token::SeqEnd, Token::TupleEnd,
], ],
} }
test_vec { test_vec {
@ -301,11 +301,11 @@ declare_tests! {
} }
test_boxed_slice { test_boxed_slice {
Box::new([0, 1, 2]) => &[ Box::new([0, 1, 2]) => &[
Token::SeqFixedSize(3), Token::Tuple(3),
Token::I32(0), Token::I32(0),
Token::I32(1), Token::I32(1),
Token::I32(2), Token::I32(2),
Token::SeqEnd, Token::TupleEnd,
], ],
} }
test_duration { test_duration {