diff --git a/serde/src/de/impls.rs b/serde/src/de/impls.rs index 21d6d807..3f6cbf80 100644 --- a/serde/src/de/impls.rs +++ b/serde/src/de/impls.rs @@ -1,3 +1,4 @@ +use std::borrow::Cow; use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; use std::hash::Hash; use std::marker::PhantomData; @@ -854,3 +855,13 @@ impl Deserialize for Rc { Ok(Rc::new(val)) } } + +impl<'a, T: ?Sized> Deserialize for Cow<'a, T> where T: ToOwned, T::Owned: Deserialize, { + #[inline] + fn deserialize(deserializer: &mut D) -> Result, D::Error> + where D: Deserializer, + { + let val = try!(Deserialize::deserialize(deserializer)); + Ok(Cow::Owned(val)) + } +} diff --git a/serde/src/ser/impls.rs b/serde/src/ser/impls.rs index 3483af4f..88183654 100644 --- a/serde/src/ser/impls.rs +++ b/serde/src/ser/impls.rs @@ -1,3 +1,4 @@ +use std::borrow::Cow; use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; use std::hash::Hash; use std::path; @@ -518,6 +519,15 @@ impl Serialize for Arc where T: Serialize, { } } +impl<'a, T: ?Sized> Serialize for Cow<'a, T> where T: Serialize + ToOwned, { + #[inline] + fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> + where S: Serializer, + { + (**self).serialize(serializer) + } +} + /////////////////////////////////////////////////////////////////////////////// impl Serialize for path::Path {