1 //! Support code for encoding and decoding types.
4 Core encoding and decoding interfaces.
8 use std::cell::{Cell, RefCell};
9 use std::marker::PhantomData;
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>;
39 fn emit_enum<F>(&mut self, _name: &str, f: F) -> Result<(), Self::Error>
41 F: FnOnce(&mut Self) -> Result<(), Self::Error>,
46 fn emit_enum_variant<F>(
52 ) -> Result<(), Self::Error>
54 F: FnOnce(&mut Self) -> Result<(), Self::Error>,
56 self.emit_usize(v_id)?;
61 fn emit_enum_variant_arg<F>(&mut self, _a_idx: usize, f: F) -> Result<(), Self::Error>
63 F: FnOnce(&mut Self) -> Result<(), Self::Error>,
68 fn emit_enum_struct_variant<F>(
74 ) -> Result<(), Self::Error>
76 F: FnOnce(&mut Self) -> Result<(), Self::Error>,
78 self.emit_enum_variant(v_name, v_id, len, f)
81 fn emit_enum_struct_variant_field<F>(
86 ) -> Result<(), Self::Error>
88 F: FnOnce(&mut Self) -> Result<(), Self::Error>,
90 self.emit_enum_variant_arg(f_idx, f)
94 fn emit_struct<F>(&mut self, _name: &str, _len: usize, f: F) -> Result<(), Self::Error>
96 F: FnOnce(&mut Self) -> Result<(), Self::Error>,
102 fn emit_struct_field<F>(
107 ) -> Result<(), Self::Error>
109 F: FnOnce(&mut Self) -> Result<(), Self::Error>,
115 fn emit_tuple<F>(&mut self, _len: usize, f: F) -> Result<(), Self::Error>
117 F: FnOnce(&mut Self) -> Result<(), Self::Error>,
123 fn emit_tuple_arg<F>(&mut self, _idx: usize, f: F) -> Result<(), Self::Error>
125 F: FnOnce(&mut Self) -> Result<(), Self::Error>,
130 fn emit_tuple_struct<F>(&mut self, _name: &str, len: usize, f: F) -> Result<(), Self::Error>
132 F: FnOnce(&mut Self) -> Result<(), Self::Error>,
134 self.emit_tuple(len, f)
137 fn emit_tuple_struct_arg<F>(&mut self, f_idx: usize, f: F) -> Result<(), Self::Error>
139 F: FnOnce(&mut Self) -> Result<(), Self::Error>,
141 self.emit_tuple_arg(f_idx, f)
144 // Specialized types:
145 fn emit_option<F>(&mut self, f: F) -> Result<(), Self::Error>
147 F: FnOnce(&mut Self) -> Result<(), Self::Error>,
149 self.emit_enum("Option", f)
153 fn emit_option_none(&mut self) -> Result<(), Self::Error> {
154 self.emit_enum_variant("None", 0, 0, |_| Ok(()))
157 fn emit_option_some<F>(&mut self, f: F) -> Result<(), Self::Error>
159 F: FnOnce(&mut Self) -> Result<(), Self::Error>,
161 self.emit_enum_variant("Some", 1, 1, f)
164 fn emit_seq<F>(&mut self, len: usize, f: F) -> Result<(), Self::Error>
166 F: FnOnce(&mut Self) -> Result<(), Self::Error>,
168 self.emit_usize(len)?;
173 fn emit_seq_elt<F>(&mut self, _idx: usize, f: F) -> Result<(), Self::Error>
175 F: FnOnce(&mut Self) -> Result<(), Self::Error>,
180 fn emit_map<F>(&mut self, len: usize, f: F) -> Result<(), Self::Error>
182 F: FnOnce(&mut Self) -> Result<(), Self::Error>,
184 self.emit_usize(len)?;
189 fn emit_map_elt_key<F>(&mut self, _idx: usize, f: F) -> Result<(), Self::Error>
191 F: FnOnce(&mut Self) -> Result<(), Self::Error>,
197 fn emit_map_elt_val<F>(&mut self, _idx: usize, f: F) -> Result<(), Self::Error>
199 F: FnOnce(&mut Self) -> Result<(), Self::Error>,
209 fn read_nil(&mut self) -> Result<(), Self::Error>;
210 fn read_usize(&mut self) -> Result<usize, Self::Error>;
211 fn read_u128(&mut self) -> Result<u128, Self::Error>;
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>;
216 fn read_isize(&mut self) -> Result<isize, Self::Error>;
217 fn read_i128(&mut self) -> Result<i128, Self::Error>;
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>;
226 fn read_str(&mut self) -> Result<Cow<'_, str>, Self::Error>;
230 fn read_enum<T, F>(&mut self, _name: &str, f: F) -> Result<T, Self::Error>
232 F: FnOnce(&mut Self) -> Result<T, Self::Error>,
238 fn read_enum_variant<T, F>(&mut self, _names: &[&str], mut f: F) -> Result<T, Self::Error>
240 F: FnMut(&mut Self, usize) -> Result<T, Self::Error>,
242 let disr = self.read_usize()?;
247 fn read_enum_variant_arg<T, F>(&mut self, _a_idx: usize, f: F) -> Result<T, Self::Error>
249 F: FnOnce(&mut Self) -> Result<T, Self::Error>,
254 fn read_enum_struct_variant<T, F>(&mut self, names: &[&str], f: F) -> Result<T, Self::Error>
256 F: FnMut(&mut Self, usize) -> Result<T, Self::Error>,
258 self.read_enum_variant(names, f)
261 fn read_enum_struct_variant_field<T, F>(
266 ) -> Result<T, Self::Error>
268 F: FnOnce(&mut Self) -> Result<T, Self::Error>,
270 self.read_enum_variant_arg(f_idx, f)
274 fn read_struct<T, F>(&mut self, _s_name: &str, _len: usize, f: F) -> Result<T, Self::Error>
276 F: FnOnce(&mut Self) -> Result<T, Self::Error>,
282 fn read_struct_field<T, F>(
287 ) -> Result<T, Self::Error>
289 F: FnOnce(&mut Self) -> Result<T, Self::Error>,
295 fn read_tuple<T, F>(&mut self, _len: usize, f: F) -> Result<T, Self::Error>
297 F: FnOnce(&mut Self) -> Result<T, Self::Error>,
303 fn read_tuple_arg<T, F>(&mut self, _a_idx: usize, f: F) -> Result<T, Self::Error>
305 F: FnOnce(&mut Self) -> Result<T, Self::Error>,
310 fn read_tuple_struct<T, F>(&mut self, _s_name: &str, len: usize, f: F) -> Result<T, Self::Error>
312 F: FnOnce(&mut Self) -> Result<T, Self::Error>,
314 self.read_tuple(len, f)
317 fn read_tuple_struct_arg<T, F>(&mut self, a_idx: usize, f: F) -> Result<T, Self::Error>
319 F: FnOnce(&mut Self) -> Result<T, Self::Error>,
321 self.read_tuple_arg(a_idx, f)
324 // Specialized types:
325 fn read_option<T, F>(&mut self, mut f: F) -> Result<T, Self::Error>
327 F: FnMut(&mut Self, bool) -> Result<T, Self::Error>,
329 self.read_enum("Option", move |this| {
330 this.read_enum_variant(&["None", "Some"], move |this, idx| match idx {
333 _ => Err(this.error("read_option: expected 0 for None or 1 for Some")),
338 fn read_seq<T, F>(&mut self, f: F) -> Result<T, Self::Error>
340 F: FnOnce(&mut Self, usize) -> Result<T, Self::Error>,
342 let len = self.read_usize()?;
347 fn read_seq_elt<T, F>(&mut self, _idx: usize, f: F) -> Result<T, Self::Error>
349 F: FnOnce(&mut Self) -> Result<T, Self::Error>,
354 fn read_map<T, F>(&mut self, f: F) -> Result<T, Self::Error>
356 F: FnOnce(&mut Self, usize) -> Result<T, Self::Error>,
358 let len = self.read_usize()?;
363 fn read_map_elt_key<T, F>(&mut self, _idx: usize, f: F) -> Result<T, Self::Error>
365 F: FnOnce(&mut Self) -> Result<T, Self::Error>,
371 fn read_map_elt_val<T, F>(&mut self, _idx: usize, f: F) -> Result<T, Self::Error>
373 F: FnOnce(&mut Self) -> Result<T, Self::Error>,
379 fn error(&mut self, err: &str) -> Self::Error;
382 /// Trait for types that can be serialized
384 /// This can be implemented using the `Encodable`, `TyEncodable` and
385 /// `MetadataEncodable` macros.
387 /// * `Encodable` should be used in crates that don't depend on
388 /// `librustc_middle`.
389 /// * `TyEncodable` should be used for types that are only serialized in crate
390 /// metadata or the incremental cache, except for simple enums.where
391 /// * `MetadataEncodable` is used in `rustc_metadata` for types that are only
392 /// serialized in crate metadata.
393 pub trait Encodable<S: Encoder> {
394 fn encode(&self, s: &mut S) -> Result<(), S::Error>;
397 /// Trait for types that can be deserialized
399 /// This can be implemented using the `Decodable`, `TyDecodable` and
400 /// `MetadataDecodable` macros.
402 /// * `Decodable` should be used in crates that don't depend on
403 /// `librustc_middle`.
404 /// * `TyDecodable` should be used for types that are only serialized in crate
405 /// metadata or the incremental cache, except for simple enums.where
406 /// * `MetadataDecodable` is used in `rustc_metadata` for types that are only
407 /// serialized in crate metadata.
408 pub trait Decodable<D: Decoder>: Sized {
409 fn decode(d: &mut D) -> Result<Self, D::Error>;
412 impl<S: Encoder> Encodable<S> for usize {
413 fn encode(&self, s: &mut S) -> Result<(), S::Error> {
418 impl<D: Decoder> Decodable<D> for usize {
419 fn decode(d: &mut D) -> Result<usize, D::Error> {
424 impl<S: Encoder> Encodable<S> for u8 {
425 fn encode(&self, s: &mut S) -> Result<(), S::Error> {
430 impl<D: Decoder> Decodable<D> for u8 {
431 fn decode(d: &mut D) -> Result<u8, D::Error> {
436 impl<S: Encoder> Encodable<S> for u16 {
437 fn encode(&self, s: &mut S) -> Result<(), S::Error> {
442 impl<D: Decoder> Decodable<D> for u16 {
443 fn decode(d: &mut D) -> Result<u16, D::Error> {
448 impl<S: Encoder> Encodable<S> for u32 {
449 fn encode(&self, s: &mut S) -> Result<(), S::Error> {
454 impl<D: Decoder> Decodable<D> for u32 {
455 fn decode(d: &mut D) -> Result<u32, D::Error> {
460 impl<S: Encoder> Encodable<S> for ::std::num::NonZeroU32 {
461 fn encode(&self, s: &mut S) -> Result<(), S::Error> {
462 s.emit_u32(self.get())
466 impl<D: Decoder> Decodable<D> for ::std::num::NonZeroU32 {
467 fn decode(d: &mut D) -> Result<Self, D::Error> {
468 d.read_u32().map(|d| ::std::num::NonZeroU32::new(d).unwrap())
472 impl<S: Encoder> Encodable<S> for u64 {
473 fn encode(&self, s: &mut S) -> Result<(), S::Error> {
478 impl<D: Decoder> Decodable<D> for u64 {
479 fn decode(d: &mut D) -> Result<u64, D::Error> {
484 impl<S: Encoder> Encodable<S> for u128 {
485 fn encode(&self, s: &mut S) -> Result<(), S::Error> {
490 impl<D: Decoder> Decodable<D> for u128 {
491 fn decode(d: &mut D) -> Result<u128, D::Error> {
496 impl<S: Encoder> Encodable<S> for isize {
497 fn encode(&self, s: &mut S) -> Result<(), S::Error> {
502 impl<D: Decoder> Decodable<D> for isize {
503 fn decode(d: &mut D) -> Result<isize, D::Error> {
508 impl<S: Encoder> Encodable<S> for i8 {
509 fn encode(&self, s: &mut S) -> Result<(), S::Error> {
514 impl<D: Decoder> Decodable<D> for i8 {
515 fn decode(d: &mut D) -> Result<i8, D::Error> {
520 impl<S: Encoder> Encodable<S> for i16 {
521 fn encode(&self, s: &mut S) -> Result<(), S::Error> {
526 impl<D: Decoder> Decodable<D> for i16 {
527 fn decode(d: &mut D) -> Result<i16, D::Error> {
532 impl<S: Encoder> Encodable<S> for i32 {
533 fn encode(&self, s: &mut S) -> Result<(), S::Error> {
538 impl<D: Decoder> Decodable<D> for i32 {
539 fn decode(d: &mut D) -> Result<i32, D::Error> {
544 impl<S: Encoder> Encodable<S> for i64 {
545 fn encode(&self, s: &mut S) -> Result<(), S::Error> {
550 impl<D: Decoder> Decodable<D> for i64 {
551 fn decode(d: &mut D) -> Result<i64, D::Error> {
556 impl<S: Encoder> Encodable<S> for i128 {
557 fn encode(&self, s: &mut S) -> Result<(), S::Error> {
562 impl<D: Decoder> Decodable<D> for i128 {
563 fn decode(d: &mut D) -> Result<i128, D::Error> {
568 impl<S: Encoder> Encodable<S> for str {
569 fn encode(&self, s: &mut S) -> Result<(), S::Error> {
574 impl<S: Encoder> Encodable<S> for &str {
575 fn encode(&self, s: &mut S) -> Result<(), S::Error> {
580 impl<S: Encoder> Encodable<S> for String {
581 fn encode(&self, s: &mut S) -> Result<(), S::Error> {
582 s.emit_str(&self[..])
586 impl<D: Decoder> Decodable<D> for String {
587 fn decode(d: &mut D) -> Result<String, D::Error> {
588 Ok(d.read_str()?.into_owned())
592 impl<S: Encoder> Encodable<S> for f32 {
593 fn encode(&self, s: &mut S) -> Result<(), S::Error> {
598 impl<D: Decoder> Decodable<D> for f32 {
599 fn decode(d: &mut D) -> Result<f32, D::Error> {
604 impl<S: Encoder> Encodable<S> for f64 {
605 fn encode(&self, s: &mut S) -> Result<(), S::Error> {
610 impl<D: Decoder> Decodable<D> for f64 {
611 fn decode(d: &mut D) -> Result<f64, D::Error> {
616 impl<S: Encoder> Encodable<S> for bool {
617 fn encode(&self, s: &mut S) -> Result<(), S::Error> {
622 impl<D: Decoder> Decodable<D> for bool {
623 fn decode(d: &mut D) -> Result<bool, D::Error> {
628 impl<S: Encoder> Encodable<S> for char {
629 fn encode(&self, s: &mut S) -> Result<(), S::Error> {
634 impl<D: Decoder> Decodable<D> for char {
635 fn decode(d: &mut D) -> Result<char, D::Error> {
640 impl<S: Encoder> Encodable<S> for () {
641 fn encode(&self, s: &mut S) -> Result<(), S::Error> {
646 impl<D: Decoder> Decodable<D> for () {
647 fn decode(d: &mut D) -> Result<(), D::Error> {
652 impl<S: Encoder, T> Encodable<S> for PhantomData<T> {
653 fn encode(&self, s: &mut S) -> Result<(), S::Error> {
658 impl<D: Decoder, T> Decodable<D> for PhantomData<T> {
659 fn decode(d: &mut D) -> Result<PhantomData<T>, D::Error> {
665 impl<D: Decoder, T: Decodable<D>> Decodable<D> for Box<[T]> {
666 fn decode(d: &mut D) -> Result<Box<[T]>, D::Error> {
667 let v: Vec<T> = Decodable::decode(d)?;
668 Ok(v.into_boxed_slice())
672 impl<S: Encoder, T: Encodable<S>> Encodable<S> for Rc<T> {
673 fn encode(&self, s: &mut S) -> Result<(), S::Error> {
678 impl<D: Decoder, T: Decodable<D>> Decodable<D> for Rc<T> {
679 fn decode(d: &mut D) -> Result<Rc<T>, D::Error> {
680 Ok(Rc::new(Decodable::decode(d)?))
684 impl<S: Encoder, T: Encodable<S>> Encodable<S> for [T] {
685 fn encode(&self, s: &mut S) -> Result<(), S::Error> {
686 s.emit_seq(self.len(), |s| {
687 for (i, e) in self.iter().enumerate() {
688 s.emit_seq_elt(i, |s| e.encode(s))?
695 impl<S: Encoder, T: Encodable<S>> Encodable<S> for Vec<T> {
696 fn encode(&self, s: &mut S) -> Result<(), S::Error> {
697 s.emit_seq(self.len(), |s| {
698 for (i, e) in self.iter().enumerate() {
699 s.emit_seq_elt(i, |s| e.encode(s))?
706 impl<D: Decoder, T: Decodable<D>> Decodable<D> for Vec<T> {
707 fn decode(d: &mut D) -> Result<Vec<T>, D::Error> {
708 d.read_seq(|d, len| {
709 let mut v = Vec::with_capacity(len);
711 v.push(d.read_seq_elt(i, |d| Decodable::decode(d))?);
718 impl<S: Encoder> Encodable<S> for [u8; 20] {
719 fn encode(&self, s: &mut S) -> Result<(), S::Error> {
720 s.emit_seq(self.len(), |s| {
721 for (i, e) in self.iter().enumerate() {
722 s.emit_seq_elt(i, |s| e.encode(s))?
729 impl<D: Decoder> Decodable<D> for [u8; 20] {
730 fn decode(d: &mut D) -> Result<[u8; 20], D::Error> {
731 d.read_seq(|d, len| {
733 let mut v = [0u8; 20];
735 v[i] = d.read_seq_elt(i, |d| Decodable::decode(d))?;
742 impl<'a, S: Encoder, T: Encodable<S>> Encodable<S> for Cow<'a, [T]>
744 [T]: ToOwned<Owned = Vec<T>>,
746 fn encode(&self, s: &mut S) -> Result<(), S::Error> {
747 s.emit_seq(self.len(), |s| {
748 for (i, e) in self.iter().enumerate() {
749 s.emit_seq_elt(i, |s| e.encode(s))?
756 impl<D: Decoder, T: Decodable<D> + ToOwned> Decodable<D> for Cow<'static, [T]>
758 [T]: ToOwned<Owned = Vec<T>>,
760 fn decode(d: &mut D) -> Result<Cow<'static, [T]>, D::Error> {
761 d.read_seq(|d, len| {
762 let mut v = Vec::with_capacity(len);
764 v.push(d.read_seq_elt(i, |d| Decodable::decode(d))?);
771 impl<S: Encoder, T: Encodable<S>> Encodable<S> for Option<T> {
772 fn encode(&self, s: &mut S) -> Result<(), S::Error> {
773 s.emit_option(|s| match *self {
774 None => s.emit_option_none(),
775 Some(ref v) => s.emit_option_some(|s| v.encode(s)),
780 impl<D: Decoder, T: Decodable<D>> Decodable<D> for Option<T> {
781 fn decode(d: &mut D) -> Result<Option<T>, D::Error> {
782 d.read_option(|d, b| if b { Ok(Some(Decodable::decode(d)?)) } else { Ok(None) })
786 impl<S: Encoder, T1: Encodable<S>, T2: Encodable<S>> Encodable<S> for Result<T1, T2> {
787 fn encode(&self, s: &mut S) -> Result<(), S::Error> {
788 s.emit_enum("Result", |s| match *self {
790 s.emit_enum_variant("Ok", 0, 1, |s| s.emit_enum_variant_arg(0, |s| v.encode(s)))
793 s.emit_enum_variant("Err", 1, 1, |s| s.emit_enum_variant_arg(0, |s| v.encode(s)))
799 impl<D: Decoder, T1: Decodable<D>, T2: Decodable<D>> Decodable<D> for Result<T1, T2> {
800 fn decode(d: &mut D) -> Result<Result<T1, T2>, D::Error> {
801 d.read_enum("Result", |d| {
802 d.read_enum_variant(&["Ok", "Err"], |d, disr| match disr {
803 0 => Ok(Ok(d.read_enum_variant_arg(0, |d| T1::decode(d))?)),
804 1 => Ok(Err(d.read_enum_variant_arg(0, |d| T2::decode(d))?)),
807 "Encountered invalid discriminant while \
817 ($name:ident, $($other:ident,)*) => (tuple! { $($other,)* })
820 /// Evaluates to the number of tokens passed to it.
822 /// Logarithmic counting: every one or two recursive expansions, the number of
823 /// tokens to count is divided by two, instead of being reduced by one.
824 /// Therefore, the recursion depth is the binary logarithm of the number of
825 /// tokens to count, and the expanded tree is likewise very small.
828 ($one:tt) => (1usize);
829 ($($pairs:tt $_p:tt)*) => (count!($($pairs)*) << 1usize);
830 ($odd:tt $($rest:tt)*) => (count!($($rest)*) | 1usize);
835 ( $($name:ident,)+ ) => (
836 impl<D: Decoder, $($name: Decodable<D>),+> Decodable<D> for ($($name,)+) {
837 #[allow(non_snake_case)]
838 fn decode(d: &mut D) -> Result<($($name,)+), D::Error> {
839 let len: usize = count!($($name)+);
840 d.read_tuple(len, |d| {
842 let ret = ($(d.read_tuple_arg({ i+=1; i-1 }, |d| -> Result<$name, D::Error> {
849 impl<S: Encoder, $($name: Encodable<S>),+> Encodable<S> for ($($name,)+) {
850 #[allow(non_snake_case)]
851 fn encode(&self, s: &mut S) -> Result<(), S::Error> {
852 let ($(ref $name,)+) = *self;
854 $(let $name = $name; n += 1;)+
855 s.emit_tuple(n, |s| {
857 $(s.emit_tuple_arg({ i+=1; i-1 }, |s| $name.encode(s))?;)+
866 tuple! { T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, }
868 impl<S: Encoder> Encodable<S> for path::Path {
869 fn encode(&self, e: &mut S) -> Result<(), S::Error> {
870 self.to_str().unwrap().encode(e)
874 impl<S: Encoder> Encodable<S> for path::PathBuf {
875 fn encode(&self, e: &mut S) -> Result<(), S::Error> {
876 path::Path::encode(self, e)
880 impl<D: Decoder> Decodable<D> for path::PathBuf {
881 fn decode(d: &mut D) -> Result<path::PathBuf, D::Error> {
882 let bytes: String = Decodable::decode(d)?;
883 Ok(path::PathBuf::from(bytes))
887 impl<S: Encoder, T: Encodable<S> + Copy> Encodable<S> for Cell<T> {
888 fn encode(&self, s: &mut S) -> Result<(), S::Error> {
893 impl<D: Decoder, T: Decodable<D> + Copy> Decodable<D> for Cell<T> {
894 fn decode(d: &mut D) -> Result<Cell<T>, D::Error> {
895 Ok(Cell::new(Decodable::decode(d)?))
900 // Should use `try_borrow`, returning a
901 // `encoder.error("attempting to Encode borrowed RefCell")`
902 // from `encode` when `try_borrow` returns `None`.
904 impl<S: Encoder, T: Encodable<S>> Encodable<S> for RefCell<T> {
905 fn encode(&self, s: &mut S) -> Result<(), S::Error> {
906 self.borrow().encode(s)
910 impl<D: Decoder, T: Decodable<D>> Decodable<D> for RefCell<T> {
911 fn decode(d: &mut D) -> Result<RefCell<T>, D::Error> {
912 Ok(RefCell::new(Decodable::decode(d)?))
916 impl<S: Encoder, T: Encodable<S>> Encodable<S> for Arc<T> {
917 fn encode(&self, s: &mut S) -> Result<(), S::Error> {
922 impl<D: Decoder, T: Decodable<D>> Decodable<D> for Arc<T> {
923 fn decode(d: &mut D) -> Result<Arc<T>, D::Error> {
924 Ok(Arc::new(Decodable::decode(d)?))
928 impl<S: Encoder, T: ?Sized + Encodable<S>> Encodable<S> for Box<T> {
929 fn encode(&self, s: &mut S) -> Result<(), S::Error> {
933 impl<D: Decoder, T: Decodable<D>> Decodable<D> for Box<T> {
934 fn decode(d: &mut D) -> Result<Box<T>, D::Error> {
935 Ok(box Decodable::decode(d)?)