implement Serializable/Deserializable for box types

This commit is contained in:
Erick Tryzelaar 2014-08-04 16:38:43 -07:00
parent d689bc8b85
commit 6357c56674
2 changed files with 62 additions and 10 deletions

View File

@ -9,8 +9,11 @@
// except according to those terms.
use std::collections::{HashMap, TreeMap};
use std::gc::{GC, Gc};
use std::hash::Hash;
use std::num;
use std::rc::Rc;
use std::sync::Arc;
#[deriving(Clone, PartialEq, Show)]
pub enum Token {
@ -471,6 +474,48 @@ impl_deserializable!(String, expect_string)
//////////////////////////////////////////////////////////////////////////////
impl<T: Deserializable> Deserializable for Box<T> {
#[inline]
fn deserialize_token<
D: Deserializer<E>,
E
>(d: &mut D, token: Token) -> Result<Box<T>, E> {
Ok(box try!(Deserializable::deserialize_token(d, token)))
}
}
impl<T: Deserializable + 'static> Deserializable for Gc<T> {
#[inline]
fn deserialize_token<
D: Deserializer<E>,
E
>(d: &mut D, token: Token) -> Result<Gc<T>, E> {
Ok(box (GC) try!(Deserializable::deserialize_token(d, token)))
}
}
impl<T: Deserializable> Deserializable for Rc<T> {
#[inline]
fn deserialize_token<
D: Deserializer<E>,
E
>(d: &mut D, token: Token) -> Result<Rc<T>, E> {
Ok(Rc::new(try!(Deserializable::deserialize_token(d, token))))
}
}
impl<T: Deserializable + Send + Share> Deserializable for Arc<T> {
#[inline]
fn deserialize_token<
D: Deserializer<E>,
E
>(d: &mut D, token: Token) -> Result<Arc<T>, E> {
Ok(Arc::new(try!(Deserializable::deserialize_token(d, token))))
}
}
//////////////////////////////////////////////////////////////////////////////
impl<T: Deserializable> Deserializable for Option<T> {
#[inline]
fn deserialize_token<

View File

@ -9,7 +9,10 @@
// except according to those terms.
use std::collections::{HashMap, TreeMap};
use std::gc::Gc;
use std::hash::Hash;
use std::rc::Rc;
use std::sync::Arc;
//////////////////////////////////////////////////////////////////////////////
@ -162,19 +165,23 @@ impl Serializable for String {
//////////////////////////////////////////////////////////////////////////////
impl<
'a,
T: Serializable
> Serializable for &'a T {
#[inline]
fn serialize<
S: Serializer<E>,
E
>(&self, s: &mut S) -> Result<(), E> {
(*self).serialize(s)
macro_rules! impl_serializable_box {
($ty:ty) => {
impl<'a, T: Serializable> Serializable for $ty {
#[inline]
fn serialize<S: Serializer<E>, E>(&self, s: &mut S) -> Result<(), E> {
(*self).serialize(s)
}
}
}
}
impl_serializable_box!(&'a T)
impl_serializable_box!(Box<T>)
impl_serializable_box!(Gc<T>)
impl_serializable_box!(Rc<T>)
impl_serializable_box!(Arc<T>)
//////////////////////////////////////////////////////////////////////////////
impl<