stateful map/seq serialization
This commit is contained in:
parent
60938913b2
commit
16ba32dbe1
@ -152,11 +152,11 @@ impl<T> Serialize for [T]
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
try!(serializer.serialize_seq(Some(self.len())));
|
||||
let state = try!(serializer.serialize_seq(Some(self.len())));
|
||||
for e in self.iter() {
|
||||
try!(serializer.serialize_seq_elt(e));
|
||||
}
|
||||
serializer.serialize_seq_end(Some(self.len()))
|
||||
serializer.serialize_seq_end(Some(self.len()), state)
|
||||
}
|
||||
}
|
||||
|
||||
@ -169,11 +169,11 @@ macro_rules! array_impls {
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
try!(serializer.serialize_fixed_size_array($len));
|
||||
let state = try!(serializer.serialize_fixed_size_array($len));
|
||||
for e in self.iter() {
|
||||
try!(serializer.serialize_seq_elt(e));
|
||||
}
|
||||
serializer.serialize_seq_end(Some(self.len()))
|
||||
serializer.serialize_seq_end(Some(self.len()), state)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -223,11 +223,11 @@ impl<T> Serialize for BinaryHeap<T>
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
try!(serializer.serialize_seq(Some(self.len())));
|
||||
let state = try!(serializer.serialize_seq(Some(self.len())));
|
||||
for e in self.iter() {
|
||||
try!(serializer.serialize_seq_elt(e));
|
||||
}
|
||||
serializer.serialize_seq_end(Some(self.len()))
|
||||
serializer.serialize_seq_end(Some(self.len()), state)
|
||||
}
|
||||
}
|
||||
|
||||
@ -239,11 +239,11 @@ impl<T> Serialize for BTreeSet<T>
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
try!(serializer.serialize_seq(Some(self.len())));
|
||||
let state = try!(serializer.serialize_seq(Some(self.len())));
|
||||
for e in self.iter() {
|
||||
try!(serializer.serialize_seq_elt(e));
|
||||
}
|
||||
serializer.serialize_seq_end(Some(self.len()))
|
||||
serializer.serialize_seq_end(Some(self.len()), state)
|
||||
}
|
||||
}
|
||||
|
||||
@ -272,11 +272,11 @@ impl<T, H> Serialize for HashSet<T, H>
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
try!(serializer.serialize_seq(Some(self.len())));
|
||||
let state = try!(serializer.serialize_seq(Some(self.len())));
|
||||
for e in self.iter() {
|
||||
try!(serializer.serialize_seq_elt(e));
|
||||
}
|
||||
serializer.serialize_seq_end(Some(self.len()))
|
||||
serializer.serialize_seq_end(Some(self.len()), state)
|
||||
}
|
||||
}
|
||||
|
||||
@ -288,11 +288,11 @@ impl<T> Serialize for LinkedList<T>
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
try!(serializer.serialize_seq(Some(self.len())));
|
||||
let state = try!(serializer.serialize_seq(Some(self.len())));
|
||||
for e in self.iter() {
|
||||
try!(serializer.serialize_seq_elt(e));
|
||||
}
|
||||
serializer.serialize_seq_end(Some(self.len()))
|
||||
serializer.serialize_seq_end(Some(self.len()), state)
|
||||
}
|
||||
}
|
||||
|
||||
@ -329,11 +329,11 @@ impl<T> Serialize for VecDeque<T> where T: Serialize {
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
try!(serializer.serialize_seq(Some(self.len())));
|
||||
let state = try!(serializer.serialize_seq(Some(self.len())));
|
||||
for e in self.iter() {
|
||||
try!(serializer.serialize_seq_elt(e));
|
||||
}
|
||||
serializer.serialize_seq_end(Some(self.len()))
|
||||
serializer.serialize_seq_end(Some(self.len()), state)
|
||||
}
|
||||
}
|
||||
|
||||
@ -369,11 +369,11 @@ macro_rules! tuple_impls {
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
try!(serializer.serialize_tuple($len));
|
||||
let state = try!(serializer.serialize_tuple($len));
|
||||
$(
|
||||
try!(serializer.serialize_tuple_elt(&e!(self.$idx)));
|
||||
)+
|
||||
serializer.serialize_tuple_end($len)
|
||||
serializer.serialize_tuple_end($len, state)
|
||||
}
|
||||
}
|
||||
)+
|
||||
@ -562,11 +562,11 @@ impl<K, V> Serialize for BTreeMap<K, V>
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
try!(serializer.serialize_map(Some(self.len())));
|
||||
let state = try!(serializer.serialize_map(Some(self.len())));
|
||||
for (k, v) in self.iter() {
|
||||
try!(serializer.serialize_map_elt(k, v));
|
||||
}
|
||||
serializer.serialize_map_end(Some(self.len()))
|
||||
serializer.serialize_map_end(Some(self.len()), state)
|
||||
}
|
||||
}
|
||||
|
||||
@ -580,11 +580,11 @@ impl<K, V, H> Serialize for HashMap<K, V, H>
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
try!(serializer.serialize_map(Some(self.len())));
|
||||
let state = try!(serializer.serialize_map(Some(self.len())));
|
||||
for (k, v) in self.iter() {
|
||||
try!(serializer.serialize_map_elt(k, v));
|
||||
}
|
||||
serializer.serialize_map_end(Some(self.len()))
|
||||
serializer.serialize_map_end(Some(self.len()), state)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -44,6 +44,12 @@ pub trait Serialize {
|
||||
pub trait Serializer {
|
||||
/// The error type that can be returned if some error occurs during serialization.
|
||||
type Error: Error;
|
||||
/// A state object that is returned from `serialize_seq` and needs to be re-inserted into
|
||||
/// `serialize_seq_end` when the serialization of the sequence is done
|
||||
type SeqState;
|
||||
/// A state object that is returned from `serialize_map` and needs to be re-inserted into
|
||||
/// `serialize_map_end` when the serialization of the map is done
|
||||
type MapState;
|
||||
|
||||
/// Serializes a `bool` value.
|
||||
fn serialize_bool(&mut self, v: bool) -> Result<(), Self::Error>;
|
||||
@ -136,11 +142,11 @@ pub trait Serializer {
|
||||
/// byte slices separately from generic arrays. By default it serializes as a regular array.
|
||||
#[inline]
|
||||
fn serialize_bytes(&mut self, value: &[u8]) -> Result<(), Self::Error> {
|
||||
try!(self.serialize_seq(Some(value.len())));
|
||||
let state = try!(self.serialize_seq(Some(value.len())));
|
||||
for b in value.iter() {
|
||||
try!(self.serialize_seq_elt(b));
|
||||
}
|
||||
self.serialize_seq_end(Some(value.len()))
|
||||
self.serialize_seq_end(Some(value.len()), state)
|
||||
}
|
||||
|
||||
/// Serializes a `()` value.
|
||||
@ -174,9 +180,9 @@ pub trait Serializer {
|
||||
value: T) -> Result<(), Self::Error>
|
||||
where T: Serialize,
|
||||
{
|
||||
try!(self.serialize_tuple_struct(name, 1));
|
||||
let state = try!(self.serialize_tuple_struct(name, 1));
|
||||
try!(self.serialize_tuple_struct_elt(value));
|
||||
self.serialize_tuple_struct_end(name, 1)
|
||||
self.serialize_tuple_struct_end(name, 1, state)
|
||||
}
|
||||
|
||||
/// Allows a variant with a single item to be more efficiently
|
||||
@ -190,9 +196,9 @@ pub trait Serializer {
|
||||
value: T) -> Result<(), Self::Error>
|
||||
where T: Serialize,
|
||||
{
|
||||
try!(self.serialize_tuple_variant(name, variant_index, variant, 1));
|
||||
let state = try!(self.serialize_tuple_variant(name, variant_index, variant, 1));
|
||||
try!(self.serialize_tuple_variant_elt(value));
|
||||
self.serialize_tuple_variant_end(name, variant_index, variant, 1)
|
||||
self.serialize_tuple_variant_end(name, variant_index, variant, 1, state)
|
||||
}
|
||||
|
||||
/// Serializes a `None` value..serialize
|
||||
@ -206,20 +212,20 @@ pub trait Serializer {
|
||||
///
|
||||
/// Callees of this method need to construct a `SeqVisitor`, which iterates through each item
|
||||
/// in the sequence.
|
||||
fn serialize_seq(&mut self, len: Option<usize>) -> Result<(), Self::Error>;
|
||||
fn serialize_seq(&mut self, len: Option<usize>) -> Result<Self::SeqState, Self::Error>;
|
||||
|
||||
/// Serializes a sequence element.
|
||||
fn serialize_seq_elt<T>(&mut self, value: T) -> Result<(), Self::Error>
|
||||
where T: Serialize;
|
||||
|
||||
/// Finish serializing a sequence.
|
||||
fn serialize_seq_end(&mut self, len: Option<usize>) -> Result<(), Self::Error>;
|
||||
fn serialize_seq_end(&mut self, len: Option<usize>, state: Self::SeqState) -> Result<(), Self::Error>;
|
||||
|
||||
/// Serializes a tuple.
|
||||
///
|
||||
/// By default this serializes a tuple as a sequence.
|
||||
#[inline]
|
||||
fn serialize_tuple(&mut self, len: usize) -> Result<(), Self::Error>
|
||||
fn serialize_tuple(&mut self, len: usize) -> Result<Self::SeqState, Self::Error>
|
||||
{
|
||||
self.serialize_seq(Some(len))
|
||||
}
|
||||
@ -237,15 +243,15 @@ pub trait Serializer {
|
||||
///
|
||||
/// By default, tuples are serialized as a sequence.
|
||||
#[inline]
|
||||
fn serialize_tuple_end(&mut self, len: usize) -> Result<(), Self::Error> {
|
||||
self.serialize_seq_end(Some(len))
|
||||
fn serialize_tuple_end(&mut self, len: usize, state: Self::SeqState) -> Result<(), Self::Error> {
|
||||
self.serialize_seq_end(Some(len), state)
|
||||
}
|
||||
|
||||
/// Serializes a fixed-size array.
|
||||
///
|
||||
/// By default this serializes an array as a sequence.
|
||||
#[inline]
|
||||
fn serialize_fixed_size_array(&mut self, size: usize) -> Result<(), Self::Error>
|
||||
fn serialize_fixed_size_array(&mut self, size: usize) -> Result<Self::SeqState, Self::Error>
|
||||
{
|
||||
self.serialize_seq(Some(size))
|
||||
}
|
||||
@ -257,7 +263,7 @@ pub trait Serializer {
|
||||
fn serialize_tuple_struct(&mut self,
|
||||
_name: &'static str,
|
||||
len: usize,
|
||||
) -> Result<(), Self::Error>
|
||||
) -> Result<Self::SeqState, Self::Error>
|
||||
{
|
||||
self.serialize_tuple(len)
|
||||
}
|
||||
@ -279,8 +285,9 @@ pub trait Serializer {
|
||||
fn serialize_tuple_struct_end(&mut self,
|
||||
_name: &'static str,
|
||||
len: usize,
|
||||
state: Self::SeqState,
|
||||
) -> Result<(), Self::Error> {
|
||||
self.serialize_tuple_end(len)
|
||||
self.serialize_tuple_end(len, state)
|
||||
}
|
||||
|
||||
/// Serializes a tuple variant.
|
||||
@ -292,7 +299,7 @@ pub trait Serializer {
|
||||
_variant_index: usize,
|
||||
variant: &'static str,
|
||||
len: usize,
|
||||
) -> Result<(), Self::Error>
|
||||
) -> Result<Self::SeqState, Self::Error>
|
||||
{
|
||||
self.serialize_tuple_struct(variant, len)
|
||||
}
|
||||
@ -316,18 +323,19 @@ pub trait Serializer {
|
||||
_variant_index: usize,
|
||||
variant: &'static str,
|
||||
len: usize,
|
||||
state: Self::SeqState,
|
||||
) -> Result<(), Self::Error> {
|
||||
self.serialize_tuple_struct_end(variant, len)
|
||||
self.serialize_tuple_struct_end(variant, len, state)
|
||||
}
|
||||
|
||||
/// Serialize a map.
|
||||
fn serialize_map(&mut self, len: Option<usize>) -> Result<(), Self::Error>;
|
||||
fn serialize_map(&mut self, len: Option<usize>) -> Result<Self::MapState, Self::Error>;
|
||||
|
||||
/// Serialize a map element
|
||||
fn serialize_map_elt<K, V>(&mut self, key: K, value: V) -> Result<(), Self::Error> where K: Serialize, V: Serialize;
|
||||
|
||||
/// Finishes serializing a map
|
||||
fn serialize_map_end(&mut self, len: Option<usize>) -> Result<(), Self::Error>;
|
||||
fn serialize_map_end(&mut self, len: Option<usize>, state: Self::MapState) -> Result<(), Self::Error>;
|
||||
|
||||
/// Serializes a struct.
|
||||
///
|
||||
@ -336,7 +344,7 @@ pub trait Serializer {
|
||||
fn serialize_struct(&mut self,
|
||||
_name: &'static str,
|
||||
len: usize,
|
||||
) -> Result<(), Self::Error>
|
||||
) -> Result<Self::MapState, Self::Error>
|
||||
{
|
||||
self.serialize_map(Some(len))
|
||||
}
|
||||
@ -354,8 +362,9 @@ pub trait Serializer {
|
||||
fn serialize_struct_end(&mut self,
|
||||
_name: &'static str,
|
||||
len: usize,
|
||||
state: Self::MapState,
|
||||
) -> Result<(), Self::Error> {
|
||||
self.serialize_map_end(Some(len))
|
||||
self.serialize_map_end(Some(len), state)
|
||||
}
|
||||
|
||||
/// Serializes a struct variant.
|
||||
@ -367,7 +376,7 @@ pub trait Serializer {
|
||||
_variant_index: usize,
|
||||
variant: &'static str,
|
||||
len: usize,
|
||||
) -> Result<(), Self::Error>
|
||||
) -> Result<Self::MapState, Self::Error>
|
||||
{
|
||||
self.serialize_struct(variant, len)
|
||||
}
|
||||
@ -387,7 +396,8 @@ pub trait Serializer {
|
||||
_variant_index: usize,
|
||||
variant: &'static str,
|
||||
len: usize,
|
||||
state: Self::MapState,
|
||||
) -> Result<(), Self::Error> {
|
||||
self.serialize_struct_end(variant, len)
|
||||
self.serialize_struct_end(variant, len, state)
|
||||
}
|
||||
}
|
||||
|
@ -230,9 +230,9 @@ fn serialize_tuple_struct(
|
||||
let len = serialize_stmts.len();
|
||||
|
||||
quote_expr!(cx, {
|
||||
try!(_serializer.serialize_tuple_struct($type_name, $len));
|
||||
let state = try!(_serializer.serialize_tuple_struct($type_name, $len));
|
||||
$serialize_stmts
|
||||
_serializer.serialize_tuple_struct_end($type_name, $len)
|
||||
_serializer.serialize_tuple_struct_end($type_name, $len, state)
|
||||
})
|
||||
}
|
||||
|
||||
@ -270,9 +270,9 @@ fn serialize_struct(
|
||||
|
||||
quote_expr!(cx, {
|
||||
let len = $len;
|
||||
try!(_serializer.serialize_struct($type_name, len));
|
||||
let state = try!(_serializer.serialize_struct($type_name, len));
|
||||
$serialize_fields
|
||||
_serializer.serialize_struct_end($type_name, len)
|
||||
_serializer.serialize_struct_end($type_name, len, state)
|
||||
})
|
||||
}
|
||||
|
||||
@ -464,9 +464,9 @@ fn serialize_tuple_variant(
|
||||
let len = serialize_stmts.len();
|
||||
|
||||
quote_expr!(cx, {
|
||||
try!(_serializer.serialize_tuple_variant($type_name, $variant_index, $variant_name, $len));
|
||||
let state = try!(_serializer.serialize_tuple_variant($type_name, $variant_index, $variant_name, $len));
|
||||
$serialize_stmts
|
||||
_serializer.serialize_tuple_variant_end($type_name, $variant_index, $variant_name, $len)
|
||||
_serializer.serialize_tuple_variant_end($type_name, $variant_index, $variant_name, $len, state)
|
||||
})
|
||||
}
|
||||
|
||||
@ -507,7 +507,7 @@ fn serialize_struct_variant(
|
||||
|
||||
quote_expr!(cx, {
|
||||
let len = $len;
|
||||
try!(_serializer.serialize_struct_variant(
|
||||
let state = try!(_serializer.serialize_struct_variant(
|
||||
$item_name,
|
||||
$variant_index,
|
||||
$variant_name,
|
||||
@ -519,6 +519,7 @@ fn serialize_struct_variant(
|
||||
$variant_index,
|
||||
$variant_name,
|
||||
len,
|
||||
state,
|
||||
)
|
||||
})
|
||||
}
|
||||
|
@ -34,6 +34,8 @@ impl<'a, I> ser::Serializer for Serializer<'a, I>
|
||||
where I: Iterator<Item=&'a Token<'a>>,
|
||||
{
|
||||
type Error = Error;
|
||||
type MapState = ();
|
||||
type SeqState = ();
|
||||
|
||||
fn serialize_unit(&mut self) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::Unit));
|
||||
@ -165,7 +167,7 @@ impl<'a, I> ser::Serializer for Serializer<'a, I>
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn serialize_seq_end(&mut self, _len: Option<usize>) -> Result<(), Error> {
|
||||
fn serialize_seq_end(&mut self, _len: Option<usize>, _: ()) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::SeqEnd));
|
||||
Ok(())
|
||||
}
|
||||
@ -189,7 +191,7 @@ impl<'a, I> ser::Serializer for Serializer<'a, I>
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn serialize_tuple_end(&mut self, _len: usize) -> Result<(), Error> {
|
||||
fn serialize_tuple_end(&mut self, _len: usize, _: ()) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::TupleEnd));
|
||||
Ok(())
|
||||
}
|
||||
@ -217,7 +219,7 @@ impl<'a, I> ser::Serializer for Serializer<'a, I>
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn serialize_tuple_struct_end(&mut self, _name: &str, _len: usize) -> Result<(), Error> {
|
||||
fn serialize_tuple_struct_end(&mut self, _name: &str, _len: usize, _: ()) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::TupleStructEnd));
|
||||
Ok(())
|
||||
}
|
||||
@ -244,7 +246,7 @@ impl<'a, I> ser::Serializer for Serializer<'a, I>
|
||||
_name: &str,
|
||||
_variant_index: usize,
|
||||
_variant: &str,
|
||||
_len: usize) -> Result<(), Error> {
|
||||
_len: usize, _: ()) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::EnumSeqEnd));
|
||||
Ok(())
|
||||
}
|
||||
@ -262,7 +264,7 @@ impl<'a, I> ser::Serializer for Serializer<'a, I>
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn serialize_map_end(&mut self, _len: Option<usize>) -> Result<(), Self::Error> {
|
||||
fn serialize_map_end(&mut self, _len: Option<usize>, _: ()) -> Result<(), Self::Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::MapEnd));
|
||||
Ok(())
|
||||
}
|
||||
@ -280,7 +282,7 @@ impl<'a, I> ser::Serializer for Serializer<'a, I>
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn serialize_struct_end(&mut self, _name: &str, _len: usize) -> Result<(), Self::Error> {
|
||||
fn serialize_struct_end(&mut self, _name: &str, _len: usize, _: ()) -> Result<(), Self::Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::StructEnd));
|
||||
Ok(())
|
||||
}
|
||||
@ -306,7 +308,7 @@ impl<'a, I> ser::Serializer for Serializer<'a, I>
|
||||
_name: &str,
|
||||
_variant_index: usize,
|
||||
_variant: &str,
|
||||
_len: usize) -> Result<(), Self::Error> {
|
||||
_len: usize, _: ()) -> Result<(), Self::Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::EnumMapEnd));
|
||||
Ok(())
|
||||
}
|
||||
|
@ -52,6 +52,8 @@ impl BytesSerializer {
|
||||
|
||||
impl serde::Serializer for BytesSerializer {
|
||||
type Error = Error;
|
||||
type SeqState = ();
|
||||
type MapState = ();
|
||||
|
||||
fn serialize_unit(&mut self) -> Result<(), Error> {
|
||||
Err(Error)
|
||||
@ -106,7 +108,7 @@ impl serde::Serializer for BytesSerializer {
|
||||
Err(Error)
|
||||
}
|
||||
|
||||
fn serialize_seq_end(&mut self, _: Option<usize>) -> Result<(), Error>
|
||||
fn serialize_seq_end(&mut self, _: Option<usize>, _: ()) -> Result<(), Error>
|
||||
{
|
||||
Err(Error)
|
||||
}
|
||||
@ -123,7 +125,7 @@ impl serde::Serializer for BytesSerializer {
|
||||
Err(Error)
|
||||
}
|
||||
|
||||
fn serialize_map_end(&mut self, _: Option<usize>) -> Result<(), Error>
|
||||
fn serialize_map_end(&mut self, _: Option<usize>, _: ()) -> Result<(), Error>
|
||||
{
|
||||
Err(Error)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user