Follow rust std: Stabilized Hash and introduced PhantomData

This commit is contained in:
Thomas Bahn 2015-02-24 10:52:10 +01:00
parent e9c7ea1a64
commit 61c38127af
3 changed files with 35 additions and 25 deletions

View File

@ -1,4 +1,4 @@
use std::collections::hash_map::Hasher; use std::marker::PhantomData;
use std::collections::{HashMap, BTreeMap}; use std::collections::{HashMap, BTreeMap};
use std::hash::Hash; use std::hash::Hash;
use std::num::FromPrimitive; use std::num::FromPrimitive;
@ -357,7 +357,9 @@ macro_rules! impl_deserialize_num_method {
} }
} }
pub struct PrimitiveVisitor<T>; pub struct PrimitiveVisitor<T> {
marker: PhantomData<T>,
}
impl< impl<
T: Deserialize + FromPrimitive T: Deserialize + FromPrimitive
@ -385,7 +387,7 @@ macro_rules! impl_deserialize_num {
fn deserialize< fn deserialize<
D: Deserializer, D: Deserializer,
>(deserializer: &mut D) -> Result<$ty, D::Error> { >(deserializer: &mut D) -> Result<$ty, D::Error> {
deserializer.visit(&mut PrimitiveVisitor) deserializer.visit(&mut PrimitiveVisitor { marker: PhantomData })
} }
} }
} }
@ -474,7 +476,9 @@ impl Deserialize for String {
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
struct OptionVisitor<T>; struct OptionVisitor<T> {
marker: PhantomData<T>,
}
impl< impl<
T: Deserialize, T: Deserialize,
@ -500,13 +504,15 @@ impl<T> Deserialize for Option<T> where T: Deserialize {
fn deserialize< fn deserialize<
D: Deserializer, D: Deserializer,
>(deserializer: &mut D) -> Result<Option<T>, D::Error> { >(deserializer: &mut D) -> Result<Option<T>, D::Error> {
deserializer.visit_option(&mut OptionVisitor) deserializer.visit_option(&mut OptionVisitor { marker: PhantomData })
} }
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
struct VecVisitor<T>; struct VecVisitor<T> {
marker: PhantomData<T>,
}
impl<T> Visitor for VecVisitor<T> where T: Deserialize { impl<T> Visitor for VecVisitor<T> where T: Deserialize {
type Value = Vec<T>; type Value = Vec<T>;
@ -529,7 +535,7 @@ impl<T: Deserialize> Deserialize for Vec<T> {
fn deserialize< fn deserialize<
D: Deserializer, D: Deserializer,
>(deserializer: &mut D) -> Result<Vec<T>, D::Error> { >(deserializer: &mut D) -> Result<Vec<T>, D::Error> {
deserializer.visit(&mut VecVisitor) deserializer.visit(&mut VecVisitor { marker: PhantomData })
} }
} }
@ -539,7 +545,9 @@ macro_rules! tuple_impls {
() => {}; () => {};
($($visitor:ident => ($($name:ident),+),)+) => { ($($visitor:ident => ($($name:ident),+),)+) => {
$( $(
struct $visitor<$($name,)+>; struct $visitor<$($name,)+> {
marker: PhantomData<($($name,)+)>,
}
impl< impl<
$($name: Deserialize,)+ $($name: Deserialize,)+
@ -571,7 +579,7 @@ macro_rules! tuple_impls {
fn deserialize< fn deserialize<
D: Deserializer, D: Deserializer,
>(deserializer: &mut D) -> Result<($($name,)+), D::Error> { >(deserializer: &mut D) -> Result<($($name,)+), D::Error> {
deserializer.visit(&mut $visitor) deserializer.visit(&mut $visitor { marker: PhantomData })
} }
} }
)+ )+
@ -595,10 +603,12 @@ tuple_impls! {
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
struct HashMapVisitor<K, V>; struct HashMapVisitor<K, V> {
marker: PhantomData<HashMap<K, V>>,
}
impl<K, V> Visitor for HashMapVisitor<K, V> impl<K, V> Visitor for HashMapVisitor<K, V>
where K: Deserialize + Eq + Hash<Hasher>, where K: Deserialize + Eq + Hash,
V: Deserialize, V: Deserialize,
{ {
type Value = HashMap<K, V>; type Value = HashMap<K, V>;
@ -619,19 +629,21 @@ impl<K, V> Visitor for HashMapVisitor<K, V>
} }
impl<K, V> Deserialize for HashMap<K, V> impl<K, V> Deserialize for HashMap<K, V>
where K: Deserialize + Eq + Hash<Hasher>, where K: Deserialize + Eq + Hash,
V: Deserialize, V: Deserialize,
{ {
fn deserialize< fn deserialize<
D: Deserializer, D: Deserializer,
>(deserializer: &mut D) -> Result<HashMap<K, V>, D::Error> { >(deserializer: &mut D) -> Result<HashMap<K, V>, D::Error> {
deserializer.visit(&mut HashMapVisitor) deserializer.visit(&mut HashMapVisitor { marker: PhantomData })
} }
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
struct BTreeMapVisitor<K, V>; struct BTreeMapVisitor<K, V> {
marker: PhantomData<BTreeMap<K, V>>,
}
impl<K, V> Visitor for BTreeMapVisitor<K, V> impl<K, V> Visitor for BTreeMapVisitor<K, V>
where K: Deserialize + Ord, where K: Deserialize + Ord,
@ -660,7 +672,7 @@ impl<
fn deserialize< fn deserialize<
D: Deserializer, D: Deserializer,
>(deserializer: &mut D) -> Result<BTreeMap<K, V>, D::Error> { >(deserializer: &mut D) -> Result<BTreeMap<K, V>, D::Error> {
deserializer.visit(&mut BTreeMapVisitor) deserializer.visit(&mut BTreeMapVisitor { marker: PhantomData })
} }
} }

View File

@ -1,4 +1,4 @@
#![feature(core, hash, io, std_misc, unicode)] #![feature(core, io, std_misc, unicode)]
extern crate unicode; extern crate unicode;

View File

@ -1,6 +1,6 @@
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::collections::hash_state::HashState; use std::collections::hash_state::HashState;
use std::hash::{Hash, Hasher}; use std::hash::Hash;
use std::rc::Rc; use std::rc::Rc;
use std::str; use std::str;
use std::sync::Arc; use std::sync::Arc;
@ -333,10 +333,9 @@ impl<T> Serialize for BTreeSet<T> where T: Serialize {
} }
} }
impl<T, S, H> Serialize for HashSet<T, S> impl<T, S> Serialize for HashSet<T, S>
where T: Serialize + Eq + Hash<H>, where T: Serialize + Eq + Hash,
S: HashState<Hasher=H>, S: HashState,
H: Hasher<Output=u64>,
{ {
#[inline] #[inline]
fn visit<V: Visitor>(&self, visitor: &mut V) -> Result<V::Value, V::Error> { fn visit<V: Visitor>(&self, visitor: &mut V) -> Result<V::Value, V::Error> {
@ -586,11 +585,10 @@ impl<K, V> Serialize for BTreeMap<K, V>
} }
} }
impl<K, V, S, H> Serialize for HashMap<K, V, S> impl<K, V, S> Serialize for HashMap<K, V, S>
where K: Serialize + Eq + Hash<H>, where K: Serialize + Eq + Hash,
V: Serialize, V: Serialize,
S: HashState<Hasher=H>, S: HashState,
H: Hasher<Output=u64>,
{ {
#[inline] #[inline]
fn visit<V_: Visitor>(&self, visitor: &mut V_) -> Result<V_::Value, V_::Error> { fn visit<V_: Visitor>(&self, visitor: &mut V_) -> Result<V_::Value, V_::Error> {