Compare commits

..

1 Commits

Author SHA1 Message Date
Erick Tryzelaar 37c0ad19bb Version bump 2015-06-25 16:10:52 -07:00
13 changed files with 335 additions and 839 deletions
+1 -1
View File
@@ -174,7 +174,7 @@ impl<K, V, I> MapVisitor for MapIteratorVisitor<I>
``` ```
Serializing structs follow this same pattern. In fact, structs are represented Serializing structs follow this same pattern. In fact, structs are represented
as a named map. Its visitor uses a simple state machine to iterate through all as a named map. It's visitor uses a simple state machine to iterate through all
the fields: the fields:
```rust ```rust
+1 -5
View File
@@ -1,6 +1,6 @@
[package] [package]
name = "serde" name = "serde"
version = "0.4.3" version = "0.4.2"
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"] authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
license = "MIT/Apache-2.0" license = "MIT/Apache-2.0"
description = "A generic serialization/deserialization framework" description = "A generic serialization/deserialization framework"
@@ -11,7 +11,3 @@ keywords = ["serialization"]
[dependencies] [dependencies]
num = "*" num = "*"
[features]
nightly = []
+271 -389
View File
@@ -1,17 +1,4 @@
use std::borrow::Cow; use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::collections::{
BinaryHeap,
BTreeMap,
BTreeSet,
LinkedList,
HashMap,
HashSet,
VecDeque,
};
#[cfg(feature = "nightly")]
use collections::enum_set::{CLike, EnumSet};
#[cfg(feature = "nightly")]
use collections::vec_map::VecMap;
use std::hash::Hash; use std::hash::Hash;
use std::marker::PhantomData; use std::marker::PhantomData;
use std::path; use std::path;
@@ -21,20 +8,12 @@ use std::sync::Arc;
use num::FromPrimitive; use num::FromPrimitive;
#[cfg(feature = "nightly")]
use core::nonzero::{NonZero, Zeroable};
#[cfg(feature = "nightly")]
use std::num::Zero;
use de::{ use de::{
Deserialize, Deserialize,
Deserializer, Deserializer,
EnumVisitor,
Error, Error,
MapVisitor, MapVisitor,
SeqVisitor, SeqVisitor,
VariantVisitor,
Visitor, Visitor,
}; };
@@ -62,13 +41,13 @@ impl Deserialize for () {
fn deserialize<D>(deserializer: &mut D) -> Result<(), D::Error> fn deserialize<D>(deserializer: &mut D) -> Result<(), D::Error>
where D: Deserializer, where D: Deserializer,
{ {
deserializer.visit_unit(UnitVisitor) deserializer.visit(UnitVisitor)
} }
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
pub struct BoolVisitor; struct BoolVisitor;
impl Visitor for BoolVisitor { impl Visitor for BoolVisitor {
type Value = bool; type Value = bool;
@@ -94,7 +73,7 @@ impl Deserialize for bool {
fn deserialize<D>(deserializer: &mut D) -> Result<bool, D::Error> fn deserialize<D>(deserializer: &mut D) -> Result<bool, D::Error>
where D: Deserializer, where D: Deserializer,
{ {
deserializer.visit_bool(BoolVisitor) deserializer.visit(BoolVisitor)
} }
} }
@@ -154,30 +133,30 @@ impl<
} }
macro_rules! impl_deserialize_num { macro_rules! impl_deserialize_num {
($ty:ty, $method:ident) => { ($ty:ty) => {
impl Deserialize for $ty { impl Deserialize for $ty {
#[inline] #[inline]
fn deserialize<D>(deserializer: &mut D) -> Result<$ty, D::Error> fn deserialize<D>(deserializer: &mut D) -> Result<$ty, D::Error>
where D: Deserializer, where D: Deserializer,
{ {
deserializer.$method(PrimitiveVisitor::new()) deserializer.visit(PrimitiveVisitor::new())
} }
} }
} }
} }
impl_deserialize_num!(isize, visit_isize); impl_deserialize_num!(isize);
impl_deserialize_num!(i8, visit_i8); impl_deserialize_num!(i8);
impl_deserialize_num!(i16, visit_i16); impl_deserialize_num!(i16);
impl_deserialize_num!(i32, visit_i32); impl_deserialize_num!(i32);
impl_deserialize_num!(i64, visit_i64); impl_deserialize_num!(i64);
impl_deserialize_num!(usize, visit_usize); impl_deserialize_num!(usize);
impl_deserialize_num!(u8, visit_u8); impl_deserialize_num!(u8);
impl_deserialize_num!(u16, visit_u16); impl_deserialize_num!(u16);
impl_deserialize_num!(u32, visit_u32); impl_deserialize_num!(u32);
impl_deserialize_num!(u64, visit_u64); impl_deserialize_num!(u64);
impl_deserialize_num!(f32, visit_f32); impl_deserialize_num!(f32);
impl_deserialize_num!(f64, visit_f64); impl_deserialize_num!(f64);
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
@@ -215,7 +194,7 @@ impl Deserialize for char {
fn deserialize<D>(deserializer: &mut D) -> Result<char, D::Error> fn deserialize<D>(deserializer: &mut D) -> Result<char, D::Error>
where D: Deserializer, where D: Deserializer,
{ {
deserializer.visit_char(CharVisitor) deserializer.visit(CharVisitor)
} }
} }
@@ -261,7 +240,7 @@ impl Deserialize for String {
fn deserialize<D>(deserializer: &mut D) -> Result<String, D::Error> fn deserialize<D>(deserializer: &mut D) -> Result<String, D::Error>
where D: Deserializer, where D: Deserializer,
{ {
deserializer.visit_string(StringVisitor) deserializer.visit(StringVisitor)
} }
} }
@@ -301,131 +280,159 @@ impl<T> Deserialize for Option<T> where T: Deserialize {
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
macro_rules! set_impl { pub struct BTreeSetVisitor<T> {
( marker: PhantomData<T>,
$ty:ty, }
< $($constraints:ident),* >,
$visitor_name:ident,
$visitor:ident,
$ctor:expr,
$with_capacity:expr,
$insert:expr
) => {
pub struct $visitor_name<T> {
marker: PhantomData<T>,
}
impl<T> $visitor_name<T> { impl<T> BTreeSetVisitor<T> {
pub fn new() -> Self { pub fn new() -> Self {
$visitor_name { BTreeSetVisitor {
marker: PhantomData, marker: PhantomData,
}
}
}
impl<T> Visitor for $visitor_name<T>
where T: $($constraints +)*,
{
type Value = $ty;
#[inline]
fn visit_unit<E>(&mut self) -> Result<$ty, E>
where E: Error,
{
Ok($ctor)
}
#[inline]
fn visit_seq<V>(&mut self, mut $visitor: V) -> Result<$ty, V::Error>
where V: SeqVisitor,
{
let mut values = $with_capacity;
while let Some(value) = try!($visitor.visit()) {
$insert(&mut values, value);
}
try!($visitor.end());
Ok(values)
}
}
impl<T> Deserialize for $ty
where T: $($constraints +)*,
{
fn deserialize<D>(deserializer: &mut D) -> Result<$ty, D::Error>
where D: Deserializer,
{
deserializer.visit_seq($visitor_name::new())
}
} }
} }
} }
set_impl!( impl<T> Visitor for BTreeSetVisitor<T>
BinaryHeap<T>, where T: Deserialize + Eq + Ord,
<Deserialize, Ord>, {
BinaryHeapVisitor, type Value = BTreeSet<T>;
visitor,
BinaryHeap::new(),
BinaryHeap::with_capacity(visitor.size_hint().0),
BinaryHeap::push);
set_impl!( #[inline]
BTreeSet<T>, fn visit_unit<E>(&mut self) -> Result<BTreeSet<T>, E>
<Deserialize, Eq, Ord>, where E: Error,
BTreeSetVisitor, {
visitor, Ok(BTreeSet::new())
BTreeSet::new(), }
BTreeSet::new(),
BTreeSet::insert);
#[cfg(feature = "nightly")] #[inline]
set_impl!( fn visit_seq<V>(&mut self, mut visitor: V) -> Result<BTreeSet<T>, V::Error>
EnumSet<T>, where V: SeqVisitor,
<Deserialize, CLike>, {
EnumSetVisitor, let mut values = BTreeSet::new();
visitor,
EnumSet::new(),
EnumSet::new(),
EnumSet::insert);
set_impl!( while let Some(value) = try!(visitor.visit()) {
LinkedList<T>, values.insert(value);
<Deserialize>, }
LinkedListVisitor,
visitor,
LinkedList::new(),
LinkedList::new(),
LinkedList::push_back);
set_impl!( try!(visitor.end());
HashSet<T>,
<Deserialize, Eq, Hash>,
HashSetVisitor,
visitor,
HashSet::new(),
HashSet::with_capacity(visitor.size_hint().0),
HashSet::insert);
set_impl!( Ok(values)
Vec<T>, }
<Deserialize>, }
VecVisitor,
visitor,
Vec::new(),
Vec::with_capacity(visitor.size_hint().0),
Vec::push);
set_impl!( impl<T> Deserialize for BTreeSet<T>
VecDeque<T>, where T: Deserialize + Eq + Ord,
<Deserialize>, {
VecDequeVisitor, fn deserialize<D>(deserializer: &mut D) -> Result<BTreeSet<T>, D::Error>
visitor, where D: Deserializer,
VecDeque::new(), {
VecDeque::with_capacity(visitor.size_hint().0), deserializer.visit(BTreeSetVisitor::new())
VecDeque::push_back); }
}
///////////////////////////////////////////////////////////////////////////////
pub struct HashSetVisitor<T> {
marker: PhantomData<T>,
}
impl<T> HashSetVisitor<T> {
pub fn new() -> Self {
HashSetVisitor {
marker: PhantomData,
}
}
}
impl<T> Visitor for HashSetVisitor<T>
where T: Deserialize + Eq + Hash,
{
type Value = HashSet<T>;
#[inline]
fn visit_unit<E>(&mut self) -> Result<HashSet<T>, E>
where E: Error,
{
Ok(HashSet::new())
}
#[inline]
fn visit_seq<V>(&mut self, mut visitor: V) -> Result<HashSet<T>, V::Error>
where V: SeqVisitor,
{
let (len, _) = visitor.size_hint();
let mut values = HashSet::with_capacity(len);
while let Some(value) = try!(visitor.visit()) {
values.insert(value);
}
try!(visitor.end());
Ok(values)
}
}
impl<T> Deserialize for HashSet<T>
where T: Deserialize + Eq + Hash,
{
fn deserialize<D>(deserializer: &mut D) -> Result<HashSet<T>, D::Error>
where D: Deserializer,
{
deserializer.visit(HashSetVisitor::new())
}
}
///////////////////////////////////////////////////////////////////////////////
pub struct VecVisitor<T> {
marker: PhantomData<T>,
}
impl<T> VecVisitor<T> {
pub fn new() -> Self {
VecVisitor {
marker: PhantomData,
}
}
}
impl<T> Visitor for VecVisitor<T> where T: Deserialize {
type Value = Vec<T>;
#[inline]
fn visit_unit<E>(&mut self) -> Result<Vec<T>, E>
where E: Error,
{
Ok(Vec::new())
}
#[inline]
fn visit_seq<V>(&mut self, mut visitor: V) -> Result<Vec<T>, V::Error>
where V: SeqVisitor,
{
let (len, _) = visitor.size_hint();
let mut values = Vec::with_capacity(len);
while let Some(value) = try!(visitor.visit()) {
values.push(value);
}
try!(visitor.end());
Ok(values)
}
}
impl<T> Deserialize for Vec<T>
where T: Deserialize,
{
fn deserialize<D>(deserializer: &mut D) -> Result<Vec<T>, D::Error>
where D: Deserializer,
{
deserializer.visit_seq(VecVisitor::new())
}
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
@@ -466,7 +473,7 @@ impl<T> Deserialize for [T; 0]
fn deserialize<D>(deserializer: &mut D) -> Result<[T; 0], D::Error> fn deserialize<D>(deserializer: &mut D) -> Result<[T; 0], D::Error>
where D: Deserializer, where D: Deserializer,
{ {
deserializer.visit_seq(ArrayVisitor0::new()) deserializer.visit(ArrayVisitor0::new())
} }
} }
@@ -511,7 +518,7 @@ macro_rules! array_impls {
fn deserialize<D>(deserializer: &mut D) -> Result<[T; $len], D::Error> fn deserialize<D>(deserializer: &mut D) -> Result<[T; $len], D::Error>
where D: Deserializer, where D: Deserializer,
{ {
deserializer.visit_seq($visitor::new()) deserializer.visit($visitor::new())
} }
} }
)+ )+
@@ -601,7 +608,7 @@ macro_rules! tuple_impls {
fn deserialize<D>(deserializer: &mut D) -> Result<($($name,)+), D::Error> fn deserialize<D>(deserializer: &mut D) -> Result<($($name,)+), D::Error>
where D: Deserializer, where D: Deserializer,
{ {
deserializer.visit_tuple($visitor { marker: PhantomData }) deserializer.visit_seq($visitor { marker: PhantomData })
} }
} }
)+ )+
@@ -625,96 +632,123 @@ tuple_impls! {
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
macro_rules! map_impl { pub struct BTreeMapVisitor<K, V> {
( marker: PhantomData<BTreeMap<K, V>>,
$ty:ty, }
< $($constraints:ident),* >,
$visitor_name:ident,
$visitor:ident,
$ctor:expr,
$with_capacity:expr,
$insert:expr
) => {
pub struct $visitor_name<K, V> {
marker: PhantomData<$ty>,
}
impl<K, V> $visitor_name<K, V> { impl<K, V> BTreeMapVisitor<K, V> {
pub fn new() -> Self { #[inline]
$visitor_name { pub fn new() -> Self {
marker: PhantomData, BTreeMapVisitor {
} marker: PhantomData,
}
}
impl<K, V> Visitor for $visitor_name<K, V>
where K: $($constraints +)*,
V: Deserialize,
{
type Value = $ty;
#[inline]
fn visit_unit<E>(&mut self) -> Result<$ty, E>
where E: Error,
{
Ok($ctor)
}
#[inline]
fn visit_map<Visitor>(&mut self, mut $visitor: Visitor) -> Result<$ty, Visitor::Error>
where Visitor: MapVisitor,
{
let mut values = $with_capacity;
while let Some((key, value)) = try!($visitor.visit()) {
$insert(&mut values, key, value);
}
try!($visitor.end());
Ok(values)
}
}
impl<K, V> Deserialize for $ty
where K: $($constraints +)*,
V: Deserialize,
{
fn deserialize<D>(deserializer: &mut D) -> Result<$ty, D::Error>
where D: Deserializer,
{
deserializer.visit_map($visitor_name::new())
}
} }
} }
} }
map_impl!( impl<K, V> Visitor for BTreeMapVisitor<K, V>
BTreeMap<K, V>, where K: Deserialize + Ord,
<Deserialize, Eq, Ord>, V: Deserialize
BTreeMapVisitor, {
visitor, type Value = BTreeMap<K, V>;
BTreeMap::new(),
BTreeMap::new(),
BTreeMap::insert);
map_impl!( #[inline]
HashMap<K, V>, fn visit_unit<E>(&mut self) -> Result<BTreeMap<K, V>, E>
<Deserialize, Eq, Hash>, where E: Error,
HashMapVisitor, {
visitor, Ok(BTreeMap::new())
HashMap::new(), }
HashMap::with_capacity(visitor.size_hint().0),
HashMap::insert); #[inline]
fn visit_map<Visitor>(&mut self, mut visitor: Visitor) -> Result<BTreeMap<K, V>, Visitor::Error>
where Visitor: MapVisitor,
{
let mut values = BTreeMap::new();
while let Some((key, value)) = try!(visitor.visit()) {
values.insert(key, value);
}
try!(visitor.end());
Ok(values)
}
}
impl<K, V> Deserialize for BTreeMap<K, V>
where K: Deserialize + Eq + Ord,
V: Deserialize,
{
fn deserialize<D>(deserializer: &mut D) -> Result<BTreeMap<K, V>, D::Error>
where D: Deserializer,
{
deserializer.visit(BTreeMapVisitor::new())
}
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#[cfg(feature = "nightly")] pub struct HashMapVisitor<K, V> {
marker: PhantomData<HashMap<K, V>>,
}
impl<K, V> HashMapVisitor<K, V> {
#[inline]
pub fn new() -> Self {
HashMapVisitor {
marker: PhantomData,
}
}
}
impl<K, V> Visitor for HashMapVisitor<K, V>
where K: Deserialize + Eq + Hash,
V: Deserialize,
{
type Value = HashMap<K, V>;
#[inline]
fn visit_unit<E>(&mut self) -> Result<HashMap<K, V>, E>
where E: Error,
{
Ok(HashMap::new())
}
#[inline]
fn visit_map<V_>(&mut self, mut visitor: V_) -> Result<HashMap<K, V>, V_::Error>
where V_: MapVisitor,
{
let (len, _) = visitor.size_hint();
let mut values = HashMap::with_capacity(len);
while let Some((key, value)) = try!(visitor.visit()) {
values.insert(key, value);
}
try!(visitor.end());
Ok(values)
}
}
impl<K, V> Deserialize for HashMap<K, V>
where K: Deserialize + Eq + Hash,
V: Deserialize,
{
fn deserialize<D>(deserializer: &mut D) -> Result<HashMap<K, V>, D::Error>
where D: Deserializer,
{
deserializer.visit(HashMapVisitor::new())
}
}
///////////////////////////////////////////////////////////////////////////////
// FIXME: `VecMap` is unstable.
/*
pub struct VecMapVisitor<V> { pub struct VecMapVisitor<V> {
marker: PhantomData<VecMap<V>>, marker: PhantomData<VecMap<V>>,
} }
#[cfg(feature = "nightly")]
impl<V> VecMapVisitor<V> { impl<V> VecMapVisitor<V> {
#[inline] #[inline]
pub fn new() -> Self { pub fn new() -> Self {
@@ -724,7 +758,6 @@ impl<V> VecMapVisitor<V> {
} }
} }
#[cfg(feature = "nightly")]
impl<V> Visitor for VecMapVisitor<V> impl<V> Visitor for VecMapVisitor<V>
where V: Deserialize, where V: Deserialize,
{ {
@@ -754,16 +787,16 @@ impl<V> Visitor for VecMapVisitor<V>
} }
} }
#[cfg(feature = "nightly")]
impl<V> Deserialize for VecMap<V> impl<V> Deserialize for VecMap<V>
where V: Deserialize, where V: Deserialize,
{ {
fn deserialize<D>(deserializer: &mut D) -> Result<VecMap<V>, D::Error> fn deserialize<D>(deserializer: &mut D) -> Result<VecMap<V>, D::Error>
where D: Deserializer, where D: Deserializer,
{ {
deserializer.visit_map(VecMapVisitor::new()) deserializer.visit(VecMapVisitor::new())
} }
} }
*/
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
@@ -821,154 +854,3 @@ impl<T: Deserialize> Deserialize for Rc<T> {
Ok(Rc::new(val)) Ok(Rc::new(val))
} }
} }
impl<'a, T: ?Sized> Deserialize for Cow<'a, T> where T: ToOwned, T::Owned: Deserialize, {
#[inline]
fn deserialize<D>(deserializer: &mut D) -> Result<Cow<'a, T>, D::Error>
where D: Deserializer,
{
let val = try!(Deserialize::deserialize(deserializer));
Ok(Cow::Owned(val))
}
}
///////////////////////////////////////////////////////////////////////////////
#[cfg(feature = "nightly")]
impl<T> Deserialize for NonZero<T> where T: Deserialize + PartialEq + Zeroable + Zero {
fn deserialize<D>(deserializer: &mut D) -> Result<NonZero<T>, D::Error> where D: Deserializer {
let value = try!(Deserialize::deserialize(deserializer));
if value == Zero::zero() {
return Err(Error::syntax_error())
}
unsafe {
Ok(NonZero::new(value))
}
}
}
///////////////////////////////////////////////////////////////////////////////
impl<T, E> Deserialize for Result<T, E> where T: Deserialize, E: Deserialize {
fn deserialize<D>(deserializer: &mut D) -> Result<Result<T, E>, D::Error>
where D: Deserializer {
enum Field {
Field0,
Field1,
}
impl Deserialize for Field {
#[inline]
fn deserialize<D>(deserializer: &mut D) -> Result<Field, D::Error>
where D: Deserializer {
struct FieldVisitor<D> {
phantom: PhantomData<D>,
}
impl<D> ::de::Visitor for FieldVisitor<D> where D: Deserializer {
type Value = Field;
fn visit_str<E>(&mut self, value: &str) -> Result<Field, E> where E: Error {
match value {
"Ok" => Ok(Field::Field0),
"Err" => Ok(Field::Field1),
_ => Err(Error::unknown_field_error(value)),
}
}
fn visit_bytes<E>(&mut self, value: &[u8]) -> Result<Field, E> where E: Error {
match str::from_utf8(value) {
Ok(s) => self.visit_str(s),
_ => Err(Error::syntax_error()),
}
}
}
deserializer.visit(FieldVisitor::<D> {
phantom: PhantomData,
})
}
}
struct Visitor<D, T, E>(PhantomData<D>, PhantomData<T>, PhantomData<E>)
where D: Deserializer, T: Deserialize, E: Deserialize;
impl<D, T, E> EnumVisitor for Visitor<D, T, E> where D: Deserializer,
T: Deserialize,
E: Deserialize {
type Value = Result<T, E>;
fn visit<V>(&mut self, mut visitor: V) -> Result<Result<T, E>, V::Error>
where V: VariantVisitor {
match match visitor.visit_variant() {
Ok(val) => val,
Err(err) => return Err(From::from(err)),
} {
Field::Field0 => {
struct Visitor<D, T, E>(PhantomData<D>, PhantomData<T>, PhantomData<E>)
where D: Deserializer,
T: Deserialize,
E: Deserialize;
impl <D, T, E> ::de::Visitor for Visitor<D, T, E> where D: Deserializer,
T: Deserialize,
E: Deserialize {
type Value = Result<T, E>;
fn visit_seq<V>(&mut self, mut visitor: V)
-> Result<Result<T, E>, V::Error> where V: SeqVisitor {
let field0 = match match visitor.visit() {
Ok(val) => val,
Err(err) => return Err(From::from(err)),
} {
Some(value) => value,
None => return Err(Error::end_of_stream_error()),
};
match visitor.end() {
Ok(val) => val,
Err(err) => return Err(From::from(err)),
};
Ok(Result::Ok(field0))
}
}
visitor.visit_seq(Visitor::<D, T, E>(PhantomData,
PhantomData,
PhantomData))
}
Field::Field1 => {
struct Visitor<D, T, E>(PhantomData<D>, PhantomData<T>, PhantomData<E>)
where D: Deserializer,
T: Deserialize,
E: Deserialize;
impl <D, T, E> ::de::Visitor for Visitor<D, T, E> where D: Deserializer,
T: Deserialize,
E: Deserialize {
type Value = Result<T, E>;
fn visit_seq<V>(&mut self, mut visitor: V)
-> Result<Result<T, E>, V::Error> where V: SeqVisitor {
let field0 = match match visitor.visit() {
Ok(val) => val,
Err(err) => return Err(From::from(err)),
} {
Some(value) => value,
None => return Err(Error::end_of_stream_error()),
};
match visitor.end() {
Ok(val) => val,
Err(err) => return Err(From::from(err)),
};
Ok(Result::Err(field0))
}
}
visitor.visit_seq(Visitor::<D, T, E>(PhantomData,
PhantomData,
PhantomData))
}
}
}
}
deserializer.visit_enum("Result",
Visitor::<D, T, E>(PhantomData, PhantomData, PhantomData))
}
}
+27 -177
View File
@@ -25,164 +25,18 @@ pub trait Deserialize {
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
/// `Deserializer` is a trait that can deserialize values by threading a `Visitor` trait through a /// `Deserializer` is an abstract trait that can deserialize values into a `Visitor`.
/// value. It supports two entry point styles which enables different kinds of deserialization.
///
/// 1) The `visit` method. File formats like JSON embed the type of it's construct in it's file
/// format. This allows the `Deserializer` to deserialize into a generic type like
/// `json::Value`, which can represent all JSON types.
///
/// 2) The `visit_*` methods. File formats like bincode do not embed in it's format how to decode
/// it's values. It relies instead on the `Deserialize` type to hint to the `Deserializer` with
/// the `visit_*` methods how it should parse the next value. One downside though to only
/// supporting the `visit_*` types is that it does not allow for deserializing into a generic
/// `json::Value`-esque type.
pub trait Deserializer { pub trait Deserializer {
type Error: Error; type Error: Error;
/// This method walks a visitor through a value as it is being deserialized. /// The `visit` method walks a visitor through a value as it is being deserialized.
fn visit<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error> fn visit<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor; where V: Visitor;
/// This method hints that the `Deserialize` type is expecting a `bool` value. /// The `visit_option` method allows a `Deserialize` type to inform the `Deserializer` that
#[inline] /// it's expecting an optional value. This allows deserializers that encode an optional value
fn visit_bool<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error> /// as a nullable value to convert the null value into a `None`, and a regular value as
where V: Visitor, /// `Some(value)`.
{
self.visit(visitor)
}
/// This method hints that the `Deserialize` type is expecting an `usize` value.
#[inline]
fn visit_usize<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.visit(visitor)
}
/// This method hints that the `Deserialize` type is expecting an `u8` value.
#[inline]
fn visit_u8<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.visit(visitor)
}
/// This method hints that the `Deserialize` type is expecting an `u16` value.
#[inline]
fn visit_u16<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.visit(visitor)
}
/// This method hints that the `Deserialize` type is expecting an `u32` value.
#[inline]
fn visit_u32<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.visit(visitor)
}
/// This method hints that the `Deserialize` type is expecting an `u64` value.
#[inline]
fn visit_u64<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.visit(visitor)
}
/// This method hints that the `Deserialize` type is expecting an `isize` value.
#[inline]
fn visit_isize<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.visit(visitor)
}
/// This method hints that the `Deserialize` type is expecting an `i8` value.
#[inline]
fn visit_i8<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.visit(visitor)
}
/// This method hints that the `Deserialize` type is expecting an `i16` value.
#[inline]
fn visit_i16<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.visit(visitor)
}
/// This method hints that the `Deserialize` type is expecting an `i32` value.
#[inline]
fn visit_i32<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.visit(visitor)
}
/// This method hints that the `Deserialize` type is expecting an `i64` value.
#[inline]
fn visit_i64<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.visit(visitor)
}
/// This method hints that the `Deserialize` type is expecting a `f32` value.
#[inline]
fn visit_f32<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.visit(visitor)
}
/// This method hints that the `Deserialize` type is expecting a `f64` value.
#[inline]
fn visit_f64<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.visit(visitor)
}
/// This method hints that the `Deserialize` type is expecting a `char` value.
#[inline]
fn visit_char<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.visit(visitor)
}
/// This method hints that the `Deserialize` type is expecting a `&str` value.
#[inline]
fn visit_str<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.visit(visitor)
}
/// This method hints that the `Deserialize` type is expecting a `String` value.
#[inline]
fn visit_string<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.visit_str(visitor)
}
/// This method hints that the `Deserialize` type is expecting an `unit` value.
#[inline]
fn visit_unit<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.visit(visitor)
}
/// This method hints that the `Deserialize` type is expecting an `Option` value. This allows
/// deserializers that encode an optional value as a nullable value to convert the null value
/// into a `None`, and a regular value as `Some(value)`.
#[inline] #[inline]
fn visit_option<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error> fn visit_option<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor, where V: Visitor,
@@ -190,8 +44,9 @@ pub trait Deserializer {
self.visit(visitor) self.visit(visitor)
} }
/// This method hints that the `Deserialize` type is expecting a sequence value. This allows /// The `visit_seq` method allows a `Deserialize` type to inform the `Deserializer` that it's
/// deserializers to parse sequences that aren't tagged as sequences. /// expecting a sequence of values. This allows deserializers to parse sequences that aren't
/// tagged as sequences.
#[inline] #[inline]
fn visit_seq<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error> fn visit_seq<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor, where V: Visitor,
@@ -199,8 +54,9 @@ pub trait Deserializer {
self.visit(visitor) self.visit(visitor)
} }
/// This method hints that the `Deserialize` type is expecting a map of values. This allows /// The `visit_map` method allows a `Deserialize` type to inform the `Deserializer` that it's
/// deserializers to parse sequences that aren't tagged as maps. /// expecting a map of values. This allows deserializers to parse sequences that aren't tagged
/// as maps.
#[inline] #[inline]
fn visit_map<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error> fn visit_map<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor, where V: Visitor,
@@ -208,8 +64,9 @@ pub trait Deserializer {
self.visit(visitor) self.visit(visitor)
} }
/// This method hints that the `Deserialize` type is expecting a named unit. This allows /// The `visit_named_unit` method allows a `Deserialize` type to inform the `Deserializer` that
/// deserializers to a named unit that aren't tagged as a named unit. /// it's expecting a named unit. This allows deserializers to a named unit that aren't tagged
/// as a named unit.
#[inline] #[inline]
fn visit_named_unit<V>(&mut self, _name: &str, visitor: V) -> Result<V::Value, Self::Error> fn visit_named_unit<V>(&mut self, _name: &str, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor, where V: Visitor,
@@ -217,8 +74,9 @@ pub trait Deserializer {
self.visit(visitor) self.visit(visitor)
} }
/// This method hints that the `Deserialize` type is expecting a named sequence. /// The `visit_named_seq` method allows a `Deserialize` type to inform the `Deserializer` that
/// This allows deserializers to parse sequences that aren't tagged as sequences. /// it's expecting a named sequence of values. This allows deserializers to parse sequences
/// that aren't tagged as sequences.
#[inline] #[inline]
fn visit_named_seq<V>(&mut self, _name: &str, visitor: V) -> Result<V::Value, Self::Error> fn visit_named_seq<V>(&mut self, _name: &str, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor, where V: Visitor,
@@ -226,8 +84,9 @@ pub trait Deserializer {
self.visit_seq(visitor) self.visit_seq(visitor)
} }
/// This method hints that the `Deserialize` type is expecting a named map. This allows /// The `visit_named_map` method allows a `Deserialize` type to inform the `Deserializer` that
/// deserializers to parse sequences that aren't tagged as maps. /// it's expecting a map of values. This allows deserializers to parse sequences that aren't
/// tagged as maps.
#[inline] #[inline]
fn visit_named_map<V>(&mut self, _name: &str, visitor: V) -> Result<V::Value, Self::Error> fn visit_named_map<V>(&mut self, _name: &str, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor, where V: Visitor,
@@ -235,18 +94,9 @@ pub trait Deserializer {
self.visit_map(visitor) self.visit_map(visitor)
} }
/// This method hints that the `Deserialize` type is expecting a tuple value. This allows /// The `visit_enum` method allows a `Deserialize` type to inform the `Deserializer` that it's
/// deserializers that provide a custom tuple serialization to properly deserialize the type. /// expecting an enum value. This allows deserializers that provide a custom enumeration
#[inline] /// serialization to properly deserialize the type.
fn visit_tuple<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor,
{
self.visit(visitor)
}
/// This method hints that the `Deserialize` type is expecting an enum value. This allows
/// deserializers that provide a custom enumeration serialization to properly deserialize the
/// type.
#[inline] #[inline]
fn visit_enum<V>(&mut self, _enum: &str, _visitor: V) -> Result<V::Value, Self::Error> fn visit_enum<V>(&mut self, _enum: &str, _visitor: V) -> Result<V::Value, Self::Error>
where V: EnumVisitor, where V: EnumVisitor,
@@ -254,9 +104,9 @@ pub trait Deserializer {
Err(Error::syntax_error()) Err(Error::syntax_error())
} }
/// This method hints that the `Deserialize` type is expecting a `Vec<u8>`. This allows /// The `visit_bytes` method allows a `Deserialize` type to inform the `Deserializer` that it's
/// deserializers that provide a custom byte vector serialization to properly deserialize the /// expecting a `Vec<u8>`. This allows deserializers that provide a custom byte vector
/// type. /// serialization to properly deserialize the type.
#[inline] #[inline]
fn visit_bytes<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error> fn visit_bytes<V>(&mut self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor, where V: Visitor,
-7
View File
@@ -6,16 +6,9 @@
//! leaving serde to perform roughly the same speed as a hand written serializer for a specific //! leaving serde to perform roughly the same speed as a hand written serializer for a specific
//! type. //! type.
#![doc(html_root_url="http://erickt.github.io/rust-serde")] #![doc(html_root_url="http://erickt.github.io/rust-serde")]
#![cfg_attr(feature = "nightly", feature(collections, core, enumset, nonzero, step_trait, vecmap, zero_one))]
extern crate num; extern crate num;
#[cfg(feature = "nightly")]
extern crate collections;
#[cfg(feature = "nightly")]
extern crate core;
pub use ser::{Serialize, Serializer}; pub use ser::{Serialize, Serializer};
pub use de::{Deserialize, Deserializer, Error}; pub use de::{Deserialize, Deserializer, Error};
+9 -194
View File
@@ -1,32 +1,9 @@
use std::borrow::Cow; use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::collections::{
BinaryHeap,
BTreeMap,
BTreeSet,
LinkedList,
HashMap,
HashSet,
VecDeque,
};
#[cfg(feature = "nightly")]
use collections::enum_set::{CLike, EnumSet};
#[cfg(feature = "nightly")]
use std::collections::vec_map::VecMap;
use std::hash::Hash; use std::hash::Hash;
#[cfg(feature = "nightly")]
use std::iter;
use std::marker::PhantomData;
#[cfg(feature = "nightly")]
use std::num;
#[cfg(feature = "nightly")]
use std::ops;
use std::path; use std::path;
use std::rc::Rc; use std::rc::Rc;
use std::sync::Arc; use std::sync::Arc;
#[cfg(feature = "nightly")]
use core::nonzero::{NonZero, Zeroable};
use super::{ use super::{
Serialize, Serialize,
Serializer, Serializer,
@@ -142,7 +119,7 @@ impl<T, Iter> SeqVisitor for SeqIteratorVisitor<Iter>
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
impl<T> Serialize for [T] impl<'a, T> Serialize for &'a [T]
where T: Serialize, where T: Serialize,
{ {
#[inline] #[inline]
@@ -200,16 +177,12 @@ array_impls!(30);
array_impls!(31); array_impls!(31);
array_impls!(32); array_impls!(32);
/////////////////////////////////////////////////////////////////////////////// impl<T> Serialize for Vec<T> where T: Serialize {
impl<T> Serialize for BinaryHeap<T>
where T: Serialize + Ord
{
#[inline] #[inline]
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer, where S: Serializer,
{ {
serializer.visit_seq(SeqIteratorVisitor::new(self.iter(), Some(self.len()))) (&self[..]).serialize(serializer)
} }
} }
@@ -224,18 +197,6 @@ impl<T> Serialize for BTreeSet<T>
} }
} }
#[cfg(feature = "nightly")]
impl<T> Serialize for EnumSet<T>
where T: Serialize + CLike
{
#[inline]
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer,
{
serializer.visit_seq(SeqIteratorVisitor::new(self.iter(), Some(self.len())))
}
}
impl<T> Serialize for HashSet<T> impl<T> Serialize for HashSet<T>
where T: Serialize + Eq + Hash, where T: Serialize + Eq + Hash,
{ {
@@ -247,49 +208,6 @@ impl<T> Serialize for HashSet<T>
} }
} }
impl<T> Serialize for LinkedList<T>
where T: Serialize,
{
#[inline]
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer,
{
serializer.visit_seq(SeqIteratorVisitor::new(self.iter(), Some(self.len())))
}
}
#[cfg(feature = "nightly")]
impl<A> Serialize for ops::Range<A>
where A: Serialize + Clone + iter::Step + num::One,
for<'a> &'a A: ops::Add<&'a A, Output = A>,
{
#[inline]
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer,
{
let len = iter::Step::steps_between(&self.start, &self.end, &A::one());
serializer.visit_seq(SeqIteratorVisitor::new(self.clone(), len))
}
}
impl<T> Serialize for Vec<T> where T: Serialize {
#[inline]
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer,
{
(&self[..]).serialize(serializer)
}
}
impl<T> Serialize for VecDeque<T> where T: Serialize {
#[inline]
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer,
{
serializer.visit_seq(SeqIteratorVisitor::new(self.iter(), Some(self.len())))
}
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
impl Serialize for () { impl Serialize for () {
@@ -339,7 +257,7 @@ macro_rules! tuple_impls {
$( $(
$state => { $state => {
self.state += 1; self.state += 1;
Ok(Some(try!(serializer.visit_tuple_elt(&e!(self.tuple.$idx))))) Ok(Some(try!(serializer.visit_seq_elt(&e!(self.tuple.$idx)))))
} }
)+ )+
_ => { _ => {
@@ -358,7 +276,7 @@ macro_rules! tuple_impls {
{ {
#[inline] #[inline]
fn serialize<S: Serializer>(&self, serializer: &mut S) -> Result<(), S::Error> { fn serialize<S: Serializer>(&self, serializer: &mut S) -> Result<(), S::Error> {
serializer.visit_tuple($TupleVisitor::new(self)) serializer.visit_seq($TupleVisitor::new(self))
} }
} }
)+ )+
@@ -539,7 +457,8 @@ impl<K, V> Serialize for HashMap<K, V>
} }
} }
#[cfg(feature = "nightly")] // FIXME: `VecMap` is unstable.
/*
impl<V> Serialize for VecMap<V> impl<V> Serialize for VecMap<V>
where V: Serialize, where V: Serialize,
{ {
@@ -550,6 +469,7 @@ impl<V> Serialize for VecMap<V>
serializer.visit_map(MapIteratorVisitor::new(self.iter(), Some(self.len()))) serializer.visit_map(MapIteratorVisitor::new(self.iter(), Some(self.len())))
} }
} }
*/
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
@@ -598,104 +518,6 @@ impl<T> Serialize for Arc<T> where T: Serialize, {
} }
} }
impl<'a, T: ?Sized> Serialize for Cow<'a, T> where T: Serialize + ToOwned, {
#[inline]
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer,
{
(**self).serialize(serializer)
}
}
///////////////////////////////////////////////////////////////////////////////
impl<T, E> Serialize for Result<T, E> where T: Serialize, E: Serialize {
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer {
match *self {
Result::Ok(ref field0) => {
struct Visitor<'a, T, E> where T: Serialize + 'a, E: Serialize + 'a {
state: usize,
value: (&'a T,),
_structure_ty: PhantomData<&'a Result<T, E>>,
}
impl<'a, T, E> SeqVisitor for Visitor<'a, T, E> where T: Serialize + 'a,
E: Serialize + 'a {
#[inline]
fn visit<S>(&mut self, serializer: &mut S) -> Result<Option<()>, S::Error>
where S: Serializer {
match self.state {
0 => {
self.state += 1;
let v = match serializer.visit_seq_elt(&self.value.0) {
Ok(val) => val,
Err(err) => return Err(From::from(err)),
};
Ok(Some(v))
}
_ => Ok(None),
}
}
#[inline]
fn len(&self) -> Option<usize> {
Some(1)
}
}
let field0: &T = field0;
let data: PhantomData<&Result<&T,E>> = PhantomData;
let visitor = Visitor {
value: (&field0,),
state: 0,
_structure_ty: data
};
serializer.visit_enum_seq("Result", "Ok", visitor)
}
Result::Err(ref field0) => {
struct Visitor<'a, T, E> where T: Serialize + 'a, E: Serialize + 'a {
state: usize,
value: (&'a E,),
_structure_ty: PhantomData<&'a Result<T, E>>,
}
impl<'a, T, E> SeqVisitor for Visitor<'a, T, E> where T: Serialize + 'a,
E: Serialize + 'a {
#[inline]
fn visit<S>(&mut self, serializer: &mut S) -> Result<Option<()>, S::Error>
where S: Serializer {
match self.state {
0 => {
self.state += 1;
let v = match serializer.visit_seq_elt(&self.value.0) {
Ok(val) => val,
Err(err) => return Err(From::from(err)),
};
Ok(Some(v))
}
_ => Ok(None),
}
}
#[inline]
fn len(&self) -> Option<usize> {
Some(1)
}
}
let field0: &E = field0;
let data: PhantomData<&Result<T,&E>> = PhantomData;
let visitor = Visitor {
value: (&field0,),
state: 0,
_structure_ty: data
};
serializer.visit_enum_seq("Result", "Err", visitor)
}
}
}
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
impl Serialize for path::Path { impl Serialize for path::Path {
@@ -713,10 +535,3 @@ impl Serialize for path::PathBuf {
self.to_str().unwrap().serialize(serializer) self.to_str().unwrap().serialize(serializer)
} }
} }
#[cfg(feature = "nightly")]
impl<T> Serialize for NonZero<T> where T: Serialize + Zeroable {
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer {
(**self).serialize(serializer)
}
}
+10 -54
View File
@@ -132,37 +132,13 @@ pub trait Serializer {
fn visit_seq<V>(&mut self, visitor: V) -> Result<(), Self::Error> fn visit_seq<V>(&mut self, visitor: V) -> Result<(), Self::Error>
where V: SeqVisitor; where V: SeqVisitor;
fn visit_seq_elt<T>(&mut self, value: T) -> Result<(), Self::Error>
where T: Serialize;
#[inline]
fn visit_tuple<V>(&mut self, visitor: V) -> Result<(), Self::Error>
where V: SeqVisitor,
{
self.visit_seq(visitor)
}
#[inline]
fn visit_tuple_elt<T>(&mut self, value: T) -> Result<(), Self::Error>
where T: Serialize
{
self.visit_seq_elt(value)
}
#[inline] #[inline]
fn visit_named_seq<V>(&mut self, fn visit_named_seq<V>(&mut self,
_name: &'static str, _name: &'static str,
visitor: V) -> Result<(), Self::Error> visitor: V) -> Result<(), Self::Error>
where V: SeqVisitor, where V: SeqVisitor,
{ {
self.visit_tuple(visitor) self.visit_seq(visitor)
}
#[inline]
fn visit_named_seq_elt<T>(&mut self, value: T) -> Result<(), Self::Error>
where T: Serialize
{
self.visit_tuple_elt(value)
} }
#[inline] #[inline]
@@ -172,23 +148,15 @@ pub trait Serializer {
visitor: V) -> Result<(), Self::Error> visitor: V) -> Result<(), Self::Error>
where V: SeqVisitor, where V: SeqVisitor,
{ {
self.visit_tuple(visitor) self.visit_seq(visitor)
} }
#[inline] fn visit_seq_elt<T>(&mut self, value: T) -> Result<(), Self::Error>
fn visit_enum_seq_elt<T>(&mut self, value: T) -> Result<(), Self::Error> where T: Serialize;
where T: Serialize
{
self.visit_tuple_elt(value)
}
fn visit_map<V>(&mut self, visitor: V) -> Result<(), Self::Error> fn visit_map<V>(&mut self, visitor: V) -> Result<(), Self::Error>
where V: MapVisitor; where V: MapVisitor;
fn visit_map_elt<K, V>(&mut self, key: K, value: V) -> Result<(), Self::Error>
where K: Serialize,
V: Serialize;
#[inline] #[inline]
fn visit_named_map<V>(&mut self, fn visit_named_map<V>(&mut self,
_name: &'static str, _name: &'static str,
@@ -198,31 +166,19 @@ pub trait Serializer {
self.visit_map(visitor) self.visit_map(visitor)
} }
#[inline]
fn visit_named_map_elt<K, V>(&mut self, key: K, value: V) -> Result<(), Self::Error>
where K: Serialize,
V: Serialize,
{
self.visit_map_elt(key, value)
}
#[inline] #[inline]
fn visit_enum_map<V>(&mut self, fn visit_enum_map<V>(&mut self,
_name: &'static str, _name: &'static str,
variant: &'static str, _variant: &'static str,
visitor: V) -> Result<(), Self::Error> visitor: V) -> Result<(), Self::Error>
where V: MapVisitor, where V: MapVisitor,
{ {
self.visit_named_map(variant, visitor) self.visit_map(visitor)
} }
#[inline] fn visit_map_elt<K, V>(&mut self, key: K, value: V) -> Result<(), Self::Error>
fn visit_enum_map_elt<K, V>(&mut self, key: K, value: V) -> Result<(), Self::Error>
where K: Serialize, where K: Serialize,
V: Serialize, V: Serialize;
{
self.visit_named_map_elt(key, value)
}
/// Specify a format string for the serializer. /// Specify a format string for the serializer.
/// ///
+1 -1
View File
@@ -1,6 +1,6 @@
[package] [package]
name = "serde_codegen" name = "serde_codegen"
version = "0.4.3" version = "0.4.2"
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"] authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
license = "MIT/Apache-2.0" license = "MIT/Apache-2.0"
description = "Macros to auto-generate implementations for the serde framework" description = "Macros to auto-generate implementations for the serde framework"
+2 -2
View File
@@ -476,7 +476,7 @@ fn serialize_tuple_struct_visitor(
quote_arm!(cx, quote_arm!(cx,
$i => { $i => {
self.state += 1; self.state += 1;
let v = try!(serializer.visit_named_seq_elt(&$expr)); let v = try!(serializer.visit_seq_elt(&$expr));
Ok(Some(v)) Ok(Some(v))
} }
) )
@@ -559,7 +559,7 @@ fn serialize_struct_visitor<I>(
Ok( Ok(
Some( Some(
try!( try!(
serializer.visit_named_map_elt( serializer.visit_map_elt(
$key_expr, $key_expr,
$value_expr, $value_expr,
) )
+2 -2
View File
@@ -1,6 +1,6 @@
[package] [package]
name = "serde_macros" name = "serde_macros"
version = "0.4.4" version = "0.4.2"
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"] authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
license = "MIT/Apache-2.0" license = "MIT/Apache-2.0"
description = "Macros to auto-generate implementations for the serde framework" description = "Macros to auto-generate implementations for the serde framework"
@@ -16,4 +16,4 @@ serde_codegen = { version = "*", path = "../serde_codegen", default-features = f
[dev-dependencies] [dev-dependencies]
num = "*" num = "*"
rustc-serialize = "*" rustc-serialize = "*"
serde = { version = "*", path = "../serde", features = ["nightly"] } serde = { version = "*", path = "../serde" }
+1 -1
View File
@@ -1,6 +1,6 @@
[package] [package]
name = "serde_tests" name = "serde_tests"
version = "0.4.3" version = "0.4.1"
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"] authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
license = "MIT/Apache-2.0" license = "MIT/Apache-2.0"
description = "A generic serialization/deserialization framework" description = "A generic serialization/deserialization framework"
+6 -6
View File
@@ -50,7 +50,7 @@ impl rustc_serialize::Decodable for HttpProtocol {
impl FromStr for HttpProtocol { impl FromStr for HttpProtocol {
type Err = (); type Err = ();
fn from_str(_s: &str) -> Result<HttpProtocol, ()> { unimplemented!() } fn from_str(s: &str) -> Result<HttpProtocol, ()> { unimplemented!() }
} }
impl FromPrimitive for HttpProtocol { impl FromPrimitive for HttpProtocol {
@@ -104,7 +104,7 @@ enum HttpMethod {
impl FromStr for HttpMethod { impl FromStr for HttpMethod {
type Err = (); type Err = ();
fn from_str(_s: &str) -> Result<HttpMethod, ()> { unimplemented!() } fn from_str(s: &str) -> Result<HttpMethod, ()> { unimplemented!() }
} }
impl FromPrimitive for HttpMethod { impl FromPrimitive for HttpMethod {
@@ -174,7 +174,7 @@ enum CacheStatus {
impl FromStr for CacheStatus { impl FromStr for CacheStatus {
type Err = (); type Err = ();
fn from_str(_s: &str) -> Result<CacheStatus, ()> { unimplemented!() } fn from_str(s: &str) -> Result<CacheStatus, ()> { unimplemented!() }
} }
impl FromPrimitive for CacheStatus { impl FromPrimitive for CacheStatus {
@@ -244,7 +244,7 @@ enum OriginProtocol {
impl FromStr for OriginProtocol { impl FromStr for OriginProtocol {
type Err = (); type Err = ();
fn from_str(_s: &str) -> Result<OriginProtocol, ()> { unimplemented!() } fn from_str(s: &str) -> Result<OriginProtocol, ()> { unimplemented!() }
} }
impl FromPrimitive for OriginProtocol { impl FromPrimitive for OriginProtocol {
@@ -307,7 +307,7 @@ enum ZonePlan {
impl FromStr for ZonePlan { impl FromStr for ZonePlan {
type Err = (); type Err = ();
fn from_str(_s: &str) -> Result<ZonePlan, ()> { unimplemented!() } fn from_str(s: &str) -> Result<ZonePlan, ()> { unimplemented!() }
} }
impl FromPrimitive for ZonePlan { impl FromPrimitive for ZonePlan {
@@ -622,7 +622,7 @@ enum Country {
impl FromStr for Country { impl FromStr for Country {
type Err = (); type Err = ();
fn from_str(_s: &str) -> Result<Country, ()> { unimplemented!() } fn from_str(s: &str) -> Result<Country, ()> { unimplemented!() }
} }
impl FromPrimitive for Country { impl FromPrimitive for Country {
+4
View File
@@ -1,7 +1,11 @@
/*
mod test_annotations; mod test_annotations;
mod test_bytes; mod test_bytes;
mod test_de; mod test_de;
mod test_json; mod test_json;
mod test_json_builder; mod test_json_builder;
*/
mod test_macros; mod test_macros;
/*
mod test_ser; mod test_ser;
*/