Skip to content

DSTify more traits #18707

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 7, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/liballoc/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ impl<T: Default> Default for Box<T> {
fn default() -> Box<T> { box Default::default() }
}

impl<T> Default for Box<[T]> {
fn default() -> Box<[T]> { box [] }
}

#[unstable]
impl<T: Clone> Clone for Box<T> {
/// Returns a copy of the owned box.
Expand Down
10 changes: 5 additions & 5 deletions src/libserialize/base64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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>;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions src/libserialize/hex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ 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;
}

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.
*
Expand Down Expand Up @@ -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>;
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions src/libserialize/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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()) }
}

Expand Down
21 changes: 14 additions & 7 deletions src/libserialize/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
}

Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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> {
Expand All @@ -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() {
Expand Down
22 changes: 22 additions & 0 deletions src/test/run-pass/deriving-default-box.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::default::Default;

#[deriving(Default)]
struct A {
foo: Box<[bool]>,
}

pub fn main() {
let a: A = Default::default();
let b: Box<[_]> = box [];
assert_eq!(a.foo, b);
}
26 changes: 26 additions & 0 deletions src/test/run-pass/deriving-encodable-decodable-box.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

extern crate serialize;

use serialize::{Encodable, Decodable};
use serialize::json;

#[deriving(Encodable, Decodable)]
struct A {
foo: Box<[bool]>,
}

fn main() {
let obj = A { foo: box [true, false] };
let s = json::encode(&obj);
let obj2: A = json::decode(s.as_slice()).unwrap();
assert!(obj.foo == obj2.foo);
}