Move the Deserializer typarams onto from the method to the Deserializable trait
This commit is contained in:
parent
25891838c4
commit
a24075559f
@ -73,8 +73,22 @@ fn expand_deriving_serializable(cx: &mut ExtCtxt,
|
||||
span: sp,
|
||||
attributes: vec!(),
|
||||
path: Path::new(vec!("serde", "ser", "Serializable")),
|
||||
/*
|
||||
path: Path::new_(vec!("serde", "ser", "Serializable"), None,
|
||||
vec!(box Literal(Path::new_local("__S")),
|
||||
box Literal(Path::new_local("__E"))), true),
|
||||
*/
|
||||
additional_bounds: Vec::new(),
|
||||
generics: LifetimeBounds::empty(),
|
||||
/*
|
||||
generics: LifetimeBounds {
|
||||
lifetimes: Vec::new(),
|
||||
bounds: vec!(("__S", None, vec!(Path::new_(
|
||||
vec!("serde", "ser", "Serializer"), None,
|
||||
vec!(box Literal(Path::new_local("__E"))), true))),
|
||||
("__E", None, vec!()))
|
||||
},
|
||||
*/
|
||||
methods: vec!(
|
||||
MethodDef {
|
||||
name: "serialize",
|
||||
@ -204,36 +218,21 @@ pub fn expand_deriving_deserializable(cx: &mut ExtCtxt,
|
||||
let trait_def = TraitDef {
|
||||
span: span,
|
||||
attributes: Vec::new(),
|
||||
path: Path::new(vec!("serde", "de", "Deserializable")),
|
||||
path: Path::new_(vec!("serde", "de", "Deserializable"), None,
|
||||
vec!(box Literal(Path::new_local("__D")),
|
||||
box Literal(Path::new_local("__E"))), true),
|
||||
additional_bounds: Vec::new(),
|
||||
generics: LifetimeBounds::empty(),
|
||||
generics: LifetimeBounds {
|
||||
lifetimes: Vec::new(),
|
||||
bounds: vec!(("__D", None, vec!(Path::new_(
|
||||
vec!("serde", "de", "Deserializer"), None,
|
||||
vec!(box Literal(Path::new_local("__E"))), true))),
|
||||
("__E", None, vec!()))
|
||||
},
|
||||
methods: vec!(
|
||||
MethodDef {
|
||||
name: "deserialize_token",
|
||||
generics: LifetimeBounds {
|
||||
lifetimes: Vec::new(),
|
||||
bounds: vec!(
|
||||
(
|
||||
"__D",
|
||||
None,
|
||||
vec!(
|
||||
Path::new_(
|
||||
vec!("serde", "de", "Deserializer"),
|
||||
None,
|
||||
vec!(
|
||||
box Literal(Path::new_local("__E"))
|
||||
),
|
||||
true
|
||||
)
|
||||
)
|
||||
),
|
||||
(
|
||||
"__E",
|
||||
None,
|
||||
vec!(),
|
||||
),
|
||||
)
|
||||
},
|
||||
generics: LifetimeBounds::empty(),
|
||||
explicit_self: None,
|
||||
args: vec!(
|
||||
Ptr(
|
||||
|
@ -252,7 +252,7 @@ mod deserializer {
|
||||
|
||||
#[inline]
|
||||
fn missing_field<
|
||||
T: de::Deserializable
|
||||
T: de::Deserializable<AnimalDeserializer, Error>
|
||||
>(&mut self, _field: &'static str) -> Result<T, Error> {
|
||||
Err(SyntaxError)
|
||||
}
|
||||
|
@ -45,12 +45,9 @@ impl ser::Serializable for HttpProtocol {
|
||||
}
|
||||
}
|
||||
|
||||
impl de::Deserializable for HttpProtocol {
|
||||
impl<D: de::Deserializer<E>, E> de::Deserializable<D, E> for HttpProtocol {
|
||||
#[inline]
|
||||
fn deserialize_token<
|
||||
D: de::Deserializer<E>,
|
||||
E
|
||||
>(d: &mut D, token: de::Token) -> Result<HttpProtocol, E> {
|
||||
fn deserialize_token(d: &mut D, token: de::Token) -> Result<HttpProtocol, E> {
|
||||
d.expect_from_primitive(token)
|
||||
}
|
||||
}
|
||||
@ -80,12 +77,9 @@ impl ser::Serializable for HttpMethod {
|
||||
}
|
||||
}
|
||||
|
||||
impl de::Deserializable for HttpMethod {
|
||||
impl<D: de::Deserializer<E>, E> de::Deserializable<D, E> for HttpMethod {
|
||||
#[inline]
|
||||
fn deserialize_token<
|
||||
D: de::Deserializer<E>,
|
||||
E
|
||||
>(d: &mut D, token: de::Token) -> Result<HttpMethod, E> {
|
||||
fn deserialize_token(d: &mut D, token: de::Token) -> Result<HttpMethod, E> {
|
||||
d.expect_from_primitive(token)
|
||||
}
|
||||
}
|
||||
@ -108,12 +102,9 @@ impl ser::Serializable for CacheStatus {
|
||||
}
|
||||
}
|
||||
|
||||
impl de::Deserializable for CacheStatus {
|
||||
impl<D: de::Deserializer<E>, E> de::Deserializable<D, E> for CacheStatus {
|
||||
#[inline]
|
||||
fn deserialize_token<
|
||||
D: de::Deserializer<E>,
|
||||
E
|
||||
>(d: &mut D, token: de::Token) -> Result<CacheStatus, E> {
|
||||
fn deserialize_token(d: &mut D, token: de::Token) -> Result<CacheStatus, E> {
|
||||
d.expect_from_primitive(token)
|
||||
}
|
||||
}
|
||||
@ -145,12 +136,9 @@ impl ser::Serializable for OriginProtocol {
|
||||
}
|
||||
}
|
||||
|
||||
impl de::Deserializable for OriginProtocol {
|
||||
impl<D: de::Deserializer<E>, E> de::Deserializable<D, E> for OriginProtocol {
|
||||
#[inline]
|
||||
fn deserialize_token<
|
||||
D: de::Deserializer<E>,
|
||||
E
|
||||
>(d: &mut D, token: de::Token) -> Result<OriginProtocol, E> {
|
||||
fn deserialize_token(d: &mut D, token: de::Token) -> Result<OriginProtocol, E> {
|
||||
d.expect_from_primitive(token)
|
||||
}
|
||||
}
|
||||
@ -174,12 +162,9 @@ impl ser::Serializable for ZonePlan {
|
||||
}
|
||||
}
|
||||
|
||||
impl de::Deserializable for ZonePlan {
|
||||
impl<D: de::Deserializer<E>, E> de::Deserializable<D, E> for ZonePlan {
|
||||
#[inline]
|
||||
fn deserialize_token<
|
||||
D: de::Deserializer<E>,
|
||||
E
|
||||
>(d: &mut D, token: de::Token) -> Result<ZonePlan, E> {
|
||||
fn deserialize_token(d: &mut D, token: de::Token) -> Result<ZonePlan, E> {
|
||||
d.expect_from_primitive(token)
|
||||
}
|
||||
}
|
||||
@ -454,12 +439,9 @@ impl ser::Serializable for Country {
|
||||
}
|
||||
}
|
||||
|
||||
impl de::Deserializable for Country {
|
||||
impl<D: de::Deserializer<E>, E> de::Deserializable<D, E> for Country {
|
||||
#[inline]
|
||||
fn deserialize_token<
|
||||
D: de::Deserializer<E>,
|
||||
E
|
||||
>(d: &mut D, token: de::Token) -> Result<Country, E> {
|
||||
fn deserialize_token(d: &mut D, token: de::Token) -> Result<Country, E> {
|
||||
d.expect_from_primitive(token)
|
||||
}
|
||||
}
|
||||
|
@ -248,7 +248,7 @@ mod deserializer {
|
||||
|
||||
#[inline]
|
||||
fn missing_field<
|
||||
T: de::Deserializable
|
||||
T: de::Deserializable<IntDeserializer, Error>
|
||||
>(&mut self, _field: &'static str) -> Result<T, Error> {
|
||||
Err(SyntaxError)
|
||||
}
|
||||
@ -298,9 +298,9 @@ fn bench_decoder_100(b: &mut Bencher) {
|
||||
}
|
||||
|
||||
fn run_deserializer<
|
||||
E: Show,
|
||||
D: Deserializer<E>,
|
||||
T: Clone + PartialEq + Show + Deserializable
|
||||
E: Show,
|
||||
T: Clone + PartialEq + Show + Deserializable<D, E>
|
||||
>(mut d: D, value: T) {
|
||||
let v: T = Deserializable::deserialize(&mut d).unwrap();
|
||||
|
||||
|
@ -372,7 +372,7 @@ mod deserializer {
|
||||
|
||||
#[inline]
|
||||
fn missing_field<
|
||||
T: de::Deserializable
|
||||
T: de::Deserializable<OuterDeserializer, Error>
|
||||
>(&mut self, _field: &'static str) -> Result<T, Error> {
|
||||
Err(SyntaxError)
|
||||
}
|
||||
|
@ -316,7 +316,7 @@ mod deserializer {
|
||||
|
||||
#[inline]
|
||||
fn missing_field<
|
||||
T: de::Deserializable
|
||||
T: de::Deserializable<IntDeserializer, Error>
|
||||
>(&mut self, _field: &'static str) -> Result<T, Error> {
|
||||
Err(SyntaxError)
|
||||
}
|
||||
@ -388,7 +388,7 @@ mod deserializer {
|
||||
|
||||
#[inline]
|
||||
fn missing_field<
|
||||
T: de::Deserializable
|
||||
T: de::Deserializable<U8Deserializer, Error>
|
||||
>(&mut self, _field: &'static str) -> Result<T, Error> {
|
||||
Err(SyntaxError)
|
||||
}
|
||||
@ -398,8 +398,8 @@ mod deserializer {
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
fn run_decoder<
|
||||
E: Show,
|
||||
D: Decoder<E>,
|
||||
E: Show,
|
||||
T: Clone + PartialEq + Show + Decodable<D, E>
|
||||
>(mut d: D, value: T) {
|
||||
let v: T = Decodable::decode(&mut d).unwrap();
|
||||
@ -408,9 +408,9 @@ fn run_decoder<
|
||||
}
|
||||
|
||||
fn run_deserializer<
|
||||
E: Show,
|
||||
D: Deserializer<E>,
|
||||
T: Clone + PartialEq + Show + Deserializable
|
||||
E: Show,
|
||||
T: Clone + PartialEq + Show + Deserializable<D, E>
|
||||
>(mut d: D, value: T) {
|
||||
let v: T = Deserializable::deserialize(&mut d).unwrap();
|
||||
|
||||
|
223
src/de.rs
223
src/de.rs
@ -192,7 +192,7 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
|
||||
/// Called when a `Deserializable` structure did not deserialize a field
|
||||
/// named `field`.
|
||||
fn missing_field<
|
||||
T: Deserializable
|
||||
T: Deserializable<Self, E>
|
||||
>(&mut self, field: &'static str) -> Result<T, E>;
|
||||
|
||||
/// Called when a deserializable has decided to not consume this token.
|
||||
@ -304,7 +304,7 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
|
||||
|
||||
#[inline]
|
||||
fn expect_option<
|
||||
T: Deserializable
|
||||
T: Deserializable<Self, E>
|
||||
>(&mut self, token: Token) -> Result<Option<T>, E> {
|
||||
match token {
|
||||
Option(false) => Ok(None),
|
||||
@ -327,7 +327,7 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
|
||||
|
||||
#[inline]
|
||||
fn expect_tuple_elt<
|
||||
T: Deserializable
|
||||
T: Deserializable<Self, E>
|
||||
>(&mut self) -> Result<T, E> {
|
||||
Deserializable::deserialize(self)
|
||||
}
|
||||
@ -356,7 +356,7 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
|
||||
|
||||
#[inline]
|
||||
fn expect_struct_field<
|
||||
T: Deserializable
|
||||
T: Deserializable<Self, E>
|
||||
>(&mut self, name: &str) -> Result<T, E> {
|
||||
match try!(self.expect_token()) {
|
||||
Str(n) => {
|
||||
@ -404,7 +404,7 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
|
||||
|
||||
#[inline]
|
||||
fn expect_enum_elt<
|
||||
T: Deserializable
|
||||
T: Deserializable<Self, E>
|
||||
>(&mut self) -> Result<T, E> {
|
||||
Deserializable::deserialize(self)
|
||||
}
|
||||
@ -428,7 +428,7 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
|
||||
|
||||
#[inline]
|
||||
fn expect_seq_elt_or_end<
|
||||
T: Deserializable
|
||||
T: Deserializable<Self, E>
|
||||
>(&mut self) -> Result<Option<T>, E> {
|
||||
match try!(self.expect_token()) {
|
||||
End => Ok(None),
|
||||
@ -442,7 +442,7 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
|
||||
#[inline]
|
||||
fn expect_seq<
|
||||
'a,
|
||||
T: Deserializable,
|
||||
T: Deserializable<Self, E>,
|
||||
C: FromIterator<T>
|
||||
>(&'a mut self, token: Token) -> Result<C, E> {
|
||||
let len = try!(self.expect_seq_start(token));
|
||||
@ -471,8 +471,8 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
|
||||
|
||||
#[inline]
|
||||
fn expect_map_elt_or_end<
|
||||
K: Deserializable,
|
||||
V: Deserializable
|
||||
K: Deserializable<Self, E>,
|
||||
V: Deserializable<Self, E>
|
||||
>(&mut self) -> Result<Option<(K, V)>, E> {
|
||||
match try!(self.expect_token()) {
|
||||
End => Ok(None),
|
||||
@ -487,8 +487,8 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
|
||||
#[inline]
|
||||
fn expect_map<
|
||||
'a,
|
||||
K: Deserializable,
|
||||
V: Deserializable,
|
||||
K: Deserializable<Self, E>,
|
||||
V: Deserializable<Self, E>,
|
||||
C: FromIterator<(K, V)>
|
||||
>(&'a mut self, token: Token) -> Result<C, E> {
|
||||
let len = try!(self.expect_map_start(token));
|
||||
@ -518,9 +518,9 @@ struct SeqDeserializer<'a, D, E> {
|
||||
|
||||
impl<
|
||||
'a,
|
||||
T: Deserializable,
|
||||
D: Deserializer<E>,
|
||||
E
|
||||
E,
|
||||
T: Deserializable<D, E>
|
||||
> Iterator<T> for SeqDeserializer<'a, D, E> {
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<T> {
|
||||
@ -549,10 +549,10 @@ struct MapDeserializer<'a, D, E> {
|
||||
|
||||
impl<
|
||||
'a,
|
||||
K: Deserializable,
|
||||
V: Deserializable,
|
||||
D: Deserializer<E>,
|
||||
E
|
||||
E,
|
||||
K: Deserializable<D, E>,
|
||||
V: Deserializable<D, E>
|
||||
> Iterator<(K, V)> for MapDeserializer<'a, D, E> {
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<(K, V)> {
|
||||
@ -573,32 +573,23 @@ impl<
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
pub trait Deserializable {
|
||||
pub trait Deserializable<D: Deserializer<E>, E> {
|
||||
#[inline]
|
||||
fn deserialize<
|
||||
D: Deserializer<E>,
|
||||
E
|
||||
>(d: &mut D) -> Result<Self, E> {
|
||||
fn deserialize(d: &mut D) -> Result<Self, E> {
|
||||
let token = try!(d.expect_token());
|
||||
Deserializable::deserialize_token(d, token)
|
||||
}
|
||||
|
||||
fn deserialize_token<
|
||||
D: Deserializer<E>,
|
||||
E
|
||||
>(d: &mut D, token: Token) -> Result<Self, E>;
|
||||
fn deserialize_token(d: &mut D, token: Token) -> Result<Self, E>;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
macro_rules! impl_deserializable {
|
||||
($ty:ty, $method:ident) => {
|
||||
impl Deserializable for $ty {
|
||||
impl<D: Deserializer<E>, E> Deserializable<D, E> for $ty {
|
||||
#[inline]
|
||||
fn deserialize_token<
|
||||
D: Deserializer<E>,
|
||||
E
|
||||
>(d: &mut D, token: Token) -> Result<$ty, E> {
|
||||
fn deserialize_token(d: &mut D, token: Token) -> Result<$ty, E> {
|
||||
d.$method(token)
|
||||
}
|
||||
}
|
||||
@ -624,66 +615,72 @@ impl_deserializable!(String, expect_string)
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
impl<T: Deserializable> Deserializable for Box<T> {
|
||||
impl<
|
||||
D: Deserializer<E>,
|
||||
E,
|
||||
T: Deserializable<D, E>
|
||||
> Deserializable<D, E> for Box<T> {
|
||||
#[inline]
|
||||
fn deserialize_token<
|
||||
D: Deserializer<E>,
|
||||
E
|
||||
>(d: &mut D, token: Token) -> Result<Box<T>, E> {
|
||||
fn deserialize_token(d: &mut D, token: Token) -> Result<Box<T>, E> {
|
||||
Ok(box try!(Deserializable::deserialize_token(d, token)))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Deserializable + 'static> Deserializable for Gc<T> {
|
||||
impl<
|
||||
D: Deserializer<E>,
|
||||
E,
|
||||
T: Deserializable<D, E> + 'static
|
||||
> Deserializable<D, E> for Gc<T> {
|
||||
#[inline]
|
||||
fn deserialize_token<
|
||||
D: Deserializer<E>,
|
||||
E
|
||||
>(d: &mut D, token: Token) -> Result<Gc<T>, E> {
|
||||
fn deserialize_token(d: &mut D, token: Token) -> Result<Gc<T>, E> {
|
||||
Ok(box (GC) try!(Deserializable::deserialize_token(d, token)))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Deserializable> Deserializable for Rc<T> {
|
||||
impl<
|
||||
D: Deserializer<E>,
|
||||
E,
|
||||
T: Deserializable<D, E>
|
||||
> Deserializable<D, E> for Rc<T> {
|
||||
#[inline]
|
||||
fn deserialize_token<
|
||||
D: Deserializer<E>,
|
||||
E
|
||||
>(d: &mut D, token: Token) -> Result<Rc<T>, E> {
|
||||
fn deserialize_token(d: &mut D, token: Token) -> Result<Rc<T>, E> {
|
||||
Ok(Rc::new(try!(Deserializable::deserialize_token(d, token))))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Deserializable + Send + Sync> Deserializable for Arc<T> {
|
||||
impl<
|
||||
D: Deserializer<E>,
|
||||
E,
|
||||
T: Deserializable<D, E> + Send + Sync
|
||||
> Deserializable<D, E> for Arc<T> {
|
||||
#[inline]
|
||||
fn deserialize_token<
|
||||
D: Deserializer<E>,
|
||||
E
|
||||
>(d: &mut D, token: Token) -> Result<Arc<T>, E> {
|
||||
fn deserialize_token(d: &mut D, token: Token) -> Result<Arc<T>, E> {
|
||||
Ok(Arc::new(try!(Deserializable::deserialize_token(d, token))))
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
impl<T: Deserializable> Deserializable for Option<T> {
|
||||
impl<
|
||||
D: Deserializer<E>,
|
||||
E,
|
||||
T: Deserializable<D ,E>
|
||||
> Deserializable<D, E> for Option<T> {
|
||||
#[inline]
|
||||
fn deserialize_token<
|
||||
D: Deserializer<E>,
|
||||
E
|
||||
>(d: &mut D, token: Token) -> Result<Option<T>, E> {
|
||||
fn deserialize_token(d: &mut D, token: Token) -> Result<Option<T>, E> {
|
||||
d.expect_option(token)
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
impl<T: Deserializable> Deserializable for Vec<T> {
|
||||
impl<
|
||||
D: Deserializer<E>,
|
||||
E,
|
||||
T: Deserializable<D ,E>
|
||||
> Deserializable<D, E> for Vec<T> {
|
||||
#[inline]
|
||||
fn deserialize_token<
|
||||
D: Deserializer<E>,
|
||||
E
|
||||
>(d: &mut D, token: Token) -> Result<Vec<T>, E> {
|
||||
fn deserialize_token(d: &mut D, token: Token) -> Result<Vec<T>, E> {
|
||||
d.expect_seq(token)
|
||||
}
|
||||
}
|
||||
@ -691,27 +688,25 @@ impl<T: Deserializable> Deserializable for Vec<T> {
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
impl<
|
||||
K: Deserializable + Eq + Hash,
|
||||
V: Deserializable
|
||||
> Deserializable for HashMap<K, V> {
|
||||
D: Deserializer<E>,
|
||||
E,
|
||||
K: Deserializable<D, E> + Eq + Hash,
|
||||
V: Deserializable<D, E>
|
||||
> Deserializable<D, E> for HashMap<K, V> {
|
||||
#[inline]
|
||||
fn deserialize_token<
|
||||
D: Deserializer<E>,
|
||||
E
|
||||
>(d: &mut D, token: Token) -> Result<HashMap<K, V>, E> {
|
||||
fn deserialize_token(d: &mut D, token: Token) -> Result<HashMap<K, V>, E> {
|
||||
d.expect_map(token)
|
||||
}
|
||||
}
|
||||
|
||||
impl<
|
||||
K: Deserializable + Ord,
|
||||
V: Deserializable
|
||||
> Deserializable for TreeMap<K, V> {
|
||||
D: Deserializer<E>,
|
||||
E,
|
||||
K: Deserializable<D, E> + Ord,
|
||||
V: Deserializable<D, E>
|
||||
> Deserializable<D, E> for TreeMap<K, V> {
|
||||
#[inline]
|
||||
fn deserialize_token<
|
||||
D: Deserializer<E>,
|
||||
E
|
||||
>(d: &mut D, token: Token) -> Result<TreeMap<K, V>, E> {
|
||||
fn deserialize_token(d: &mut D, token: Token) -> Result<TreeMap<K, V>, E> {
|
||||
d.expect_map(token)
|
||||
}
|
||||
}
|
||||
@ -719,25 +714,23 @@ impl<
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
impl<
|
||||
T: Deserializable + Eq + Hash
|
||||
> Deserializable for HashSet<T> {
|
||||
D: Deserializer<E>,
|
||||
E,
|
||||
T: Deserializable<D, E> + Eq + Hash
|
||||
> Deserializable<D, E> for HashSet<T> {
|
||||
#[inline]
|
||||
fn deserialize_token<
|
||||
D: Deserializer<E>,
|
||||
E
|
||||
>(d: &mut D, token: Token) -> Result<HashSet<T>, E> {
|
||||
fn deserialize_token(d: &mut D, token: Token) -> Result<HashSet<T>, E> {
|
||||
d.expect_seq(token)
|
||||
}
|
||||
}
|
||||
|
||||
impl<
|
||||
T: Deserializable + Ord
|
||||
> Deserializable for TreeSet<T> {
|
||||
D: Deserializer<E>,
|
||||
E,
|
||||
T: Deserializable<D, E> + Ord
|
||||
> Deserializable<D, E> for TreeSet<T> {
|
||||
#[inline]
|
||||
fn deserialize_token<
|
||||
D: Deserializer<E>,
|
||||
E
|
||||
>(d: &mut D, token: Token) -> Result<TreeSet<T>, E> {
|
||||
fn deserialize_token(d: &mut D, token: Token) -> Result<TreeSet<T>, E> {
|
||||
d.expect_seq(token)
|
||||
}
|
||||
}
|
||||
@ -750,26 +743,25 @@ macro_rules! peel {
|
||||
|
||||
macro_rules! impl_deserialize_tuple {
|
||||
() => {
|
||||
impl Deserializable for () {
|
||||
impl<
|
||||
D: Deserializer<E>,
|
||||
E
|
||||
> Deserializable<D, E> for () {
|
||||
#[inline]
|
||||
fn deserialize_token<
|
||||
D: Deserializer<E>,
|
||||
E
|
||||
>(d: &mut D, token: Token) -> Result<(), E> {
|
||||
fn deserialize_token(d: &mut D, token: Token) -> Result<(), E> {
|
||||
d.expect_null(token)
|
||||
}
|
||||
}
|
||||
};
|
||||
( $($name:ident,)+ ) => {
|
||||
impl<
|
||||
$($name: Deserializable),*
|
||||
> Deserializable for ($($name,)*) {
|
||||
D: Deserializer<E>,
|
||||
E,
|
||||
$($name: Deserializable<D, E>),*
|
||||
> Deserializable<D, E> for ($($name,)*) {
|
||||
#[inline]
|
||||
#[allow(uppercase_variables)]
|
||||
fn deserialize_token<
|
||||
D: Deserializer<E>,
|
||||
E
|
||||
>(d: &mut D, token: Token) -> Result<($($name,)*), E> {
|
||||
fn deserialize_token(d: &mut D, token: Token) -> Result<($($name,)*), E> {
|
||||
try!(d.expect_tuple_start(token));
|
||||
|
||||
let result = ($({
|
||||
@ -794,12 +786,9 @@ impl_deserialize_tuple! { T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, }
|
||||
/// recursive structures.
|
||||
pub struct IgnoreTokens;
|
||||
|
||||
impl Deserializable for IgnoreTokens {
|
||||
impl<D: Deserializer<E>, E> Deserializable<D, E> for IgnoreTokens {
|
||||
#[inline]
|
||||
fn deserialize_token<
|
||||
D: Deserializer<E>,
|
||||
E
|
||||
>(d: &mut D, token: Token) -> Result<IgnoreTokens, E> {
|
||||
fn deserialize_token(d: &mut D, token: Token) -> Result<IgnoreTokens, E> {
|
||||
match token {
|
||||
Option(true) => {
|
||||
Deserializable::deserialize(d)
|
||||
@ -984,12 +973,9 @@ impl GatherTokens {
|
||||
}
|
||||
}
|
||||
|
||||
impl Deserializable for GatherTokens {
|
||||
impl<D: Deserializer<E>, E> Deserializable<D, E> for GatherTokens {
|
||||
#[inline]
|
||||
fn deserialize_token<
|
||||
D: Deserializer<E>,
|
||||
E
|
||||
>(d: &mut D, token: Token) -> Result<GatherTokens, E> {
|
||||
fn deserialize_token(d: &mut D, token: Token) -> Result<GatherTokens, E> {
|
||||
let mut tokens = GatherTokens {
|
||||
tokens: vec!(),
|
||||
};
|
||||
@ -1050,11 +1036,12 @@ mod tests {
|
||||
c: TreeMap<String, Option<char>>,
|
||||
}
|
||||
|
||||
impl Deserializable for Inner {
|
||||
impl<
|
||||
D: Deserializer<E>,
|
||||
E
|
||||
> Deserializable<D, E> for Inner {
|
||||
#[inline]
|
||||
fn deserialize_token<
|
||||
D: Deserializer<E>, E
|
||||
>(d: &mut D, token: Token) -> Result<Inner, E> {
|
||||
fn deserialize_token(d: &mut D, token: Token) -> Result<Inner, E> {
|
||||
try!(d.expect_struct_start(token, "Inner"));
|
||||
let a = try!(d.expect_struct_field("a"));
|
||||
let b = try!(d.expect_struct_field("b"));
|
||||
@ -1071,11 +1058,9 @@ mod tests {
|
||||
inner: Vec<Inner>,
|
||||
}
|
||||
|
||||
impl Deserializable for Outer {
|
||||
impl<D: Deserializer<E>, E> Deserializable<D, E> for Outer {
|
||||
#[inline]
|
||||
fn deserialize_token<
|
||||
D: Deserializer<E>, E
|
||||
>(d: &mut D, token: Token) -> Result<Outer, E> {
|
||||
fn deserialize_token(d: &mut D, token: Token) -> Result<Outer, E> {
|
||||
try!(d.expect_struct_start(token, "Outer"));
|
||||
let inner = try!(d.expect_struct_field("inner"));
|
||||
try!(d.expect_struct_end());
|
||||
@ -1091,11 +1076,9 @@ mod tests {
|
||||
Frog(String, int)
|
||||
}
|
||||
|
||||
impl Deserializable for Animal {
|
||||
impl<D: Deserializer<E>, E> Deserializable<D, E> for Animal {
|
||||
#[inline]
|
||||
fn deserialize_token<
|
||||
D: Deserializer<E>, E
|
||||
>(d: &mut D, token: Token) -> Result<Animal, E> {
|
||||
fn deserialize_token(d: &mut D, token: Token) -> Result<Animal, E> {
|
||||
match try!(d.expect_enum_start(token, "Animal", ["Dog", "Frog"])) {
|
||||
0 => {
|
||||
try!(d.expect_enum_end());
|
||||
@ -1165,7 +1148,7 @@ mod tests {
|
||||
|
||||
#[inline]
|
||||
fn missing_field<
|
||||
T: Deserializable
|
||||
T: Deserializable<TokenDeserializer<Iter>, Error>
|
||||
>(&mut self, _field: &'static str) -> Result<T, Error> {
|
||||
Err(SyntaxError)
|
||||
}
|
||||
|
@ -521,12 +521,9 @@ impl ser::Serializable for Json {
|
||||
}
|
||||
}
|
||||
|
||||
impl de::Deserializable for Json {
|
||||
impl<D: de::Deserializer<E>, E> de::Deserializable<D, E> for Json {
|
||||
#[inline]
|
||||
fn deserialize_token<
|
||||
D: de::Deserializer<E>,
|
||||
E
|
||||
>(d: &mut D, token: de::Token) -> Result<Json, E> {
|
||||
fn deserialize_token(d: &mut D, token: de::Token) -> Result<Json, E> {
|
||||
match token {
|
||||
de::Null => Ok(Null),
|
||||
de::Bool(x) => Ok(Boolean(x)),
|
||||
@ -665,7 +662,7 @@ impl de::Deserializer<ParserError> for JsonDeserializer {
|
||||
|
||||
#[inline]
|
||||
fn missing_field<
|
||||
T: de::Deserializable
|
||||
T: de::Deserializable<JsonDeserializer, ParserError>
|
||||
>(&mut self, _field: &'static str) -> Result<T, ParserError> {
|
||||
// JSON can represent `null` values as a missing value, so this isn't
|
||||
// necessarily an error.
|
||||
@ -675,7 +672,7 @@ impl de::Deserializer<ParserError> for JsonDeserializer {
|
||||
// Special case treating options as a nullable value.
|
||||
#[inline]
|
||||
fn expect_option<
|
||||
U: de::Deserializable
|
||||
U: de::Deserializable<JsonDeserializer, ParserError>
|
||||
>(&mut self, token: de::Token) -> Result<Option<U>, ParserError> {
|
||||
match token {
|
||||
de::Null => Ok(None),
|
||||
@ -1592,8 +1589,8 @@ impl Stack {
|
||||
|
||||
/// A streaming JSON parser implemented as an iterator of JsonEvent, consuming
|
||||
/// an iterator of char.
|
||||
pub struct Parser<T> {
|
||||
rdr: T,
|
||||
pub struct Parser<Iter> {
|
||||
rdr: Iter,
|
||||
ch: Option<char>,
|
||||
line: uint,
|
||||
col: uint,
|
||||
@ -1601,7 +1598,7 @@ pub struct Parser<T> {
|
||||
state_stack: Vec<ParserState>,
|
||||
}
|
||||
|
||||
impl<T: Iterator<char>> Iterator<Result<de::Token, ParserError>> for Parser<T> {
|
||||
impl<Iter: Iterator<char>> Iterator<Result<de::Token, ParserError>> for Parser<Iter> {
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<Result<de::Token, ParserError>> {
|
||||
let state = match self.state_stack.pop() {
|
||||
@ -1632,9 +1629,9 @@ impl<T: Iterator<char>> Iterator<Result<de::Token, ParserError>> for Parser<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Iterator<char>> Parser<T> {
|
||||
impl<Iter: Iterator<char>> Parser<Iter> {
|
||||
/// Creates the JSON parser.
|
||||
pub fn new(rdr: T) -> Parser<T> {
|
||||
pub fn new(rdr: Iter) -> Parser<Iter> {
|
||||
let mut p = Parser {
|
||||
rdr: rdr,
|
||||
ch: Some('\x00'),
|
||||
@ -2027,7 +2024,7 @@ impl<T: Iterator<char>> Parser<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Iterator<char>> de::Deserializer<ParserError> for Parser<T> {
|
||||
impl<Iter: Iterator<char>> de::Deserializer<ParserError> for Parser<Iter> {
|
||||
fn end_of_stream_error(&mut self) -> ParserError {
|
||||
SyntaxError(EOFWhileParsingValue, self.line, self.col)
|
||||
}
|
||||
@ -2046,7 +2043,7 @@ impl<T: Iterator<char>> de::Deserializer<ParserError> for Parser<T> {
|
||||
|
||||
#[inline]
|
||||
fn missing_field<
|
||||
T: de::Deserializable
|
||||
T: de::Deserializable<Parser<Iter>, ParserError>
|
||||
>(&mut self, _field: &'static str) -> Result<T, ParserError> {
|
||||
// JSON can represent `null` values as a missing value, so this isn't
|
||||
// necessarily an error.
|
||||
@ -2056,7 +2053,7 @@ impl<T: Iterator<char>> de::Deserializer<ParserError> for Parser<T> {
|
||||
// Special case treating options as a nullable value.
|
||||
#[inline]
|
||||
fn expect_option<
|
||||
U: de::Deserializable
|
||||
U: de::Deserializable<Parser<Iter>, ParserError>
|
||||
>(&mut self, token: de::Token) -> Result<Option<U>, ParserError> {
|
||||
match token {
|
||||
de::Null => Ok(None),
|
||||
@ -2113,7 +2110,7 @@ impl<T: Iterator<char>> de::Deserializer<ParserError> for Parser<T> {
|
||||
/// Decodes a json value from an `Iterator<Char>`.
|
||||
pub fn from_iter<
|
||||
Iter: Iterator<char>,
|
||||
T: de::Deserializable
|
||||
T: de::Deserializable<Parser<Iter>, ParserError>
|
||||
>(iter: Iter) -> Result<T, ParserError> {
|
||||
let mut parser = Parser::new(iter);
|
||||
let value = try!(de::Deserializable::deserialize(&mut parser));
|
||||
@ -2128,14 +2125,15 @@ pub fn from_iter<
|
||||
|
||||
/// Decodes a json value from a string
|
||||
pub fn from_str<
|
||||
T: de::Deserializable
|
||||
>(s: &str) -> Result<T, BuilderError> {
|
||||
'a,
|
||||
T: de::Deserializable<Parser<str::Chars<'a>>, ParserError>
|
||||
>(s: &'a str) -> Result<T, BuilderError> {
|
||||
from_iter(s.chars())
|
||||
}
|
||||
|
||||
/// Decodes a json value from a `Json`.
|
||||
pub fn from_json<
|
||||
T: de::Deserializable
|
||||
T: de::Deserializable<JsonDeserializer, ParserError>
|
||||
>(json: Json) -> Result<T, ParserError> {
|
||||
let mut d = JsonDeserializer::new(json);
|
||||
de::Deserializable::deserialize(&mut d)
|
||||
@ -2304,11 +2302,12 @@ impl<A:ToJson> ToJson for Option<A> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::fmt::Show;
|
||||
use std::str;
|
||||
use std::collections::TreeMap;
|
||||
|
||||
use super::{Json, Null, Boolean, Floating, String, List, Object};
|
||||
use super::{ParserError, from_iter, from_str};
|
||||
use super::{from_json, ToJson};
|
||||
use super::{Parser, ParserError, from_iter, from_str};
|
||||
use super::{JsonDeserializer, ToJson, from_json};
|
||||
use super::{
|
||||
EOFWhileParsingList,
|
||||
EOFWhileParsingObject,
|
||||
@ -2751,8 +2750,9 @@ mod tests {
|
||||
|
||||
// FIXME (#5527): these could be merged once UFCS is finished.
|
||||
fn test_parse_err<
|
||||
T: Show + de::Deserializable
|
||||
>(errors: &[(&str, ParserError)]) {
|
||||
'a,
|
||||
T: Show + de::Deserializable<Parser<str::Chars<'a>>, ParserError>
|
||||
>(errors: &[(&'a str, ParserError)]) {
|
||||
for &(s, ref err) in errors.iter() {
|
||||
let v: Result<T, ParserError> = from_iter(s.chars());
|
||||
assert_eq!(v.unwrap_err(), *err);
|
||||
@ -2760,8 +2760,9 @@ mod tests {
|
||||
}
|
||||
|
||||
fn test_parse_ok<
|
||||
T: PartialEq + Show + ToJson + de::Deserializable
|
||||
>(errors: &[(&str, T)]) {
|
||||
'a,
|
||||
T: PartialEq + Show + ToJson + de::Deserializable<Parser<str::Chars<'a>>, ParserError>
|
||||
>(errors: &[(&'a str, T)]) {
|
||||
for &(s, ref value) in errors.iter() {
|
||||
let v: T = from_iter(s.chars()).unwrap();
|
||||
assert_eq!(v, *value);
|
||||
@ -2772,7 +2773,7 @@ mod tests {
|
||||
}
|
||||
|
||||
fn test_json_deserialize_ok<
|
||||
T: PartialEq + Show + ToJson + de::Deserializable
|
||||
T: PartialEq + Show + ToJson + de::Deserializable<JsonDeserializer, ParserError>
|
||||
>(errors: &[T]) {
|
||||
for value in errors.iter() {
|
||||
let v: T = from_json(value.to_json()).unwrap();
|
||||
|
Loading…
x
Reference in New Issue
Block a user