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