]> git.proxmox.com Git - rustc.git/blob - src/libserialize/collection_impls.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / libserialize / collection_impls.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Implementations of serialization for structures found in libcollections
12
13 use std::hash::{Hash, BuildHasher};
14 use std::mem;
15
16 use {Decodable, Encodable, Decoder, Encoder};
17 use std::collections::{LinkedList, VecDeque, BTreeMap, BTreeSet, HashMap, HashSet};
18 use collections::enum_set::{EnumSet, CLike};
19
20 impl<
21 T: Encodable
22 > Encodable for LinkedList<T> {
23 fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
24 s.emit_seq(self.len(), |s| {
25 for (i, e) in self.iter().enumerate() {
26 s.emit_seq_elt(i, |s| e.encode(s))?;
27 }
28 Ok(())
29 })
30 }
31 }
32
33 impl<T:Decodable> Decodable for LinkedList<T> {
34 fn decode<D: Decoder>(d: &mut D) -> Result<LinkedList<T>, D::Error> {
35 d.read_seq(|d, len| {
36 let mut list = LinkedList::new();
37 for i in 0..len {
38 list.push_back(d.read_seq_elt(i, |d| Decodable::decode(d))?);
39 }
40 Ok(list)
41 })
42 }
43 }
44
45 impl<T: Encodable> Encodable for VecDeque<T> {
46 fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
47 s.emit_seq(self.len(), |s| {
48 for (i, e) in self.iter().enumerate() {
49 s.emit_seq_elt(i, |s| e.encode(s))?;
50 }
51 Ok(())
52 })
53 }
54 }
55
56 impl<T:Decodable> Decodable for VecDeque<T> {
57 fn decode<D: Decoder>(d: &mut D) -> Result<VecDeque<T>, D::Error> {
58 d.read_seq(|d, len| {
59 let mut deque: VecDeque<T> = VecDeque::new();
60 for i in 0..len {
61 deque.push_back(d.read_seq_elt(i, |d| Decodable::decode(d))?);
62 }
63 Ok(deque)
64 })
65 }
66 }
67
68 impl<
69 K: Encodable + PartialEq + Ord,
70 V: Encodable + PartialEq
71 > Encodable for BTreeMap<K, V> {
72 fn encode<S: Encoder>(&self, e: &mut S) -> Result<(), S::Error> {
73 e.emit_map(self.len(), |e| {
74 let mut i = 0;
75 for (key, val) in self {
76 e.emit_map_elt_key(i, |e| key.encode(e))?;
77 e.emit_map_elt_val(i, |e| val.encode(e))?;
78 i += 1;
79 }
80 Ok(())
81 })
82 }
83 }
84
85 impl<
86 K: Decodable + PartialEq + Ord,
87 V: Decodable + PartialEq
88 > Decodable for BTreeMap<K, V> {
89 fn decode<D: Decoder>(d: &mut D) -> Result<BTreeMap<K, V>, D::Error> {
90 d.read_map(|d, len| {
91 let mut map = BTreeMap::new();
92 for i in 0..len {
93 let key = d.read_map_elt_key(i, |d| Decodable::decode(d))?;
94 let val = d.read_map_elt_val(i, |d| Decodable::decode(d))?;
95 map.insert(key, val);
96 }
97 Ok(map)
98 })
99 }
100 }
101
102 impl<
103 T: Encodable + PartialEq + Ord
104 > Encodable for BTreeSet<T> {
105 fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
106 s.emit_seq(self.len(), |s| {
107 let mut i = 0;
108 for e in self {
109 s.emit_seq_elt(i, |s| e.encode(s))?;
110 i += 1;
111 }
112 Ok(())
113 })
114 }
115 }
116
117 impl<
118 T: Decodable + PartialEq + Ord
119 > Decodable for BTreeSet<T> {
120 fn decode<D: Decoder>(d: &mut D) -> Result<BTreeSet<T>, D::Error> {
121 d.read_seq(|d, len| {
122 let mut set = BTreeSet::new();
123 for i in 0..len {
124 set.insert(d.read_seq_elt(i, |d| Decodable::decode(d))?);
125 }
126 Ok(set)
127 })
128 }
129 }
130
131 impl<
132 T: Encodable + CLike
133 > Encodable for EnumSet<T> {
134 fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
135 let mut bits = 0;
136 for item in self {
137 bits |= item.to_usize();
138 }
139 s.emit_uint(bits)
140 }
141 }
142
143 impl<
144 T: Decodable + CLike
145 > Decodable for EnumSet<T> {
146 fn decode<D: Decoder>(d: &mut D) -> Result<EnumSet<T>, D::Error> {
147 let bits = d.read_uint()?;
148 let mut set = EnumSet::new();
149 for bit in 0..(mem::size_of::<usize>()*8) {
150 if bits & (1 << bit) != 0 {
151 set.insert(CLike::from_usize(1 << bit));
152 }
153 }
154 Ok(set)
155 }
156 }
157
158 impl<K, V, S> Encodable for HashMap<K, V, S>
159 where K: Encodable + Hash + Eq,
160 V: Encodable,
161 S: BuildHasher,
162 {
163 fn encode<E: Encoder>(&self, e: &mut E) -> Result<(), E::Error> {
164 e.emit_map(self.len(), |e| {
165 let mut i = 0;
166 for (key, val) in self {
167 e.emit_map_elt_key(i, |e| key.encode(e))?;
168 e.emit_map_elt_val(i, |e| val.encode(e))?;
169 i += 1;
170 }
171 Ok(())
172 })
173 }
174 }
175
176 impl<K, V, S> Decodable for HashMap<K, V, S>
177 where K: Decodable + Hash + Eq,
178 V: Decodable,
179 S: BuildHasher + Default,
180 {
181 fn decode<D: Decoder>(d: &mut D) -> Result<HashMap<K, V, S>, D::Error> {
182 d.read_map(|d, len| {
183 let state = Default::default();
184 let mut map = HashMap::with_capacity_and_hasher(len, state);
185 for i in 0..len {
186 let key = d.read_map_elt_key(i, |d| Decodable::decode(d))?;
187 let val = d.read_map_elt_val(i, |d| Decodable::decode(d))?;
188 map.insert(key, val);
189 }
190 Ok(map)
191 })
192 }
193 }
194
195 impl<T, S> Encodable for HashSet<T, S>
196 where T: Encodable + Hash + Eq,
197 S: BuildHasher,
198 {
199 fn encode<E: Encoder>(&self, s: &mut E) -> Result<(), E::Error> {
200 s.emit_seq(self.len(), |s| {
201 let mut i = 0;
202 for e in self {
203 s.emit_seq_elt(i, |s| e.encode(s))?;
204 i += 1;
205 }
206 Ok(())
207 })
208 }
209 }
210
211 impl<T, S> Decodable for HashSet<T, S>
212 where T: Decodable + Hash + Eq,
213 S: BuildHasher + Default,
214 {
215 fn decode<D: Decoder>(d: &mut D) -> Result<HashSet<T, S>, D::Error> {
216 d.read_seq(|d, len| {
217 let state = Default::default();
218 let mut set = HashSet::with_capacity_and_hasher(len, state);
219 for i in 0..len {
220 set.insert(d.read_seq_elt(i, |d| Decodable::decode(d))?);
221 }
222 Ok(set)
223 })
224 }
225 }