// 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 or the MIT license // , 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; use std::default::Default; use std::hash::{Hash, Hasher}; use std::collections::hash_state::HashState; use {Decodable, Encodable, Decoder, Encoder}; use std::collections::{DList, RingBuf, BTreeMap, BTreeSet, HashMap, HashSet, VecMap}; use collections::enum_set::{EnumSet, CLike}; impl< T: Encodable > Encodable for DList { fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_seq(self.len(), |s| { for (i, e) in self.iter().enumerate() { try!(s.emit_seq_elt(i, |s| e.encode(s))); } Ok(()) }) } } impl Decodable for DList { fn decode(d: &mut D) -> Result, D::Error> { d.read_seq(|d, len| { let mut list = DList::new(); for i in range(0u, len) { list.push_back(try!(d.read_seq_elt(i, |d| Decodable::decode(d)))); } Ok(list) }) } } impl Encodable for RingBuf { fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_seq(self.len(), |s| { for (i, e) in self.iter().enumerate() { try!(s.emit_seq_elt(i, |s| e.encode(s))); } Ok(()) }) } } impl Decodable for RingBuf { fn decode(d: &mut D) -> Result, D::Error> { d.read_seq(|d, len| { let mut deque: RingBuf = RingBuf::new(); for i in range(0u, len) { deque.push_back(try!(d.read_seq_elt(i, |d| Decodable::decode(d)))); } Ok(deque) }) } } impl< K: Encodable + PartialEq + Ord, V: Encodable + PartialEq > Encodable for BTreeMap { fn encode(&self, e: &mut S) -> Result<(), S::Error> { e.emit_map(self.len(), |e| { let mut i = 0; for (key, val) in self.iter() { try!(e.emit_map_elt_key(i, |e| key.encode(e))); try!(e.emit_map_elt_val(i, |e| val.encode(e))); i += 1; } Ok(()) }) } } impl< K: Decodable + PartialEq + Ord, V: Decodable + PartialEq > Decodable for BTreeMap { fn decode(d: &mut D) -> Result, D::Error> { d.read_map(|d, len| { let mut map = BTreeMap::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) }) } } impl< T: Encodable + PartialEq + Ord > Encodable for BTreeSet { fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_seq(self.len(), |s| { let mut i = 0; for e in self.iter() { try!(s.emit_seq_elt(i, |s| e.encode(s))); i += 1; } Ok(()) }) } } impl< T: Decodable + PartialEq + Ord > Decodable for BTreeSet { fn decode(d: &mut D) -> Result, D::Error> { d.read_seq(|d, len| { let mut set = BTreeSet::new(); for i in range(0u, len) { set.insert(try!(d.read_seq_elt(i, |d| Decodable::decode(d)))); } Ok(set) }) } } impl< T: Encodable + CLike > Encodable for EnumSet { fn encode(&self, s: &mut S) -> Result<(), S::Error> { let mut bits = 0; for item in self.iter() { bits |= item.to_uint(); } s.emit_uint(bits) } } impl< T: Decodable + CLike > Decodable for EnumSet { fn decode(d: &mut D) -> Result, D::Error> { let bits = try!(d.read_uint()); let mut set = EnumSet::new(); for bit in range(0, uint::BITS) { if bits & (1 << bit) != 0 { set.insert(CLike::from_uint(1 << bit)); } } Ok(set) } } impl Encodable for HashMap where K: Encodable + Hash< ::Hasher> + Eq, V: Encodable, S: HashState, ::Hasher: Hasher { fn encode(&self, e: &mut E) -> Result<(), E::Error> { e.emit_map(self.len(), |e| { let mut i = 0; for (key, val) in self.iter() { try!(e.emit_map_elt_key(i, |e| key.encode(e))); try!(e.emit_map_elt_val(i, |e| val.encode(e))); i += 1; } Ok(()) }) } } impl Decodable for HashMap where K: Decodable + Hash< ::Hasher> + Eq, V: Decodable, S: HashState + Default, ::Hasher: Hasher { fn decode(d: &mut D) -> Result, D::Error> { d.read_map(|d, len| { let state = Default::default(); let mut map = HashMap::with_capacity_and_hash_state(len, state); 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) }) } } impl Encodable for HashSet where T: Encodable + Hash< ::Hasher> + Eq, S: HashState, ::Hasher: Hasher { fn encode(&self, s: &mut E) -> Result<(), E::Error> { s.emit_seq(self.len(), |s| { let mut i = 0; for e in self.iter() { try!(s.emit_seq_elt(i, |s| e.encode(s))); i += 1; } Ok(()) }) } } impl Decodable for HashSet where T: Decodable + Hash< ::Hasher> + Eq, S: HashState + Default, ::Hasher: Hasher { fn decode(d: &mut D) -> Result, D::Error> { d.read_seq(|d, len| { let state = Default::default(); let mut set = HashSet::with_capacity_and_hash_state(len, state); for i in range(0u, len) { set.insert(try!(d.read_seq_elt(i, |d| Decodable::decode(d)))); } Ok(set) }) } } impl Encodable for VecMap { fn encode(&self, e: &mut S) -> Result<(), S::Error> { 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 Decodable for VecMap { fn decode(d: &mut D) -> Result, D::Error> { 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) }) } }