about summary refs log tree commit diff
path: root/src/libserialize
diff options
context:
space:
mode:
authorJorge Aparicio <japaricious@gmail.com>2014-11-06 11:25:09 -0500
committerJorge Aparicio <japaricious@gmail.com>2014-11-06 13:08:24 -0500
commit679eb9191dd06a2e0dacf3602da56765211fd76a (patch)
tree5f54112030558429db3a1e1729faa5ef5b0da9ed /src/libserialize
parent60a669a1743b845dfa349684ef057bc98ec6d840 (diff)
downloadrust-679eb9191dd06a2e0dacf3602da56765211fd76a.tar.gz
rust-679eb9191dd06a2e0dacf3602da56765211fd76a.zip
DTSify libserialize traits
- ToBase64
- FromBase64
- ToHex
- FromHex
- ToJson
- Encodable
Diffstat (limited to 'src/libserialize')
-rw-r--r--src/libserialize/base64.rs10
-rw-r--r--src/libserialize/hex.rs8
-rw-r--r--src/libserialize/json.rs4
-rw-r--r--src/libserialize/serialize.rs21
4 files changed, 25 insertions, 18 deletions
diff --git a/src/libserialize/base64.rs b/src/libserialize/base64.rs
index e69a0ea7929..f287fb99750 100644
--- a/src/libserialize/base64.rs
+++ b/src/libserialize/base64.rs
@@ -54,13 +54,13 @@ static URLSAFE_CHARS: &'static[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\
                                        0123456789-_";
 
 /// A trait for converting a value to base64 encoding.
-pub trait ToBase64 {
+pub trait ToBase64 for Sized? {
     /// Converts the value of `self` to a base64 value following the specified
     /// format configuration, returning the owned string.
     fn to_base64(&self, config: Config) -> String;
 }
 
-impl<'a> ToBase64 for &'a [u8] {
+impl ToBase64 for [u8] {
     /**
      * Turn a vector of `u8` bytes into a base64 string.
      *
@@ -155,7 +155,7 @@ impl<'a> ToBase64 for &'a [u8] {
 }
 
 /// A trait for converting from base64 encoded values.
-pub trait FromBase64 {
+pub trait FromBase64 for Sized? {
     /// Converts the value of `self`, interpreted as base64 encoded data, into
     /// an owned vector of bytes, returning the vector.
     fn from_base64(&self) -> Result<Vec<u8>, FromBase64Error>;
@@ -192,7 +192,7 @@ impl error::Error for FromBase64Error {
     }
 }
 
-impl<'a> FromBase64 for &'a str {
+impl FromBase64 for str {
     /**
      * Convert any base64 encoded string (literal, `@`, `&`, or `~`)
      * to the byte values it encodes.
@@ -227,7 +227,7 @@ impl<'a> FromBase64 for &'a str {
     }
 }
 
-impl<'a> FromBase64 for &'a [u8] {
+impl FromBase64 for [u8] {
     fn from_base64(&self) -> Result<Vec<u8>, FromBase64Error> {
         let mut r = Vec::new();
         let mut buf: u32 = 0;
diff --git a/src/libserialize/hex.rs b/src/libserialize/hex.rs
index b591d35c67c..e045f94c08e 100644
--- a/src/libserialize/hex.rs
+++ b/src/libserialize/hex.rs
@@ -16,7 +16,7 @@ use std::string;
 use std::error;
 
 /// A trait for converting a value to hexadecimal encoding
-pub trait ToHex {
+pub trait ToHex for Sized? {
     /// Converts the value of `self` to a hex value, returning the owned
     /// string.
     fn to_hex(&self) -> String;
@@ -24,7 +24,7 @@ pub trait ToHex {
 
 static CHARS: &'static[u8] = b"0123456789abcdef";
 
-impl<'a> ToHex for &'a [u8] {
+impl ToHex for [u8] {
     /**
      * Turn a vector of `u8` bytes into a hexadecimal string.
      *
@@ -54,7 +54,7 @@ impl<'a> ToHex for &'a [u8] {
 }
 
 /// A trait for converting hexadecimal encoded values
-pub trait FromHex {
+pub trait FromHex for Sized? {
     /// Converts the value of `self`, interpreted as hexadecimal encoded data,
     /// into an owned vector of bytes, returning the vector.
     fn from_hex(&self) -> Result<Vec<u8>, FromHexError>;
@@ -92,7 +92,7 @@ impl error::Error for FromHexError {
 }
 
 
-impl<'a> FromHex for &'a str {
+impl FromHex for str {
     /**
      * Convert any hexadecimal encoded string (literal, `@`, `&`, or `~`)
      * to the byte values it encodes.
diff --git a/src/libserialize/json.rs b/src/libserialize/json.rs
index dbdfa17bfc2..7093fefeb8f 100644
--- a/src/libserialize/json.rs
+++ b/src/libserialize/json.rs
@@ -2303,7 +2303,7 @@ impl ::Decoder<DecoderError> for Decoder {
 }
 
 /// A trait for converting values to JSON
-pub trait ToJson {
+pub trait ToJson for Sized? {
     /// Converts the value of `self` to an instance of JSON
     fn to_json(&self) -> Json;
 }
@@ -2389,7 +2389,7 @@ tuple_impl!{A, B, C, D, E, F, G, H, I, J}
 tuple_impl!{A, B, C, D, E, F, G, H, I, J, K}
 tuple_impl!{A, B, C, D, E, F, G, H, I, J, K, L}
 
-impl<'a, A: ToJson> ToJson for &'a [A] {
+impl<A: ToJson> ToJson for [A] {
     fn to_json(&self) -> Json { List(self.iter().map(|elt| elt.to_json()).collect()) }
 }
 
diff --git a/src/libserialize/serialize.rs b/src/libserialize/serialize.rs
index b7c37defbfa..7539a6dc348 100644
--- a/src/libserialize/serialize.rs
+++ b/src/libserialize/serialize.rs
@@ -169,7 +169,7 @@ pub trait Decoder<E> {
     fn error(&mut self, err: &str) -> E;
 }
 
-pub trait Encodable<S:Encoder<E>, E> {
+pub trait Encodable<S:Encoder<E>, E> for Sized? {
     fn encode(&self, s: &mut S) -> Result<(), E>;
 }
 
@@ -297,9 +297,9 @@ impl<E, D:Decoder<E>> Decodable<D, E> for i64 {
     }
 }
 
-impl<'a, E, S:Encoder<E>> Encodable<S, E> for &'a str {
+impl<E, S:Encoder<E>> Encodable<S, E> for str {
     fn encode(&self, s: &mut S) -> Result<(), E> {
-        s.emit_str(*self)
+        s.emit_str(self)
     }
 }
 
@@ -375,24 +375,31 @@ impl<E, D:Decoder<E>> Decodable<D, E> for () {
     }
 }
 
-impl<'a, E, S:Encoder<E>,T:Encodable<S, E>> Encodable<S, E> for &'a T {
+impl<'a, E, S: Encoder<E>, Sized? T: Encodable<S, E>> Encodable<S, E> for &'a T {
     fn encode(&self, s: &mut S) -> Result<(), E> {
         (**self).encode(s)
     }
 }
 
-impl<E, S:Encoder<E>,T:Encodable<S, E>> Encodable<S, E> for Box<T> {
+impl<E, S: Encoder<E>, Sized? T: Encodable<S, E>> Encodable<S, E> for Box<T> {
     fn encode(&self, s: &mut S) -> Result<(), E> {
         (**self).encode(s)
     }
 }
 
-impl<E, D:Decoder<E>,T:Decodable<D, E>> Decodable<D, E> for Box<T> {
+impl<E, D:Decoder<E>, T: Decodable<D, E>> Decodable<D, E> for Box<T> {
     fn decode(d: &mut D) -> Result<Box<T>, E> {
         Ok(box try!(Decodable::decode(d)))
     }
 }
 
+impl<E, D:Decoder<E>, T: Decodable<D, E>> Decodable<D, E> for Box<[T]> {
+    fn decode(d: &mut D) -> Result<Box<[T]>, E> {
+        let v: Vec<T> = try!(Decodable::decode(d));
+        Ok(v.into_boxed_slice())
+    }
+}
+
 impl<E, S:Encoder<E>,T:Encodable<S, E>> Encodable<S, E> for Rc<T> {
     #[inline]
     fn encode(&self, s: &mut S) -> Result<(), E> {
@@ -407,7 +414,7 @@ impl<E, D:Decoder<E>,T:Decodable<D, E>> Decodable<D, E> for Rc<T> {
     }
 }
 
-impl<'a, E, S:Encoder<E>,T:Encodable<S, E>> Encodable<S, E> for &'a [T] {
+impl<E, S:Encoder<E>,T:Encodable<S, E>> Encodable<S, E> for [T] {
     fn encode(&self, s: &mut S) -> Result<(), E> {
         s.emit_seq(self.len(), |s| {
             for (i, e) in self.iter().enumerate() {