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.
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.
11 //! Implementations of serialization for structures found in libcollections
13 use std
::hash
::{Hash, BuildHasher}
;
16 use {Decodable, Encodable, Decoder, Encoder}
;
17 use std
::collections
::{LinkedList, VecDeque, BTreeMap, BTreeSet, HashMap, HashSet}
;
18 use collections
::enum_set
::{EnumSet, CLike}
;
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
))?
;
33 impl<T
:Decodable
> Decodable
for LinkedList
<T
> {
34 fn decode
<D
: Decoder
>(d
: &mut D
) -> Result
<LinkedList
<T
>, D
::Error
> {
36 let mut list
= LinkedList
::new();
38 list
.push_back(d
.read_seq_elt(i
, |d
| Decodable
::decode(d
))?
);
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
))?
;
56 impl<T
:Decodable
> Decodable
for VecDeque
<T
> {
57 fn decode
<D
: Decoder
>(d
: &mut D
) -> Result
<VecDeque
<T
>, D
::Error
> {
59 let mut deque
: VecDeque
<T
> = VecDeque
::new();
61 deque
.push_back(d
.read_seq_elt(i
, |d
| Decodable
::decode(d
))?
);
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
| {
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
))?
;
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
> {
91 let mut map
= BTreeMap
::new();
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
))?
;
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
| {
109 s
.emit_seq_elt(i
, |s
| e
.encode(s
))?
;
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();
124 set
.insert(d
.read_seq_elt(i
, |d
| Decodable
::decode(d
))?
);
133 > Encodable
for EnumSet
<T
> {
134 fn encode
<S
: Encoder
>(&self, s
: &mut S
) -> Result
<(), S
::Error
> {
137 bits
|= item
.to_usize();
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
));
158 impl<K
, V
, S
> Encodable
for HashMap
<K
, V
, S
>
159 where K
: Encodable
+ Hash
+ Eq
,
163 fn encode
<E
: Encoder
>(&self, e
: &mut E
) -> Result
<(), E
::Error
> {
164 e
.emit_map(self.len(), |e
| {
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
))?
;
176 impl<K
, V
, S
> Decodable
for HashMap
<K
, V
, S
>
177 where K
: Decodable
+ Hash
+ Eq
,
179 S
: BuildHasher
+ Default
,
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
);
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
);
195 impl<T
, S
> Encodable
for HashSet
<T
, S
>
196 where T
: Encodable
+ Hash
+ Eq
,
199 fn encode
<E
: Encoder
>(&self, s
: &mut E
) -> Result
<(), E
::Error
> {
200 s
.emit_seq(self.len(), |s
| {
203 s
.emit_seq_elt(i
, |s
| e
.encode(s
))?
;
211 impl<T
, S
> Decodable
for HashSet
<T
, S
>
212 where T
: Decodable
+ Hash
+ Eq
,
213 S
: BuildHasher
+ Default
,
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
);
220 set
.insert(d
.read_seq_elt(i
, |d
| Decodable
::decode(d
))?
);