Skip to content

Commit

Permalink
Merge pull request rust-lang#87 from Mingun/into-deserializer
Browse files Browse the repository at this point in the history
Implement IntoDeserializer for IndexMap/IndexSet
  • Loading branch information
cuviper authored Mar 29, 2019
2 parents d8e5225 + 9807f52 commit 0a06966
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ Recent Changes
- Implemented ``Debug`` for ``map::{Entry, IntoIter, Iter, Keys, Values}`` and
``set::{Difference, Intersection, IntoIter, Iter, SymmetricDifference, Union}``

- Serde trait ``IntoDeserializer`` are implemented for ``IndexMap`` and ``IndexSet``.

- 1.0.2

- The new methods ``IndexMap::insert_full`` and ``IndexSet::insert_full`` are
Expand Down
28 changes: 27 additions & 1 deletion src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
extern crate serde;

use self::serde::ser::{Serialize, Serializer, SerializeMap, SerializeSeq};
use self::serde::de::{Deserialize, Deserializer, MapAccess, SeqAccess, Visitor};
use self::serde::de::{Deserialize, Deserializer, Error, IntoDeserializer, MapAccess, SeqAccess, Visitor};
use self::serde::de::value::{MapDeserializer, SeqDeserializer};

use std::fmt::{self, Formatter};
use std::hash::{BuildHasher, Hash};
Expand Down Expand Up @@ -66,6 +67,19 @@ impl<'de, K, V, S> Deserialize<'de> for IndexMap<K, V, S>
}
}

impl<'de, K, V, S, E> IntoDeserializer<'de, E> for IndexMap<K, V, S>
where K: IntoDeserializer<'de, E> + Eq + Hash,
V: IntoDeserializer<'de, E>,
S: BuildHasher,
E: Error,
{
type Deserializer = MapDeserializer<'de, <Self as IntoIterator>::IntoIter, E>;

fn into_deserializer(self) -> Self::Deserializer {
MapDeserializer::new(self.into_iter())
}
}


use IndexSet;

Expand Down Expand Up @@ -121,3 +135,15 @@ impl<'de, T, S> Deserialize<'de> for IndexSet<T, S>
deserializer.deserialize_seq(OrderSetVisitor(PhantomData))
}
}

impl<'de, T, S, E> IntoDeserializer<'de, E> for IndexSet<T, S>
where T: IntoDeserializer<'de, E> + Eq + Hash,
S: BuildHasher,
E: Error,
{
type Deserializer = SeqDeserializer<<Self as IntoIterator>::IntoIter, E>;

fn into_deserializer(self) -> Self::Deserializer {
SeqDeserializer::new(self.into_iter())
}
}

0 comments on commit 0a06966

Please sign in to comment.