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