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