update to rust HEAD

This commit is contained in:
Erick Tryzelaar 2014-07-09 11:33:15 -07:00
parent aeffb413c4
commit 3a518887b5
4 changed files with 75 additions and 46 deletions

View File

@ -258,7 +258,7 @@ fn bench_decoder_003(b: &mut Bencher) {
b.iter(|| {
let mut m: HashMap<String, int> = HashMap::new();
for i in range(0i, 3) {
m.insert(i.to_str(), i);
m.insert(i.to_string(), i);
}
run_decoder(decoder::IntDecoder::new(m.clone()), m)
})
@ -269,7 +269,7 @@ fn bench_decoder_100(b: &mut Bencher) {
b.iter(|| {
let mut m: HashMap<String, int> = HashMap::new();
for i in range(0i, 100) {
m.insert(i.to_str(), i);
m.insert(i.to_string(), i);
}
run_decoder(decoder::IntDecoder::new(m.clone()), m)
})
@ -298,7 +298,7 @@ fn bench_deserializer_003(b: &mut Bencher) {
b.iter(|| {
let mut m: HashMap<String, int> = HashMap::new();
for i in range(0i, 3) {
m.insert(i.to_str(), i);
m.insert(i.to_string(), i);
}
run_deserializer(deserializer::IntDeserializer::new(m.clone()), m)
})
@ -309,7 +309,7 @@ fn bench_deserializer_100(b: &mut Bencher) {
b.iter(|| {
let mut m: HashMap<String, int> = HashMap::new();
for i in range(0i, 100) {
m.insert(i.to_str(), i);
m.insert(i.to_string(), i);
}
run_deserializer(deserializer::IntDeserializer::new(m.clone()), m)
})

28
json.rs
View File

@ -120,7 +120,7 @@ impl ToJson for MyStruct {
fn main() {
let test2: MyStruct = MyStruct {attr1: 1, attr2:"test".to_string()};
let tjson: json::Json = test2.to_json();
let json_str: String = tjson.to_str().into_string();
let json_str: String = tjson.to_string();
}
```
@ -214,7 +214,7 @@ fn main() {
let test2: TestStruct1 = TestStruct1 {data_int: 1, data_str:"toto".to_string(),
data_vector:vec![2,3,4,5]};
let tjson: json::Json = test2.to_json();
let json_str: String = tjson.to_str().into_string();
let json_str: String = tjson.to_string().into_string();
// Deserialize like before.
@ -275,7 +275,7 @@ impl Json {
}
/// Encodes a json value into a string
pub fn to_pretty_str(&self) -> String {
pub fn to_pretty_string(&self) -> String {
let mut wr = MemWriter::new();
self.to_pretty_writer(wr.by_ref()).unwrap();
str::from_utf8(wr.unwrap().as_slice()).unwrap().to_string()
@ -470,8 +470,8 @@ impl de::Deserializable for Json {
de::U64(x) => Ok(Number(x as f64)),
de::F32(x) => Ok(Number(x as f64)),
de::F64(x) => Ok(Number(x)),
de::Char(x) => Ok(String(x.to_str())),
de::Str(x) => Ok(String(x.to_str())),
de::Char(x) => Ok(String(x.to_string())),
de::Str(x) => Ok(String(x.to_string())),
de::String(x) => Ok(String(x)),
de::Option(false) => Ok(Null),
de::Option(true) => de::Deserializable::deserialize(d),
@ -1313,7 +1313,7 @@ pub fn to_vec<T: ser::Serializable>(value: &T) -> Vec<u8> {
/// Encode the specified struct into a json `String` buffer.
#[inline]
pub fn to_str<T: ser::Serializable>(value: &T) -> Result<String, Vec<u8>> {
pub fn to_string<T: ser::Serializable>(value: &T) -> Result<String, Vec<u8>> {
let buf = to_vec(value);
String::from_utf8(buf)
}
@ -1329,7 +1329,7 @@ pub fn to_pretty_vec<T: ser::Serializable>(value: &T) -> Vec<u8> {
}
/// Encode the specified struct into a json `String` buffer.
pub fn to_pretty_str<T: ser::Serializable>(value: &T) -> Result<String, Vec<u8>> {
pub fn to_pretty_string<T: ser::Serializable>(value: &T) -> Result<String, Vec<u8>> {
let buf = to_pretty_vec(value);
String::from_utf8(buf)
}
@ -2491,10 +2491,10 @@ mod tests {
for &(ref value, out) in errors.iter() {
let out = out.to_string();
let s = super::to_str(value).unwrap();
let s = super::to_string(value).unwrap();
assert_eq!(s, out);
let s = super::to_str(&value.to_json()).unwrap();
let s = super::to_string(&value.to_json()).unwrap();
assert_eq!(s, out);
}
}
@ -2505,10 +2505,10 @@ mod tests {
for &(ref value, out) in errors.iter() {
let out = out.to_string();
let s = super::to_pretty_str(value).unwrap();
let s = super::to_pretty_string(value).unwrap();
assert_eq!(s, out);
let s = super::to_pretty_str(&value.to_json()).unwrap();
let s = super::to_pretty_string(&value.to_json()).unwrap();
assert_eq!(s, out);
}
}
@ -3783,7 +3783,7 @@ mod bench {
let json = encoder_json(count);
b.iter(|| {
assert_eq!(json.to_str(), src);
assert_eq!(json.to_string(), src);
});
}
@ -3801,7 +3801,7 @@ mod bench {
let json = serializer_json(count);
b.iter(|| {
assert_eq!(json.to_str(), src);
assert_eq!(json.to_string(), src);
});
}
@ -3810,7 +3810,7 @@ mod bench {
let json = serializer_json(count);
b.iter(|| {
assert_eq!(json.to_pretty_str(), src);
assert_eq!(json.to_pretty_string(), src);
});
}

65
ser.rs
View File

@ -113,35 +113,54 @@ pub trait Serializable {
//////////////////////////////////////////////////////////////////////////////
macro_rules! impl_serializable {
($ty:ty, $method:ident, $variant:expr) => {
impl<'a> Serializable for $ty {
($ty:ty, $method:ident) => {
impl Serializable for $ty {
#[inline]
fn serialize<
S: Serializer<E>,
E
>(&self, s: &mut S) -> Result<(), E> {
s.$method($variant)
fn serialize<S: Serializer<E>, E>(&self, s: &mut S) -> Result<(), E> {
s.$method(*self)
}
}
}
}
impl_serializable!(bool, serialize_bool, *self)
impl_serializable!(int, serialize_int, *self)
impl_serializable!(i8, serialize_i8, *self)
impl_serializable!(i16, serialize_i16, *self)
impl_serializable!(i32, serialize_i32, *self)
impl_serializable!(i64, serialize_i64, *self)
impl_serializable!(uint, serialize_uint, *self)
impl_serializable!(u8, serialize_u8, *self)
impl_serializable!(u16, serialize_u16, *self)
impl_serializable!(u32, serialize_u32, *self)
impl_serializable!(u64, serialize_u64, *self)
impl_serializable!(f32, serialize_f32, *self)
impl_serializable!(f64, serialize_f64, *self)
impl_serializable!(char, serialize_char, *self)
impl_serializable!(&'a str, serialize_str, *self)
impl_serializable!(String, serialize_str, self.as_slice())
impl_serializable!(bool, serialize_bool)
impl_serializable!(int, serialize_int)
impl_serializable!(i8, serialize_i8)
impl_serializable!(i16, serialize_i16)
impl_serializable!(i32, serialize_i32)
impl_serializable!(i64, serialize_i64)
impl_serializable!(uint, serialize_uint)
impl_serializable!(u8, serialize_u8)
impl_serializable!(u16, serialize_u16)
impl_serializable!(u32, serialize_u32)
impl_serializable!(u64, serialize_u64)
impl_serializable!(f32, serialize_f32)
impl_serializable!(f64, serialize_f64)
impl_serializable!(char, serialize_char)
//////////////////////////////////////////////////////////////////////////////
impl<'a> Serializable for &'a str {
#[inline]
fn serialize<
S: Serializer<E>,
E
>(&self, s: &mut S) -> Result<(), E> {
s.serialize_str(*self)
}
}
impl Serializable for String {
#[inline]
fn serialize<
S: Serializer<E>,
E
>(&self, s: &mut S) -> Result<(), E> {
self.as_slice().serialize(s)
}
}
//////////////////////////////////////////////////////////////////////////////
impl<
'a,

View File

@ -82,7 +82,7 @@ fn expand_deriving_serializable(cx: &mut ExtCtxt,
bounds: vec!(
(
"__S",
ast::StaticSize,
None,
vec!(
Path::new_(
vec!("serde", "ser", "Serializer"),
@ -90,9 +90,14 @@ fn expand_deriving_serializable(cx: &mut ExtCtxt,
vec!(box Literal(Path::new_local("__E"))),
true
)
)
),
),
("__E", ast::StaticSize, vec!()))
(
"__E",
None,
vec!(),
),
)
},
explicit_self: borrowed_explicit_self(),
args: vec!(Ptr(box Literal(Path::new_local("__S")),
@ -247,7 +252,7 @@ pub fn expand_deriving_deserializable(cx: &mut ExtCtxt,
bounds: vec!(
(
"__D",
ast::StaticSize,
None,
vec!(
Path::new_(
vec!("serde", "de", "Deserializer"),
@ -259,7 +264,12 @@ pub fn expand_deriving_deserializable(cx: &mut ExtCtxt,
)
)
),
("__E", ast::StaticSize, vec!()))
(
"__E",
None,
vec!(),
),
)
},
explicit_self: None,
args: vec!(