De/serialize for HashSet<T, S>
This commit is contained in:
parent
322d7a90db
commit
decc571988
@ -381,29 +381,30 @@ impl<T> Deserialize for PhantomData<T> where T: Deserialize {
|
||||
macro_rules! seq_impl {
|
||||
(
|
||||
$ty:ty,
|
||||
< $($constraints:ident),* >,
|
||||
$visitor_name:ident,
|
||||
$visitor_ty:ident < $($typaram:ident : $bound1:ident $(+ $bound2:ident)*),* >,
|
||||
$visitor:ident,
|
||||
$ctor:expr,
|
||||
$with_capacity:expr,
|
||||
$insert:expr
|
||||
) => {
|
||||
/// A visitor that produces a sequence.
|
||||
pub struct $visitor_name<T> {
|
||||
marker: PhantomData<T>,
|
||||
pub struct $visitor_ty<$($typaram),*> {
|
||||
marker: PhantomData<$ty>,
|
||||
}
|
||||
|
||||
impl<T> $visitor_name<T> {
|
||||
impl<$($typaram),*> $visitor_ty<$($typaram),*>
|
||||
where $($typaram: $bound1 $(+ $bound2)*),*
|
||||
{
|
||||
/// Construct a new sequence visitor.
|
||||
pub fn new() -> Self {
|
||||
$visitor_name {
|
||||
$visitor_ty {
|
||||
marker: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Visitor for $visitor_name<T>
|
||||
where T: $($constraints +)*,
|
||||
impl<$($typaram),*> Visitor for $visitor_ty<$($typaram),*>
|
||||
where $($typaram: $bound1 $(+ $bound2)*),*
|
||||
{
|
||||
type Value = $ty;
|
||||
|
||||
@ -430,13 +431,13 @@ macro_rules! seq_impl {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Deserialize for $ty
|
||||
where T: $($constraints +)*,
|
||||
impl<$($typaram),*> Deserialize for $ty
|
||||
where $($typaram: $bound1 $(+ $bound2)*),*
|
||||
{
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<$ty, D::Error>
|
||||
where D: Deserializer,
|
||||
{
|
||||
deserializer.deserialize_seq($visitor_name::new())
|
||||
deserializer.deserialize_seq($visitor_ty::new())
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -445,8 +446,7 @@ macro_rules! seq_impl {
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
seq_impl!(
|
||||
BinaryHeap<T>,
|
||||
<Deserialize, Ord>,
|
||||
BinaryHeapVisitor,
|
||||
BinaryHeapVisitor<T: Deserialize + Ord>,
|
||||
visitor,
|
||||
BinaryHeap::new(),
|
||||
BinaryHeap::with_capacity(visitor.size_hint().0),
|
||||
@ -455,8 +455,7 @@ seq_impl!(
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
seq_impl!(
|
||||
BTreeSet<T>,
|
||||
<Deserialize, Eq, Ord>,
|
||||
BTreeSetVisitor,
|
||||
BTreeSetVisitor<T: Deserialize + Eq + Ord>,
|
||||
visitor,
|
||||
BTreeSet::new(),
|
||||
BTreeSet::new(),
|
||||
@ -465,8 +464,7 @@ seq_impl!(
|
||||
#[cfg(all(feature = "nightly", feature = "collections"))]
|
||||
seq_impl!(
|
||||
EnumSet<T>,
|
||||
<Deserialize, CLike>,
|
||||
EnumSetVisitor,
|
||||
EnumSetVisitor<T: Deserialize + CLike>,
|
||||
visitor,
|
||||
EnumSet::new(),
|
||||
EnumSet::new(),
|
||||
@ -475,8 +473,7 @@ seq_impl!(
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
seq_impl!(
|
||||
LinkedList<T>,
|
||||
<Deserialize>,
|
||||
LinkedListVisitor,
|
||||
LinkedListVisitor<T: Deserialize>,
|
||||
visitor,
|
||||
LinkedList::new(),
|
||||
LinkedList::new(),
|
||||
@ -484,19 +481,18 @@ seq_impl!(
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
seq_impl!(
|
||||
HashSet<T>,
|
||||
<Deserialize, Eq, Hash>,
|
||||
HashSetVisitor,
|
||||
HashSet<T, S>,
|
||||
HashSetVisitor<T: Deserialize + Eq + Hash,
|
||||
S: BuildHasher + Default>,
|
||||
visitor,
|
||||
HashSet::new(),
|
||||
HashSet::with_capacity(visitor.size_hint().0),
|
||||
HashSet::with_hasher(S::default()),
|
||||
HashSet::with_capacity_and_hasher(visitor.size_hint().0, S::default()),
|
||||
HashSet::insert);
|
||||
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
seq_impl!(
|
||||
Vec<T>,
|
||||
<Deserialize>,
|
||||
VecVisitor,
|
||||
VecVisitor<T: Deserialize>,
|
||||
visitor,
|
||||
Vec::new(),
|
||||
Vec::with_capacity(visitor.size_hint().0),
|
||||
@ -505,8 +501,7 @@ seq_impl!(
|
||||
#[cfg(any(feature = "std", feature = "collections"))]
|
||||
seq_impl!(
|
||||
VecDeque<T>,
|
||||
<Deserialize>,
|
||||
VecDequeVisitor,
|
||||
VecDequeVisitor<T: Deserialize>,
|
||||
visitor,
|
||||
VecDeque::new(),
|
||||
VecDeque::with_capacity(visitor.size_hint().0),
|
||||
|
@ -330,8 +330,9 @@ impl<T> Serialize for EnumSet<T>
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<T> Serialize for HashSet<T>
|
||||
impl<T, H> Serialize for HashSet<T, H>
|
||||
where T: Serialize + Eq + Hash,
|
||||
H: BuildHasher,
|
||||
{
|
||||
#[inline]
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
|
@ -62,6 +62,14 @@ macro_rules! hashset {
|
||||
$(set.insert($value);)+
|
||||
set
|
||||
}
|
||||
};
|
||||
($hasher:ident @ $($value:expr),+) => {
|
||||
{
|
||||
use std::hash::BuildHasherDefault;
|
||||
let mut set = HashSet::with_hasher(BuildHasherDefault::<$hasher>::default());
|
||||
$(set.insert($value);)+
|
||||
set
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -287,6 +287,18 @@ declare_tests! {
|
||||
Token::TupleStructStart("Anything", Some(0)),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
hashset![FnvHasher @ 1, 2, 3] => vec![
|
||||
Token::SeqStart(Some(3)),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(2),
|
||||
|
||||
Token::SeqSep,
|
||||
Token::I32(3),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
}
|
||||
test_vec {
|
||||
Vec::<isize>::new() => vec![
|
||||
|
@ -1,4 +1,4 @@
|
||||
use std::collections::{BTreeMap, HashMap};
|
||||
use std::collections::{BTreeMap, HashMap, HashSet};
|
||||
use std::net;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::str;
|
||||
@ -147,6 +147,24 @@ declare_ser_tests! {
|
||||
Token::SeqEnd,
|
||||
],
|
||||
}
|
||||
test_hashset {
|
||||
HashSet::<isize>::new() => &[
|
||||
Token::SeqStart(Some(0)),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
hashset![1] => &[
|
||||
Token::SeqStart(Some(1)),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
hashset![FnvHasher @ 1] => &[
|
||||
Token::SeqStart(Some(1)),
|
||||
Token::SeqSep,
|
||||
Token::I32(1),
|
||||
Token::SeqEnd,
|
||||
],
|
||||
}
|
||||
test_tuple {
|
||||
(1,) => &[
|
||||
Token::TupleStart(1),
|
||||
|
Loading…
x
Reference in New Issue
Block a user