]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
1 | //! Support code for encoding and decoding types. |
2 | ||
3 | /* | |
4 | Core encoding and decoding interfaces. | |
5 | */ | |
6 | ||
c30ab7b3 | 7 | use std::borrow::Cow; |
dfeec247 | 8 | use std::cell::{Cell, RefCell}; |
0bf4aa26 | 9 | use std::marker::PhantomData; |
c34b1796 | 10 | use std::path; |
1a4d82fc | 11 | use std::rc::Rc; |
1a4d82fc JJ |
12 | use std::sync::Arc; |
13 | ||
14 | pub trait Encoder { | |
15 | type Error; | |
16 | ||
17 | // Primitive types: | |
b7449926 | 18 | fn emit_unit(&mut self) -> Result<(), Self::Error>; |
9e0c209e | 19 | fn emit_usize(&mut self, v: usize) -> Result<(), Self::Error>; |
32a655c1 | 20 | fn emit_u128(&mut self, v: u128) -> Result<(), Self::Error>; |
1a4d82fc JJ |
21 | fn emit_u64(&mut self, v: u64) -> Result<(), Self::Error>; |
22 | fn emit_u32(&mut self, v: u32) -> Result<(), Self::Error>; | |
23 | fn emit_u16(&mut self, v: u16) -> Result<(), Self::Error>; | |
24 | fn emit_u8(&mut self, v: u8) -> Result<(), Self::Error>; | |
9e0c209e | 25 | fn emit_isize(&mut self, v: isize) -> Result<(), Self::Error>; |
32a655c1 | 26 | fn emit_i128(&mut self, v: i128) -> Result<(), Self::Error>; |
1a4d82fc JJ |
27 | fn emit_i64(&mut self, v: i64) -> Result<(), Self::Error>; |
28 | fn emit_i32(&mut self, v: i32) -> Result<(), Self::Error>; | |
29 | fn emit_i16(&mut self, v: i16) -> Result<(), Self::Error>; | |
30 | fn emit_i8(&mut self, v: i8) -> Result<(), Self::Error>; | |
31 | fn emit_bool(&mut self, v: bool) -> Result<(), Self::Error>; | |
32 | fn emit_f64(&mut self, v: f64) -> Result<(), Self::Error>; | |
33 | fn emit_f32(&mut self, v: f32) -> Result<(), Self::Error>; | |
34 | fn emit_char(&mut self, v: char) -> Result<(), Self::Error>; | |
35 | fn emit_str(&mut self, v: &str) -> Result<(), Self::Error>; | |
36 | ||
37 | // Compound types: | |
74b04a01 | 38 | #[inline] |
9e0c209e | 39 | fn emit_enum<F>(&mut self, _name: &str, f: F) -> Result<(), Self::Error> |
dfeec247 XL |
40 | where |
41 | F: FnOnce(&mut Self) -> Result<(), Self::Error>, | |
b7449926 XL |
42 | { |
43 | f(self) | |
44 | } | |
45 | ||
dfeec247 XL |
46 | fn emit_enum_variant<F>( |
47 | &mut self, | |
48 | _v_name: &str, | |
49 | v_id: usize, | |
50 | _len: usize, | |
51 | f: F, | |
52 | ) -> Result<(), Self::Error> | |
53 | where | |
54 | F: FnOnce(&mut Self) -> Result<(), Self::Error>, | |
9e0c209e SL |
55 | { |
56 | self.emit_usize(v_id)?; | |
57 | f(self) | |
58 | } | |
1a4d82fc | 59 | |
74b04a01 | 60 | #[inline] |
b7449926 | 61 | fn emit_enum_variant_arg<F>(&mut self, _a_idx: usize, f: F) -> Result<(), Self::Error> |
dfeec247 XL |
62 | where |
63 | F: FnOnce(&mut Self) -> Result<(), Self::Error>, | |
b7449926 XL |
64 | { |
65 | f(self) | |
66 | } | |
67 | ||
dfeec247 XL |
68 | fn emit_enum_struct_variant<F>( |
69 | &mut self, | |
70 | v_name: &str, | |
71 | v_id: usize, | |
72 | len: usize, | |
73 | f: F, | |
74 | ) -> Result<(), Self::Error> | |
75 | where | |
76 | F: FnOnce(&mut Self) -> Result<(), Self::Error>, | |
9e0c209e SL |
77 | { |
78 | self.emit_enum_variant(v_name, v_id, len, f) | |
79 | } | |
b7449926 | 80 | |
dfeec247 XL |
81 | fn emit_enum_struct_variant_field<F>( |
82 | &mut self, | |
83 | _f_name: &str, | |
84 | f_idx: usize, | |
85 | f: F, | |
86 | ) -> Result<(), Self::Error> | |
87 | where | |
88 | F: FnOnce(&mut Self) -> Result<(), Self::Error>, | |
9e0c209e SL |
89 | { |
90 | self.emit_enum_variant_arg(f_idx, f) | |
91 | } | |
1a4d82fc | 92 | |
74b04a01 | 93 | #[inline] |
b7449926 | 94 | fn emit_struct<F>(&mut self, _name: &str, _len: usize, f: F) -> Result<(), Self::Error> |
dfeec247 XL |
95 | where |
96 | F: FnOnce(&mut Self) -> Result<(), Self::Error>, | |
b7449926 XL |
97 | { |
98 | f(self) | |
99 | } | |
100 | ||
74b04a01 | 101 | #[inline] |
dfeec247 XL |
102 | fn emit_struct_field<F>( |
103 | &mut self, | |
104 | _f_name: &str, | |
105 | _f_idx: usize, | |
106 | f: F, | |
107 | ) -> Result<(), Self::Error> | |
108 | where | |
109 | F: FnOnce(&mut Self) -> Result<(), Self::Error>, | |
b7449926 XL |
110 | { |
111 | f(self) | |
112 | } | |
1a4d82fc | 113 | |
74b04a01 | 114 | #[inline] |
9e0c209e | 115 | fn emit_tuple<F>(&mut self, _len: usize, f: F) -> Result<(), Self::Error> |
dfeec247 XL |
116 | where |
117 | F: FnOnce(&mut Self) -> Result<(), Self::Error>, | |
b7449926 XL |
118 | { |
119 | f(self) | |
120 | } | |
121 | ||
74b04a01 | 122 | #[inline] |
9e0c209e | 123 | fn emit_tuple_arg<F>(&mut self, _idx: usize, f: F) -> Result<(), Self::Error> |
dfeec247 XL |
124 | where |
125 | F: FnOnce(&mut Self) -> Result<(), Self::Error>, | |
b7449926 XL |
126 | { |
127 | f(self) | |
128 | } | |
1a4d82fc | 129 | |
b7449926 | 130 | fn emit_tuple_struct<F>(&mut self, _name: &str, len: usize, f: F) -> Result<(), Self::Error> |
dfeec247 XL |
131 | where |
132 | F: FnOnce(&mut Self) -> Result<(), Self::Error>, | |
9e0c209e SL |
133 | { |
134 | self.emit_tuple(len, f) | |
135 | } | |
b7449926 XL |
136 | |
137 | fn emit_tuple_struct_arg<F>(&mut self, f_idx: usize, f: F) -> Result<(), Self::Error> | |
dfeec247 XL |
138 | where |
139 | F: FnOnce(&mut Self) -> Result<(), Self::Error>, | |
9e0c209e SL |
140 | { |
141 | self.emit_tuple_arg(f_idx, f) | |
142 | } | |
1a4d82fc JJ |
143 | |
144 | // Specialized types: | |
145 | fn emit_option<F>(&mut self, f: F) -> Result<(), Self::Error> | |
dfeec247 XL |
146 | where |
147 | F: FnOnce(&mut Self) -> Result<(), Self::Error>, | |
9e0c209e SL |
148 | { |
149 | self.emit_enum("Option", f) | |
150 | } | |
b7449926 XL |
151 | |
152 | #[inline] | |
9e0c209e SL |
153 | fn emit_option_none(&mut self) -> Result<(), Self::Error> { |
154 | self.emit_enum_variant("None", 0, 0, |_| Ok(())) | |
155 | } | |
b7449926 | 156 | |
1a4d82fc | 157 | fn emit_option_some<F>(&mut self, f: F) -> Result<(), Self::Error> |
dfeec247 XL |
158 | where |
159 | F: FnOnce(&mut Self) -> Result<(), Self::Error>, | |
9e0c209e | 160 | { |
9e0c209e SL |
161 | self.emit_enum_variant("Some", 1, 1, f) |
162 | } | |
1a4d82fc | 163 | |
c34b1796 | 164 | fn emit_seq<F>(&mut self, len: usize, f: F) -> Result<(), Self::Error> |
dfeec247 XL |
165 | where |
166 | F: FnOnce(&mut Self) -> Result<(), Self::Error>, | |
9e0c209e SL |
167 | { |
168 | self.emit_usize(len)?; | |
169 | f(self) | |
170 | } | |
b7449926 | 171 | |
74b04a01 | 172 | #[inline] |
9e0c209e | 173 | fn emit_seq_elt<F>(&mut self, _idx: usize, f: F) -> Result<(), Self::Error> |
dfeec247 XL |
174 | where |
175 | F: FnOnce(&mut Self) -> Result<(), Self::Error>, | |
b7449926 XL |
176 | { |
177 | f(self) | |
178 | } | |
1a4d82fc | 179 | |
c34b1796 | 180 | fn emit_map<F>(&mut self, len: usize, f: F) -> Result<(), Self::Error> |
dfeec247 XL |
181 | where |
182 | F: FnOnce(&mut Self) -> Result<(), Self::Error>, | |
9e0c209e SL |
183 | { |
184 | self.emit_usize(len)?; | |
185 | f(self) | |
186 | } | |
b7449926 | 187 | |
74b04a01 | 188 | #[inline] |
9e0c209e | 189 | fn emit_map_elt_key<F>(&mut self, _idx: usize, f: F) -> Result<(), Self::Error> |
dfeec247 XL |
190 | where |
191 | F: FnOnce(&mut Self) -> Result<(), Self::Error>, | |
b7449926 XL |
192 | { |
193 | f(self) | |
194 | } | |
195 | ||
74b04a01 | 196 | #[inline] |
9e0c209e | 197 | fn emit_map_elt_val<F>(&mut self, _idx: usize, f: F) -> Result<(), Self::Error> |
dfeec247 XL |
198 | where |
199 | F: FnOnce(&mut Self) -> Result<(), Self::Error>, | |
b7449926 XL |
200 | { |
201 | f(self) | |
202 | } | |
1a4d82fc JJ |
203 | } |
204 | ||
205 | pub trait Decoder { | |
206 | type Error; | |
207 | ||
208 | // Primitive types: | |
209 | fn read_nil(&mut self) -> Result<(), Self::Error>; | |
9e0c209e | 210 | fn read_usize(&mut self) -> Result<usize, Self::Error>; |
32a655c1 | 211 | fn read_u128(&mut self) -> Result<u128, Self::Error>; |
1a4d82fc JJ |
212 | fn read_u64(&mut self) -> Result<u64, Self::Error>; |
213 | fn read_u32(&mut self) -> Result<u32, Self::Error>; | |
214 | fn read_u16(&mut self) -> Result<u16, Self::Error>; | |
215 | fn read_u8(&mut self) -> Result<u8, Self::Error>; | |
9e0c209e | 216 | fn read_isize(&mut self) -> Result<isize, Self::Error>; |
32a655c1 | 217 | fn read_i128(&mut self) -> Result<i128, Self::Error>; |
1a4d82fc JJ |
218 | fn read_i64(&mut self) -> Result<i64, Self::Error>; |
219 | fn read_i32(&mut self) -> Result<i32, Self::Error>; | |
220 | fn read_i16(&mut self) -> Result<i16, Self::Error>; | |
221 | fn read_i8(&mut self) -> Result<i8, Self::Error>; | |
222 | fn read_bool(&mut self) -> Result<bool, Self::Error>; | |
223 | fn read_f64(&mut self) -> Result<f64, Self::Error>; | |
224 | fn read_f32(&mut self) -> Result<f32, Self::Error>; | |
225 | fn read_char(&mut self) -> Result<char, Self::Error>; | |
9fa01778 | 226 | fn read_str(&mut self) -> Result<Cow<'_, str>, Self::Error>; |
1a4d82fc JJ |
227 | |
228 | // Compound types: | |
74b04a01 | 229 | #[inline] |
9e0c209e | 230 | fn read_enum<T, F>(&mut self, _name: &str, f: F) -> Result<T, Self::Error> |
dfeec247 XL |
231 | where |
232 | F: FnOnce(&mut Self) -> Result<T, Self::Error>, | |
b7449926 XL |
233 | { |
234 | f(self) | |
235 | } | |
1a4d82fc | 236 | |
74b04a01 | 237 | #[inline] |
b7449926 | 238 | fn read_enum_variant<T, F>(&mut self, _names: &[&str], mut f: F) -> Result<T, Self::Error> |
dfeec247 XL |
239 | where |
240 | F: FnMut(&mut Self, usize) -> Result<T, Self::Error>, | |
9e0c209e SL |
241 | { |
242 | let disr = self.read_usize()?; | |
243 | f(self, disr) | |
244 | } | |
1a4d82fc | 245 | |
74b04a01 | 246 | #[inline] |
b7449926 | 247 | fn read_enum_variant_arg<T, F>(&mut self, _a_idx: usize, f: F) -> Result<T, Self::Error> |
dfeec247 XL |
248 | where |
249 | F: FnOnce(&mut Self) -> Result<T, Self::Error>, | |
b7449926 XL |
250 | { |
251 | f(self) | |
252 | } | |
253 | ||
254 | fn read_enum_struct_variant<T, F>(&mut self, names: &[&str], f: F) -> Result<T, Self::Error> | |
dfeec247 XL |
255 | where |
256 | F: FnMut(&mut Self, usize) -> Result<T, Self::Error>, | |
9e0c209e SL |
257 | { |
258 | self.read_enum_variant(names, f) | |
259 | } | |
b7449926 | 260 | |
dfeec247 XL |
261 | fn read_enum_struct_variant_field<T, F>( |
262 | &mut self, | |
263 | _f_name: &str, | |
264 | f_idx: usize, | |
265 | f: F, | |
266 | ) -> Result<T, Self::Error> | |
267 | where | |
268 | F: FnOnce(&mut Self) -> Result<T, Self::Error>, | |
9e0c209e SL |
269 | { |
270 | self.read_enum_variant_arg(f_idx, f) | |
271 | } | |
1a4d82fc | 272 | |
74b04a01 | 273 | #[inline] |
b7449926 | 274 | fn read_struct<T, F>(&mut self, _s_name: &str, _len: usize, f: F) -> Result<T, Self::Error> |
dfeec247 XL |
275 | where |
276 | F: FnOnce(&mut Self) -> Result<T, Self::Error>, | |
b7449926 XL |
277 | { |
278 | f(self) | |
279 | } | |
280 | ||
74b04a01 | 281 | #[inline] |
dfeec247 XL |
282 | fn read_struct_field<T, F>( |
283 | &mut self, | |
284 | _f_name: &str, | |
285 | _f_idx: usize, | |
286 | f: F, | |
287 | ) -> Result<T, Self::Error> | |
288 | where | |
289 | F: FnOnce(&mut Self) -> Result<T, Self::Error>, | |
b7449926 XL |
290 | { |
291 | f(self) | |
292 | } | |
1a4d82fc | 293 | |
74b04a01 | 294 | #[inline] |
9e0c209e | 295 | fn read_tuple<T, F>(&mut self, _len: usize, f: F) -> Result<T, Self::Error> |
dfeec247 XL |
296 | where |
297 | F: FnOnce(&mut Self) -> Result<T, Self::Error>, | |
b7449926 XL |
298 | { |
299 | f(self) | |
300 | } | |
1a4d82fc | 301 | |
74b04a01 | 302 | #[inline] |
b7449926 | 303 | fn read_tuple_arg<T, F>(&mut self, _a_idx: usize, f: F) -> Result<T, Self::Error> |
dfeec247 XL |
304 | where |
305 | F: FnOnce(&mut Self) -> Result<T, Self::Error>, | |
b7449926 XL |
306 | { |
307 | f(self) | |
308 | } | |
309 | ||
310 | fn read_tuple_struct<T, F>(&mut self, _s_name: &str, len: usize, f: F) -> Result<T, Self::Error> | |
dfeec247 XL |
311 | where |
312 | F: FnOnce(&mut Self) -> Result<T, Self::Error>, | |
9e0c209e SL |
313 | { |
314 | self.read_tuple(len, f) | |
315 | } | |
b7449926 XL |
316 | |
317 | fn read_tuple_struct_arg<T, F>(&mut self, a_idx: usize, f: F) -> Result<T, Self::Error> | |
dfeec247 XL |
318 | where |
319 | F: FnOnce(&mut Self) -> Result<T, Self::Error>, | |
9e0c209e SL |
320 | { |
321 | self.read_tuple_arg(a_idx, f) | |
322 | } | |
1a4d82fc JJ |
323 | |
324 | // Specialized types: | |
9e0c209e | 325 | fn read_option<T, F>(&mut self, mut f: F) -> Result<T, Self::Error> |
dfeec247 XL |
326 | where |
327 | F: FnMut(&mut Self, bool) -> Result<T, Self::Error>, | |
9e0c209e SL |
328 | { |
329 | self.read_enum("Option", move |this| { | |
dfeec247 XL |
330 | this.read_enum_variant(&["None", "Some"], move |this, idx| match idx { |
331 | 0 => f(this, false), | |
332 | 1 => f(this, true), | |
333 | _ => Err(this.error("read_option: expected 0 for None or 1 for Some")), | |
9e0c209e SL |
334 | }) |
335 | }) | |
336 | } | |
1a4d82fc JJ |
337 | |
338 | fn read_seq<T, F>(&mut self, f: F) -> Result<T, Self::Error> | |
dfeec247 XL |
339 | where |
340 | F: FnOnce(&mut Self, usize) -> Result<T, Self::Error>, | |
9e0c209e SL |
341 | { |
342 | let len = self.read_usize()?; | |
343 | f(self, len) | |
344 | } | |
b7449926 | 345 | |
74b04a01 | 346 | #[inline] |
9e0c209e | 347 | fn read_seq_elt<T, F>(&mut self, _idx: usize, f: F) -> Result<T, Self::Error> |
dfeec247 XL |
348 | where |
349 | F: FnOnce(&mut Self) -> Result<T, Self::Error>, | |
b7449926 XL |
350 | { |
351 | f(self) | |
352 | } | |
1a4d82fc JJ |
353 | |
354 | fn read_map<T, F>(&mut self, f: F) -> Result<T, Self::Error> | |
dfeec247 XL |
355 | where |
356 | F: FnOnce(&mut Self, usize) -> Result<T, Self::Error>, | |
9e0c209e SL |
357 | { |
358 | let len = self.read_usize()?; | |
359 | f(self, len) | |
360 | } | |
b7449926 | 361 | |
74b04a01 | 362 | #[inline] |
b7449926 | 363 | fn read_map_elt_key<T, F>(&mut self, _idx: usize, f: F) -> Result<T, Self::Error> |
dfeec247 XL |
364 | where |
365 | F: FnOnce(&mut Self) -> Result<T, Self::Error>, | |
b7449926 XL |
366 | { |
367 | f(self) | |
368 | } | |
369 | ||
74b04a01 | 370 | #[inline] |
b7449926 | 371 | fn read_map_elt_val<T, F>(&mut self, _idx: usize, f: F) -> Result<T, Self::Error> |
dfeec247 XL |
372 | where |
373 | F: FnOnce(&mut Self) -> Result<T, Self::Error>, | |
b7449926 XL |
374 | { |
375 | f(self) | |
376 | } | |
1a4d82fc JJ |
377 | |
378 | // Failure | |
379 | fn error(&mut self, err: &str) -> Self::Error; | |
380 | } | |
381 | ||
3dfed10e XL |
382 | /// Trait for types that can be serialized |
383 | /// | |
384 | /// This can be implemented using the `Encodable`, `TyEncodable` and | |
385 | /// `MetadataEncodable` macros. | |
386 | /// | |
387 | /// * `Encodable` should be used in crates that don't depend on | |
388 | /// `rustc_middle`. | |
389 | /// * `MetadataEncodable` is used in `rustc_metadata` for types that contain | |
390 | /// `rustc_metadata::rmeta::Lazy`. | |
391 | /// * `TyEncodable` should be used for types that are only serialized in crate | |
392 | /// metadata or the incremental cache. This is most types in `rustc_middle`. | |
393 | pub trait Encodable<S: Encoder> { | |
394 | fn encode(&self, s: &mut S) -> Result<(), S::Error>; | |
1a4d82fc JJ |
395 | } |
396 | ||
3dfed10e XL |
397 | /// Trait for types that can be deserialized |
398 | /// | |
399 | /// This can be implemented using the `Decodable`, `TyDecodable` and | |
400 | /// `MetadataDecodable` macros. | |
401 | /// | |
402 | /// * `Decodable` should be used in crates that don't depend on | |
403 | /// `rustc_middle`. | |
404 | /// * `MetadataDecodable` is used in `rustc_metadata` for types that contain | |
405 | /// `rustc_metadata::rmeta::Lazy`. | |
406 | /// * `TyDecodable` should be used for types that are only serialized in crate | |
407 | /// metadata or the incremental cache. This is most types in `rustc_middle`. | |
408 | pub trait Decodable<D: Decoder>: Sized { | |
409 | fn decode(d: &mut D) -> Result<Self, D::Error>; | |
410 | } | |
411 | ||
412 | macro_rules! direct_serialize_impls { | |
413 | ($($ty:ident $emit_method:ident $read_method:ident),*) => { | |
414 | $( | |
415 | impl<S: Encoder> Encodable<S> for $ty { | |
416 | fn encode(&self, s: &mut S) -> Result<(), S::Error> { | |
417 | s.$emit_method(*self) | |
418 | } | |
419 | } | |
1a4d82fc | 420 | |
3dfed10e XL |
421 | impl<D: Decoder> Decodable<D> for $ty { |
422 | fn decode(d: &mut D) -> Result<$ty, D::Error> { | |
423 | d.$read_method() | |
424 | } | |
425 | } | |
426 | )* | |
427 | } | |
428 | } | |
429 | ||
430 | direct_serialize_impls! { | |
431 | usize emit_usize read_usize, | |
432 | u8 emit_u8 read_u8, | |
433 | u16 emit_u16 read_u16, | |
434 | u32 emit_u32 read_u32, | |
435 | u64 emit_u64 read_u64, | |
436 | u128 emit_u128 read_u128, | |
437 | isize emit_isize read_isize, | |
438 | i8 emit_i8 read_i8, | |
439 | i16 emit_i16 read_i16, | |
440 | i32 emit_i32 read_i32, | |
441 | i64 emit_i64 read_i64, | |
442 | i128 emit_i128 read_i128, | |
443 | f32 emit_f32 read_f32, | |
444 | f64 emit_f64 read_f64, | |
445 | bool emit_bool read_bool, | |
446 | char emit_char read_char | |
447 | } | |
448 | ||
449 | impl<S: Encoder> Encodable<S> for ::std::num::NonZeroU32 { | |
450 | fn encode(&self, s: &mut S) -> Result<(), S::Error> { | |
b7449926 XL |
451 | s.emit_u32(self.get()) |
452 | } | |
453 | } | |
454 | ||
3dfed10e XL |
455 | impl<D: Decoder> Decodable<D> for ::std::num::NonZeroU32 { |
456 | fn decode(d: &mut D) -> Result<Self, D::Error> { | |
b7449926 XL |
457 | d.read_u32().map(|d| ::std::num::NonZeroU32::new(d).unwrap()) |
458 | } | |
459 | } | |
460 | ||
3dfed10e XL |
461 | impl<S: Encoder> Encodable<S> for str { |
462 | fn encode(&self, s: &mut S) -> Result<(), S::Error> { | |
463 | s.emit_str(self) | |
32a655c1 SL |
464 | } |
465 | } | |
466 | ||
3dfed10e XL |
467 | impl<S: Encoder> Encodable<S> for &str { |
468 | fn encode(&self, s: &mut S) -> Result<(), S::Error> { | |
1a4d82fc JJ |
469 | s.emit_str(self) |
470 | } | |
471 | } | |
472 | ||
3dfed10e XL |
473 | impl<S: Encoder> Encodable<S> for String { |
474 | fn encode(&self, s: &mut S) -> Result<(), S::Error> { | |
85aaf69f | 475 | s.emit_str(&self[..]) |
1a4d82fc JJ |
476 | } |
477 | } | |
478 | ||
3dfed10e XL |
479 | impl<D: Decoder> Decodable<D> for String { |
480 | fn decode(d: &mut D) -> Result<String, D::Error> { | |
c30ab7b3 | 481 | Ok(d.read_str()?.into_owned()) |
1a4d82fc JJ |
482 | } |
483 | } | |
484 | ||
3dfed10e XL |
485 | impl<S: Encoder> Encodable<S> for () { |
486 | fn encode(&self, s: &mut S) -> Result<(), S::Error> { | |
b7449926 | 487 | s.emit_unit() |
1a4d82fc JJ |
488 | } |
489 | } | |
490 | ||
3dfed10e XL |
491 | impl<D: Decoder> Decodable<D> for () { |
492 | fn decode(d: &mut D) -> Result<(), D::Error> { | |
1a4d82fc JJ |
493 | d.read_nil() |
494 | } | |
495 | } | |
496 | ||
3dfed10e XL |
497 | impl<S: Encoder, T> Encodable<S> for PhantomData<T> { |
498 | fn encode(&self, s: &mut S) -> Result<(), S::Error> { | |
0bf4aa26 XL |
499 | s.emit_unit() |
500 | } | |
501 | } | |
502 | ||
3dfed10e XL |
503 | impl<D: Decoder, T> Decodable<D> for PhantomData<T> { |
504 | fn decode(d: &mut D) -> Result<PhantomData<T>, D::Error> { | |
0bf4aa26 XL |
505 | d.read_nil()?; |
506 | Ok(PhantomData) | |
507 | } | |
508 | } | |
509 | ||
3dfed10e XL |
510 | impl<D: Decoder, T: Decodable<D>> Decodable<D> for Box<[T]> { |
511 | fn decode(d: &mut D) -> Result<Box<[T]>, D::Error> { | |
54a0048b | 512 | let v: Vec<T> = Decodable::decode(d)?; |
1a4d82fc JJ |
513 | Ok(v.into_boxed_slice()) |
514 | } | |
515 | } | |
516 | ||
3dfed10e XL |
517 | impl<S: Encoder, T: Encodable<S>> Encodable<S> for Rc<T> { |
518 | fn encode(&self, s: &mut S) -> Result<(), S::Error> { | |
1a4d82fc JJ |
519 | (**self).encode(s) |
520 | } | |
521 | } | |
522 | ||
3dfed10e XL |
523 | impl<D: Decoder, T: Decodable<D>> Decodable<D> for Rc<T> { |
524 | fn decode(d: &mut D) -> Result<Rc<T>, D::Error> { | |
54a0048b | 525 | Ok(Rc::new(Decodable::decode(d)?)) |
1a4d82fc JJ |
526 | } |
527 | } | |
528 | ||
3dfed10e XL |
529 | impl<S: Encoder, T: Encodable<S>> Encodable<S> for [T] { |
530 | fn encode(&self, s: &mut S) -> Result<(), S::Error> { | |
1a4d82fc JJ |
531 | s.emit_seq(self.len(), |s| { |
532 | for (i, e) in self.iter().enumerate() { | |
54a0048b | 533 | s.emit_seq_elt(i, |s| e.encode(s))? |
1a4d82fc JJ |
534 | } |
535 | Ok(()) | |
536 | }) | |
537 | } | |
538 | } | |
539 | ||
3dfed10e XL |
540 | impl<S: Encoder, T: Encodable<S>> Encodable<S> for Vec<T> { |
541 | fn encode(&self, s: &mut S) -> Result<(), S::Error> { | |
1a4d82fc JJ |
542 | s.emit_seq(self.len(), |s| { |
543 | for (i, e) in self.iter().enumerate() { | |
54a0048b | 544 | s.emit_seq_elt(i, |s| e.encode(s))? |
1a4d82fc JJ |
545 | } |
546 | Ok(()) | |
547 | }) | |
548 | } | |
549 | } | |
550 | ||
3dfed10e XL |
551 | impl<D: Decoder, T: Decodable<D>> Decodable<D> for Vec<T> { |
552 | fn decode(d: &mut D) -> Result<Vec<T>, D::Error> { | |
1a4d82fc JJ |
553 | d.read_seq(|d, len| { |
554 | let mut v = Vec::with_capacity(len); | |
85aaf69f | 555 | for i in 0..len { |
54a0048b | 556 | v.push(d.read_seq_elt(i, |d| Decodable::decode(d))?); |
1a4d82fc JJ |
557 | } |
558 | Ok(v) | |
559 | }) | |
560 | } | |
561 | } | |
562 | ||
3dfed10e XL |
563 | impl<S: Encoder> Encodable<S> for [u8; 20] { |
564 | fn encode(&self, s: &mut S) -> Result<(), S::Error> { | |
ba9703b0 XL |
565 | s.emit_seq(self.len(), |s| { |
566 | for (i, e) in self.iter().enumerate() { | |
567 | s.emit_seq_elt(i, |s| e.encode(s))? | |
568 | } | |
569 | Ok(()) | |
570 | }) | |
571 | } | |
572 | } | |
573 | ||
3dfed10e XL |
574 | impl<D: Decoder> Decodable<D> for [u8; 20] { |
575 | fn decode(d: &mut D) -> Result<[u8; 20], D::Error> { | |
ba9703b0 XL |
576 | d.read_seq(|d, len| { |
577 | assert!(len == 20); | |
578 | let mut v = [0u8; 20]; | |
579 | for i in 0..len { | |
580 | v[i] = d.read_seq_elt(i, |d| Decodable::decode(d))?; | |
581 | } | |
582 | Ok(v) | |
583 | }) | |
584 | } | |
585 | } | |
586 | ||
3dfed10e | 587 | impl<'a, S: Encoder, T: Encodable<S>> Encodable<S> for Cow<'a, [T]> |
dfeec247 XL |
588 | where |
589 | [T]: ToOwned<Owned = Vec<T>>, | |
590 | { | |
3dfed10e | 591 | fn encode(&self, s: &mut S) -> Result<(), S::Error> { |
8bb4bdeb XL |
592 | s.emit_seq(self.len(), |s| { |
593 | for (i, e) in self.iter().enumerate() { | |
594 | s.emit_seq_elt(i, |s| e.encode(s))? | |
595 | } | |
596 | Ok(()) | |
597 | }) | |
598 | } | |
599 | } | |
600 | ||
3dfed10e | 601 | impl<D: Decoder, T: Decodable<D> + ToOwned> Decodable<D> for Cow<'static, [T]> |
dfeec247 XL |
602 | where |
603 | [T]: ToOwned<Owned = Vec<T>>, | |
8bb4bdeb | 604 | { |
3dfed10e | 605 | fn decode(d: &mut D) -> Result<Cow<'static, [T]>, D::Error> { |
8bb4bdeb XL |
606 | d.read_seq(|d, len| { |
607 | let mut v = Vec::with_capacity(len); | |
608 | for i in 0..len { | |
609 | v.push(d.read_seq_elt(i, |d| Decodable::decode(d))?); | |
610 | } | |
611 | Ok(Cow::Owned(v)) | |
612 | }) | |
613 | } | |
614 | } | |
615 | ||
3dfed10e XL |
616 | impl<S: Encoder, T: Encodable<S>> Encodable<S> for Option<T> { |
617 | fn encode(&self, s: &mut S) -> Result<(), S::Error> { | |
dfeec247 XL |
618 | s.emit_option(|s| match *self { |
619 | None => s.emit_option_none(), | |
620 | Some(ref v) => s.emit_option_some(|s| v.encode(s)), | |
1a4d82fc JJ |
621 | }) |
622 | } | |
623 | } | |
624 | ||
3dfed10e XL |
625 | impl<D: Decoder, T: Decodable<D>> Decodable<D> for Option<T> { |
626 | fn decode(d: &mut D) -> Result<Option<T>, D::Error> { | |
dfeec247 | 627 | d.read_option(|d, b| if b { Ok(Some(Decodable::decode(d)?)) } else { Ok(None) }) |
ff7c6d11 XL |
628 | } |
629 | } | |
630 | ||
3dfed10e XL |
631 | impl<S: Encoder, T1: Encodable<S>, T2: Encodable<S>> Encodable<S> for Result<T1, T2> { |
632 | fn encode(&self, s: &mut S) -> Result<(), S::Error> { | |
dfeec247 XL |
633 | s.emit_enum("Result", |s| match *self { |
634 | Ok(ref v) => { | |
635 | s.emit_enum_variant("Ok", 0, 1, |s| s.emit_enum_variant_arg(0, |s| v.encode(s))) | |
636 | } | |
637 | Err(ref v) => { | |
638 | s.emit_enum_variant("Err", 1, 1, |s| s.emit_enum_variant_arg(0, |s| v.encode(s))) | |
ff7c6d11 XL |
639 | } |
640 | }) | |
641 | } | |
642 | } | |
643 | ||
3dfed10e XL |
644 | impl<D: Decoder, T1: Decodable<D>, T2: Decodable<D>> Decodable<D> for Result<T1, T2> { |
645 | fn decode(d: &mut D) -> Result<Result<T1, T2>, D::Error> { | |
ff7c6d11 | 646 | d.read_enum("Result", |d| { |
dfeec247 XL |
647 | d.read_enum_variant(&["Ok", "Err"], |d, disr| match disr { |
648 | 0 => Ok(Ok(d.read_enum_variant_arg(0, |d| T1::decode(d))?)), | |
649 | 1 => Ok(Err(d.read_enum_variant_arg(0, |d| T2::decode(d))?)), | |
650 | _ => { | |
651 | panic!( | |
652 | "Encountered invalid discriminant while \ | |
653 | decoding `Result`." | |
654 | ); | |
ff7c6d11 XL |
655 | } |
656 | }) | |
657 | }) | |
1a4d82fc JJ |
658 | } |
659 | } | |
660 | ||
661 | macro_rules! peel { | |
662 | ($name:ident, $($other:ident,)*) => (tuple! { $($other,)* }) | |
663 | } | |
664 | ||
dc9dc135 XL |
665 | /// Evaluates to the number of tokens passed to it. |
666 | /// | |
667 | /// Logarithmic counting: every one or two recursive expansions, the number of | |
668 | /// tokens to count is divided by two, instead of being reduced by one. | |
669 | /// Therefore, the recursion depth is the binary logarithm of the number of | |
670 | /// tokens to count, and the expanded tree is likewise very small. | |
671 | macro_rules! count { | |
672 | () => (0usize); | |
673 | ($one:tt) => (1usize); | |
674 | ($($pairs:tt $_p:tt)*) => (count!($($pairs)*) << 1usize); | |
675 | ($odd:tt $($rest:tt)*) => (count!($($rest)*) | 1usize); | |
1a4d82fc JJ |
676 | } |
677 | ||
678 | macro_rules! tuple { | |
679 | () => (); | |
680 | ( $($name:ident,)+ ) => ( | |
3dfed10e | 681 | impl<D: Decoder, $($name: Decodable<D>),+> Decodable<D> for ($($name,)+) { |
1a4d82fc | 682 | #[allow(non_snake_case)] |
3dfed10e | 683 | fn decode(d: &mut D) -> Result<($($name,)+), D::Error> { |
dc9dc135 | 684 | let len: usize = count!($($name)+); |
1a4d82fc JJ |
685 | d.read_tuple(len, |d| { |
686 | let mut i = 0; | |
b7449926 | 687 | let ret = ($(d.read_tuple_arg({ i+=1; i-1 }, |d| -> Result<$name, D::Error> { |
1a4d82fc | 688 | Decodable::decode(d) |
dc9dc135 | 689 | })?,)+); |
e9174d1e | 690 | Ok(ret) |
1a4d82fc JJ |
691 | }) |
692 | } | |
693 | } | |
3dfed10e | 694 | impl<S: Encoder, $($name: Encodable<S>),+> Encodable<S> for ($($name,)+) { |
1a4d82fc | 695 | #[allow(non_snake_case)] |
3dfed10e | 696 | fn encode(&self, s: &mut S) -> Result<(), S::Error> { |
dc9dc135 | 697 | let ($(ref $name,)+) = *self; |
1a4d82fc | 698 | let mut n = 0; |
dc9dc135 | 699 | $(let $name = $name; n += 1;)+ |
1a4d82fc JJ |
700 | s.emit_tuple(n, |s| { |
701 | let mut i = 0; | |
dc9dc135 | 702 | $(s.emit_tuple_arg({ i+=1; i-1 }, |s| $name.encode(s))?;)+ |
1a4d82fc JJ |
703 | Ok(()) |
704 | }) | |
705 | } | |
706 | } | |
dc9dc135 | 707 | peel! { $($name,)+ } |
1a4d82fc JJ |
708 | ) |
709 | } | |
710 | ||
711 | tuple! { T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, } | |
712 | ||
3dfed10e XL |
713 | impl<S: Encoder> Encodable<S> for path::Path { |
714 | fn encode(&self, e: &mut S) -> Result<(), S::Error> { | |
c34b1796 AL |
715 | self.to_str().unwrap().encode(e) |
716 | } | |
717 | } | |
718 | ||
3dfed10e XL |
719 | impl<S: Encoder> Encodable<S> for path::PathBuf { |
720 | fn encode(&self, e: &mut S) -> Result<(), S::Error> { | |
48663c56 XL |
721 | path::Path::encode(self, e) |
722 | } | |
723 | } | |
724 | ||
3dfed10e XL |
725 | impl<D: Decoder> Decodable<D> for path::PathBuf { |
726 | fn decode(d: &mut D) -> Result<path::PathBuf, D::Error> { | |
54a0048b | 727 | let bytes: String = Decodable::decode(d)?; |
c34b1796 AL |
728 | Ok(path::PathBuf::from(bytes)) |
729 | } | |
730 | } | |
731 | ||
3dfed10e XL |
732 | impl<S: Encoder, T: Encodable<S> + Copy> Encodable<S> for Cell<T> { |
733 | fn encode(&self, s: &mut S) -> Result<(), S::Error> { | |
1a4d82fc JJ |
734 | self.get().encode(s) |
735 | } | |
736 | } | |
737 | ||
3dfed10e XL |
738 | impl<D: Decoder, T: Decodable<D> + Copy> Decodable<D> for Cell<T> { |
739 | fn decode(d: &mut D) -> Result<Cell<T>, D::Error> { | |
54a0048b | 740 | Ok(Cell::new(Decodable::decode(d)?)) |
1a4d82fc JJ |
741 | } |
742 | } | |
743 | ||
744 | // FIXME: #15036 | |
745 | // Should use `try_borrow`, returning a | |
746 | // `encoder.error("attempting to Encode borrowed RefCell")` | |
747 | // from `encode` when `try_borrow` returns `None`. | |
748 | ||
3dfed10e XL |
749 | impl<S: Encoder, T: Encodable<S>> Encodable<S> for RefCell<T> { |
750 | fn encode(&self, s: &mut S) -> Result<(), S::Error> { | |
1a4d82fc JJ |
751 | self.borrow().encode(s) |
752 | } | |
753 | } | |
754 | ||
3dfed10e XL |
755 | impl<D: Decoder, T: Decodable<D>> Decodable<D> for RefCell<T> { |
756 | fn decode(d: &mut D) -> Result<RefCell<T>, D::Error> { | |
54a0048b | 757 | Ok(RefCell::new(Decodable::decode(d)?)) |
1a4d82fc JJ |
758 | } |
759 | } | |
760 | ||
3dfed10e XL |
761 | impl<S: Encoder, T: Encodable<S>> Encodable<S> for Arc<T> { |
762 | fn encode(&self, s: &mut S) -> Result<(), S::Error> { | |
1a4d82fc JJ |
763 | (**self).encode(s) |
764 | } | |
765 | } | |
766 | ||
3dfed10e XL |
767 | impl<D: Decoder, T: Decodable<D>> Decodable<D> for Arc<T> { |
768 | fn decode(d: &mut D) -> Result<Arc<T>, D::Error> { | |
54a0048b | 769 | Ok(Arc::new(Decodable::decode(d)?)) |
1a4d82fc JJ |
770 | } |
771 | } | |
772 | ||
3dfed10e XL |
773 | impl<S: Encoder, T: ?Sized + Encodable<S>> Encodable<S> for Box<T> { |
774 | fn encode(&self, s: &mut S) -> Result<(), S::Error> { | |
f035d41b XL |
775 | (**self).encode(s) |
776 | } | |
777 | } | |
3dfed10e XL |
778 | impl<D: Decoder, T: Decodable<D>> Decodable<D> for Box<T> { |
779 | fn decode(d: &mut D) -> Result<Box<T>, D::Error> { | |
f035d41b XL |
780 | Ok(box Decodable::decode(d)?) |
781 | } | |
782 | } |