Update to rust HEAD

This commit is contained in:
Erick Tryzelaar
2015-01-07 07:51:59 -08:00
parent b98719a4a0
commit 8715a41158
23 changed files with 295 additions and 255 deletions
+14 -6
View File
@@ -10,8 +10,8 @@
use std::collections::BTreeMap;
use ser::{mod, Serialize};
use json::value::{mod, Value};
use ser::{self, Serialize};
use json::value::{self, Value};
pub struct ArrayBuilder {
array: Vec<Value>,
@@ -31,13 +31,17 @@ impl ArrayBuilder {
self
}
pub fn push_array(mut self, f: |ArrayBuilder| -> ArrayBuilder) -> ArrayBuilder {
pub fn push_array<F>(mut self, f: F) -> ArrayBuilder where
F: FnOnce(ArrayBuilder) -> ArrayBuilder
{
let builder = ArrayBuilder::new();
self.array.push(f(builder).unwrap());
self
}
pub fn push_object(mut self, f: |ObjectBuilder| -> ObjectBuilder) -> ArrayBuilder {
pub fn push_object<F>(mut self, f: F) -> ArrayBuilder where
F: FnOnce(ObjectBuilder) -> ObjectBuilder
{
let builder = ObjectBuilder::new();
self.array.push(f(builder).unwrap());
self
@@ -62,13 +66,17 @@ impl ObjectBuilder {
self
}
pub fn insert_array(mut self, key: String, f: |ArrayBuilder| -> ArrayBuilder) -> ObjectBuilder {
pub fn insert_array<F>(mut self, key: String, f: F) -> ObjectBuilder where
F: FnOnce(ArrayBuilder) -> ArrayBuilder
{
let builder = ArrayBuilder::new();
self.object.insert(key, f(builder).unwrap());
self
}
pub fn insert_object(mut self, key: String, f: |ObjectBuilder| -> ObjectBuilder) -> ObjectBuilder {
pub fn insert_object<F>(mut self, key: String, f: F) -> ObjectBuilder where
F: FnOnce(ObjectBuilder) -> ObjectBuilder
{
let builder = ObjectBuilder::new();
self.object.insert(key, f(builder).unwrap());
self
+6 -6
View File
@@ -15,7 +15,7 @@ pub struct Parser<Iter> {
buf: Vec<u8>,
}
impl<Iter: Iterator<u8>> Parser<Iter> {
impl<Iter: Iterator<Item=u8>> Parser<Iter> {
/// Creates the JSON parser.
#[inline]
pub fn new(rdr: Iter) -> Parser<Iter> {
@@ -353,7 +353,7 @@ impl<Iter: Iterator<u8>> Parser<Iter> {
}
};
let buf = &mut [0u8, .. 4];
let buf = &mut [0; 4];
let len = c.encode_utf8(buf).unwrap_or(0);
self.buf.extend(buf.slice_to(len).iter().map(|b| *b));
}
@@ -380,7 +380,7 @@ impl<Iter: Iterator<u8>> Parser<Iter> {
}
}
impl<Iter: Iterator<u8>> Deserializer<Error> for Parser<Iter> {
impl<Iter: Iterator<Item=u8>> Deserializer<Error> for Parser<Iter> {
#[inline]
fn visit<
R,
@@ -395,7 +395,7 @@ struct SeqVisitor<'a, Iter: 'a> {
first: bool,
}
impl<'a, Iter: Iterator<u8>> de::SeqVisitor<Parser<Iter>, Error> for SeqVisitor<'a, Iter> {
impl<'a, Iter: Iterator<Item=u8>> de::SeqVisitor<Parser<Iter>, Error> for SeqVisitor<'a, Iter> {
fn visit<
T: de::Deserialize<Parser<Iter>, Error>,
>(&mut self) -> Result<Option<T>, Error> {
@@ -439,7 +439,7 @@ struct MapVisitor<'a, Iter: 'a> {
first: bool,
}
impl<'a, Iter: Iterator<u8>> de::MapVisitor<Parser<Iter>, Error> for MapVisitor<'a, Iter> {
impl<'a, Iter: Iterator<Item=u8>> de::MapVisitor<Parser<Iter>, Error> for MapVisitor<'a, Iter> {
fn visit_key<
K: de::Deserialize<Parser<Iter>, Error>,
>(&mut self) -> Result<Option<K>, Error> {
@@ -506,7 +506,7 @@ impl<'a, Iter: Iterator<u8>> de::MapVisitor<Parser<Iter>, Error> for MapVisitor<
/// Decodes a json value from an `Iterator<u8>`.
pub fn from_iter<
Iter: Iterator<u8>,
Iter: Iterator<Item=u8>,
T: de::Deserialize<Parser<Iter>, Error>
>(iter: Iter) -> Result<T, Error> {
let mut parser = Parser::new(iter);
+2 -2
View File
@@ -5,7 +5,7 @@ use std::io;
use de;
/// The errors that can arise while parsing a JSON stream.
#[deriving(Copy, Clone, PartialEq)]
#[derive(Copy, Clone, PartialEq)]
pub enum ErrorCode {
EOFWhileParsingList,
EOFWhileParsingObject,
@@ -75,7 +75,7 @@ impl fmt::Show for ErrorCode {
}
}
#[deriving(Clone, PartialEq, Show)]
#[derive(Clone, PartialEq, Show)]
pub enum Error {
/// msg, line, col
SyntaxError(ErrorCode, uint, uint),
+2 -2
View File
@@ -1,5 +1,5 @@
use std::f64;
use std::io::{mod, ByRefWriter, IoError};
use std::io::{self, ByRefWriter, IoError};
use std::num::{Float, FpCategory};
use std::string::FromUtf8Error;
@@ -218,7 +218,7 @@ pub fn escape_str<W: io::Writer>(wr: &mut W, value: &str) -> Result<(), IoError>
#[inline]
pub fn escape_char<W: io::Writer>(wr: &mut W, value: char) -> Result<(), IoError> {
let mut buf = &mut [0, .. 4];
let mut buf = &mut [0; 4];
value.encode_utf8(buf);
escape_bytes(wr, buf)
}
+8 -6
View File
@@ -1,10 +1,11 @@
use std::collections::BTreeMap;
use std::fmt;
use std::io;
use std::str;
use ser::{mod, Serializer};
use ser::{self, Serializer};
#[deriving(PartialEq)]
#[derive(PartialEq)]
pub enum Value {
Null,
Bool(bool),
@@ -49,19 +50,20 @@ impl ser::Serialize for Value {
}
}
struct WriterFormatter<'a, 'b: 'a>(&'a mut fmt::Formatter<'b>);
struct WriterFormatter<'a, 'b: 'a> {
inner: &'a mut fmt::Formatter<'b>,
}
impl<'a, 'b> io::Writer for WriterFormatter<'a, 'b> {
fn write(&mut self, buf: &[u8]) -> io::IoResult<()> {
let WriterFormatter(ref mut f) = *self;
f.write(buf).map_err(|_| io::IoError::last_error())
self.inner.write_str(str::from_utf8(buf).unwrap()).map_err(|_| io::IoError::last_error())
}
}
impl fmt::Show for Value {
/// Serializes a json value into a string
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut wr = WriterFormatter(f);
let mut wr = WriterFormatter { inner: f };
super::ser::to_writer(&mut wr, self).map_err(|_| fmt::Error)
}
}