Remove seq_fixed_size in favor of tuple
This commit is contained in:
parent
739ad64c7c
commit
86deb8db79
@ -634,7 +634,7 @@ impl<'de, T> Deserialize<'de> for [T; 0] {
|
||||
where
|
||||
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
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
deserializer.deserialize_seq_fixed_size($len, ArrayVisitor::<[T; $len]>::new())
|
||||
deserializer.deserialize_tuple($len, ArrayVisitor::<[T; $len]>::new())
|
||||
}
|
||||
}
|
||||
)+
|
||||
|
@ -704,7 +704,7 @@ where
|
||||
/// 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
|
||||
/// the types of the data model.
|
||||
///
|
||||
@ -714,47 +714,54 @@ where
|
||||
///
|
||||
/// The types that make up the Serde data model are:
|
||||
///
|
||||
/// - 12 primitive types:
|
||||
/// - **12 primitive types**
|
||||
/// - bool
|
||||
/// - i8, i16, i32, i64
|
||||
/// - u8, u16, u32, u64
|
||||
/// - f32, f64
|
||||
/// - char
|
||||
/// - string
|
||||
/// - byte array - [u8]
|
||||
/// - option
|
||||
/// - either none or some value
|
||||
/// - unit
|
||||
/// - unit is the type of () in Rust
|
||||
/// - unit_struct
|
||||
/// - for example `struct Unit` or `PhantomData<T>`
|
||||
/// - unit_variant
|
||||
/// - the `E::A` and `E::B` in `enum E { A, B }`
|
||||
/// - newtype_struct
|
||||
/// - for example `struct Millimeters(u8)`
|
||||
/// - newtype_variant
|
||||
/// - the `E::N` in `enum E { N(u8) }`
|
||||
/// - seq
|
||||
/// - a variably sized sequence of values, for example `Vec<T>` or
|
||||
/// `HashSet<T>`
|
||||
/// - seq_fixed_size
|
||||
/// - a statically sized sequence of values for which the size will be known
|
||||
/// at deserialization time without looking at the serialized data, for
|
||||
/// example `[u64; 10]`
|
||||
/// - tuple
|
||||
/// - for example `(u8,)` or `(String, u64, Vec<T>)`
|
||||
/// - tuple_struct
|
||||
/// - for example `struct Rgb(u8, u8, u8)`
|
||||
/// - tuple_variant
|
||||
/// - the `E::T` in `enum E { T(u8, u8) }`
|
||||
/// - map
|
||||
/// - for example `BTreeMap<K, V>`
|
||||
/// - struct
|
||||
/// - a key-value pairing in which the keys will be known at deserialization
|
||||
/// time without looking at the serialized data, for example `struct S { r:
|
||||
/// u8, g: u8, b: u8 }`
|
||||
/// - struct_variant
|
||||
/// - the `E::S` in `enum E { S { r: u8, g: u8, b: u8 } }`
|
||||
/// - **string**
|
||||
/// - UTF-8 bytes with a length and no null terminator.
|
||||
/// - When serializing, all strings are handled equally. When deserializing,
|
||||
/// there are three flavors of strings: transient, owned, and borrowed.
|
||||
/// - **byte array** - [u8]
|
||||
/// - Similar to strings, during deserialization byte arrays can be transient,
|
||||
/// owned, or borrowed.
|
||||
/// - **option**
|
||||
/// - Either none or some value.
|
||||
/// - **unit**
|
||||
/// - The type of `()` in Rust. It represents an anonymous value containing no
|
||||
/// data.
|
||||
/// - **unit_struct**
|
||||
/// - For example `struct Unit` or `PhantomData<T>`. It represents a named value
|
||||
/// containing no data.
|
||||
/// - **unit_variant**
|
||||
/// - For example the `E::A` and `E::B` in `enum E { A, B }`.
|
||||
/// - **newtype_struct**
|
||||
/// - For example `struct Millimeters(u8)`.
|
||||
/// - **newtype_variant**
|
||||
/// - For example the `E::N` in `enum E { N(u8) }`.
|
||||
/// - **seq**
|
||||
/// - A variably sized heterogeneous sequence of values, for example `Vec<T>` or
|
||||
/// `HashSet<T>`. When serializing, the length may or may not be known before
|
||||
/// iterating through all the data. When deserializing, the length is determined
|
||||
/// by looking at the serialized data.
|
||||
/// - **tuple**
|
||||
/// - A statically sized heterogeneous sequence of values for which the length
|
||||
/// will be known at deserialization time without looking at the serialized
|
||||
/// data, for example `(u8,)` or `(String, u64, Vec<T>)` or `[u64; 10]`.
|
||||
/// - **tuple_struct**
|
||||
/// - A named tuple, for example `struct Rgb(u8, u8, u8)`.
|
||||
/// - **tuple_variant**
|
||||
/// - For example the `E::T` in `enum E { T(u8, 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
|
||||
/// 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
|
||||
/// 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>
|
||||
where
|
||||
V: Visitor<'de>;
|
||||
|
@ -130,8 +130,8 @@ where
|
||||
|
||||
forward_to_deserialize_any! {
|
||||
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
|
||||
tuple_struct map struct enum identifier ignored_any
|
||||
byte_buf unit unit_struct newtype_struct seq tuple tuple_struct map
|
||||
struct enum identifier ignored_any
|
||||
}
|
||||
|
||||
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
@ -183,8 +183,8 @@ macro_rules! primitive_deserializer {
|
||||
|
||||
forward_to_deserialize_any! {
|
||||
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
|
||||
tuple tuple_struct map struct enum identifier ignored_any
|
||||
byte_buf option unit unit_struct newtype_struct seq tuple
|
||||
tuple_struct map struct enum identifier ignored_any
|
||||
}
|
||||
|
||||
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
@ -240,8 +240,8 @@ where
|
||||
|
||||
forward_to_deserialize_any! {
|
||||
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
|
||||
tuple tuple_struct map struct identifier ignored_any
|
||||
byte_buf option unit unit_struct newtype_struct seq tuple tuple_struct
|
||||
map struct identifier ignored_any
|
||||
}
|
||||
|
||||
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
|
||||
@ -333,8 +333,8 @@ where
|
||||
|
||||
forward_to_deserialize_any! {
|
||||
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
|
||||
tuple tuple_struct map struct identifier ignored_any
|
||||
byte_buf option unit unit_struct newtype_struct seq tuple tuple_struct
|
||||
map struct identifier ignored_any
|
||||
}
|
||||
}
|
||||
|
||||
@ -408,8 +408,8 @@ where
|
||||
|
||||
forward_to_deserialize_any! {
|
||||
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
|
||||
tuple tuple_struct map struct identifier ignored_any
|
||||
byte_buf option unit unit_struct newtype_struct seq tuple tuple_struct
|
||||
map struct identifier ignored_any
|
||||
}
|
||||
}
|
||||
|
||||
@ -487,8 +487,8 @@ where
|
||||
|
||||
forward_to_deserialize_any! {
|
||||
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
|
||||
tuple tuple_struct map struct identifier ignored_any
|
||||
byte_buf option unit unit_struct newtype_struct seq tuple tuple_struct
|
||||
map struct identifier ignored_any
|
||||
}
|
||||
}
|
||||
|
||||
@ -573,8 +573,8 @@ where
|
||||
|
||||
forward_to_deserialize_any! {
|
||||
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
|
||||
tuple tuple_struct map struct enum identifier ignored_any
|
||||
byte_buf option unit unit_struct newtype_struct seq tuple tuple_struct
|
||||
map struct enum identifier ignored_any
|
||||
}
|
||||
}
|
||||
|
||||
@ -687,8 +687,8 @@ where
|
||||
|
||||
forward_to_deserialize_any! {
|
||||
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
|
||||
tuple tuple_struct map struct enum identifier ignored_any
|
||||
byte_buf option unit unit_struct newtype_struct seq tuple tuple_struct
|
||||
map struct enum identifier ignored_any
|
||||
}
|
||||
}
|
||||
|
||||
@ -791,7 +791,7 @@ where
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
fn deserialize_seq_fixed_size<V>(
|
||||
fn deserialize_tuple<V>(
|
||||
self,
|
||||
len: usize,
|
||||
visitor: V,
|
||||
@ -805,8 +805,8 @@ where
|
||||
|
||||
forward_to_deserialize_any! {
|
||||
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
|
||||
map struct enum identifier ignored_any
|
||||
byte_buf option unit unit_struct newtype_struct tuple_struct map struct
|
||||
enum identifier ignored_any
|
||||
}
|
||||
}
|
||||
|
||||
@ -946,8 +946,8 @@ where
|
||||
|
||||
forward_to_deserialize_any! {
|
||||
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
|
||||
map struct enum identifier ignored_any
|
||||
byte_buf option unit unit_struct newtype_struct tuple_struct map struct
|
||||
enum identifier ignored_any
|
||||
}
|
||||
|
||||
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
|
||||
V: de::Visitor<'de>,
|
||||
{
|
||||
@ -1093,8 +1093,8 @@ where
|
||||
|
||||
forward_to_deserialize_any! {
|
||||
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
|
||||
tuple tuple_struct map struct enum identifier ignored_any
|
||||
byte_buf option unit unit_struct newtype_struct seq tuple tuple_struct
|
||||
map struct enum identifier ignored_any
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -44,9 +44,9 @@
|
||||
/// }
|
||||
/// #
|
||||
/// # forward_to_deserialize_any! {
|
||||
/// # u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit option
|
||||
/// # seq seq_fixed_size bytes byte_buf map unit_struct newtype_struct
|
||||
/// # tuple_struct struct identifier tuple enum ignored_any
|
||||
/// # i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes
|
||||
/// # byte_buf option unit unit_struct newtype_struct seq tuple
|
||||
/// # tuple_struct map struct enum identifier ignored_any
|
||||
/// # }
|
||||
/// # }
|
||||
/// #
|
||||
@ -78,8 +78,8 @@
|
||||
///
|
||||
/// forward_to_deserialize_any! {
|
||||
/// 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
|
||||
/// tuple tuple_struct map struct enum identifier ignored_any
|
||||
/// byte_buf option unit unit_struct newtype_struct seq tuple
|
||||
/// tuple_struct map struct enum identifier ignored_any
|
||||
/// }
|
||||
/// }
|
||||
/// #
|
||||
@ -113,8 +113,8 @@
|
||||
/// forward_to_deserialize_any! {
|
||||
/// <W: Visitor<'q>>
|
||||
/// 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
|
||||
/// tuple tuple_struct map struct enum identifier ignored_any
|
||||
/// byte_buf option unit unit_struct newtype_struct seq tuple tuple_struct
|
||||
/// map struct enum identifier ignored_any
|
||||
/// }
|
||||
/// # }
|
||||
/// #
|
||||
@ -218,9 +218,6 @@ macro_rules! forward_to_deserialize_any_helper {
|
||||
(seq<$l:tt, $v:ident>) => {
|
||||
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>) => {
|
||||
forward_to_deserialize_any_method!{deserialize_tuple<$l, $v>(len: usize)}
|
||||
};
|
||||
|
@ -49,8 +49,8 @@ where
|
||||
|
||||
forward_to_deserialize_any! {
|
||||
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
|
||||
tuple_struct map struct enum identifier ignored_any
|
||||
byte_buf unit unit_struct newtype_struct seq tuple tuple_struct map
|
||||
struct enum identifier ignored_any
|
||||
}
|
||||
}
|
||||
|
||||
@ -995,8 +995,8 @@ mod content {
|
||||
|
||||
forward_to_deserialize_any! {
|
||||
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
|
||||
struct identifier ignored_any
|
||||
byte_buf unit unit_struct seq tuple tuple_struct map struct
|
||||
identifier ignored_any
|
||||
}
|
||||
}
|
||||
|
||||
@ -1153,8 +1153,8 @@ mod content {
|
||||
|
||||
forward_to_deserialize_any! {
|
||||
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
|
||||
tuple tuple_struct map struct enum identifier ignored_any
|
||||
byte_buf option unit unit_struct newtype_struct seq tuple
|
||||
tuple_struct map struct enum identifier ignored_any
|
||||
}
|
||||
}
|
||||
|
||||
@ -1254,8 +1254,8 @@ mod content {
|
||||
|
||||
forward_to_deserialize_any! {
|
||||
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
|
||||
tuple tuple_struct map struct enum identifier ignored_any
|
||||
byte_buf option unit unit_struct newtype_struct seq tuple
|
||||
tuple_struct map struct enum identifier ignored_any
|
||||
}
|
||||
}
|
||||
|
||||
@ -1390,8 +1390,8 @@ mod content {
|
||||
|
||||
forward_to_deserialize_any! {
|
||||
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
|
||||
struct identifier ignored_any
|
||||
byte_buf unit unit_struct seq tuple tuple_struct map struct
|
||||
identifier ignored_any
|
||||
}
|
||||
}
|
||||
|
||||
@ -1545,8 +1545,8 @@ mod content {
|
||||
|
||||
forward_to_deserialize_any! {
|
||||
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
|
||||
tuple tuple_struct map struct enum identifier ignored_any
|
||||
byte_buf option unit unit_struct newtype_struct seq tuple
|
||||
tuple_struct map struct enum identifier ignored_any
|
||||
}
|
||||
}
|
||||
|
||||
@ -1647,8 +1647,8 @@ mod content {
|
||||
|
||||
forward_to_deserialize_any! {
|
||||
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
|
||||
tuple tuple_struct map struct enum identifier ignored_any
|
||||
byte_buf option unit unit_struct newtype_struct seq tuple
|
||||
tuple_struct map struct enum identifier ignored_any
|
||||
}
|
||||
}
|
||||
|
||||
@ -1804,8 +1804,8 @@ where
|
||||
|
||||
forward_to_deserialize_any! {
|
||||
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
|
||||
tuple tuple_struct map struct enum identifier ignored_any
|
||||
byte_buf option unit unit_struct newtype_struct seq tuple tuple_struct
|
||||
map struct enum identifier ignored_any
|
||||
}
|
||||
}
|
||||
|
||||
@ -1843,7 +1843,7 @@ where
|
||||
|
||||
forward_to_deserialize_any! {
|
||||
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
|
||||
tuple tuple_struct map struct enum identifier ignored_any
|
||||
byte_buf option unit unit_struct newtype_struct seq tuple tuple_struct
|
||||
map struct enum identifier ignored_any
|
||||
}
|
||||
}
|
||||
|
@ -121,9 +121,6 @@ macro_rules! __serialize_unimplemented_helper {
|
||||
type SerializeSeq = $crate::ser::Impossible<Self::Ok, Self::Error>;
|
||||
__serialize_unimplemented_method!(serialize_seq(Option<usize>) -> SerializeSeq);
|
||||
};
|
||||
(seq_fixed_size) => {
|
||||
__serialize_unimplemented_method!(serialize_seq_fixed_size(usize) -> SerializeSeq);
|
||||
};
|
||||
(tuple) => {
|
||||
type SerializeTuple = $crate::ser::Impossible<Self::Ok, Self::Error>;
|
||||
__serialize_unimplemented_method!(serialize_tuple(usize) -> SerializeTuple);
|
||||
|
@ -245,10 +245,6 @@ where
|
||||
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> {
|
||||
Err(self.bad_type(Unsupported::Tuple))
|
||||
}
|
||||
@ -484,7 +480,6 @@ mod content {
|
||||
NewtypeVariant(&'static str, u32, &'static str, Box<Content>),
|
||||
|
||||
Seq(Vec<Content>),
|
||||
SeqFixedSize(Vec<Content>),
|
||||
Tuple(Vec<Content>),
|
||||
TupleStruct(&'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)
|
||||
}
|
||||
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) => {
|
||||
use ser::SerializeTuple;
|
||||
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> {
|
||||
Ok(
|
||||
SerializeSeq {
|
||||
fixed_size: false,
|
||||
elements: Vec::with_capacity(len.unwrap_or(0)),
|
||||
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> {
|
||||
Ok(
|
||||
SerializeTuple {
|
||||
@ -828,7 +804,6 @@ mod content {
|
||||
}
|
||||
|
||||
struct SerializeSeq<E> {
|
||||
fixed_size: bool,
|
||||
elements: Vec<Content>,
|
||||
error: PhantomData<E>,
|
||||
}
|
||||
@ -850,13 +825,7 @@ mod content {
|
||||
}
|
||||
|
||||
fn end(self) -> Result<Content, E> {
|
||||
Ok(
|
||||
if self.fixed_size {
|
||||
Content::SeqFixedSize(self.elements)
|
||||
} else {
|
||||
Content::Seq(self.elements)
|
||||
},
|
||||
)
|
||||
Ok(Content::Seq(self.elements))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -8,7 +8,7 @@
|
||||
|
||||
use lib::*;
|
||||
|
||||
use ser::{Serialize, SerializeSeq, SerializeTuple, Serializer};
|
||||
use ser::{Serialize, SerializeTuple, Serializer};
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
use ser::Error;
|
||||
@ -130,7 +130,7 @@ impl<T> Serialize for [T; 0] {
|
||||
where
|
||||
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
|
||||
S: Serializer,
|
||||
{
|
||||
let mut seq = try!(serializer.serialize_seq_fixed_size($len));
|
||||
let mut seq = try!(serializer.serialize_tuple($len));
|
||||
for e in self {
|
||||
try!(seq.serialize_element(e));
|
||||
}
|
||||
|
@ -53,8 +53,7 @@ use ser::{self, Serialize, SerializeSeq, SerializeTuple, SerializeTupleStruct,
|
||||
/// # __serialize_unimplemented! {
|
||||
/// # 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
|
||||
/// # seq_fixed_size tuple tuple_struct tuple_variant map struct
|
||||
/// # struct_variant
|
||||
/// # tuple tuple_struct tuple_variant map struct struct_variant
|
||||
/// # }
|
||||
/// }
|
||||
/// #
|
||||
|
@ -239,7 +239,7 @@ pub trait Serialize {
|
||||
/// 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
|
||||
/// 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
|
||||
/// the types of the data model.
|
||||
///
|
||||
@ -248,47 +248,54 @@ pub trait Serialize {
|
||||
///
|
||||
/// The types that make up the Serde data model are:
|
||||
///
|
||||
/// - 12 primitive types:
|
||||
/// - **12 primitive types**
|
||||
/// - bool
|
||||
/// - i8, i16, i32, i64
|
||||
/// - u8, u16, u32, u64
|
||||
/// - f32, f64
|
||||
/// - char
|
||||
/// - string
|
||||
/// - byte array - [u8]
|
||||
/// - option
|
||||
/// - either none or some value
|
||||
/// - unit
|
||||
/// - unit is the type of () in Rust
|
||||
/// - unit_struct
|
||||
/// - for example `struct Unit` or `PhantomData<T>`
|
||||
/// - unit_variant
|
||||
/// - the `E::A` and `E::B` in `enum E { A, B }`
|
||||
/// - newtype_struct
|
||||
/// - for example `struct Millimeters(u8)`
|
||||
/// - newtype_variant
|
||||
/// - the `E::N` in `enum E { N(u8) }`
|
||||
/// - seq
|
||||
/// - a variably sized sequence of values, for example `Vec<T>` or
|
||||
/// `HashSet<T>`
|
||||
/// - seq_fixed_size
|
||||
/// - a statically sized sequence of values for which the size will be known
|
||||
/// at deserialization time without looking at the serialized data, for
|
||||
/// example `[u64; 10]`
|
||||
/// - tuple
|
||||
/// - for example `(u8,)` or `(String, u64, Vec<T>)`
|
||||
/// - tuple_struct
|
||||
/// - for example `struct Rgb(u8, u8, u8)`
|
||||
/// - tuple_variant
|
||||
/// - the `E::T` in `enum E { T(u8, u8) }`
|
||||
/// - map
|
||||
/// - for example `BTreeMap<K, V>`
|
||||
/// - struct
|
||||
/// - a key-value pairing in which the keys will be known at deserialization
|
||||
/// time without looking at the serialized data, for example `struct S { r:
|
||||
/// u8, g: u8, b: u8 }`
|
||||
/// - struct_variant
|
||||
/// - the `E::S` in `enum E { S { r: u8, g: u8, b: u8 } }`
|
||||
/// - **string**
|
||||
/// - UTF-8 bytes with a length and no null terminator.
|
||||
/// - When serializing, all strings are handled equally. When deserializing,
|
||||
/// there are three flavors of strings: transient, owned, and borrowed.
|
||||
/// - **byte array** - [u8]
|
||||
/// - Similar to strings, during deserialization byte arrays can be transient,
|
||||
/// owned, or borrowed.
|
||||
/// - **option**
|
||||
/// - Either none or some value.
|
||||
/// - **unit**
|
||||
/// - The type of `()` in Rust. It represents an anonymous value containing no
|
||||
/// data.
|
||||
/// - **unit_struct**
|
||||
/// - For example `struct Unit` or `PhantomData<T>`. It represents a named value
|
||||
/// containing no data.
|
||||
/// - **unit_variant**
|
||||
/// - For example the `E::A` and `E::B` in `enum E { A, B }`.
|
||||
/// - **newtype_struct**
|
||||
/// - For example `struct Millimeters(u8)`.
|
||||
/// - **newtype_variant**
|
||||
/// - For example the `E::N` in `enum E { N(u8) }`.
|
||||
/// - **seq**
|
||||
/// - A variably sized heterogeneous sequence of values, for example `Vec<T>` or
|
||||
/// `HashSet<T>`. When serializing, the length may or may not be known before
|
||||
/// iterating through all the data. When deserializing, the length is determined
|
||||
/// by looking at the serialized data.
|
||||
/// - **tuple**
|
||||
/// - A statically sized heterogeneous sequence of values for which the length
|
||||
/// will be known at deserialization time without looking at the serialized
|
||||
/// data, for example `(u8,)` or `(String, u64, Vec<T>)` or `[u64; 10]`.
|
||||
/// - **tuple_struct**
|
||||
/// - A named tuple, for example `struct Rgb(u8, u8, u8)`.
|
||||
/// - **tuple_variant**
|
||||
/// - For example the `E::T` in `enum E { T(u8, 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
|
||||
/// 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.
|
||||
type Error: Error;
|
||||
|
||||
/// Type returned from [`serialize_seq`] and [`serialize_seq_fixed_size`]
|
||||
/// for serializing the content of the sequence.
|
||||
/// Type returned from [`serialize_seq`] for serializing the content of the
|
||||
/// sequence.
|
||||
///
|
||||
/// [`serialize_seq`]: #tymethod.serialize_seq
|
||||
/// [`serialize_seq_fixed_size`]: #tymethod.serialize_seq_fixed_size
|
||||
type SerializeSeq: SerializeSeq<Ok = Self::Ok, Error = Self::Error>;
|
||||
|
||||
/// Type returned from [`serialize_tuple`] for serializing the content of
|
||||
@ -702,8 +708,7 @@ pub trait Serializer: Sized {
|
||||
/// # __serialize_unimplemented! {
|
||||
/// # bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str none some
|
||||
/// # unit unit_struct unit_variant newtype_struct newtype_variant
|
||||
/// # seq seq_fixed_size tuple tuple_struct tuple_variant map struct
|
||||
/// # struct_variant
|
||||
/// # seq tuple tuple_struct tuple_variant map struct struct_variant
|
||||
/// # }
|
||||
/// # }
|
||||
/// #
|
||||
@ -966,29 +971,6 @@ pub trait Serializer: Sized {
|
||||
/// then a call to `end`.
|
||||
///
|
||||
/// ```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};
|
||||
///
|
||||
/// # 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>;
|
||||
|
||||
/// Begin to serialize a tuple struct like `struct Rgb(u8, u8, u8)`. This
|
||||
@ -1359,34 +1360,26 @@ pub trait Serializer: Sized {
|
||||
T: Display;
|
||||
}
|
||||
|
||||
/// Returned from `Serializer::serialize_seq` and
|
||||
/// `Serializer::serialize_seq_fixed_size`.
|
||||
/// Returned from `Serializer::serialize_seq`.
|
||||
///
|
||||
/// ```rust
|
||||
/// # use std::marker::PhantomData;
|
||||
/// #
|
||||
/// # macro_rules! unimplemented_vec {
|
||||
/// # ($name:ident) => {
|
||||
/// # struct $name<T>(PhantomData<T>);
|
||||
/// # struct Vec<T>(PhantomData<T>);
|
||||
/// #
|
||||
/// # impl<T> $name<T> {
|
||||
/// # fn len(&self) -> usize {
|
||||
/// # 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!()
|
||||
/// # }
|
||||
/// # }
|
||||
/// # impl<T> Vec<T> {
|
||||
/// # fn len(&self) -> usize {
|
||||
/// # unimplemented!()
|
||||
/// # }
|
||||
/// # }
|
||||
/// #
|
||||
/// # unimplemented_vec!(Vec);
|
||||
/// # unimplemented_vec!(Array);
|
||||
/// # impl<'a, T> IntoIterator for &'a Vec<T> {
|
||||
/// # type Item = &'a T;
|
||||
/// # type IntoIter = Box<Iterator<Item = &'a T>>;
|
||||
/// # fn into_iter(self) -> Self::IntoIter {
|
||||
/// # unimplemented!()
|
||||
/// # }
|
||||
/// # }
|
||||
/// #
|
||||
/// use serde::ser::{Serialize, Serializer, SerializeSeq};
|
||||
///
|
||||
@ -1403,26 +1396,6 @@ pub trait Serializer: Sized {
|
||||
/// 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 {
|
||||
/// 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 {
|
||||
/// Must match the `Ok` type of our `Serializer`.
|
||||
type Ok;
|
||||
|
@ -132,7 +132,6 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
|
||||
Token::UnitStruct(_name) => visitor.visit_unit(),
|
||||
Token::NewtypeStruct(_name) => visitor.visit_newtype_struct(self),
|
||||
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::TupleStruct(_, len) => self.visit_seq(Some(len), Token::TupleStructEnd, 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>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
@ -290,10 +275,6 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
|
||||
self.next_token();
|
||||
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(_)) => {
|
||||
self.next_token();
|
||||
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.visit_seq(Some(len), Token::SeqEnd, visitor)
|
||||
}
|
||||
Some(&Token::SeqFixedSize(_)) => {
|
||||
self.next_token();
|
||||
self.visit_seq(Some(len), Token::SeqEnd, visitor)
|
||||
}
|
||||
Some(&Token::Tuple(_)) => {
|
||||
self.next_token();
|
||||
self.visit_seq(Some(len), Token::TupleEnd, visitor)
|
||||
@ -656,7 +633,7 @@ impl<'de> de::Deserializer<'de> for BytesDeserializer {
|
||||
|
||||
forward_to_deserialize_any! {
|
||||
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
|
||||
tuple tuple_struct map struct enum identifier ignored_any
|
||||
byte_buf option unit unit_struct newtype_struct seq tuple tuple_struct
|
||||
map struct enum identifier ignored_any
|
||||
}
|
||||
}
|
||||
|
@ -212,11 +212,6 @@ impl<'s, 'a> ser::Serializer for &'s mut Serializer<'a> {
|
||||
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> {
|
||||
assert_next_token!(self, Tuple(len));
|
||||
Ok(self)
|
||||
|
@ -103,12 +103,6 @@ pub enum Token {
|
||||
/// header is a list of elements, followed by `SeqEnd`.
|
||||
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.
|
||||
SeqEnd,
|
||||
|
||||
|
@ -360,8 +360,8 @@ declare_tests! {
|
||||
Token::SeqEnd,
|
||||
],
|
||||
[0; 0] => &[
|
||||
Token::SeqFixedSize(0),
|
||||
Token::SeqEnd,
|
||||
Token::Tuple(0),
|
||||
Token::TupleEnd,
|
||||
],
|
||||
([0; 0], [1], [2, 3]) => &[
|
||||
Token::Seq(Some(3)),
|
||||
@ -379,19 +379,19 @@ declare_tests! {
|
||||
Token::SeqEnd,
|
||||
],
|
||||
([0; 0], [1], [2, 3]) => &[
|
||||
Token::SeqFixedSize(3),
|
||||
Token::SeqFixedSize(0),
|
||||
Token::SeqEnd,
|
||||
Token::Tuple(3),
|
||||
Token::Tuple(0),
|
||||
Token::TupleEnd,
|
||||
|
||||
Token::SeqFixedSize(1),
|
||||
Token::Tuple(1),
|
||||
Token::I32(1),
|
||||
Token::SeqEnd,
|
||||
Token::TupleEnd,
|
||||
|
||||
Token::SeqFixedSize(2),
|
||||
Token::Tuple(2),
|
||||
Token::I32(2),
|
||||
Token::I32(3),
|
||||
Token::SeqEnd,
|
||||
Token::SeqEnd,
|
||||
Token::TupleEnd,
|
||||
Token::TupleEnd,
|
||||
],
|
||||
[0; 0] => &[
|
||||
Token::TupleStruct("Anything", 0),
|
||||
|
@ -138,15 +138,15 @@ declare_tests! {
|
||||
}
|
||||
test_array {
|
||||
[0; 0] => &[
|
||||
Token::SeqFixedSize(0),
|
||||
Token::SeqEnd,
|
||||
Token::Tuple(0),
|
||||
Token::TupleEnd,
|
||||
],
|
||||
[1, 2, 3] => &[
|
||||
Token::SeqFixedSize(3),
|
||||
Token::Tuple(3),
|
||||
Token::I32(1),
|
||||
Token::I32(2),
|
||||
Token::I32(3),
|
||||
Token::SeqEnd,
|
||||
Token::TupleEnd,
|
||||
],
|
||||
}
|
||||
test_vec {
|
||||
@ -301,11 +301,11 @@ declare_tests! {
|
||||
}
|
||||
test_boxed_slice {
|
||||
Box::new([0, 1, 2]) => &[
|
||||
Token::SeqFixedSize(3),
|
||||
Token::Tuple(3),
|
||||
Token::I32(0),
|
||||
Token::I32(1),
|
||||
Token::I32(2),
|
||||
Token::SeqEnd,
|
||||
Token::TupleEnd,
|
||||
],
|
||||
}
|
||||
test_duration {
|
||||
|
Loading…
Reference in New Issue
Block a user