about summary refs log tree commit diff
path: root/compiler/rustc_serialize/src
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/rustc_serialize/src')
-rw-r--r--compiler/rustc_serialize/src/json.rs16
-rw-r--r--compiler/rustc_serialize/src/serialize.rs14
2 files changed, 30 insertions, 0 deletions
diff --git a/compiler/rustc_serialize/src/json.rs b/compiler/rustc_serialize/src/json.rs
index 0cbea3a07a8..c915dd5bbf6 100644
--- a/compiler/rustc_serialize/src/json.rs
+++ b/compiler/rustc_serialize/src/json.rs
@@ -170,6 +170,7 @@ use self::JsonEvent::*;
 use self::ParserError::*;
 use self::ParserState::*;
 
+use std::borrow::Cow;
 use std::collections::{BTreeMap, HashMap};
 use std::mem::swap;
 use std::num::FpCategory as Fp;
@@ -2196,6 +2197,12 @@ impl ToJson for string::String {
     }
 }
 
+impl<'a> ToJson for Cow<'a, str> {
+    fn to_json(&self) -> Json {
+        Json::String(self.to_string())
+    }
+}
+
 macro_rules! tuple_impl {
     // use variables to indicate the arity of the tuple
     ($($tyvar:ident),* ) => {
@@ -2240,6 +2247,15 @@ impl<A: ToJson> ToJson for Vec<A> {
     }
 }
 
+impl<'a, A: ToJson> ToJson for Cow<'a, [A]>
+where
+    [A]: ToOwned,
+{
+    fn to_json(&self) -> Json {
+        Json::Array(self.iter().map(|elt| elt.to_json()).collect())
+    }
+}
+
 impl<T: ToString, A: ToJson> ToJson for BTreeMap<T, A> {
     fn to_json(&self) -> Json {
         let mut d = BTreeMap::new();
diff --git a/compiler/rustc_serialize/src/serialize.rs b/compiler/rustc_serialize/src/serialize.rs
index 42bf6ff2a98..d5053034ed8 100644
--- a/compiler/rustc_serialize/src/serialize.rs
+++ b/compiler/rustc_serialize/src/serialize.rs
@@ -431,6 +431,20 @@ where
     }
 }
 
+impl<'a, S: Encoder> Encodable<S> for Cow<'a, str> {
+    fn encode(&self, s: &mut S) -> Result<(), S::Error> {
+        let val: &str = self;
+        val.encode(s)
+    }
+}
+
+impl<'a, D: Decoder> Decodable<D> for Cow<'a, str> {
+    fn decode(d: &mut D) -> Cow<'static, str> {
+        let v: String = Decodable::decode(d);
+        Cow::Owned(v)
+    }
+}
+
 impl<S: Encoder, T: Encodable<S>> Encodable<S> for Option<T> {
     fn encode(&self, s: &mut S) -> Result<(), S::Error> {
         s.emit_option(|s| match *self {