mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-06-14 04:41:01 +00:00
Add {,de}serializer for VecMap
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
|
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecMap};
|
||||||
use std::hash::Hash;
|
use std::hash::Hash;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use std::num::FromPrimitive;
|
use std::num::FromPrimitive;
|
||||||
@@ -995,6 +995,60 @@ impl<K, V> Deserialize for HashMap<K, V>
|
|||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
pub struct VecMapVisitor<V> {
|
||||||
|
marker: PhantomData<VecMap<V>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<V> VecMapVisitor<V> {
|
||||||
|
#[inline]
|
||||||
|
pub fn new() -> Self {
|
||||||
|
VecMapVisitor {
|
||||||
|
marker: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<V> Visitor for VecMapVisitor<V>
|
||||||
|
where V: Deserialize,
|
||||||
|
{
|
||||||
|
type Value = VecMap<V>;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn visit_unit<E>(&mut self) -> Result<VecMap<V>, E>
|
||||||
|
where E: Error,
|
||||||
|
{
|
||||||
|
Ok(VecMap::new())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn visit_map<V_>(&mut self, mut visitor: V_) -> Result<VecMap<V>, V_::Error>
|
||||||
|
where V_: MapVisitor,
|
||||||
|
{
|
||||||
|
let (len, _) = visitor.size_hint();
|
||||||
|
let mut values = VecMap::with_capacity(len);
|
||||||
|
|
||||||
|
while let Some((key, value)) = try!(visitor.visit()) {
|
||||||
|
values.insert(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
try!(visitor.end());
|
||||||
|
|
||||||
|
Ok(values)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<V> Deserialize for VecMap<V>
|
||||||
|
where V: Deserialize,
|
||||||
|
{
|
||||||
|
fn deserialize<D>(deserializer: &mut D) -> Result<VecMap<V>, D::Error>
|
||||||
|
where D: Deserializer,
|
||||||
|
{
|
||||||
|
deserializer.visit(VecMapVisitor::new())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
struct PathBufVisitor;
|
struct PathBufVisitor;
|
||||||
|
|
||||||
impl Visitor for PathBufVisitor {
|
impl Visitor for PathBufVisitor {
|
||||||
|
|||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
#![feature(convert, core, std_misc, unicode)]
|
#![feature(collections, convert, core, std_misc, unicode)]
|
||||||
|
|
||||||
extern crate unicode;
|
extern crate unicode;
|
||||||
|
|
||||||
|
|||||||
+12
-1
@@ -1,5 +1,5 @@
|
|||||||
use std::collections::hash_state::HashState;
|
use std::collections::hash_state::HashState;
|
||||||
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
|
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecMap};
|
||||||
use std::hash::Hash;
|
use std::hash::Hash;
|
||||||
use std::path;
|
use std::path;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
@@ -587,6 +587,17 @@ impl<K, V, H> Serialize for HashMap<K, V, H>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<V> Serialize for VecMap<V>
|
||||||
|
where V: Serialize,
|
||||||
|
{
|
||||||
|
#[inline]
|
||||||
|
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||||
|
where S: Serializer,
|
||||||
|
{
|
||||||
|
serializer.visit_map(MapIteratorVisitor::new(self.iter(), Some(self.len())))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
impl<'a, T> Serialize for &'a T where T: Serialize {
|
impl<'a, T> Serialize for &'a T where T: Serialize {
|
||||||
|
|||||||
Reference in New Issue
Block a user