auto merge of #18813 : csherratt/rust/add-encode-decode-vecmap, r=alexcrichton

This commit is contained in:
bors 2014-11-11 22:37:05 +00:00
commit e82f60eb47

View File

@ -16,7 +16,7 @@ use std::hash::{Hash, Hasher};
use {Decodable, Encodable, Decoder, Encoder};
use std::collections::{DList, RingBuf, TreeMap, TreeSet, HashMap, HashSet,
TrieMap, TrieSet};
TrieMap, TrieSet, VecMap};
use std::collections::enum_set::{EnumSet, CLike};
impl<
@ -311,3 +311,37 @@ impl<E, D: Decoder<E>> Decodable<D, E> for TrieSet {
})
}
}
impl<
E,
S: Encoder<E>,
V: Encodable<S, E>
> Encodable<S, E> for VecMap<V> {
fn encode(&self, e: &mut S) -> Result<(), E> {
e.emit_map(self.len(), |e| {
for (i, (key, val)) in self.iter().enumerate() {
try!(e.emit_map_elt_key(i, |e| key.encode(e)));
try!(e.emit_map_elt_val(i, |e| val.encode(e)));
}
Ok(())
})
}
}
impl<
E,
D: Decoder<E>,
V: Decodable<D, E>
> Decodable<D, E> for VecMap<V> {
fn decode(d: &mut D) -> Result<VecMap<V>, E> {
d.read_map(|d, len| {
let mut map = VecMap::new();
for i in range(0u, len) {
let key = try!(d.read_map_elt_key(i, |d| Decodable::decode(d)));
let val = try!(d.read_map_elt_val(i, |d| Decodable::decode(d)));
map.insert(key, val);
}
Ok(map)
})
}
}