Merge pull request #15 from tomprogrammer/rust-head

Follow rustc changes: Various fixes due to std api stabilization
This commit is contained in:
Erick Tryzelaar 2014-12-23 13:02:10 -08:00
commit 1366bf47e7
20 changed files with 203 additions and 199 deletions

View File

@ -589,7 +589,7 @@ macro_rules! likely(
unsafe { expect(x as u8, 1) != 0 }
}
}
)
);
macro_rules! unlikely(
($val:expr) => {
@ -602,7 +602,7 @@ macro_rules! unlikely(
unsafe { expect(x as u8, 0) != 0 }
}
}
)
);
struct MyMemWriter0 {
buf: Vec<u8>,

View File

@ -28,7 +28,7 @@ pub enum Error {
mod decoder {
use std::collections::HashMap;
use std::collections::hash_map::MoveEntries;
use std::collections::hash_map::IntoIter;
use serialize;
use super::Error;
@ -42,7 +42,7 @@ mod decoder {
pub struct IntDecoder {
len: uint,
iter: MoveEntries<String, int>,
iter: IntoIter<String, int>,
stack: Vec<Value>,
}
@ -216,7 +216,7 @@ mod decoder {
mod deserializer {
use std::collections::HashMap;
use std::collections::hash_map::MoveEntries;
use std::collections::hash_map::IntoIter;
use super::Error;
use super::Error::{EndOfStream, SyntaxError};
@ -235,7 +235,7 @@ mod deserializer {
pub struct IntDeserializer {
stack: Vec<State>,
len: uint,
iter: MoveEntries<String, int>,
iter: IntoIter<String, int>,
}
impl IntDeserializer {

View File

@ -34,7 +34,7 @@ mod decoder {
pub struct IntDecoder {
len: uint,
iter: vec::MoveItems<int>,
iter: vec::IntoIter<int>,
}
impl IntDecoder {
@ -186,7 +186,7 @@ mod decoder {
pub struct U8Decoder {
len: uint,
iter: vec::MoveItems<u8>,
iter: vec::IntoIter<u8>,
}
impl U8Decoder {
@ -359,7 +359,7 @@ mod deserializer {
pub struct IntDeserializer {
state: State,
len: uint,
iter: vec::MoveItems<int>,
iter: vec::IntoIter<int>,
}
impl IntDeserializer {
@ -431,7 +431,7 @@ mod deserializer {
pub struct U8Deserializer {
state: State,
len: uint,
iter: vec::MoveItems<u8>,
iter: vec::IntoIter<u8>,
}
impl U8Deserializer {

View File

@ -932,7 +932,7 @@ macro_rules! likely(
unsafe { expect(x as u8, 1) != 0 }
}
}
)
);
macro_rules! unlikely(
($val:expr) => {
@ -945,7 +945,7 @@ macro_rules! unlikely(
unsafe { expect(x as u8, 0) != 0 }
}
}
)
);
struct MyMemWriter0 {
buf: Vec<u8>,

View File

@ -253,7 +253,7 @@ impl<
///////////////////////////////////////////////////////////////////////////////
mod json {
use std::collections::TreeMap;
use std::collections::BTreeMap;
use serde2::de;
#[deriving(Show)]
@ -263,7 +263,7 @@ mod json {
Int(int),
//String(String),
List(Vec<Value>),
Map(TreeMap<String, Value>),
Map(BTreeMap<String, Value>),
}
impl<
@ -323,7 +323,7 @@ mod json {
fn visit_map<
Visitor: de::MapVisitor<D, E>,
>(&mut self, mut visitor: Visitor) -> Result<Value, E> {
let mut values = TreeMap::new();
let mut values = BTreeMap::new();
loop {
match try!(visitor.visit()) {
@ -500,7 +500,7 @@ pub fn main() {
/*
use std::collections::TreeMap;
use std::collections::BTreeMap;
use serde::{Serialize, GatherTokens};
use serde::json;
@ -578,7 +578,7 @@ fn main() {
////
let mut value = TreeMap::new();
let mut value = BTreeMap::new();
value.insert("a", 1i);
value.insert("b", 2);
value.insert("c", 3);

View File

@ -1,4 +1,4 @@
use std::collections::{HashMap, TreeMap};
use std::collections::{HashMap, BTreeMap};
use std::hash::Hash;
///////////////////////////////////////////////////////////////////////////////
@ -237,18 +237,18 @@ pub fn deserialize_from_primitive<
E: Error,
T: Deserialize<S, E> + FromPrimitive
> self::Visitor<S, T, E> for Visitor {
impl_deserialize_num_method!(int, visit_int, from_int)
impl_deserialize_num_method!(i8, visit_i8, from_i8)
impl_deserialize_num_method!(i16, visit_i16, from_i16)
impl_deserialize_num_method!(i32, visit_i32, from_i32)
impl_deserialize_num_method!(i64, visit_i64, from_i64)
impl_deserialize_num_method!(uint, visit_uint, from_uint)
impl_deserialize_num_method!(u8, visit_u8, from_u8)
impl_deserialize_num_method!(u16, visit_u16, from_u16)
impl_deserialize_num_method!(u32, visit_u32, from_u32)
impl_deserialize_num_method!(u64, visit_u64, from_u64)
impl_deserialize_num_method!(f32, visit_f32, from_f32)
impl_deserialize_num_method!(f64, visit_f64, from_f64)
impl_deserialize_num_method!(int, visit_int, from_int);
impl_deserialize_num_method!(i8, visit_i8, from_i8);
impl_deserialize_num_method!(i16, visit_i16, from_i16);
impl_deserialize_num_method!(i32, visit_i32, from_i32);
impl_deserialize_num_method!(i64, visit_i64, from_i64);
impl_deserialize_num_method!(uint, visit_uint, from_uint);
impl_deserialize_num_method!(u8, visit_u8, from_u8);
impl_deserialize_num_method!(u16, visit_u16, from_u16);
impl_deserialize_num_method!(u32, visit_u32, from_u32);
impl_deserialize_num_method!(u64, visit_u64, from_u64);
impl_deserialize_num_method!(f32, visit_f32, from_f32);
impl_deserialize_num_method!(f64, visit_f64, from_f64);
}
state.visit(&mut Visitor)
@ -268,18 +268,18 @@ macro_rules! impl_deserialize_num {
}
}
impl_deserialize_num!(int)
impl_deserialize_num!(i8)
impl_deserialize_num!(i16)
impl_deserialize_num!(i32)
impl_deserialize_num!(i64)
impl_deserialize_num!(uint)
impl_deserialize_num!(u8)
impl_deserialize_num!(u16)
impl_deserialize_num!(u32)
impl_deserialize_num!(u64)
impl_deserialize_num!(f32)
impl_deserialize_num!(f64)
impl_deserialize_num!(int);
impl_deserialize_num!(i8);
impl_deserialize_num!(i16);
impl_deserialize_num!(i32);
impl_deserialize_num!(i64);
impl_deserialize_num!(uint);
impl_deserialize_num!(u8);
impl_deserialize_num!(u16);
impl_deserialize_num!(u32);
impl_deserialize_num!(u64);
impl_deserialize_num!(f32);
impl_deserialize_num!(f64);
///////////////////////////////////////////////////////////////////////////////
@ -377,14 +377,14 @@ impl<
macro_rules! peel {
($name:ident, $($other:ident,)*) => {
impl_deserialize_tuple!($($other,)*)
impl_deserialize_tuple!($($other,)*);
}
}
macro_rules! impl_deserialize_tuple {
() => {};
( $($name:ident,)+ ) => {
peel!($($name,)*)
peel!($($name,)*);
impl<
S: Deserializer<E>,
@ -472,8 +472,8 @@ impl<
V: Deserialize<S, E>,
S: Deserializer<E>,
E: Error,
> Deserialize<S, E> for TreeMap<K, V> {
fn deserialize(state: &mut S) -> Result<TreeMap<K, V>, E> {
> Deserialize<S, E> for BTreeMap<K, V> {
fn deserialize(state: &mut S) -> Result<BTreeMap<K, V>, E> {
struct Visitor;
impl<
@ -481,11 +481,11 @@ impl<
V: Deserialize<S, E>,
S: Deserializer<E>,
E: Error,
> self::Visitor<S, TreeMap<K, V>, E> for Visitor {
> self::Visitor<S, BTreeMap<K, V>, E> for Visitor {
fn visit_map<
Visitor: MapVisitor<S, E>,
>(&mut self, mut visitor: Visitor) -> Result<TreeMap<K, V>, E> {
let mut values = TreeMap::new();
>(&mut self, mut visitor: Visitor) -> Result<BTreeMap<K, V>, E> {
let mut values = BTreeMap::new();
loop {
match try!(visitor.visit()) {

View File

@ -8,8 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use std::collections::TreeMap;
use std::str::StrAllocating;
use std::collections::BTreeMap;
use ser::{mod, Serialize};
use json::value::{mod, Value};
@ -46,39 +45,39 @@ impl ArrayBuilder {
}
pub struct ObjectBuilder {
object: TreeMap<String, Value>,
object: BTreeMap<String, Value>,
}
impl ObjectBuilder {
pub fn new() -> ObjectBuilder {
ObjectBuilder { object: TreeMap::new() }
ObjectBuilder { object: BTreeMap::new() }
}
pub fn unwrap(self) -> Value {
Value::Object(self.object)
}
pub fn insert<K: StrAllocating, V: ser::Serialize>(mut self, k: K, v: V) -> ObjectBuilder {
self.object.insert(k.into_string(), value::to_value(&v));
pub fn insert<V: ser::Serialize>(mut self, k: String, v: V) -> ObjectBuilder {
self.object.insert(k, value::to_value(&v));
self
}
pub fn insert_array<S: StrAllocating>(mut self, key: S, f: |ArrayBuilder| -> ArrayBuilder) -> ObjectBuilder {
pub fn insert_array(mut self, key: String, f: |ArrayBuilder| -> ArrayBuilder) -> ObjectBuilder {
let builder = ArrayBuilder::new();
self.object.insert(key.into_string(), f(builder).unwrap());
self.object.insert(key, f(builder).unwrap());
self
}
pub fn insert_object<S: StrAllocating>(mut self, key: S, f: |ObjectBuilder| -> ObjectBuilder) -> ObjectBuilder {
pub fn insert_object(mut self, key: String, f: |ObjectBuilder| -> ObjectBuilder) -> ObjectBuilder {
let builder = ObjectBuilder::new();
self.object.insert(key.into_string(), f(builder).unwrap());
self.object.insert(key, f(builder).unwrap());
self
}
}
#[cfg(test)]
mod tests {
use std::collections::TreeMap;
use std::collections::BTreeMap;
use json::value::Value;
use super::{ArrayBuilder, ObjectBuilder};
@ -107,7 +106,7 @@ mod tests {
.insert("b".to_string(), 2i))
.unwrap();
let mut map = TreeMap::new();
let mut map = BTreeMap::new();
map.insert("a".to_string(), Value::I64(1));
map.insert("b".to_string(), Value::I64(2));
assert_eq!(value, Value::Array(vec!(Value::Object(map))));
@ -116,14 +115,14 @@ mod tests {
#[test]
fn test_object_builder() {
let value = ObjectBuilder::new().unwrap();
assert_eq!(value, Value::Object(TreeMap::new()));
assert_eq!(value, Value::Object(BTreeMap::new()));
let value = ObjectBuilder::new()
.insert("a".to_string(), 1i)
.insert("b".to_string(), 2i)
.unwrap();
let mut map = TreeMap::new();
let mut map = BTreeMap::new();
map.insert("a".to_string(), Value::I64(1));
map.insert("b".to_string(), Value::I64(2));
assert_eq!(value, Value::Object(map));

View File

@ -1,6 +1,6 @@
use std::char;
use std::num::Float;
use std::str::ScalarValue;
use unicode::str::Utf16Item;
use std::str;
use de;
@ -337,8 +337,8 @@ impl<Iter: Iterator<u8>> Parser<Iter> {
}
let buf = &[n1, try!(self.decode_hex_escape())];
match str::utf16_items(buf.as_slice()).next() {
Some(ScalarValue(c)) => c,
match ::unicode::str::utf16_items(buf.as_slice()).next() {
Some(Utf16Item::ScalarValue(c)) => c,
_ => {
return Err(self.error(ErrorCode::LoneLeadingSurrogateInHexEscape));
}
@ -529,7 +529,7 @@ pub fn from_str<
mod tests {
use std::str;
use std::fmt::Show;
use std::collections::TreeMap;
use std::collections::BTreeMap;
use de::Deserialize;
use super::{Parser, from_str};
@ -537,7 +537,7 @@ mod tests {
macro_rules! treemap {
($($k:expr => $v:expr),*) => ({
let mut _m = TreeMap::new();
let mut _m = BTreeMap::new();
$(_m.insert($k, $v);)*
_m
})
@ -694,7 +694,7 @@ mod tests {
#[test]
fn test_parse_object() {
test_parse_err::<TreeMap<String, int>>(vec![
test_parse_err::<BTreeMap<String, int>>(vec![
("{", Error::SyntaxError(ErrorCode::EOFWhileParsingValue, 1, 2)),
("{ ", Error::SyntaxError(ErrorCode::EOFWhileParsingValue, 1, 3)),
("{1", Error::SyntaxError(ErrorCode::KeyMustBeAString, 1, 2)),

View File

@ -1,6 +1,7 @@
use std::f64;
use std::io::{mod, ByRefWriter, IoError};
use std::num::{Float, FPNaN, FPInfinite};
use std::str::Utf8Error;
use ser;
use ser::Serializer;
@ -251,7 +252,7 @@ pub fn to_vec<
#[inline]
pub fn to_string<
T: ser::Serialize,
>(value: &T) -> Result<Result<String, Vec<u8>>, IoError> {
>(value: &T) -> Result<Result<String, (Vec<u8>, Utf8Error)>, IoError> {
let vec = try!(to_vec(value));
Ok(String::from_utf8(vec))
}

View File

@ -1,4 +1,4 @@
use std::collections::TreeMap;
use std::collections::BTreeMap;
use std::fmt;
use std::io;
@ -12,7 +12,7 @@ pub enum Value {
F64(f64),
String(String),
Array(Vec<Value>),
Object(TreeMap<String, Value>),
Object(BTreeMap<String, Value>),
}
impl ser::Serialize for Value {
@ -77,7 +77,7 @@ pub fn to_value<
enum State {
Value(Value),
Array(Vec<Value>),
Object(TreeMap<String, Value>),
Object(BTreeMap<String, Value>),
}
pub struct Writer {
@ -203,7 +203,7 @@ impl ser::Visitor<Writer, (), ()> for Visitor {
fn visit_map<
V: ser::MapVisitor<Writer, (), ()>
>(&self, state: &mut Writer, mut visitor: V) -> Result<(), ()> {
let values = TreeMap::new();
let values = BTreeMap::new();
state.state.push(State::Object(values));

View File

@ -1,5 +1,7 @@
#![feature(macro_rules)]
extern crate unicode;
pub use ser::{Serialize, Serializer};
//pub use ser::{Visitor, VisitorState};
//pub use ser::GatherTokens;

View File

@ -1,4 +1,4 @@
use std::collections::TreeMap;
use std::collections::BTreeMap;
///////////////////////////////////////////////////////////////////////////////
@ -176,20 +176,20 @@ macro_rules! impl_visit {
}
}
impl_visit!(bool, visit_bool)
impl_visit!(int, visit_int)
impl_visit!(i8, visit_i8)
impl_visit!(i16, visit_i16)
impl_visit!(i32, visit_i32)
impl_visit!(i64, visit_i64)
impl_visit!(uint, visit_uint)
impl_visit!(u8, visit_u8)
impl_visit!(u16, visit_u16)
impl_visit!(u32, visit_u32)
impl_visit!(u64, visit_u64)
impl_visit!(f32, visit_f32)
impl_visit!(f64, visit_f64)
impl_visit!(char, visit_char)
impl_visit!(bool, visit_bool);
impl_visit!(int, visit_int);
impl_visit!(i8, visit_i8);
impl_visit!(i16, visit_i16);
impl_visit!(i32, visit_i32);
impl_visit!(i64, visit_i64);
impl_visit!(uint, visit_uint);
impl_visit!(u8, visit_u8);
impl_visit!(u16, visit_u16);
impl_visit!(u32, visit_u32);
impl_visit!(u64, visit_u64);
impl_visit!(f32, visit_f32);
impl_visit!(f64, visit_f64);
impl_visit!(char, visit_char);
///////////////////////////////////////////////////////////////////////////////
@ -491,7 +491,7 @@ impl<
impl<
K: Serialize + Ord,
V: Serialize,
> Serialize for TreeMap<K, V> {
> Serialize for BTreeMap<K, V> {
#[inline]
fn visit<
S,

View File

@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use std::collections::{HashMap, HashSet, TreeMap, TreeSet};
use std::collections::{HashMap, HashSet, BTreeMap, BTreeSet};
use std::hash::Hash;
use std::num;
use std::rc::Rc;
@ -288,10 +288,10 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
fn expect_char(&mut self, token: Token) -> Result<char, E> {
match token {
Token::Char(value) => Ok(value),
Token::Str(value) if value.char_len() == 1 => {
Token::Str(value) if value.chars().count() == 1 => {
Ok(value.char_at(0))
}
Token::String(ref value) if value.as_slice().char_len() == 1 => {
Token::String(ref value) if value.as_slice().chars().count() == 1 => {
Ok(value.as_slice().char_at(0))
}
token => {
@ -673,22 +673,22 @@ macro_rules! impl_deserialize {
}
}
impl_deserialize!(bool, expect_bool)
impl_deserialize!(int, expect_num)
impl_deserialize!(i8, expect_num)
impl_deserialize!(i16, expect_num)
impl_deserialize!(i32, expect_num)
impl_deserialize!(i64, expect_num)
impl_deserialize!(uint, expect_num)
impl_deserialize!(u8, expect_num)
impl_deserialize!(u16, expect_num)
impl_deserialize!(u32, expect_num)
impl_deserialize!(u64, expect_num)
impl_deserialize!(f32, expect_num)
impl_deserialize!(f64, expect_num)
impl_deserialize!(char, expect_char)
impl_deserialize!(&'static str, expect_str)
impl_deserialize!(string::String, expect_string)
impl_deserialize!(bool, expect_bool);
impl_deserialize!(int, expect_num);
impl_deserialize!(i8, expect_num);
impl_deserialize!(i16, expect_num);
impl_deserialize!(i32, expect_num);
impl_deserialize!(i64, expect_num);
impl_deserialize!(uint, expect_num);
impl_deserialize!(u8, expect_num);
impl_deserialize!(u16, expect_num);
impl_deserialize!(u32, expect_num);
impl_deserialize!(u64, expect_num);
impl_deserialize!(f32, expect_num);
impl_deserialize!(f64, expect_num);
impl_deserialize!(char, expect_char);
impl_deserialize!(&'static str, expect_str);
impl_deserialize!(string::String, expect_string);
//////////////////////////////////////////////////////////////////////////////
@ -770,9 +770,9 @@ impl<
E,
K: Deserialize<D, E> + Ord,
V: Deserialize<D, E>
> Deserialize<D, E> for TreeMap<K, V> {
> Deserialize<D, E> for BTreeMap<K, V> {
#[inline]
fn deserialize_token(d: &mut D, token: Token) -> Result<TreeMap<K, V>, E> {
fn deserialize_token(d: &mut D, token: Token) -> Result<BTreeMap<K, V>, E> {
d.expect_map(token)
}
}
@ -794,9 +794,9 @@ impl<
D: Deserializer<E>,
E,
T: Deserialize<D, E> + Ord
> Deserialize<D, E> for TreeSet<T> {
> Deserialize<D, E> for BTreeSet<T> {
#[inline]
fn deserialize_token(d: &mut D, token: Token) -> Result<TreeSet<T>, E> {
fn deserialize_token(d: &mut D, token: Token) -> Result<BTreeSet<T>, E> {
d.expect_seq(token)
}
}
@ -804,7 +804,7 @@ impl<
//////////////////////////////////////////////////////////////////////////////
macro_rules! peel {
($name:ident, $($other:ident,)*) => (impl_deserialize_tuple!($($other,)*))
($name:ident, $($other:ident,)*) => ( impl_deserialize_tuple!($($other,)*); )
}
macro_rules! impl_deserialize_tuple {
@ -840,7 +840,7 @@ macro_rules! impl_deserialize_tuple {
Ok(result)
}
}
peel!($($name,)*)
peel!($($name,)*);
}
}
@ -1067,7 +1067,7 @@ impl<D: Deserializer<E>, E> Deserialize<D, E> for GatherTokens {
#[cfg(test)]
mod tests {
use std::collections::TreeMap;
use std::collections::BTreeMap;
use std::{option, string};
use serialize::Decoder;
@ -1075,7 +1075,7 @@ mod tests {
macro_rules! treemap {
($($k:expr => $v:expr),*) => ({
let mut _m = ::std::collections::TreeMap::new();
let mut _m = ::std::collections::BTreeMap::new();
$(_m.insert($k, $v);)*
_m
})
@ -1087,7 +1087,7 @@ mod tests {
struct Inner {
a: (),
b: uint,
c: TreeMap<string::String, option::Option<char>>,
c: BTreeMap<string::String, option::Option<char>>,
}
impl<
@ -1276,7 +1276,7 @@ mod tests {
vec!(Token::Char('c')) => 'c': char,
vec!(Token::Str("abc")) => "abc": &str,
vec!(Token::String("abc".to_string())) => "abc".to_string(): string::String
])
]);
test_value!(test_tuples, [
vec!(
@ -1306,7 +1306,7 @@ mod tests {
Token::End,
Token::End,
) => ((), (), (5, "a")): ((), (), (int, &'static str))
])
]);
test_value!(test_options, [
vec!(Token::Option(false)) => None: option::Option<int>,
@ -1315,7 +1315,7 @@ mod tests {
Token::Option(true),
Token::Int(5),
) => Some(5): option::Option<int>
])
]);
test_value!(test_structs, [
vec!(
@ -1356,7 +1356,7 @@ mod tests {
},
),
}: Outer
])
]);
test_value!(test_enums, [
vec!(
@ -1370,7 +1370,7 @@ mod tests {
Token::Int(349),
Token::End,
) => Animal::Frog("Henry".to_string(), 349): Animal
])
]);
test_value!(test_vecs, [
vec!(
@ -1410,13 +1410,13 @@ mod tests {
Token::End,
Token::End,
) => vec!(vec!(1), vec!(2, 3), vec!(4, 5, 6)): Vec<Vec<int>>
])
]);
test_value!(test_treemaps, [
vec!(
Token::MapStart(0),
Token::End,
) => treemap!(): TreeMap<int, string::String>,
) => treemap!(): BTreeMap<int, string::String>,
vec!(
Token::MapStart(2),
@ -1426,7 +1426,7 @@ mod tests {
Token::Int(6),
Token::String("b".to_string()),
Token::End,
) => treemap!(5i => "a".to_string(), 6i => "b".to_string()): TreeMap<int, string::
) => treemap!(5i => "a".to_string(), 6i => "b".to_string()): BTreeMap<int, string::
String>
])
]);
}

View File

@ -8,8 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use std::collections::TreeMap;
use std::str::StrAllocating;
use std::collections::BTreeMap;
use json::value::{ToJson, Value};
@ -44,38 +43,38 @@ impl ArrayBuilder {
}
pub struct ObjectBuilder {
object: TreeMap<String, Value>,
object: BTreeMap<String, Value>,
}
impl ObjectBuilder {
pub fn new() -> ObjectBuilder {
ObjectBuilder { object: TreeMap::new() }
ObjectBuilder { object: BTreeMap::new() }
}
pub fn unwrap(self) -> Value {
Value::Object(self.object)
}
pub fn insert<K: StrAllocating, V: ToJson>(self, key: K, value: V) -> ObjectBuilder {
pub fn insert<V: ToJson>(self, key: String, value: V) -> ObjectBuilder {
let mut builder = self;
builder.object.insert(key.into_string(), value.to_json());
builder.object.insert(key, value.to_json());
builder
}
pub fn insert_array<S: StrAllocating>(self, key: S, f: |ArrayBuilder| -> ArrayBuilder) -> ObjectBuilder {
pub fn insert_array(self, key: String, f: |ArrayBuilder| -> ArrayBuilder) -> ObjectBuilder {
let builder = ArrayBuilder::new();
self.insert(key.into_string(), f(builder).unwrap())
self.insert(key, f(builder).unwrap())
}
pub fn insert_object<S: StrAllocating>(self, key: S, f: |ObjectBuilder| -> ObjectBuilder) -> ObjectBuilder {
pub fn insert_object(self, key: String, f: |ObjectBuilder| -> ObjectBuilder) -> ObjectBuilder {
let builder = ObjectBuilder::new();
self.insert(key.into_string(), f(builder).unwrap())
self.insert(key, f(builder).unwrap())
}
}
#[cfg(test)]
mod tests {
use std::collections::TreeMap;
use std::collections::BTreeMap;
use json::value::Value;
use super::{ArrayBuilder, ObjectBuilder};
@ -104,7 +103,7 @@ mod tests {
.insert("b".to_string(), 2i))
.unwrap();
let mut map = TreeMap::new();
let mut map = BTreeMap::new();
map.insert("a".to_string(), Value::Integer(1));
map.insert("b".to_string(), Value::Integer(2));
assert_eq!(value, Value::Array(vec!(Value::Object(map))));
@ -113,14 +112,14 @@ mod tests {
#[test]
fn test_object_builder() {
let value = ObjectBuilder::new().unwrap();
assert_eq!(value, Value::Object(TreeMap::new()));
assert_eq!(value, Value::Object(BTreeMap::new()));
let value = ObjectBuilder::new()
.insert("a".to_string(), 1i)
.insert("b".to_string(), 2i)
.unwrap();
let mut map = TreeMap::new();
let mut map = BTreeMap::new();
map.insert("a".to_string(), Value::Integer(1));
map.insert("b".to_string(), Value::Integer(2));
assert_eq!(value, Value::Object(map));

View File

@ -1,6 +1,6 @@
use std::str;
use std::num::Float;
use std::str::ScalarValue;
use unicode::str::Utf16Item;
use std::char;
use de;
@ -379,8 +379,8 @@ impl<Iter: Iterator<u8>> Parser<Iter> {
}
let buf = &[n1, try!(self.decode_hex_escape())];
match str::utf16_items(buf.as_slice()).next() {
Some(ScalarValue(c)) => c,
match ::unicode::str::utf16_items(buf.as_slice()).next() {
Some(Utf16Item::ScalarValue(c)) => c,
_ => {
return Err(self.error(ErrorCode::LoneLeadingSurrogateInHexEscape));
}

View File

@ -106,7 +106,7 @@ the API provide writer to serialize them into a stream or a string ...
When using `ToJson` the `Serialize` trait implementation is not mandatory.
A basic `ToJson` example using a TreeMap of attribute name / attribute value:
A basic `ToJson` example using a BTreeMap of attribute name / attribute value:
```rust
@ -115,7 +115,7 @@ A basic `ToJson` example using a TreeMap of attribute name / attribute value:
extern crate serde_macros;
extern crate serde;
use std::collections::TreeMap;
use std::collections::BTreeMap;
use serde::json::{ToJson, Value};
pub struct MyStruct {
@ -125,7 +125,7 @@ pub struct MyStruct {
impl ToJson for MyStruct {
fn to_json( &self ) -> Value {
let mut d = TreeMap::new();
let mut d = BTreeMap::new();
d.insert("attr1".to_string(), self.attr1.to_json());
d.insert("attr2".to_string(), self.attr2.to_json());
d.to_json()
@ -157,8 +157,8 @@ pub struct MyStruct {
impl ToJson for MyStruct {
fn to_json( &self ) -> Value {
ObjectBuilder::new()
.insert("attr1", &self.attr1)
.insert("attr2", &self.attr2)
.insert("attr1".to_string(), &self.attr1)
.insert("attr2".to_string(), &self.attr2)
.unwrap()
}
}
@ -265,9 +265,9 @@ pub struct TestStruct1 {
impl ToJson for TestStruct1 {
fn to_json( &self ) -> json::Value {
json::builder::ObjectBuilder::new()
.insert("data_int", &self.data_int)
.insert("data_str", &self.data_str)
.insert("data_vector", &self.data_vector)
.insert("data_int".to_string(), &self.data_int)
.insert("data_str".to_string(), &self.data_str)
.insert("data_vector".to_string(), &self.data_vector)
.unwrap()
}
}
@ -281,7 +281,7 @@ fn main() {
data_vector: vec![2,3,4,5],
};
let json: json::Value = test.to_json();
let json_str: String = json.to_string().into_string();
let json_str: String = json.to_string();
// Deserialize like before.
@ -322,7 +322,7 @@ mod tests {
use std::io;
use std::str;
use std::string;
use std::collections::TreeMap;
use std::collections::BTreeMap;
use de;
use ser::{Serialize, Serializer};
@ -351,7 +351,7 @@ mod tests {
macro_rules! treemap {
($($k:expr => $v:expr),*) => ({
let mut _m = ::std::collections::TreeMap::new();
let mut _m = ::std::collections::BTreeMap::new();
$(_m.insert($k, $v);)*
_m
})
@ -1002,7 +1002,7 @@ mod tests {
#[test]
fn test_parse_object() {
test_parse_err::<TreeMap<string::String, int>>(&[
test_parse_err::<BTreeMap<string::String, int>>(&[
("{", SyntaxError(EOFWhileParsingString, 1, 2)),
("{ ", SyntaxError(EOFWhileParsingString, 1, 3)),
("{1", SyntaxError(KeyMustBeAString, 1, 2)),
@ -1171,7 +1171,7 @@ mod tests {
#[test]
fn test_multiline_errors() {
test_parse_err::<TreeMap<string::String, string::String>>(&[
test_parse_err::<BTreeMap<string::String, string::String>>(&[
("{\n \"foo\":\n \"bar\"", SyntaxError(EOFWhileParsingObject, 3u, 8u)),
]);
}
@ -1267,7 +1267,7 @@ mod tests {
fn test_as_object() {
let json_value: Value = from_str("{}").unwrap();
let json_object = json_value.as_object();
let map = TreeMap::<string::String, Value>::new();
let map = BTreeMap::<string::String, Value>::new();
assert_eq!(json_object, Some(&map));
}
@ -1698,7 +1698,7 @@ mod tests {
#[cfg(test)]
mod bench {
use std::collections::TreeMap;
use std::collections::BTreeMap;
use std::string;
use serialize;
use test::Bencher;
@ -1709,7 +1709,7 @@ mod bench {
macro_rules! treemap {
($($k:expr => $v:expr),*) => ({
let mut _m = ::std::collections::TreeMap::new();
let mut _m = ::std::collections::BTreeMap::new();
$(_m.insert($k, $v);)*
_m
})
@ -1764,7 +1764,7 @@ mod bench {
))
)));
}
list.push(Json::Object(TreeMap::new()));
list.push(Json::Object(BTreeMap::new()));
Json::Array(list)
}
@ -1783,7 +1783,7 @@ mod bench {
))
)));
}
list.push(Value::Object(TreeMap::new()));
list.push(Value::Object(BTreeMap::new()));
Value::Array(list)
}

View File

@ -2,6 +2,7 @@ use std::f32;
use std::f64;
use std::num::{Float, FPNaN, FPInfinite};
use std::io::{IoError, IoResult};
use std::str::Utf8Error;
use ser::Serialize;
use ser;
@ -599,7 +600,7 @@ pub fn to_vec<
#[inline]
pub fn to_string<
T: Serialize<Serializer<Vec<u8>>, IoError>
>(value: &T) -> Result<String, Vec<u8>> {
>(value: &T) -> Result<String, (Vec<u8>, Utf8Error)> {
let buf = to_vec(value);
String::from_utf8(buf)
}
@ -628,7 +629,7 @@ pub fn to_pretty_vec<
/// Encode the specified struct into a json `String` buffer.
pub fn to_pretty_string<
T: Serialize<PrettySerializer<Vec<u8>>, IoError>
>(value: &T) -> Result<String, Vec<u8>> {
>(value: &T) -> Result<String, (Vec<u8>, Utf8Error)> {
let buf = to_pretty_vec(value);
String::from_utf8(buf)
}

View File

@ -1,4 +1,4 @@
use std::collections::{HashMap, TreeMap, tree_map};
use std::collections::{HashMap, BTreeMap, btree_map};
use std::fmt;
use std::io::{ByRefWriter, IoResult};
use std::io;
@ -21,7 +21,7 @@ pub enum Value {
Floating(f64),
String(String),
Array(Vec<Value>),
Object(TreeMap<String, Value>),
Object(BTreeMap<String, Value>),
}
impl Value {
@ -99,7 +99,7 @@ impl Value {
/// If the Json value is an Object, returns the associated TreeMap.
/// Returns None otherwise.
pub fn as_object<'a>(&'a self) -> Option<&'a TreeMap<String, Value>> {
pub fn as_object<'a>(&'a self) -> Option<&'a BTreeMap<String, Value>> {
match *self {
Value::Object(ref map) => Some(map),
_ => None
@ -286,7 +286,7 @@ impl<D: de::Deserializer<E>, E> de::Deserialize<D, E> for Value {
Token::EnumStart(_, name, len) => {
let token = Token::SeqStart(len);
let fields: Vec<Value> = try!(de::Deserialize::deserialize_token(d, token));
let mut object = TreeMap::new();
let mut object = BTreeMap::new();
object.insert(name.to_string(), Value::Array(fields));
Ok(Value::Object(object))
}
@ -302,8 +302,8 @@ impl<D: de::Deserializer<E>, E> de::Deserialize<D, E> for Value {
enum State {
Value(Value),
Array(vec::MoveItems<Value>),
Object(tree_map::MoveEntries<String, Value>),
Array(vec::IntoIter<Value>),
Object(btree_map::IntoIter<String, Value>),
End,
}
@ -584,7 +584,7 @@ impl ToJson for String {
}
macro_rules! peel_to_json_tuple {
($name:ident, $($other:ident,)*) => (impl_to_json_tuple!($($other,)*))
($name:ident, $($other:ident,)*) => ( impl_to_json_tuple!($($other,)*); )
}
macro_rules! impl_to_json_tuple {
@ -615,7 +615,7 @@ macro_rules! impl_to_json_tuple {
Value::Array(array)
}
}
peel_to_json_tuple!($($name,)*)
peel_to_json_tuple!($($name,)*);
}
}
@ -627,9 +627,9 @@ impl<A:ToJson> ToJson for Vec<A> {
}
}
impl<A:ToJson> ToJson for TreeMap<String, A> {
impl<A:ToJson> ToJson for BTreeMap<String, A> {
fn to_json(&self) -> Value {
let mut d = TreeMap::new();
let mut d = BTreeMap::new();
for (key, value) in self.iter() {
d.insert((*key).clone(), value.to_json());
}
@ -639,7 +639,7 @@ impl<A:ToJson> ToJson for TreeMap<String, A> {
impl<A:ToJson> ToJson for HashMap<String, A> {
fn to_json(&self) -> Value {
let mut d = TreeMap::new();
let mut d = BTreeMap::new();
for (key, value) in self.iter() {
d.insert((*key).clone(), value.to_json());
}

View File

@ -12,6 +12,8 @@ extern crate serde_macros;
#[cfg(test)]
extern crate serialize;
extern crate unicode;
pub use de::{Deserializer, Deserialize};
pub use ser::{Serializer, Serialize};

View File

@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use std::collections::{HashMap, HashSet, TreeMap, TreeSet};
use std::collections::{HashMap, HashSet, BTreeMap, BTreeSet};
use std::hash::Hash;
use std::rc::Rc;
use std::sync::Arc;
@ -130,20 +130,20 @@ macro_rules! impl_serialize {
}
}
impl_serialize!(bool, serialize_bool)
impl_serialize!(int, serialize_int)
impl_serialize!(i8, serialize_i8)
impl_serialize!(i16, serialize_i16)
impl_serialize!(i32, serialize_i32)
impl_serialize!(i64, serialize_i64)
impl_serialize!(uint, serialize_uint)
impl_serialize!(u8, serialize_u8)
impl_serialize!(u16, serialize_u16)
impl_serialize!(u32, serialize_u32)
impl_serialize!(u64, serialize_u64)
impl_serialize!(f32, serialize_f32)
impl_serialize!(f64, serialize_f64)
impl_serialize!(char, serialize_char)
impl_serialize!(bool, serialize_bool);
impl_serialize!(int, serialize_int);
impl_serialize!(i8, serialize_i8);
impl_serialize!(i16, serialize_i16);
impl_serialize!(i32, serialize_i32);
impl_serialize!(i64, serialize_i64);
impl_serialize!(uint, serialize_uint);
impl_serialize!(u8, serialize_u8);
impl_serialize!(u16, serialize_u16);
impl_serialize!(u32, serialize_u32);
impl_serialize!(u64, serialize_u64);
impl_serialize!(f32, serialize_f32);
impl_serialize!(f64, serialize_f64);
impl_serialize!(char, serialize_char);
//////////////////////////////////////////////////////////////////////////////
@ -179,9 +179,9 @@ macro_rules! impl_serialize_box {
}
}
impl_serialize_box!(&'a T)
impl_serialize_box!(Box<T>)
impl_serialize_box!(Rc<T>)
impl_serialize_box!(&'a T);
impl_serialize_box!(Box<T>);
impl_serialize_box!(Rc<T>);
impl<
S: Serializer<E>,
@ -239,7 +239,7 @@ impl<
E,
K: Serialize<S, E> + Ord,
V: Serialize<S, E>
> Serialize<S, E> for TreeMap<K, V> {
> Serialize<S, E> for BTreeMap<K, V> {
#[inline]
fn serialize(&self, s: &mut S) -> Result<(), E> {
s.serialize_map(self.iter())
@ -263,7 +263,7 @@ impl<
S: Serializer<E>,
E,
T: Serialize<S, E> + Ord
> Serialize<S, E> for TreeSet<T> {
> Serialize<S, E> for BTreeSet<T> {
#[inline]
fn serialize(&self, s: &mut S) -> Result<(), E> {
s.serialize_seq(self.iter())
@ -273,7 +273,7 @@ impl<
//////////////////////////////////////////////////////////////////////////////
macro_rules! peel {
($name:ident, $($other:ident,)*) => (impl_serialize_tuple!($($other,)*))
($name:ident, $($other:ident,)*) => ( impl_serialize_tuple!($($other,)*); )
}
macro_rules! impl_serialize_tuple {
@ -307,7 +307,7 @@ macro_rules! impl_serialize_tuple {
s.serialize_tuple_end()
}
}
peel!($($name,)*)
peel!($($name,)*);
}
}
@ -317,7 +317,7 @@ impl_serialize_tuple! { T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, }
#[cfg(test)]
mod tests {
use std::collections::{HashMap, TreeMap};
use std::collections::{HashMap, BTreeMap};
use std::{option, string};
@ -825,7 +825,7 @@ mod tests {
let mut serializer = AssertSerializer::new(tokens.into_iter());
let mut map = TreeMap::new();
let mut map = BTreeMap::new();
map.insert(5i, "a".to_string());
map.insert(6i, "b".to_string());