// 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 {Decodable, Encodable, Decoder, Encoder}; use std::collections::{DList, RingBuf, TreeMap, TreeSet, HashMap, HashSet, TrieMap, TrieSet}; use std::collections::enum_set::{EnumSet, CLike}; impl< E, S: Encoder, T: Encodable > Encodable for DList { fn encode(&self, s: &mut S) -> Result<(), E> { 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,T:Decodable> Decodable for DList { fn decode(d: &mut D) -> Result, E> { d.read_seq(|d, len| { let mut list = DList::new(); for i in range(0u, len) { list.push(try!(d.read_seq_elt(i, |d| Decodable::decode(d)))); } Ok(list) }) } } impl< E, S: Encoder, T: Encodable > Encodable for RingBuf { fn encode(&self, s: &mut S) -> Result<(), E> { 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,T:Decodable> Decodable for RingBuf { fn decode(d: &mut D) -> Result, E> { d.read_seq(|d, len| { let mut deque: RingBuf = RingBuf::new(); for i in range(0u, len) { deque.push(try!(d.read_seq_elt(i, |d| Decodable::decode(d)))); } Ok(deque) }) } } impl< E, S: Encoder, K: Encodable + PartialEq + Ord, V: Encodable + PartialEq > Encodable for TreeMap { fn encode(&self, e: &mut S) -> Result<(), E> { 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< E, D: Decoder, K: Decodable + PartialEq + Ord, V: Decodable + PartialEq > Decodable for TreeMap { fn decode(d: &mut D) -> Result, E> { d.read_map(|d, len| { let mut map = TreeMap::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< E, S: Encoder, T: Encodable + PartialEq + Ord > Encodable for TreeSet { fn encode(&self, s: &mut S) -> Result<(), E> { 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< E, D: Decoder, T: Decodable + PartialEq + Ord > Decodable for TreeSet { fn decode(d: &mut D) -> Result, E> { d.read_seq(|d, len| { let mut set = TreeSet::new(); for i in range(0u, len) { set.insert(try!(d.read_seq_elt(i, |d| Decodable::decode(d)))); } Ok(set) }) } } impl< E, S: Encoder, T: Encodable + CLike > Encodable for EnumSet { fn encode(&self, s: &mut S) -> Result<(), E> { let mut bits = 0; for item in self.iter() { bits |= item.to_uint(); } s.emit_uint(bits) } } impl< E, D: Decoder, T: Decodable + CLike > Decodable for EnumSet { fn decode(d: &mut D) -> Result, E> { let bits = try!(d.read_uint()); let mut set = EnumSet::empty(); for bit in range(0, uint::BITS) { if bits & (1 << bit) != 0 { set.add(CLike::from_uint(1 << bit)); } } Ok(set) } } impl< E, S: Encoder, K: Encodable + Hash + Eq, V: Encodable, X, H: Hasher > Encodable for HashMap { fn encode(&self, e: &mut S) -> Result<(), E> { 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< E, D: Decoder, K: Decodable + Hash + Eq, V: Decodable, S, H: Hasher + Default > Decodable for HashMap { fn decode(d: &mut D) -> Result, E> { d.read_map(|d, len| { let hasher = Default::default(); let mut map = HashMap::with_capacity_and_hasher(len, hasher); 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< E, S: Encoder, T: Encodable + Hash + Eq, X, H: Hasher > Encodable for HashSet { fn encode(&self, s: &mut S) -> Result<(), E> { 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< E, D: Decoder, T: Decodable + Hash + Eq, S, H: Hasher + Default > Decodable for HashSet { fn decode(d: &mut D) -> Result, E> { d.read_seq(|d, len| { let mut set = HashSet::with_capacity_and_hasher(len, Default::default()); for i in range(0u, len) { set.insert(try!(d.read_seq_elt(i, |d| Decodable::decode(d)))); } Ok(set) }) } } impl< E, S: Encoder, V: Encodable > Encodable for TrieMap { 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, V: Decodable > Decodable for TrieMap { fn decode(d: &mut D) -> Result, E> { d.read_map(|d, len| { let mut map = TrieMap::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> Encodable for TrieSet { fn encode(&self, s: &mut S) -> Result<(), E> { 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 TrieSet { fn decode(d: &mut D) -> Result { d.read_seq(|d, len| { let mut set = TrieSet::new(); for i in range(0u, len) { set.insert(try!(d.read_seq_elt(i, |d| Decodable::decode(d)))); } Ok(set) }) } }