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::uint;
14 use std::default::Default;
15 use std::hash::{Hash, Hasher};
16
17 use {Decodable, Encodable, Decoder, Encoder};
18 use collections::{DList, RingBuf, TreeMap, TreeSet, Deque, HashMap, HashSet,
19 TrieMap, TrieSet};
20 use collections::enum_set::{EnumSet, CLike};
21
22 impl<
23 E,
24 S: Encoder<E>,
25 T: Encodable<S, E>
26 > Encodable<S, E> for DList<T> {
27 fn encode(&self, s: &mut S) -> Result<(), E> {
28 s.emit_seq(self.len(), |s| {
29 for (i, e) in self.iter().enumerate() {
30 try!(s.emit_seq_elt(i, |s| e.encode(s)));
31 }
32 Ok(())
33 })
34 }
35 }
36
37 impl<E, D:Decoder<E>,T:Decodable<D, E>> Decodable<D, E> for DList<T> {
38 fn decode(d: &mut D) -> Result<DList<T>, E> {
39 d.read_seq(|d, len| {
40 let mut list = DList::new();
41 for i in range(0u, len) {
42 list.push_back(try!(d.read_seq_elt(i, |d| Decodable::decode(d))));
43 }
44 Ok(list)
45 })
46 }
47 }
48
49 impl<
50 E,
51 S: Encoder<E>,
52 T: Encodable<S, E>
53 > Encodable<S, E> for RingBuf<T> {
54 fn encode(&self, s: &mut S) -> Result<(), E> {
55 s.emit_seq(self.len(), |s| {
56 for (i, e) in self.iter().enumerate() {
57 try!(s.emit_seq_elt(i, |s| e.encode(s)));
58 }
59 Ok(())
60 })
61 }
62 }
63
64 impl<E, D:Decoder<E>,T:Decodable<D, E>> Decodable<D, E> for RingBuf<T> {
65 fn decode(d: &mut D) -> Result<RingBuf<T>, E> {
66 d.read_seq(|d, len| {
67 let mut deque: RingBuf<T> = RingBuf::new();
68 for i in range(0u, len) {
69 deque.push_back(try!(d.read_seq_elt(i, |d| Decodable::decode(d))));
70 }
71 Ok(deque)
72 })
73 }
74 }
75
76 impl<
77 E,
78 S: Encoder<E>,
79 K: Encodable<S, E> + Eq + TotalOrd,
80 V: Encodable<S, E> + Eq
81 > Encodable<S, E> for TreeMap<K, V> {
82 fn encode(&self, e: &mut S) -> Result<(), E> {
83 e.emit_map(self.len(), |e| {
84 let mut i = 0;
85 for (key, val) in self.iter() {
86 try!(e.emit_map_elt_key(i, |e| key.encode(e)));
87 try!(e.emit_map_elt_val(i, |e| val.encode(e)));
88 i += 1;
89 }
90 Ok(())
91 })
92 }
93 }
94
95 impl<
96 E,
97 D: Decoder<E>,
98 K: Decodable<D, E> + Eq + TotalOrd,
99 V: Decodable<D, E> + Eq
100 > Decodable<D, E> for TreeMap<K, V> {
101 fn decode(d: &mut D) -> Result<TreeMap<K, V>, E> {
102 d.read_map(|d, len| {
103 let mut map = TreeMap::new();
104 for i in range(0u, len) {
105 let key = try!(d.read_map_elt_key(i, |d| Decodable::decode(d)));
106 let val = try!(d.read_map_elt_val(i, |d| Decodable::decode(d)));
107 map.insert(key, val);
108 }
109 Ok(map)
110 })
111 }
112 }
113
114 impl<
115 E,
116 S: Encoder<E>,
117 T: Encodable<S, E> + Eq + TotalOrd
118 > Encodable<S, E> for TreeSet<T> {
119 fn encode(&self, s: &mut S) -> Result<(), E> {
120 s.emit_seq(self.len(), |s| {
121 let mut i = 0;
122 for e in self.iter() {
123 try!(s.emit_seq_elt(i, |s| e.encode(s)));
124 i += 1;
125 }
126 Ok(())
127 })
128 }
129 }
130
131 impl<
132 E,
133 D: Decoder<E>,
134 T: Decodable<D, E> + Eq + TotalOrd
135 > Decodable<D, E> for TreeSet<T> {
136 fn decode(d: &mut D) -> Result<TreeSet<T>, E> {
137 d.read_seq(|d, len| {
138 let mut set = TreeSet::new();
139 for i in range(0u, len) {
140 set.insert(try!(d.read_seq_elt(i, |d| Decodable::decode(d))));
141 }
142 Ok(set)
143 })
144 }
145 }
146
147 impl<
148 E,
149 S: Encoder<E>,
150 T: Encodable<S, E> + CLike
151 > Encodable<S, E> for EnumSet<T> {
152 fn encode(&self, s: &mut S) -> Result<(), E> {
153 let mut bits = 0;
154 for item in self.iter() {
155 bits |= item.to_uint();
156 }
157 s.emit_uint(bits)
158 }
159 }
160
161 impl<
162 E,
163 D: Decoder<E>,
164 T: Decodable<D, E> + CLike
165 > Decodable<D, E> for EnumSet<T> {
166 fn decode(d: &mut D) -> Result<EnumSet<T>, E> {
167 let bits = try!(d.read_uint());
168 let mut set = EnumSet::empty();
169 for bit in range(0, uint::BITS) {
170 if bits & (1 << bit) != 0 {
171 set.add(CLike::from_uint(1 << bit));
172 }
173 }
174 Ok(set)
175 }
176 }
177
178 impl<
179 E,
180 S: Encoder<E>,
181 K: Encodable<S, E> + Hash<X> + TotalEq,
182 V: Encodable<S, E>,
183 X,
184 H: Hasher<X>
185 > Encodable<S, E> for HashMap<K, V, H> {
186 fn encode(&self, e: &mut S) -> Result<(), E> {
187 e.emit_map(self.len(), |e| {
188 let mut i = 0;
189 for (key, val) in self.iter() {
190 try!(e.emit_map_elt_key(i, |e| key.encode(e)));
191 try!(e.emit_map_elt_val(i, |e| val.encode(e)));
192 i += 1;
193 }
194 Ok(())
195 })
196 }
197 }
198
199 impl<
200 E,
201 D: Decoder<E>,
202 K: Decodable<D, E> + Hash<S> + TotalEq,
203 V: Decodable<D, E>,
204 S,
205 H: Hasher<S> + Default
206 > Decodable<D, E> for HashMap<K, V, H> {
207 fn decode(d: &mut D) -> Result<HashMap<K, V, H>, E> {
208 d.read_map(|d, len| {
209 let hasher = Default::default();
210 let mut map = HashMap::with_capacity_and_hasher(len, hasher);
211 for i in range(0u, len) {
212 let key = try!(d.read_map_elt_key(i, |d| Decodable::decode(d)));
213 let val = try!(d.read_map_elt_val(i, |d| Decodable::decode(d)));
214 map.insert(key, val);
215 }
216 Ok(map)
217 })
218 }
219 }
220
221 impl<
222 E,
223 S: Encoder<E>,
224 T: Encodable<S, E> + Hash<X> + TotalEq,
225 X,
226 H: Hasher<X>
227 > Encodable<S, E> for HashSet<T, H> {
228 fn encode(&self, s: &mut S) -> Result<(), E> {
229 s.emit_seq(self.len(), |s| {
230 let mut i = 0;
231 for e in self.iter() {
232 try!(s.emit_seq_elt(i, |s| e.encode(s)));
233 i += 1;
234 }
235 Ok(())
236 })
237 }
238 }
239
240 impl<
241 E,
242 D: Decoder<E>,
243 T: Decodable<D, E> + Hash<S> + TotalEq,
244 S,
245 H: Hasher<S> + Default
246 > Decodable<D, E> for HashSet<T, H> {
247 fn decode(d: &mut D) -> Result<HashSet<T, H>, E> {
248 d.read_seq(|d, len| {
249 let mut set = HashSet::with_capacity_and_hasher(len, Default::default());
250 for i in range(0u, len) {
251 set.insert(try!(d.read_seq_elt(i, |d| Decodable::decode(d))));
252 }
253 Ok(set)
254 })
255 }
256 }
257
258 impl<
259 E,
260 S: Encoder<E>,
261 V: Encodable<S, E>
262 > Encodable<S, E> for TrieMap<V> {
263 fn encode(&self, e: &mut S) -> Result<(), E> {
264 e.emit_map(self.len(), |e| {
265 for (i, (key, val)) in self.iter().enumerate() {
266 try!(e.emit_map_elt_key(i, |e| key.encode(e)));
267 try!(e.emit_map_elt_val(i, |e| val.encode(e)));
268 }
269 Ok(())
270 })
271 }
272 }
273
274 impl<
275 E,
276 D: Decoder<E>,
277 V: Decodable<D, E>
278 > Decodable<D, E> for TrieMap<V> {
279 fn decode(d: &mut D) -> Result<TrieMap<V>, E> {
280 d.read_map(|d, len| {
281 let mut map = TrieMap::new();
282 for i in range(0u, len) {
283 let key = try!(d.read_map_elt_key(i, |d| Decodable::decode(d)));
284 let val = try!(d.read_map_elt_val(i, |d| Decodable::decode(d)));
285 map.insert(key, val);
286 }
287 Ok(map)
288 })
289 }
290 }
291
292 impl<E, S: Encoder<E>> Encodable<S, E> for TrieSet {
293 fn encode(&self, s: &mut S) -> Result<(), E> {
294 s.emit_seq(self.len(), |s| {
295 for (i, e) in self.iter().enumerate() {
296 try!(s.emit_seq_elt(i, |s| e.encode(s)));
297 }
298 Ok(())
299 })
300 }
301 }
302
303 impl<E, D: Decoder<E>> Decodable<D, E> for TrieSet {
304 fn decode(d: &mut D) -> Result<TrieSet, E> {
305 d.read_seq(|d, len| {
306 let mut set = TrieSet::new();
307 for i in range(0u, len) {
308 set.insert(try!(d.read_seq_elt(i, |d| Decodable::decode(d))));
309 }
310 Ok(set)
311 })
312 }
313 }