2014-02-21 16:18:39 -06:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
//! Implementations of serialization for structures found in libcollections
|
|
|
|
|
|
|
|
use std::uint;
|
2014-02-25 10:04:38 -06:00
|
|
|
use std::default::Default;
|
|
|
|
use std::hash::{Hash, Hasher};
|
2014-02-21 16:18:39 -06:00
|
|
|
|
|
|
|
use {Decodable, Encodable, Decoder, Encoder};
|
2014-12-17 09:16:10 -06:00
|
|
|
use std::collections::{DList, RingBuf, BTreeMap, BTreeSet, HashMap, HashSet, VecMap};
|
|
|
|
use collections::enum_set::{EnumSet, CLike};
|
2014-02-21 16:18:39 -06:00
|
|
|
|
|
|
|
impl<
|
2014-03-18 12:58:26 -05:00
|
|
|
E,
|
|
|
|
S: Encoder<E>,
|
|
|
|
T: Encodable<S, E>
|
|
|
|
> Encodable<S, E> for DList<T> {
|
|
|
|
fn encode(&self, s: &mut S) -> Result<(), E> {
|
2014-02-21 16:18:39 -06:00
|
|
|
s.emit_seq(self.len(), |s| {
|
|
|
|
for (i, e) in self.iter().enumerate() {
|
2014-03-18 12:58:26 -05:00
|
|
|
try!(s.emit_seq_elt(i, |s| e.encode(s)));
|
2014-02-21 16:18:39 -06:00
|
|
|
}
|
2014-03-18 12:58:26 -05:00
|
|
|
Ok(())
|
2014-02-21 16:18:39 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-18 12:58:26 -05:00
|
|
|
impl<E, D:Decoder<E>,T:Decodable<D, E>> Decodable<D, E> for DList<T> {
|
|
|
|
fn decode(d: &mut D) -> Result<DList<T>, E> {
|
2014-02-21 16:18:39 -06:00
|
|
|
d.read_seq(|d, len| {
|
2014-03-18 12:58:26 -05:00
|
|
|
let mut list = DList::new();
|
2014-02-21 16:18:39 -06:00
|
|
|
for i in range(0u, len) {
|
2014-11-06 11:25:16 -06:00
|
|
|
list.push_back(try!(d.read_seq_elt(i, |d| Decodable::decode(d))));
|
2014-02-21 16:18:39 -06:00
|
|
|
}
|
2014-03-18 12:58:26 -05:00
|
|
|
Ok(list)
|
|
|
|
})
|
2014-02-21 16:18:39 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<
|
2014-03-18 12:58:26 -05:00
|
|
|
E,
|
|
|
|
S: Encoder<E>,
|
|
|
|
T: Encodable<S, E>
|
|
|
|
> Encodable<S, E> for RingBuf<T> {
|
|
|
|
fn encode(&self, s: &mut S) -> Result<(), E> {
|
2014-02-21 16:18:39 -06:00
|
|
|
s.emit_seq(self.len(), |s| {
|
|
|
|
for (i, e) in self.iter().enumerate() {
|
2014-03-18 12:58:26 -05:00
|
|
|
try!(s.emit_seq_elt(i, |s| e.encode(s)));
|
2014-02-21 16:18:39 -06:00
|
|
|
}
|
2014-03-18 12:58:26 -05:00
|
|
|
Ok(())
|
2014-02-21 16:18:39 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-18 12:58:26 -05:00
|
|
|
impl<E, D:Decoder<E>,T:Decodable<D, E>> Decodable<D, E> for RingBuf<T> {
|
|
|
|
fn decode(d: &mut D) -> Result<RingBuf<T>, E> {
|
2014-02-21 16:18:39 -06:00
|
|
|
d.read_seq(|d, len| {
|
2014-03-18 12:58:26 -05:00
|
|
|
let mut deque: RingBuf<T> = RingBuf::new();
|
2014-02-21 16:18:39 -06:00
|
|
|
for i in range(0u, len) {
|
2014-11-06 11:25:16 -06:00
|
|
|
deque.push_back(try!(d.read_seq_elt(i, |d| Decodable::decode(d))));
|
2014-02-21 16:18:39 -06:00
|
|
|
}
|
2014-03-18 12:58:26 -05:00
|
|
|
Ok(deque)
|
|
|
|
})
|
2014-02-21 16:18:39 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<
|
2014-03-18 12:58:26 -05:00
|
|
|
E,
|
|
|
|
S: Encoder<E>,
|
2014-05-31 12:43:52 -05:00
|
|
|
K: Encodable<S, E> + PartialEq + Ord,
|
2014-05-29 19:45:07 -05:00
|
|
|
V: Encodable<S, E> + PartialEq
|
2014-12-16 22:09:16 -06:00
|
|
|
> Encodable<S, E> for BTreeMap<K, V> {
|
2014-03-18 12:58:26 -05:00
|
|
|
fn encode(&self, e: &mut S) -> Result<(), E> {
|
2014-02-21 16:18:39 -06:00
|
|
|
e.emit_map(self.len(), |e| {
|
|
|
|
let mut i = 0;
|
|
|
|
for (key, val) in self.iter() {
|
2014-03-18 12:58:26 -05:00
|
|
|
try!(e.emit_map_elt_key(i, |e| key.encode(e)));
|
|
|
|
try!(e.emit_map_elt_val(i, |e| val.encode(e)));
|
2014-02-21 16:18:39 -06:00
|
|
|
i += 1;
|
|
|
|
}
|
2014-03-18 12:58:26 -05:00
|
|
|
Ok(())
|
2014-02-21 16:18:39 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<
|
2014-03-18 12:58:26 -05:00
|
|
|
E,
|
|
|
|
D: Decoder<E>,
|
2014-05-31 12:43:52 -05:00
|
|
|
K: Decodable<D, E> + PartialEq + Ord,
|
2014-05-29 19:45:07 -05:00
|
|
|
V: Decodable<D, E> + PartialEq
|
2014-12-16 22:09:16 -06:00
|
|
|
> Decodable<D, E> for BTreeMap<K, V> {
|
2014-12-17 09:16:10 -06:00
|
|
|
fn decode(d: &mut D) -> Result<BTreeMap<K, V>, E> {
|
2014-02-21 16:18:39 -06:00
|
|
|
d.read_map(|d, len| {
|
2014-12-16 22:09:16 -06:00
|
|
|
let mut map = BTreeMap::new();
|
2014-02-21 16:18:39 -06:00
|
|
|
for i in range(0u, len) {
|
2014-03-18 12:58:26 -05:00
|
|
|
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)));
|
2014-02-21 16:18:39 -06:00
|
|
|
map.insert(key, val);
|
|
|
|
}
|
2014-03-18 12:58:26 -05:00
|
|
|
Ok(map)
|
2014-02-21 16:18:39 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<
|
2014-03-18 12:58:26 -05:00
|
|
|
E,
|
|
|
|
S: Encoder<E>,
|
2014-05-31 12:43:52 -05:00
|
|
|
T: Encodable<S, E> + PartialEq + Ord
|
2014-12-16 22:09:16 -06:00
|
|
|
> Encodable<S, E> for BTreeSet<T> {
|
2014-03-18 12:58:26 -05:00
|
|
|
fn encode(&self, s: &mut S) -> Result<(), E> {
|
2014-02-21 16:18:39 -06:00
|
|
|
s.emit_seq(self.len(), |s| {
|
|
|
|
let mut i = 0;
|
|
|
|
for e in self.iter() {
|
2014-03-18 12:58:26 -05:00
|
|
|
try!(s.emit_seq_elt(i, |s| e.encode(s)));
|
2014-02-21 16:18:39 -06:00
|
|
|
i += 1;
|
|
|
|
}
|
2014-03-18 12:58:26 -05:00
|
|
|
Ok(())
|
2014-02-21 16:18:39 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<
|
2014-03-18 12:58:26 -05:00
|
|
|
E,
|
|
|
|
D: Decoder<E>,
|
2014-05-31 12:43:52 -05:00
|
|
|
T: Decodable<D, E> + PartialEq + Ord
|
2014-12-16 22:09:16 -06:00
|
|
|
> Decodable<D, E> for BTreeSet<T> {
|
2014-12-17 09:16:10 -06:00
|
|
|
fn decode(d: &mut D) -> Result<BTreeSet<T>, E> {
|
2014-02-21 16:18:39 -06:00
|
|
|
d.read_seq(|d, len| {
|
2014-12-16 22:09:16 -06:00
|
|
|
let mut set = BTreeSet::new();
|
2014-02-21 16:18:39 -06:00
|
|
|
for i in range(0u, len) {
|
2014-03-18 12:58:26 -05:00
|
|
|
set.insert(try!(d.read_seq_elt(i, |d| Decodable::decode(d))));
|
2014-02-21 16:18:39 -06:00
|
|
|
}
|
2014-03-18 12:58:26 -05:00
|
|
|
Ok(set)
|
2014-02-21 16:18:39 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<
|
2014-03-18 12:58:26 -05:00
|
|
|
E,
|
|
|
|
S: Encoder<E>,
|
|
|
|
T: Encodable<S, E> + CLike
|
|
|
|
> Encodable<S, E> for EnumSet<T> {
|
|
|
|
fn encode(&self, s: &mut S) -> Result<(), E> {
|
2014-02-21 16:18:39 -06:00
|
|
|
let mut bits = 0;
|
|
|
|
for item in self.iter() {
|
|
|
|
bits |= item.to_uint();
|
|
|
|
}
|
2014-03-18 12:58:26 -05:00
|
|
|
s.emit_uint(bits)
|
2014-02-21 16:18:39 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<
|
2014-03-18 12:58:26 -05:00
|
|
|
E,
|
|
|
|
D: Decoder<E>,
|
|
|
|
T: Decodable<D, E> + CLike
|
|
|
|
> Decodable<D, E> for EnumSet<T> {
|
|
|
|
fn decode(d: &mut D) -> Result<EnumSet<T>, E> {
|
|
|
|
let bits = try!(d.read_uint());
|
2014-11-06 11:25:16 -06:00
|
|
|
let mut set = EnumSet::new();
|
2014-02-21 16:18:39 -06:00
|
|
|
for bit in range(0, uint::BITS) {
|
|
|
|
if bits & (1 << bit) != 0 {
|
2014-11-06 11:25:16 -06:00
|
|
|
set.insert(CLike::from_uint(1 << bit));
|
2014-02-21 16:18:39 -06:00
|
|
|
}
|
|
|
|
}
|
2014-03-18 12:58:26 -05:00
|
|
|
Ok(set)
|
2014-02-21 16:18:39 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<
|
2014-03-18 12:58:26 -05:00
|
|
|
E,
|
|
|
|
S: Encoder<E>,
|
2014-05-31 12:43:52 -05:00
|
|
|
K: Encodable<S, E> + Hash<X> + Eq,
|
2014-03-18 12:58:26 -05:00
|
|
|
V: Encodable<S, E>,
|
|
|
|
X,
|
|
|
|
H: Hasher<X>
|
|
|
|
> Encodable<S, E> for HashMap<K, V, H> {
|
|
|
|
fn encode(&self, e: &mut S) -> Result<(), E> {
|
2014-02-21 16:18:39 -06:00
|
|
|
e.emit_map(self.len(), |e| {
|
|
|
|
let mut i = 0;
|
|
|
|
for (key, val) in self.iter() {
|
2014-03-18 12:58:26 -05:00
|
|
|
try!(e.emit_map_elt_key(i, |e| key.encode(e)));
|
|
|
|
try!(e.emit_map_elt_val(i, |e| val.encode(e)));
|
2014-02-21 16:18:39 -06:00
|
|
|
i += 1;
|
|
|
|
}
|
2014-03-18 12:58:26 -05:00
|
|
|
Ok(())
|
2014-02-21 16:18:39 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<
|
2014-03-18 12:58:26 -05:00
|
|
|
E,
|
|
|
|
D: Decoder<E>,
|
2014-05-31 12:43:52 -05:00
|
|
|
K: Decodable<D, E> + Hash<S> + Eq,
|
2014-03-18 12:58:26 -05:00
|
|
|
V: Decodable<D, E>,
|
2014-02-25 10:04:38 -06:00
|
|
|
S,
|
|
|
|
H: Hasher<S> + Default
|
2014-03-18 12:58:26 -05:00
|
|
|
> Decodable<D, E> for HashMap<K, V, H> {
|
|
|
|
fn decode(d: &mut D) -> Result<HashMap<K, V, H>, E> {
|
2014-02-21 16:18:39 -06:00
|
|
|
d.read_map(|d, len| {
|
2014-02-25 10:04:38 -06:00
|
|
|
let hasher = Default::default();
|
2014-03-06 20:07:52 -06:00
|
|
|
let mut map = HashMap::with_capacity_and_hasher(len, hasher);
|
2014-02-21 16:18:39 -06:00
|
|
|
for i in range(0u, len) {
|
2014-03-18 12:58:26 -05:00
|
|
|
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)));
|
2014-02-21 16:18:39 -06:00
|
|
|
map.insert(key, val);
|
|
|
|
}
|
2014-03-18 12:58:26 -05:00
|
|
|
Ok(map)
|
2014-02-21 16:18:39 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<
|
2014-03-18 12:58:26 -05:00
|
|
|
E,
|
|
|
|
S: Encoder<E>,
|
2014-05-31 12:43:52 -05:00
|
|
|
T: Encodable<S, E> + Hash<X> + Eq,
|
2014-03-18 12:58:26 -05:00
|
|
|
X,
|
|
|
|
H: Hasher<X>
|
|
|
|
> Encodable<S, E> for HashSet<T, H> {
|
|
|
|
fn encode(&self, s: &mut S) -> Result<(), E> {
|
2014-02-21 16:18:39 -06:00
|
|
|
s.emit_seq(self.len(), |s| {
|
|
|
|
let mut i = 0;
|
|
|
|
for e in self.iter() {
|
2014-03-18 12:58:26 -05:00
|
|
|
try!(s.emit_seq_elt(i, |s| e.encode(s)));
|
2014-02-21 16:18:39 -06:00
|
|
|
i += 1;
|
|
|
|
}
|
2014-03-18 12:58:26 -05:00
|
|
|
Ok(())
|
2014-02-21 16:18:39 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<
|
2014-03-18 12:58:26 -05:00
|
|
|
E,
|
|
|
|
D: Decoder<E>,
|
2014-05-31 12:43:52 -05:00
|
|
|
T: Decodable<D, E> + Hash<S> + Eq,
|
2014-02-25 10:04:38 -06:00
|
|
|
S,
|
|
|
|
H: Hasher<S> + Default
|
2014-03-18 12:58:26 -05:00
|
|
|
> Decodable<D, E> for HashSet<T, H> {
|
|
|
|
fn decode(d: &mut D) -> Result<HashSet<T, H>, E> {
|
2014-02-21 16:18:39 -06:00
|
|
|
d.read_seq(|d, len| {
|
2014-03-06 20:07:52 -06:00
|
|
|
let mut set = HashSet::with_capacity_and_hasher(len, Default::default());
|
2014-02-21 16:18:39 -06:00
|
|
|
for i in range(0u, len) {
|
2014-03-18 12:58:26 -05:00
|
|
|
set.insert(try!(d.read_seq_elt(i, |d| Decodable::decode(d))));
|
2014-02-21 16:18:39 -06:00
|
|
|
}
|
2014-03-18 12:58:26 -05:00
|
|
|
Ok(set)
|
2014-02-21 16:18:39 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2014-11-09 10:30:18 -06:00
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|