// 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::hash::Hash; use {Decodable, Encodable, Decoder, Encoder}; use collections::{DList, RingBuf, TreeMap, TreeSet, Deque, HashMap, HashSet, TrieMap, TrieSet}; use collections::enum_set::{EnumSet, CLike}; impl< S: Encoder, T: Encodable > Encodable for DList { fn encode(&self, s: &mut S) { s.emit_seq(self.len(), |s| { for (i, e) in self.iter().enumerate() { s.emit_seq_elt(i, |s| e.encode(s)); } }) } } impl> Decodable for DList { fn decode(d: &mut D) -> DList { let mut list = DList::new(); d.read_seq(|d, len| { for i in range(0u, len) { list.push_back(d.read_seq_elt(i, |d| Decodable::decode(d))); } }); list } } impl< S: Encoder, T: Encodable > Encodable for RingBuf { fn encode(&self, s: &mut S) { s.emit_seq(self.len(), |s| { for (i, e) in self.iter().enumerate() { s.emit_seq_elt(i, |s| e.encode(s)); } }) } } impl> Decodable for RingBuf { fn decode(d: &mut D) -> RingBuf { let mut deque = RingBuf::new(); d.read_seq(|d, len| { for i in range(0u, len) { deque.push_back(d.read_seq_elt(i, |d| Decodable::decode(d))); } }); deque } } impl< E: Encoder, K: Encodable + Eq + TotalOrd, V: Encodable + Eq > Encodable for TreeMap { fn encode(&self, e: &mut E) { e.emit_map(self.len(), |e| { let mut i = 0; for (key, val) in self.iter() { e.emit_map_elt_key(i, |e| key.encode(e)); e.emit_map_elt_val(i, |e| val.encode(e)); i += 1; } }) } } impl< D: Decoder, K: Decodable + Eq + TotalOrd, V: Decodable + Eq > Decodable for TreeMap { fn decode(d: &mut D) -> TreeMap { d.read_map(|d, len| { let mut map = TreeMap::new(); for i in range(0u, len) { let key = d.read_map_elt_key(i, |d| Decodable::decode(d)); let val = d.read_map_elt_val(i, |d| Decodable::decode(d)); map.insert(key, val); } map }) } } impl< S: Encoder, T: Encodable + Eq + TotalOrd > Encodable for TreeSet { fn encode(&self, s: &mut S) { s.emit_seq(self.len(), |s| { let mut i = 0; for e in self.iter() { s.emit_seq_elt(i, |s| e.encode(s)); i += 1; } }) } } impl< D: Decoder, T: Decodable + Eq + TotalOrd > Decodable for TreeSet { fn decode(d: &mut D) -> TreeSet { d.read_seq(|d, len| { let mut set = TreeSet::new(); for i in range(0u, len) { set.insert(d.read_seq_elt(i, |d| Decodable::decode(d))); } set }) } } impl< S: Encoder, T: Encodable + CLike > Encodable for EnumSet { fn encode(&self, s: &mut S) { let mut bits = 0; for item in self.iter() { bits |= item.to_uint(); } s.emit_uint(bits); } } impl< D: Decoder, T: Decodable + CLike > Decodable for EnumSet { fn decode(d: &mut D) -> EnumSet { let bits = 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)); } } set } } impl< E: Encoder, K: Encodable + Hash + Eq, V: Encodable > Encodable for HashMap { fn encode(&self, e: &mut E) { e.emit_map(self.len(), |e| { let mut i = 0; for (key, val) in self.iter() { e.emit_map_elt_key(i, |e| key.encode(e)); e.emit_map_elt_val(i, |e| val.encode(e)); i += 1; } }) } } impl< D: Decoder, K: Decodable + Hash + Eq, V: Decodable > Decodable for HashMap { fn decode(d: &mut D) -> HashMap { d.read_map(|d, len| { let mut map = HashMap::with_capacity(len); for i in range(0u, len) { let key = d.read_map_elt_key(i, |d| Decodable::decode(d)); let val = d.read_map_elt_val(i, |d| Decodable::decode(d)); map.insert(key, val); } map }) } } impl< S: Encoder, T: Encodable + Hash + Eq > Encodable for HashSet { fn encode(&self, s: &mut S) { s.emit_seq(self.len(), |s| { let mut i = 0; for e in self.iter() { s.emit_seq_elt(i, |s| e.encode(s)); i += 1; } }) } } impl< D: Decoder, T: Decodable + Hash + Eq > Decodable for HashSet { fn decode(d: &mut D) -> HashSet { d.read_seq(|d, len| { let mut set = HashSet::with_capacity(len); for i in range(0u, len) { set.insert(d.read_seq_elt(i, |d| Decodable::decode(d))); } set }) } } impl< E: Encoder, V: Encodable > Encodable for TrieMap { fn encode(&self, e: &mut E) { e.emit_map(self.len(), |e| { for (i, (key, val)) in self.iter().enumerate() { e.emit_map_elt_key(i, |e| key.encode(e)); e.emit_map_elt_val(i, |e| val.encode(e)); } }); } } impl< D: Decoder, V: Decodable > Decodable for TrieMap { fn decode(d: &mut D) -> TrieMap { d.read_map(|d, len| { let mut map = TrieMap::new(); for i in range(0u, len) { let key = d.read_map_elt_key(i, |d| Decodable::decode(d)); let val = d.read_map_elt_val(i, |d| Decodable::decode(d)); map.insert(key, val); } map }) } } impl Encodable for TrieSet { fn encode(&self, s: &mut S) { s.emit_seq(self.len(), |s| { for (i, e) in self.iter().enumerate() { s.emit_seq_elt(i, |s| e.encode(s)); } }) } } impl Decodable for TrieSet { fn decode(d: &mut D) -> TrieSet { d.read_seq(|d, len| { let mut set = TrieSet::new(); for i in range(0u, len) { set.insert(d.read_seq_elt(i, |d| Decodable::decode(d))); } set }) } }