From 86deb8db795d98a5625cb4d8a4df8074dd5b56e5 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Mon, 17 Apr 2017 12:07:49 -0700 Subject: [PATCH] Remove seq_fixed_size in favor of tuple --- serde/src/de/impls.rs | 4 +- serde/src/de/mod.rs | 91 +++++++------- serde/src/de/value.rs | 48 ++++---- serde/src/macros.rs | 17 ++- serde/src/private/de.rs | 36 +++--- serde/src/private/macros.rs | 3 - serde/src/private/ser.rs | 33 +---- serde/src/ser/impls.rs | 6 +- serde/src/ser/impossible.rs | 3 +- serde/src/ser/mod.rs | 225 +++++++++++++++++++---------------- serde_test/src/de.rs | 27 +---- serde_test/src/ser.rs | 5 - serde_test/src/token.rs | 6 - test_suite/tests/test_de.rs | 20 ++-- test_suite/tests/test_ser.rs | 12 +- 15 files changed, 238 insertions(+), 298 deletions(-) diff --git a/serde/src/de/impls.rs b/serde/src/de/impls.rs index 6e4c6502..d7e6b7f8 100644 --- a/serde/src/de/impls.rs +++ b/serde/src/de/impls.rs @@ -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()) } } )+ diff --git a/serde/src/de/mod.rs b/serde/src/de/mod.rs index 245aa6ea..3c9afa6f 100644 --- a/serde/src/de/mod.rs +++ b/serde/src/de/mod.rs @@ -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` -/// - 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` or -/// `HashSet` -/// - 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)` -/// - tuple_struct -/// - for example `struct Rgb(u8, u8, u8)` -/// - tuple_variant -/// - the `E::T` in `enum E { T(u8, u8) }` -/// - map -/// - for example `BTreeMap` -/// - 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`. 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` or +/// `HashSet`. 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)` 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`. +/// - **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( - self, - len: usize, - visitor: V, - ) -> Result - where - V: Visitor<'de>; - - /// Hint that the `Deserialize` type is expecting a tuple value with a - /// particular number of elements. fn deserialize_tuple(self, len: usize, visitor: V) -> Result where V: Visitor<'de>; diff --git a/serde/src/de/value.rs b/serde/src/de/value.rs index 97c55881..d8b0b434 100644 --- a/serde/src/de/value.rs +++ b/serde/src/de/value.rs @@ -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(self, visitor: V) -> Result @@ -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(self, visitor: V) -> Result @@ -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(self, visitor: V) -> Result @@ -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( + fn deserialize_tuple( 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(self, visitor: V) -> Result @@ -973,7 +973,7 @@ where } } - fn deserialize_seq_fixed_size(self, len: usize, visitor: V) -> Result + fn deserialize_tuple(self, len: usize, visitor: V) -> Result 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 } } diff --git a/serde/src/macros.rs b/serde/src/macros.rs index 0d654583..2a3d0872 100644 --- a/serde/src/macros.rs +++ b/serde/src/macros.rs @@ -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! { /// > /// 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)} }; diff --git a/serde/src/private/de.rs b/serde/src/private/de.rs index 75111321..6666ada2 100644 --- a/serde/src/private/de.rs +++ b/serde/src/private/de.rs @@ -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 } } diff --git a/serde/src/private/macros.rs b/serde/src/private/macros.rs index 0253e6e0..04676138 100644 --- a/serde/src/private/macros.rs +++ b/serde/src/private/macros.rs @@ -121,9 +121,6 @@ macro_rules! __serialize_unimplemented_helper { type SerializeSeq = $crate::ser::Impossible; __serialize_unimplemented_method!(serialize_seq(Option) -> SerializeSeq); }; - (seq_fixed_size) => { - __serialize_unimplemented_method!(serialize_seq_fixed_size(usize) -> SerializeSeq); - }; (tuple) => { type SerializeTuple = $crate::ser::Impossible; __serialize_unimplemented_method!(serialize_tuple(usize) -> SerializeTuple); diff --git a/serde/src/private/ser.rs b/serde/src/private/ser.rs index 994875d4..8cb26d95 100644 --- a/serde/src/private/ser.rs +++ b/serde/src/private/ser.rs @@ -245,10 +245,6 @@ where Err(self.bad_type(Unsupported::Sequence)) } - fn serialize_seq_fixed_size(self, _: usize) -> Result { - Err(self.bad_type(Unsupported::Sequence)) - } - fn serialize_tuple(self, _: usize) -> Result { Err(self.bad_type(Unsupported::Tuple)) } @@ -484,7 +480,6 @@ mod content { NewtypeVariant(&'static str, u32, &'static str, Box), Seq(Vec), - SeqFixedSize(Vec), Tuple(Vec), TupleStruct(&'static str, Vec), TupleVariant(&'static str, u32, &'static str, Vec), @@ -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) -> Result { Ok( SerializeSeq { - fixed_size: false, elements: Vec::with_capacity(len.unwrap_or(0)), error: PhantomData, }, ) } - fn serialize_seq_fixed_size(self, size: usize) -> Result { - Ok( - SerializeSeq { - fixed_size: true, - elements: Vec::with_capacity(size), - error: PhantomData, - }, - ) - } - fn serialize_tuple(self, len: usize) -> Result { Ok( SerializeTuple { @@ -828,7 +804,6 @@ mod content { } struct SerializeSeq { - fixed_size: bool, elements: Vec, error: PhantomData, } @@ -850,13 +825,7 @@ mod content { } fn end(self) -> Result { - Ok( - if self.fixed_size { - Content::SeqFixedSize(self.elements) - } else { - Content::Seq(self.elements) - }, - ) + Ok(Content::Seq(self.elements)) } } diff --git a/serde/src/ser/impls.rs b/serde/src/ser/impls.rs index b127773e..dad456f1 100644 --- a/serde/src/ser/impls.rs +++ b/serde/src/ser/impls.rs @@ -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 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)); } diff --git a/serde/src/ser/impossible.rs b/serde/src/ser/impossible.rs index 592047f4..f72748f6 100644 --- a/serde/src/ser/impossible.rs +++ b/serde/src/ser/impossible.rs @@ -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 /// # } /// } /// # diff --git a/serde/src/ser/mod.rs b/serde/src/ser/mod.rs index 0aa3c9a7..67b60fcc 100644 --- a/serde/src/ser/mod.rs +++ b/serde/src/ser/mod.rs @@ -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` -/// - 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` or -/// `HashSet` -/// - 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)` -/// - tuple_struct -/// - for example `struct Rgb(u8, u8, u8)` -/// - tuple_variant -/// - the `E::T` in `enum E { T(u8, u8) }` -/// - map -/// - for example `BTreeMap` -/// - 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`. 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` or +/// `HashSet`. 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)` 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`. +/// - **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; /// 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(&self, serializer: S) -> Result - /// 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; - - /// 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(&self, serializer: S) -> Result + /// 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; /// 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(PhantomData); +/// # struct Vec(PhantomData); /// # -/// # impl $name { -/// # fn len(&self) -> usize { -/// # unimplemented!() -/// # } -/// # } -/// # -/// # impl<'a, T> IntoIterator for &'a $name { -/// # type Item = &'a T; -/// # type IntoIter = Box>; -/// # fn into_iter(self) -> Self::IntoIter { -/// # unimplemented!() -/// # } -/// # } +/// # impl Vec { +/// # fn len(&self) -> usize { +/// # unimplemented!() /// # } /// # } /// # -/// # unimplemented_vec!(Vec); -/// # unimplemented_vec!(Array); +/// # impl<'a, T> IntoIterator for &'a Vec { +/// # type Item = &'a T; +/// # type IntoIter = Box>; +/// # 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 Serialize for [T; 16] -/// # {} -/// # } -/// # -/// # impl Serialize for Array -/// where T: Serialize -/// { -/// fn serialize(&self, serializer: S) -> Result -/// 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(PhantomData); +/// # +/// # impl Array { +/// # fn len(&self) -> usize { +/// # unimplemented!() +/// # } +/// # } +/// # +/// # impl<'a, T> IntoIterator for &'a Array { +/// # type Item = &'a T; +/// # type IntoIter = Box>; +/// # fn into_iter(self) -> Self::IntoIter { +/// # unimplemented!() +/// # } +/// # } +/// # +/// use serde::ser::{Serialize, Serializer, SerializeTuple}; +/// +/// # mod fool { +/// # trait Serialize {} +/// impl Serialize for [T; 16] +/// # {} +/// # } +/// # +/// # impl Serialize for Array +/// where T: Serialize +/// { +/// fn serialize(&self, serializer: S) -> Result +/// 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; diff --git a/serde_test/src/de.rs b/serde_test/src/de.rs index 9b5fc85a..bb201073 100644 --- a/serde_test/src/de.rs +++ b/serde_test/src/de.rs @@ -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(self, len: usize, visitor: V) -> Result - 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(self, len: usize, visitor: V) -> Result 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 } } diff --git a/serde_test/src/ser.rs b/serde_test/src/ser.rs index b7b80d3b..5c90fee4 100644 --- a/serde_test/src/ser.rs +++ b/serde_test/src/ser.rs @@ -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 { - assert_next_token!(self, SeqFixedSize(len)); - Ok(self) - } - fn serialize_tuple(self, len: usize) -> Result { assert_next_token!(self, Tuple(len)); Ok(self) diff --git a/serde_test/src/token.rs b/serde_test/src/token.rs index c640e48f..07bbc0b9 100644 --- a/serde_test/src/token.rs +++ b/serde_test/src/token.rs @@ -103,12 +103,6 @@ pub enum Token { /// header is a list of elements, followed by `SeqEnd`. Seq(Option), - /// 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, diff --git a/test_suite/tests/test_de.rs b/test_suite/tests/test_de.rs index 4a0ebb10..e76fc6a9 100644 --- a/test_suite/tests/test_de.rs +++ b/test_suite/tests/test_de.rs @@ -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), diff --git a/test_suite/tests/test_ser.rs b/test_suite/tests/test_ser.rs index 28472ce3..2de5db14 100644 --- a/test_suite/tests/test_ser.rs +++ b/test_suite/tests/test_ser.rs @@ -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 {