From e150553d589c12a59a894d1b36ae5573837a039a Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 2 Apr 2015 12:53:51 -0700 Subject: [PATCH 1/2] Add deserialize impls for smart pointers This allows deserializing into a Box/Arc/Rc pointer --- src/de/impls.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/de/impls.rs b/src/de/impls.rs index bfad1e07..73ee53ee 100644 --- a/src/de/impls.rs +++ b/src/de/impls.rs @@ -3,6 +3,8 @@ use std::hash::Hash; use std::marker::PhantomData; use std::num::FromPrimitive; use std::path; +use std::rc::Rc; +use std::sync::Arc; use de::{ Deserialize, @@ -650,3 +652,31 @@ impl Deserialize for path::PathBuf { } } +/////////////////////////////////////////////////////////////////////////////// + +impl Deserialize for Box { + fn deserialize(deserializer: &mut D) -> Result, D::Error> + where D: Deserializer, + { + let val = try!(Deserialize::deserialize(deserializer)); + Ok(Box::new(val)) + } +} + +impl Deserialize for Arc { + fn deserialize(deserializer: &mut D) -> Result, D::Error> + where D: Deserializer, + { + let val = try!(Deserialize::deserialize(deserializer)); + Ok(Arc::new(val)) + } +} + +impl Deserialize for Rc { + fn deserialize(deserializer: &mut D) -> Result, D::Error> + where D: Deserializer, + { + let val = try!(Deserialize::deserialize(deserializer)); + Ok(Rc::new(val)) + } +} From ace6d9e442532ed881bfe546c40666455ebf2c93 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 2 Apr 2015 12:54:21 -0700 Subject: [PATCH 2/2] Generalize some serialization impls * Impl Serialize for bare `str` * Expand `&T` and `&mut T` blanket impls to include `T: ?Sized` * Expand `Box` blanket impl to include `T: ?Sized` --- src/ser/impls.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/ser/impls.rs b/src/ser/impls.rs index babfa5ea..75200cf6 100644 --- a/src/ser/impls.rs +++ b/src/ser/impls.rs @@ -44,12 +44,12 @@ impl_visit!(char, visit_char); /////////////////////////////////////////////////////////////////////////////// -impl<'a> Serialize for &'a str { +impl Serialize for str { #[inline] fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer, { - serializer.visit_str(*self) + serializer.visit_str(self) } } @@ -426,7 +426,7 @@ impl Serialize for VecMap /////////////////////////////////////////////////////////////////////////////// -impl<'a, T> Serialize for &'a T where T: Serialize { +impl<'a, T: ?Sized> Serialize for &'a T where T: Serialize { #[inline] fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer, @@ -435,7 +435,7 @@ impl<'a, T> Serialize for &'a T where T: Serialize { } } -impl<'a, T> Serialize for &'a mut T where T: Serialize { +impl<'a, T: ?Sized> Serialize for &'a mut T where T: Serialize { #[inline] fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer, @@ -444,7 +444,7 @@ impl<'a, T> Serialize for &'a mut T where T: Serialize { } } -impl Serialize for Box where T: Serialize { +impl Serialize for Box where T: Serialize { #[inline] fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer,