simplify!
This commit is contained in:
parent
ee4e7413b0
commit
fb575225bc
@ -152,12 +152,11 @@ impl<T> Serialize for [T]
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
|
||||
let mut seq_serializer = try!(serializer.serialize_seq(Some(self.len())));
|
||||
try!(serializer.serialize_seq(Some(self.len())));
|
||||
for e in self.iter() {
|
||||
try!(seq_serializer.serialize_elt(e));
|
||||
try!(serializer.serialize_seq_elt(e));
|
||||
}
|
||||
seq_serializer.drop()
|
||||
serializer.serialize_seq_end()
|
||||
}
|
||||
}
|
||||
|
||||
@ -170,11 +169,11 @@ macro_rules! array_impls {
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
let mut seq_serializer = try!(serializer.serialize_fixed_size_array($len));
|
||||
try!(serializer.serialize_fixed_size_array($len));
|
||||
for e in self.iter() {
|
||||
try!(seq_serializer.serialize_elt(e));
|
||||
try!(serializer.serialize_seq_elt(e));
|
||||
}
|
||||
seq_serializer.drop()
|
||||
serializer.serialize_seq_end()
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -224,11 +223,11 @@ impl<T> Serialize for BinaryHeap<T>
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
let mut seq_serializer = try!(serializer.serialize_seq(Some(self.len())));
|
||||
try!(serializer.serialize_seq(Some(self.len())));
|
||||
for e in self.iter() {
|
||||
try!(seq_serializer.serialize_elt(e));
|
||||
try!(serializer.serialize_seq_elt(e));
|
||||
}
|
||||
seq_serializer.drop()
|
||||
serializer.serialize_seq_end()
|
||||
}
|
||||
}
|
||||
|
||||
@ -240,11 +239,11 @@ impl<T> Serialize for BTreeSet<T>
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
let mut seq_serializer = try!(serializer.serialize_seq(Some(self.len())));
|
||||
try!(serializer.serialize_seq(Some(self.len())));
|
||||
for e in self.iter() {
|
||||
try!(seq_serializer.serialize_elt(e));
|
||||
try!(serializer.serialize_seq_elt(e));
|
||||
}
|
||||
seq_serializer.drop()
|
||||
serializer.serialize_seq_end()
|
||||
}
|
||||
}
|
||||
|
||||
@ -256,11 +255,11 @@ impl<T> Serialize for EnumSet<T>
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
let mut seq_serializer = try!(serializer.serialize_seq(Some(self.len())));
|
||||
try!(serializer.serialize_seq(Some(self.len())));
|
||||
for e in self.iter() {
|
||||
try!(seq_serializer.serialize_elt(e));
|
||||
try!(serializer.serialize_seq_elt(e));
|
||||
}
|
||||
seq_serializer.drop()
|
||||
serializer.serialize_seq_end()
|
||||
}
|
||||
}
|
||||
|
||||
@ -273,11 +272,11 @@ impl<T, H> Serialize for HashSet<T, H>
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
let mut seq_serializer = try!(serializer.serialize_seq(Some(self.len())));
|
||||
try!(serializer.serialize_seq(Some(self.len())));
|
||||
for e in self.iter() {
|
||||
try!(seq_serializer.serialize_elt(e));
|
||||
try!(serializer.serialize_seq_elt(e));
|
||||
}
|
||||
seq_serializer.drop()
|
||||
serializer.serialize_seq_end()
|
||||
}
|
||||
}
|
||||
|
||||
@ -289,11 +288,11 @@ impl<T> Serialize for LinkedList<T>
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
let mut seq_serializer = try!(serializer.serialize_seq(Some(self.len())));
|
||||
try!(serializer.serialize_seq(Some(self.len())));
|
||||
for e in self.iter() {
|
||||
try!(seq_serializer.serialize_elt(e));
|
||||
try!(serializer.serialize_seq_elt(e));
|
||||
}
|
||||
seq_serializer.drop()
|
||||
serializer.serialize_seq_end()
|
||||
}
|
||||
}
|
||||
|
||||
@ -330,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,
|
||||
{
|
||||
let mut seq_serializer = try!(serializer.serialize_seq(Some(self.len())));
|
||||
try!(serializer.serialize_seq(Some(self.len())));
|
||||
for e in self.iter() {
|
||||
try!(seq_serializer.serialize_elt(e));
|
||||
try!(serializer.serialize_seq_elt(e));
|
||||
}
|
||||
seq_serializer.drop()
|
||||
serializer.serialize_seq_end()
|
||||
}
|
||||
}
|
||||
|
||||
@ -370,11 +369,11 @@ macro_rules! tuple_impls {
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
let mut ser = try!(serializer.serialize_tuple($len));
|
||||
try!(serializer.serialize_tuple($len));
|
||||
$(
|
||||
try!(ser.serialize_elt(&e!(self.$idx)));
|
||||
try!(serializer.serialize_tuple_elt(&e!(self.$idx)));
|
||||
)+
|
||||
ser.drop()
|
||||
serializer.serialize_tuple_end()
|
||||
}
|
||||
}
|
||||
)+
|
||||
@ -563,11 +562,11 @@ impl<K, V> Serialize for BTreeMap<K, V>
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
let mut map_serializer = try!(serializer.serialize_map(Some(self.len())));
|
||||
try!(serializer.serialize_map(Some(self.len())));
|
||||
for (k, v) in self.iter() {
|
||||
try!(map_serializer.serialize_elt(k, v));
|
||||
try!(serializer.serialize_map_elt(k, v));
|
||||
}
|
||||
map_serializer.drop()
|
||||
serializer.serialize_map_end()
|
||||
}
|
||||
}
|
||||
|
||||
@ -581,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,
|
||||
{
|
||||
let mut map_serializer = try!(serializer.serialize_map(Some(self.len())));
|
||||
try!(serializer.serialize_map(Some(self.len())));
|
||||
for (k, v) in self.iter() {
|
||||
try!(map_serializer.serialize_elt(k, v));
|
||||
try!(serializer.serialize_map_elt(k, v));
|
||||
}
|
||||
map_serializer.drop()
|
||||
serializer.serialize_map_end()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -38,82 +38,6 @@ pub trait Serialize {
|
||||
where S: Serializer;
|
||||
}
|
||||
|
||||
/// A trait that described a type that can serialize elements of a Map
|
||||
pub trait MapSerializer : Sized {
|
||||
/// The error type that can be returned if some error occurs during serialization.
|
||||
type Error: Error;
|
||||
|
||||
/// Serializes an element of a map (key-value pair).
|
||||
fn serialize_elt<S: ?Sized, K, V>(&mut self, serializer: &mut S, key: K, value: V) -> Result<(), Self::Error>
|
||||
where K: Serialize,
|
||||
V: Serialize,
|
||||
S: Serializer<Error = Self::Error>;
|
||||
|
||||
/// Finish serializing the map
|
||||
fn drop<S: ?Sized>(self, serializer: &mut S) -> Result<(), Self::Error>
|
||||
where S: Serializer<Error = Self::Error>;
|
||||
}
|
||||
|
||||
/// A trait that described a type that can serialize elements of a Sequence
|
||||
pub trait SeqSerializer : Sized {
|
||||
/// The error type that can be returned if some error occurs during serialization.
|
||||
type Error: Error;
|
||||
|
||||
/// Serializes an element of a sequence.
|
||||
fn serialize_elt<S: ?Sized, T>(&mut self, serializer: &mut S, value: T) -> Result<(), Self::Error>
|
||||
where T: Serialize,
|
||||
S: Serializer<Error = Self::Error>;
|
||||
|
||||
/// Finish serializing the map
|
||||
fn drop<S: ?Sized>(self, serializer: &mut S) -> Result<(), Self::Error>
|
||||
where S: Serializer<Error = Self::Error>;
|
||||
}
|
||||
|
||||
/// A helper type for serializing maps. This is a workaround for HKL associated types
|
||||
pub struct MapHelper<'a, S: Serializer + 'a + ?Sized>(&'a mut S, S::MapSerializer);
|
||||
|
||||
impl<'a, S: Serializer + 'a + ?Sized> MapHelper<'a, S> {
|
||||
/// Create a new MapHelper
|
||||
pub fn new(s: &'a mut S, ms: S::MapSerializer) -> Self {
|
||||
MapHelper(s, ms)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, S: Serializer + ?Sized> MapHelper<'a, S> {
|
||||
/// Serializes an element of a map (key-value pair).
|
||||
pub fn serialize_elt<K, V>(&mut self, key: K, value: V) -> Result<(), S::Error>
|
||||
where K: Serialize,
|
||||
V: Serialize {
|
||||
self.1.serialize_elt(self.0, key, value)
|
||||
}
|
||||
/// Closes the sequence
|
||||
pub fn drop(self) -> Result<(), S::Error> {
|
||||
self.1.drop(self.0)
|
||||
}
|
||||
}
|
||||
|
||||
/// A helper type for serializing sequences. This is a workaround for HKL associated types
|
||||
pub struct SeqHelper<'a, S: Serializer + 'a + ?Sized>(&'a mut S, S::SeqSerializer);
|
||||
|
||||
impl<'a, S: Serializer + 'a + ?Sized> SeqHelper<'a, S> {
|
||||
/// Create a new SeqHelper
|
||||
pub fn new(s: &'a mut S, ms: S::SeqSerializer) -> Self {
|
||||
SeqHelper(s, ms)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, S: Serializer + ?Sized> SeqHelper<'a, S> {
|
||||
/// Serializes an element of a sequence.
|
||||
pub fn serialize_elt<V>(&mut self, value: V) -> Result<(), S::Error>
|
||||
where V: Serialize {
|
||||
self.1.serialize_elt(self.0, value)
|
||||
}
|
||||
/// Closes the sequence
|
||||
pub fn drop(self) -> Result<(), S::Error> {
|
||||
self.1.drop(self.0)
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// A trait that describes a type that can serialize a stream of values into the underlying format.
|
||||
@ -121,11 +45,6 @@ pub trait Serializer {
|
||||
/// The error type that can be returned if some error occurs during serialization.
|
||||
type Error: Error;
|
||||
|
||||
/// A sub-type used to serialize sequences
|
||||
type SeqSerializer : SeqSerializer<Error = Self::Error>;
|
||||
/// A sub-type used to serialize maps
|
||||
type MapSerializer : MapSerializer<Error = Self::Error>;
|
||||
|
||||
/// Serializes a `bool` value.
|
||||
fn serialize_bool(&mut self, v: bool) -> Result<(), Self::Error>;
|
||||
|
||||
@ -217,11 +136,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> {
|
||||
let mut seq_serializer = try!(self.serialize_seq(Some(value.len())));
|
||||
try!(self.serialize_seq(Some(value.len())));
|
||||
for b in value.iter() {
|
||||
try!(seq_serializer.serialize_elt(b));
|
||||
try!(self.serialize_seq_elt(b));
|
||||
}
|
||||
seq_serializer.drop()
|
||||
self.serialize_seq_end()
|
||||
}
|
||||
|
||||
/// Serializes a `()` value.
|
||||
@ -255,9 +174,9 @@ pub trait Serializer {
|
||||
value: T) -> Result<(), Self::Error>
|
||||
where T: Serialize,
|
||||
{
|
||||
let mut ser = try!(self.serialize_tuple_struct(name, 1));
|
||||
try!(ser.serialize_elt((value,)));
|
||||
ser.drop()
|
||||
try!(self.serialize_tuple_struct(name, 1));
|
||||
try!(self.serialize_tuple_struct_elt(value));
|
||||
self.serialize_tuple_struct_end()
|
||||
}
|
||||
|
||||
/// Allows a variant with a single item to be more efficiently
|
||||
@ -271,9 +190,9 @@ pub trait Serializer {
|
||||
value: T) -> Result<(), Self::Error>
|
||||
where T: Serialize,
|
||||
{
|
||||
let mut ser = try!(self.serialize_tuple_variant(name, variant_index, variant, 1));
|
||||
try!(ser.serialize_elt((value,)));
|
||||
ser.drop()
|
||||
try!(self.serialize_tuple_variant(name, variant_index, variant, 1));
|
||||
try!(self.serialize_tuple_variant_elt(value));
|
||||
self.serialize_tuple_variant_end()
|
||||
}
|
||||
|
||||
/// Serializes a `None` value..serialize
|
||||
@ -287,7 +206,7 @@ pub trait Serializer {
|
||||
///
|
||||
/// Callees of this method need to construct a `SeqVisitor`, which iterates through each item
|
||||
/// in the sequence.
|
||||
fn serialize_seq<'a>(&'a mut self, len: Option<usize>) -> Result<SeqHelper<'a, Self>, Self::Error>;
|
||||
fn serialize_seq(&mut self, len: Option<usize>) -> Result<(), Self::Error>;
|
||||
|
||||
/// Serializes a sequence element.
|
||||
fn serialize_seq_elt<T>(&mut self, value: T) -> Result<(), Self::Error>
|
||||
@ -300,7 +219,7 @@ pub trait Serializer {
|
||||
///
|
||||
/// By default this serializes a tuple as a sequence.
|
||||
#[inline]
|
||||
fn serialize_tuple<'a>(&'a mut self, len: usize) -> Result<SeqHelper<'a, Self>, Self::Error>
|
||||
fn serialize_tuple(&mut self, len: usize) -> Result<(), Self::Error>
|
||||
{
|
||||
self.serialize_seq(Some(len))
|
||||
}
|
||||
@ -326,7 +245,7 @@ pub trait Serializer {
|
||||
///
|
||||
/// By default this serializes an array as a sequence.
|
||||
#[inline]
|
||||
fn serialize_fixed_size_array<'a>(&'a mut self, size: usize) -> Result<SeqHelper<'a, Self>, Self::Error>
|
||||
fn serialize_fixed_size_array(&mut self, size: usize) -> Result<(), Self::Error>
|
||||
{
|
||||
self.serialize_seq(Some(size))
|
||||
}
|
||||
@ -335,10 +254,10 @@ pub trait Serializer {
|
||||
///
|
||||
/// By default, tuple structs are serialized as a tuple.
|
||||
#[inline]
|
||||
fn serialize_tuple_struct<'a>(&'a mut self,
|
||||
_name: &'static str,
|
||||
len: usize,
|
||||
) -> Result<SeqHelper<'a, Self>, Self::Error>
|
||||
fn serialize_tuple_struct(&mut self,
|
||||
_name: &'static str,
|
||||
len: usize,
|
||||
) -> Result<(), Self::Error>
|
||||
{
|
||||
self.serialize_tuple(len)
|
||||
}
|
||||
@ -365,12 +284,12 @@ pub trait Serializer {
|
||||
///
|
||||
/// By default, tuple variants are serialized as a tuple struct.
|
||||
#[inline]
|
||||
fn serialize_tuple_variant<'a>(&'a mut self,
|
||||
_name: &'static str,
|
||||
_variant_index: usize,
|
||||
variant: &'static str,
|
||||
len: usize,
|
||||
) -> Result<SeqHelper<'a, Self>, Self::Error>
|
||||
fn serialize_tuple_variant(&mut self,
|
||||
_name: &'static str,
|
||||
_variant_index: usize,
|
||||
variant: &'static str,
|
||||
len: usize,
|
||||
) -> Result<(), Self::Error>
|
||||
{
|
||||
self.serialize_tuple_struct(variant, len)
|
||||
}
|
||||
@ -394,22 +313,22 @@ pub trait Serializer {
|
||||
}
|
||||
|
||||
/// Serialize a map.
|
||||
fn serialize_map<'a>(&'a mut self, len: Option<usize>) -> Result<MapHelper<'a, Self>, Self::Error>;
|
||||
fn serialize_map(&mut self, len: Option<usize>) -> Result<(), Self::Error>;
|
||||
|
||||
/// Serialize a map element
|
||||
fn serialize_map_elt<'a, K, V>(&'a mut self, key: K, value: V) -> Result<(), Self::Error> where K: Serialize, V: Serialize;
|
||||
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<'a>(&'a mut self) -> Result<(), Self::Error>;
|
||||
fn serialize_map_end(&mut self) -> Result<(), Self::Error>;
|
||||
|
||||
/// Serializes a struct.
|
||||
///
|
||||
/// By default, structs are serialized as a map with the field name as the key.
|
||||
#[inline]
|
||||
fn serialize_struct<'a>(&'a mut self,
|
||||
fn serialize_struct(&mut self,
|
||||
_name: &'static str,
|
||||
len: usize,
|
||||
) -> Result<MapHelper<'a, Self>, Self::Error>
|
||||
) -> Result<(), Self::Error>
|
||||
{
|
||||
self.serialize_map(Some(len))
|
||||
}
|
||||
@ -417,14 +336,14 @@ pub trait Serializer {
|
||||
/// Serialize a struct field
|
||||
///
|
||||
/// By default, structs are serialized as a map with the field name as the key.
|
||||
fn serialize_struct_elt<'a, K, V>(&'a mut self, key: K, value: V) -> Result<(), Self::Error> where K: Serialize, V: Serialize {
|
||||
fn serialize_struct_elt<K, V>(&mut self, key: K, value: V) -> Result<(), Self::Error> where K: Serialize, V: Serialize {
|
||||
self.serialize_map_elt(key, value)
|
||||
}
|
||||
|
||||
/// Finishes serializing a struct
|
||||
///
|
||||
/// By default, structs are serialized as a map with the field name as the key.
|
||||
fn serialize_struct_end<'a>(&'a mut self) -> Result<(), Self::Error> {
|
||||
fn serialize_struct_end(&mut self) -> Result<(), Self::Error> {
|
||||
self.serialize_map_end()
|
||||
}
|
||||
|
||||
@ -432,12 +351,12 @@ pub trait Serializer {
|
||||
///
|
||||
/// By default, struct variants are serialized as a struct.
|
||||
#[inline]
|
||||
fn serialize_struct_variant<'a>(&'a mut self,
|
||||
fn serialize_struct_variant(&mut self,
|
||||
_name: &'static str,
|
||||
_variant_index: usize,
|
||||
variant: &'static str,
|
||||
len: usize,
|
||||
) -> Result<MapHelper<'a, Self>, Self::Error>
|
||||
) -> Result<(), Self::Error>
|
||||
{
|
||||
self.serialize_struct(variant, len)
|
||||
}
|
||||
@ -445,14 +364,14 @@ pub trait Serializer {
|
||||
/// Serialize a struct variant element
|
||||
///
|
||||
/// By default, structs are serialized as a map with the field name as the key.
|
||||
fn serialize_struct_variant_elt<'a, K, V>(&'a mut self, key: K, value: V) -> Result<(), Self::Error> where K: Serialize, V: Serialize {
|
||||
fn serialize_struct_variant_elt<K, V>(&mut self, key: K, value: V) -> Result<(), Self::Error> where K: Serialize, V: Serialize {
|
||||
self.serialize_struct_elt(key, value)
|
||||
}
|
||||
|
||||
/// Finishes serializing a struct variant
|
||||
///
|
||||
/// By default, structs are serialized as a map with the field name as the key.
|
||||
fn serialize_struct_variant_end<'a>(&'a mut self) -> Result<(), Self::Error> {
|
||||
fn serialize_struct_variant_end(&mut self) -> Result<(), Self::Error> {
|
||||
self.serialize_struct_end()
|
||||
}
|
||||
}
|
||||
|
@ -223,15 +223,16 @@ fn serialize_tuple_struct(
|
||||
fields,
|
||||
impl_generics,
|
||||
false,
|
||||
cx.ident_of("serialize_tuple_struct_elt"),
|
||||
);
|
||||
|
||||
let type_name = name_expr(builder, item_attrs.name());
|
||||
let len = serialize_stmts.len();
|
||||
|
||||
quote_expr!(cx, {
|
||||
let mut _seq_serializer = try!(_serializer.serialize_tuple_struct($type_name, $len));
|
||||
try!(_serializer.serialize_tuple_struct($type_name, $len));
|
||||
$serialize_stmts
|
||||
_seq_serializer.drop()
|
||||
_serializer.serialize_tuple_struct_end()
|
||||
})
|
||||
}
|
||||
|
||||
@ -250,6 +251,7 @@ fn serialize_struct(
|
||||
fields,
|
||||
impl_generics,
|
||||
false,
|
||||
cx.ident_of("serialize_struct_elt"),
|
||||
);
|
||||
|
||||
let type_name = name_expr(builder, item_attrs.name());
|
||||
@ -267,9 +269,9 @@ fn serialize_struct(
|
||||
.fold(quote_expr!(cx, 0), |sum, expr| quote_expr!(cx, $sum + $expr));
|
||||
|
||||
quote_expr!(cx, {
|
||||
let mut _map_serializer = try!(_serializer.serialize_struct($type_name, $len));
|
||||
try!(_serializer.serialize_struct($type_name, $len));
|
||||
$serialize_fields
|
||||
_map_serializer.drop()
|
||||
_serializer.serialize_struct_end()
|
||||
})
|
||||
}
|
||||
|
||||
@ -455,14 +457,15 @@ fn serialize_tuple_variant(
|
||||
fields,
|
||||
generics,
|
||||
true,
|
||||
cx.ident_of("serialize_tuple_variant_elt"),
|
||||
);
|
||||
|
||||
let len = serialize_stmts.len();
|
||||
|
||||
quote_expr!(cx, {
|
||||
let mut _seq_serializer = try!(_serializer.serialize_tuple_variant($type_name, $variant_index, $variant_name, $len));
|
||||
try!(_serializer.serialize_tuple_variant($type_name, $variant_index, $variant_name, $len));
|
||||
$serialize_stmts
|
||||
_seq_serializer.drop()
|
||||
_serializer.serialize_tuple_variant_end()
|
||||
})
|
||||
}
|
||||
|
||||
@ -484,6 +487,7 @@ fn serialize_struct_variant(
|
||||
fields,
|
||||
&generics,
|
||||
true,
|
||||
cx.ident_of("serialize_struct_variant_elt"),
|
||||
);
|
||||
|
||||
let item_name = name_expr(builder, item_attrs.name());
|
||||
@ -501,14 +505,14 @@ fn serialize_struct_variant(
|
||||
.fold(quote_expr!(cx, 0), |sum, expr| quote_expr!(cx, $sum + $expr));
|
||||
|
||||
quote_expr!(cx, {
|
||||
let mut _map_serializer = try!(_serializer.serialize_struct_variant(
|
||||
try!(_serializer.serialize_struct_variant(
|
||||
$item_name,
|
||||
$variant_index,
|
||||
$variant_name,
|
||||
$len,
|
||||
));
|
||||
$serialize_fields
|
||||
_map_serializer.drop()
|
||||
_serializer.serialize_struct_variant_end()
|
||||
})
|
||||
}
|
||||
|
||||
@ -519,6 +523,7 @@ fn serialize_tuple_struct_visitor(
|
||||
fields: &[Field],
|
||||
generics: &ast::Generics,
|
||||
is_enum: bool,
|
||||
func: ast::Ident,
|
||||
) -> Vec<ast::Stmt> {
|
||||
fields.iter()
|
||||
.enumerate()
|
||||
@ -540,7 +545,7 @@ fn serialize_tuple_struct_visitor(
|
||||
|
||||
quote_stmt!(cx,
|
||||
if !$skip {
|
||||
try!(_seq_serializer.serialize_elt($field_expr));
|
||||
try!(_serializer.$func($field_expr));
|
||||
}
|
||||
).unwrap()
|
||||
})
|
||||
@ -554,6 +559,7 @@ fn serialize_struct_visitor(
|
||||
fields: &[Field],
|
||||
generics: &ast::Generics,
|
||||
is_enum: bool,
|
||||
func: ast::Ident,
|
||||
) -> Vec<ast::Stmt> {
|
||||
fields.iter()
|
||||
.filter(|&field| !field.attrs.skip_serializing())
|
||||
@ -578,7 +584,7 @@ fn serialize_struct_visitor(
|
||||
|
||||
quote_stmt!(cx,
|
||||
if !$skip {
|
||||
try!(_map_serializer.serialize_elt($key_expr, $field_expr));
|
||||
try!(_serializer.$func($key_expr, $field_expr));
|
||||
}
|
||||
).unwrap()
|
||||
})
|
||||
|
@ -3,72 +3,11 @@ use std::marker::PhantomData;
|
||||
use serde::ser::{
|
||||
self,
|
||||
Serialize,
|
||||
MapHelper,
|
||||
SeqHelper,
|
||||
};
|
||||
|
||||
use error::Error;
|
||||
use token::Token;
|
||||
|
||||
pub enum MapSerializer {
|
||||
Map,
|
||||
Struct,
|
||||
Enum,
|
||||
}
|
||||
|
||||
impl ser::MapSerializer for MapSerializer {
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_elt<S: ?Sized, K, V>(&mut self, serializer: &mut S, key: K, value: V) -> Result<(), Self::Error>
|
||||
where K: Serialize,
|
||||
V: Serialize,
|
||||
S: ser::Serializer<Error = Error> {
|
||||
match *self {
|
||||
MapSerializer::Map => serializer.serialize_map_elt(key, value),
|
||||
MapSerializer::Struct => serializer.serialize_struct_elt(key, value),
|
||||
MapSerializer::Enum => serializer.serialize_struct_variant_elt(key, value),
|
||||
}
|
||||
}
|
||||
|
||||
fn drop<S: ?Sized>(self, serializer: &mut S) -> Result<(), Self::Error> where S: ser::Serializer<Error = Error> {
|
||||
match self {
|
||||
MapSerializer::Map => serializer.serialize_map_end(),
|
||||
MapSerializer::Struct => serializer.serialize_struct_end(),
|
||||
MapSerializer::Enum => serializer.serialize_struct_variant_end(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub enum SeqSerializer {
|
||||
Seq,
|
||||
Tuple,
|
||||
TupleStruct,
|
||||
Enum,
|
||||
}
|
||||
|
||||
impl ser::SeqSerializer for SeqSerializer {
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_elt<S: ?Sized, T>(&mut self, serializer: &mut S, value: T) -> Result<(), Self::Error>
|
||||
where T: Serialize, S: ser::Serializer<Error = Error> {
|
||||
match *self {
|
||||
SeqSerializer::Seq => serializer.serialize_seq_elt(value),
|
||||
SeqSerializer::Tuple => serializer.serialize_tuple_elt(value),
|
||||
SeqSerializer::TupleStruct => serializer.serialize_tuple_struct_elt(value),
|
||||
SeqSerializer::Enum => serializer.serialize_tuple_variant_elt(value),
|
||||
}
|
||||
}
|
||||
|
||||
fn drop<S: ?Sized>(self, serializer: &mut S) -> Result<(), Self::Error> where S: ser::Serializer<Error = Error> {
|
||||
match self {
|
||||
SeqSerializer::Seq => serializer.serialize_seq_end(),
|
||||
SeqSerializer::Tuple => serializer.serialize_tuple_end(),
|
||||
SeqSerializer::TupleStruct => serializer.serialize_tuple_struct_end(),
|
||||
SeqSerializer::Enum => serializer.serialize_tuple_variant_end(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Serializer<'a, I>
|
||||
where I: Iterator<Item=&'a Token<'a>>,
|
||||
{
|
||||
@ -95,8 +34,6 @@ impl<'a, I> ser::Serializer for Serializer<'a, I>
|
||||
where I: Iterator<Item=&'a Token<'a>>,
|
||||
{
|
||||
type Error = Error;
|
||||
type SeqSerializer = SeqSerializer;
|
||||
type MapSerializer = MapSerializer;
|
||||
|
||||
fn serialize_unit(&mut self) -> Result<(), Error> {
|
||||
assert_eq!(self.tokens.next(), Some(&Token::Unit));
|
||||
@ -215,10 +152,10 @@ impl<'a, I> ser::Serializer for Serializer<'a, I>
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn serialize_seq<'b>(&'b mut self, len: Option<usize>) -> Result<SeqHelper<'b, Self>, Error>
|
||||
fn serialize_seq<'b>(&'b mut self, len: Option<usize>) -> Result<(), Error>
|
||||
{
|
||||
assert_eq!(self.tokens.next(), Some(&Token::SeqStart(len)));
|
||||
Ok(SeqHelper::new(self, SeqSerializer::Seq))
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_seq_elt<T>(&mut self, value: T) -> Result<(), Error>
|
||||
@ -233,17 +170,16 @@ impl<'a, I> ser::Serializer for Serializer<'a, I>
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_fixed_size_array<'b>(&'b mut self, len: usize) -> Result<SeqHelper<'b, Self>, Error>
|
||||
fn serialize_fixed_size_array<'b>(&'b mut self, len: usize) -> Result<(), Error>
|
||||
{
|
||||
assert_eq!(self.tokens.next(), Some(&Token::SeqArrayStart(len)));
|
||||
Ok(SeqHelper::new(self, SeqSerializer::Seq))
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_tuple<'b>(&'b mut self, len: usize) -> Result<SeqHelper<'b, Self>, Error>
|
||||
fn serialize_tuple<'b>(&'b mut self, len: usize) -> Result<(), Error>
|
||||
{
|
||||
assert_eq!(self.tokens.next(), Some(&Token::TupleStart(len)));
|
||||
|
||||
Ok(SeqHelper::new(self, SeqSerializer::Tuple))
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_tuple_elt<T>(&mut self, value: T) -> Result<(), Error>
|
||||
@ -267,11 +203,11 @@ impl<'a, I> ser::Serializer for Serializer<'a, I>
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn serialize_tuple_struct<'b>(&'b mut self, name: &str, len: usize) -> Result<SeqHelper<'b, Self>, Error>
|
||||
fn serialize_tuple_struct<'b>(&'b mut self, name: &str, len: usize) -> Result<(), Error>
|
||||
{
|
||||
assert_eq!(self.tokens.next(), Some(&Token::TupleStructStart(name, len)));
|
||||
|
||||
Ok(SeqHelper::new(self, SeqSerializer::TupleStruct))
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_tuple_struct_elt<T>(&mut self, value: T) -> Result<(), Error>
|
||||
@ -290,11 +226,11 @@ impl<'a, I> ser::Serializer for Serializer<'a, I>
|
||||
name: &str,
|
||||
_variant_index: usize,
|
||||
variant: &str,
|
||||
len: usize) -> Result<SeqHelper<'b, Self>, Error>
|
||||
len: usize) -> Result<(), Error>
|
||||
{
|
||||
assert_eq!(self.tokens.next(), Some(&Token::EnumSeqStart(name, variant, len)));
|
||||
|
||||
Ok(SeqHelper::new(self, SeqSerializer::Enum))
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_tuple_variant_elt<T>(&mut self, value: T) -> Result<(), Error>
|
||||
@ -309,11 +245,11 @@ impl<'a, I> ser::Serializer for Serializer<'a, I>
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_map<'b>(&'b mut self, len: Option<usize>) -> Result<MapHelper<'b, Self>, Error>
|
||||
fn serialize_map<'b>(&'b mut self, len: Option<usize>) -> Result<(), Error>
|
||||
{
|
||||
assert_eq!(self.tokens.next(), Some(&Token::MapStart(len)));
|
||||
|
||||
Ok(MapHelper::new(self, MapSerializer::Map))
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_map_elt<K, V>(&mut self, key: K, value: V) -> Result<(), Self::Error> where K: Serialize, V: Serialize {
|
||||
@ -327,11 +263,11 @@ impl<'a, I> ser::Serializer for Serializer<'a, I>
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_struct<'b>(&'b mut self, name: &str, len: usize) -> Result<MapHelper<'b, Self>, Error>
|
||||
fn serialize_struct<'b>(&'b mut self, name: &str, len: usize) -> Result<(), Error>
|
||||
{
|
||||
assert_eq!(self.tokens.next(), Some(&Token::StructStart(name, len)));
|
||||
|
||||
Ok(MapHelper::new(self, MapSerializer::Struct))
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_struct_elt<K, V>(&mut self, key: K, value: V) -> Result<(), Self::Error> where K: Serialize, V: Serialize {
|
||||
@ -349,11 +285,11 @@ impl<'a, I> ser::Serializer for Serializer<'a, I>
|
||||
name: &str,
|
||||
_variant_index: usize,
|
||||
variant: &str,
|
||||
len: usize) -> Result<MapHelper<'b, Self>, Error>
|
||||
len: usize) -> Result<(), Error>
|
||||
{
|
||||
assert_eq!(self.tokens.next(), Some(&Token::EnumMapStart(name, variant, len)));
|
||||
|
||||
Ok(MapHelper::new(self, MapSerializer::Enum))
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_struct_variant_elt<K, V>(&mut self, key: K, value: V) -> Result<(), Self::Error> where K: Serialize, V: Serialize {
|
||||
|
@ -50,42 +50,8 @@ impl BytesSerializer {
|
||||
}
|
||||
}
|
||||
|
||||
struct SeqSerializer;
|
||||
|
||||
impl serde::ser::SeqSerializer for SeqSerializer {
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_elt<S: ?Sized, T>(&mut self, _serializer: &mut S, _value: T) -> Result<(), Self::Error>
|
||||
where T: Serialize, S: serde::ser::Serializer<Error = Error> {
|
||||
Err(Error)
|
||||
}
|
||||
|
||||
fn drop<S: ?Sized>(self, _serializer: &mut S) -> Result<(), Self::Error> where S: serde::ser::Serializer<Error = Error> {
|
||||
Err(Error)
|
||||
}
|
||||
}
|
||||
|
||||
struct MapSerializer;
|
||||
|
||||
impl serde::ser::MapSerializer for MapSerializer {
|
||||
type Error = Error;
|
||||
|
||||
fn serialize_elt<S: ?Sized, K, V>(&mut self, _serializer: &mut S, _key: K, _value: V) -> Result<(), Self::Error>
|
||||
where K: Serialize,
|
||||
V: Serialize,
|
||||
S: serde::ser::Serializer<Error = Error> {
|
||||
Err(Error)
|
||||
}
|
||||
|
||||
fn drop<S: ?Sized>(self, _serializer: &mut S) -> Result<(), Self::Error> where S: serde::ser::Serializer<Error = Error> {
|
||||
Err(Error)
|
||||
}
|
||||
}
|
||||
|
||||
impl serde::Serializer for BytesSerializer {
|
||||
type Error = Error;
|
||||
type SeqSerializer = SeqSerializer;
|
||||
type MapSerializer = MapSerializer;
|
||||
|
||||
fn serialize_unit(&mut self) -> Result<(), Error> {
|
||||
Err(Error)
|
||||
@ -129,7 +95,7 @@ impl serde::Serializer for BytesSerializer {
|
||||
Err(Error)
|
||||
}
|
||||
|
||||
fn serialize_seq<'a>(&'a mut self, _len: Option<usize>) -> Result<serde::ser::SeqHelper<'a, Self>, Error>
|
||||
fn serialize_seq<'a>(&'a mut self, _len: Option<usize>) -> Result<(), Error>
|
||||
{
|
||||
Err(Error)
|
||||
}
|
||||
@ -145,7 +111,7 @@ impl serde::Serializer for BytesSerializer {
|
||||
Err(Error)
|
||||
}
|
||||
|
||||
fn serialize_map<'a>(&mut self, _: Option<usize>) -> Result<serde::ser::MapHelper<'a, Self>, Error>
|
||||
fn serialize_map<'a>(&mut self, _: Option<usize>) -> Result<(), Error>
|
||||
{
|
||||
Err(Error)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user