From eec145be3f5e5f763e61749a6737f90df8504e05 Mon Sep 17 00:00:00 2001 From: Alexis Beingessner Date: Thu, 6 Nov 2014 12:25:16 -0500 Subject: Fallout from collection conventions --- src/libserialize/collection_impls.rs | 8 ++++---- src/libserialize/json.rs | 10 +++++----- 2 files changed, 9 insertions(+), 9 deletions(-) (limited to 'src/libserialize') diff --git a/src/libserialize/collection_impls.rs b/src/libserialize/collection_impls.rs index 79166935a5e..d2d1f5fa8b0 100644 --- a/src/libserialize/collection_impls.rs +++ b/src/libserialize/collection_impls.rs @@ -39,7 +39,7 @@ impl,T:Decodable> Decodable for DList { d.read_seq(|d, len| { let mut list = DList::new(); for i in range(0u, len) { - list.push(try!(d.read_seq_elt(i, |d| Decodable::decode(d)))); + list.push_back(try!(d.read_seq_elt(i, |d| Decodable::decode(d)))); } Ok(list) }) @@ -66,7 +66,7 @@ impl,T:Decodable> Decodable for RingBuf { d.read_seq(|d, len| { let mut deque: RingBuf = RingBuf::new(); for i in range(0u, len) { - deque.push(try!(d.read_seq_elt(i, |d| Decodable::decode(d)))); + deque.push_back(try!(d.read_seq_elt(i, |d| Decodable::decode(d)))); } Ok(deque) }) @@ -165,10 +165,10 @@ impl< > Decodable for EnumSet { fn decode(d: &mut D) -> Result, E> { let bits = try!(d.read_uint()); - let mut set = EnumSet::empty(); + let mut set = EnumSet::new(); for bit in range(0, uint::BITS) { if bits & (1 << bit) != 0 { - set.add(CLike::from_uint(1 << bit)); + set.insert(CLike::from_uint(1 << bit)); } } Ok(set) diff --git a/src/libserialize/json.rs b/src/libserialize/json.rs index dbdfa17bfc2..9f40cd2d277 100644 --- a/src/libserialize/json.rs +++ b/src/libserialize/json.rs @@ -2113,7 +2113,7 @@ impl ::Decoder for Decoder { let name = match self.pop() { String(s) => s, Object(mut o) => { - let n = match o.pop(&"variant".to_string()) { + let n = match o.remove(&"variant".to_string()) { Some(String(s)) => s, Some(val) => { return Err(ExpectedError("String".to_string(), format!("{}", val))) @@ -2122,7 +2122,7 @@ impl ::Decoder for Decoder { return Err(MissingFieldError("variant".to_string())) } }; - match o.pop(&"fields".to_string()) { + match o.remove(&"fields".to_string()) { Some(List(l)) => { for field in l.into_iter().rev() { self.stack.push(field); @@ -2192,7 +2192,7 @@ impl ::Decoder for Decoder { debug!("read_struct_field(name={}, idx={})", name, idx); let mut obj = try!(expect!(self.pop(), Object)); - let value = match obj.pop(&name.to_string()) { + let value = match obj.remove(&name.to_string()) { None => { // Add a Null and try to parse it as an Option<_> // to get None as a default value. @@ -3072,8 +3072,8 @@ mod tests { \"fields\":[\"Henry\", 349]}}"; let mut map: TreeMap = super::decode(s).unwrap(); - assert_eq!(map.pop(&"a".to_string()), Some(Dog)); - assert_eq!(map.pop(&"b".to_string()), Some(Frog("Henry".to_string(), 349))); + assert_eq!(map.remove(&"a".to_string()), Some(Dog)); + assert_eq!(map.remove(&"b".to_string()), Some(Frog("Henry".to_string(), 349))); } #[test] -- cgit 1.4.1-3-g733a5 From 679eb9191dd06a2e0dacf3602da56765211fd76a Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Thu, 6 Nov 2014 11:25:09 -0500 Subject: DTSify libserialize traits - ToBase64 - FromBase64 - ToHex - FromHex - ToJson - Encodable --- src/libserialize/base64.rs | 10 +++++----- src/libserialize/hex.rs | 8 ++++---- src/libserialize/json.rs | 4 ++-- src/libserialize/serialize.rs | 21 ++++++++++++++------- 4 files changed, 25 insertions(+), 18 deletions(-) (limited to 'src/libserialize') 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, 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, 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, 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 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 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 { fn error(&mut self, err: &str) -> E; } -pub trait Encodable, E> { +pub trait Encodable, E> for Sized? { fn encode(&self, s: &mut S) -> Result<(), E>; } @@ -297,9 +297,9 @@ impl> Decodable for i64 { } } -impl<'a, E, S:Encoder> Encodable for &'a str { +impl> Encodable for str { fn encode(&self, s: &mut S) -> Result<(), E> { - s.emit_str(*self) + s.emit_str(self) } } @@ -375,24 +375,31 @@ impl> Decodable for () { } } -impl<'a, E, S:Encoder,T:Encodable> Encodable for &'a T { +impl<'a, E, S: Encoder, Sized? T: Encodable> Encodable for &'a T { fn encode(&self, s: &mut S) -> Result<(), E> { (**self).encode(s) } } -impl,T:Encodable> Encodable for Box { +impl, Sized? T: Encodable> Encodable for Box { fn encode(&self, s: &mut S) -> Result<(), E> { (**self).encode(s) } } -impl,T:Decodable> Decodable for Box { +impl, T: Decodable> Decodable for Box { fn decode(d: &mut D) -> Result, E> { Ok(box try!(Decodable::decode(d))) } } +impl, T: Decodable> Decodable for Box<[T]> { + fn decode(d: &mut D) -> Result, E> { + let v: Vec = try!(Decodable::decode(d)); + Ok(v.into_boxed_slice()) + } +} + impl,T:Encodable> Encodable for Rc { #[inline] fn encode(&self, s: &mut S) -> Result<(), E> { @@ -407,7 +414,7 @@ impl,T:Decodable> Decodable for Rc { } } -impl<'a, E, S:Encoder,T:Encodable> Encodable for &'a [T] { +impl,T:Encodable> Encodable for [T] { fn encode(&self, s: &mut S) -> Result<(), E> { s.emit_seq(self.len(), |s| { for (i, e) in self.iter().enumerate() { -- cgit 1.4.1-3-g733a5