diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2015-01-06 09:24:46 -0800 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2015-01-06 15:24:24 -0800 |
| commit | e2f97f51ad4cf902e5a5835b5332447fe59089c4 (patch) | |
| tree | 6158174fbd2ae40d72c113d398ff192a936804f7 | |
| parent | 5c3ddcb15dc8b40fa780a38fd7494b9b5b991d09 (diff) | |
| download | rust-e2f97f51ad4cf902e5a5835b5332447fe59089c4.tar.gz rust-e2f97f51ad4cf902e5a5835b5332447fe59089c4.zip | |
Register new snapshots
Conflicts: src/librbml/lib.rs src/libserialize/json_stage0.rs src/libserialize/serialize_stage0.rs src/libsyntax/ast.rs src/libsyntax/ext/deriving/generic/mod.rs src/libsyntax/parse/token.rs
59 files changed, 94 insertions, 6419 deletions
diff --git a/src/compiletest/compiletest.rs b/src/compiletest/compiletest.rs index 23674efe968..ff9b2de3ccf 100644 --- a/src/compiletest/compiletest.rs +++ b/src/compiletest/compiletest.rs @@ -16,14 +16,8 @@ extern crate test; extern crate getopts; -#[cfg(stage0)] -#[phase(plugin, link)] -extern crate log; - -#[cfg(not(stage0))] #[macro_use] extern crate log; - extern crate regex; use std::os; diff --git a/src/liballoc/lib.rs b/src/liballoc/lib.rs index 001e02f9c0d..d5a814d83e4 100644 --- a/src/liballoc/lib.rs +++ b/src/liballoc/lib.rs @@ -65,36 +65,16 @@ #![no_std] #![allow(unknown_features)] -#![feature(lang_items, phase, unsafe_destructor, default_type_params, old_orphan_check)] -#![feature(associated_types)] +#![feature(lang_items, phase, unsafe_destructor)] -#[cfg(stage0)] -#[phase(plugin, link)] -extern crate core; - -#[cfg(not(stage0))] #[macro_use] extern crate core; - extern crate libc; // Allow testing this library -#[cfg(all(test, stage0))] -#[phase(plugin, link)] -extern crate std; - -#[cfg(all(test, not(stage0)))] -#[macro_use] -extern crate std; - -#[cfg(all(test, stage0))] -#[phase(plugin, link)] -extern crate log; - -#[cfg(all(test, not(stage0)))] -#[macro_use] -extern crate log; +#[cfg(test)] #[macro_use] extern crate std; +#[cfg(test)] #[macro_use] extern crate log; // Heaps provided for low-level allocation strategies diff --git a/src/libcollections/lib.rs b/src/libcollections/lib.rs index 5bf5f78af94..82dfa41d9d8 100644 --- a/src/libcollections/lib.rs +++ b/src/libcollections/lib.rs @@ -21,19 +21,9 @@ html_root_url = "http://doc.rust-lang.org/nightly/", html_playground_url = "http://play.rust-lang.org/")] -#![allow(unknown_features)] -#![feature(macro_rules, default_type_params, phase, globs)] #![feature(unsafe_destructor, slicing_syntax)] -#![feature(unboxed_closures)] -#![feature(old_orphan_check)] -#![feature(associated_types)] #![no_std] -#[cfg(stage0)] -#[phase(plugin, link)] -extern crate core; - -#[cfg(not(stage0))] #[macro_use] extern crate core; @@ -41,22 +31,8 @@ extern crate unicode; extern crate alloc; #[cfg(test)] extern crate test; - -#[cfg(all(test, stage0))] -#[phase(plugin, link)] -extern crate std; - -#[cfg(all(test, not(stage0)))] -#[macro_use] -extern crate std; - -#[cfg(all(test, stage0))] -#[phase(plugin, link)] -extern crate log; - -#[cfg(all(test, not(stage0)))] -#[macro_use] -extern crate log; +#[cfg(test)] #[macro_use] extern crate std; +#[cfg(test)] #[macro_use] extern crate log; pub use binary_heap::BinaryHeap; pub use bitv::Bitv; @@ -73,8 +49,7 @@ pub use vec_map::VecMap; // Needed for the vec! macro pub use alloc::boxed; -#[cfg_attr(stage0, macro_escape)] -#[cfg_attr(not(stage0), macro_use)] +#[macro_use] mod macros; pub mod binary_heap; diff --git a/src/libcollections/macros.rs b/src/libcollections/macros.rs index 0c5929e8661..68e2482964d 100644 --- a/src/libcollections/macros.rs +++ b/src/libcollections/macros.rs @@ -8,21 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -/// Creates a `std::vec::Vec` containing the arguments. -// NOTE: remove after the next snapshot -#[cfg(stage0)] -macro_rules! vec { - ($($e:expr),*) => ({ - // leading _ to allow empty construction without a warning. - let mut _temp = ::vec::Vec::new(); - $(_temp.push($e);)* - _temp - }); - ($($e:expr),+,) => (vec!($($e),+)) -} - /// Creates a `Vec` containing the arguments. -#[cfg(not(stage0))] #[macro_export] macro_rules! vec { ($($x:expr),*) => ({ diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs index fe2511d1f7f..fdd758cedac 100644 --- a/src/libcore/fmt/mod.rs +++ b/src/libcore/fmt/mod.rs @@ -27,9 +27,6 @@ use slice::SliceExt; use slice; use str::{self, StrExt, Utf8Error}; -// NOTE: for old macros; remove after the next snapshot -#[cfg(stage0)] use result::Result::Err; - pub use self::num::radix; pub use self::num::Radix; pub use self::num::RadixFmt; diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index aff0065c527..4b598fd5033 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -56,29 +56,25 @@ html_playground_url = "http://play.rust-lang.org/")] #![no_std] -#![allow(unknown_features, raw_pointer_deriving)] -#![feature(globs, intrinsics, lang_items, macro_rules, phase)] +#![allow(unknown_features, raw_pointer_derive)] +#![feature(intrinsics, lang_items)] #![feature(simd, unsafe_destructor, slicing_syntax)] -#![feature(default_type_params, unboxed_closures, associated_types)] +#![feature(unboxed_closures)] #![deny(missing_docs)] -#[cfg_attr(stage0, macro_escape)] -#[cfg_attr(not(stage0), macro_use)] +#[macro_use] mod macros; #[path = "num/float_macros.rs"] -#[cfg_attr(stage0, macro_escape)] -#[cfg_attr(not(stage0), macro_use)] +#[macro_use] mod float_macros; #[path = "num/int_macros.rs"] -#[cfg_attr(stage0, macro_escape)] -#[cfg_attr(not(stage0), macro_use)] +#[macro_use] mod int_macros; #[path = "num/uint_macros.rs"] -#[cfg_attr(stage0, macro_escape)] -#[cfg_attr(not(stage0), macro_use)] +#[macro_use] mod uint_macros; #[path = "num/int.rs"] pub mod int; diff --git a/src/libcore/macros.rs b/src/libcore/macros.rs index 99e49cc21ed..d01621fc6d8 100644 --- a/src/libcore/macros.rs +++ b/src/libcore/macros.rs @@ -142,16 +142,9 @@ macro_rules! debug_assert_eq { ($($arg:tt)*) => (if cfg!(not(ndebug)) { assert_eq!($($arg)*); }) } -#[cfg(stage0)] -#[macro_export] -macro_rules! try { - ($e:expr) => (match $e { Ok(e) => e, Err(e) => return Err(e) }) -} - /// Short circuiting evaluation on Err /// /// `libstd` contains a more general `try!` macro that uses `FromError`. -#[cfg(not(stage0))] #[macro_export] macro_rules! try { ($e:expr) => ({ diff --git a/src/libcoretest/num/mod.rs b/src/libcoretest/num/mod.rs index f86c85f8216..9be7917e0a0 100644 --- a/src/libcoretest/num/mod.rs +++ b/src/libcoretest/num/mod.rs @@ -14,8 +14,7 @@ use core::num::{NumCast, cast}; use core::ops::{Add, Sub, Mul, Div, Rem}; use core::kinds::Copy; -#[cfg_attr(stage0, macro_escape)] -#[cfg_attr(not(stage0), macro_use)] +#[macro_use] mod int_macros; mod i8; @@ -24,8 +23,7 @@ mod i32; mod i64; mod int; -#[cfg_attr(stage0, macro_escape)] -#[cfg_attr(not(stage0), macro_use)] +#[macro_use] mod uint_macros; mod u8; diff --git a/src/libflate/lib.rs b/src/libflate/lib.rs index 6ac311fe4b6..f38440d86c6 100644 --- a/src/libflate/lib.rs +++ b/src/libflate/lib.rs @@ -21,7 +21,6 @@ #![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png", html_favicon_url = "http://www.rust-lang.org/favicon.ico", html_root_url = "http://doc.rust-lang.org/nightly/")] -#![feature(unboxed_closures, associated_types)] #[cfg(test)] #[macro_use] extern crate log; diff --git a/src/libfmt_macros/lib.rs b/src/libfmt_macros/lib.rs index 19173baf166..47cc072a636 100644 --- a/src/libfmt_macros/lib.rs +++ b/src/libfmt_macros/lib.rs @@ -23,8 +23,7 @@ html_root_url = "http://doc.rust-lang.org/nightly/", html_playground_url = "http://play.rust-lang.org/")] -#![feature(globs, slicing_syntax)] -#![feature(associated_types)] +#![feature(slicing_syntax)] pub use self::Piece::*; pub use self::Position::*; diff --git a/src/libgetopts/lib.rs b/src/libgetopts/lib.rs index 64d4de7cc8a..f50e24c6354 100644 --- a/src/libgetopts/lib.rs +++ b/src/libgetopts/lib.rs @@ -85,8 +85,7 @@ html_favicon_url = "http://www.rust-lang.org/favicon.ico", html_root_url = "http://doc.rust-lang.org/nightly/", html_playground_url = "http://play.rust-lang.org/")] -#![feature(globs, slicing_syntax)] -#![feature(unboxed_closures)] +#![feature(slicing_syntax)] #![deny(missing_docs)] #[cfg(test)] #[macro_use] extern crate log; diff --git a/src/libgraphviz/lib.rs b/src/libgraphviz/lib.rs index 7e8382b26c4..83bad70e7b1 100644 --- a/src/libgraphviz/lib.rs +++ b/src/libgraphviz/lib.rs @@ -271,8 +271,7 @@ #![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png", html_favicon_url = "http://www.rust-lang.org/favicon.ico", html_root_url = "http://doc.rust-lang.org/nightly/")] -#![feature(globs, slicing_syntax)] -#![feature(unboxed_closures)] +#![feature(slicing_syntax)] use self::LabelText::*; diff --git a/src/liblibc/lib.rs b/src/liblibc/lib.rs index e3f02146a75..1aaa0528a68 100644 --- a/src/liblibc/lib.rs +++ b/src/liblibc/lib.rs @@ -8,7 +8,6 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#![feature(globs)] #![crate_name = "libc"] #![experimental] #![no_std] // we don't need std, and we can't have std, since it doesn't exist @@ -76,7 +75,7 @@ #![allow(non_upper_case_globals)] #![allow(missing_docs)] #![allow(non_snake_case)] -#![allow(raw_pointer_deriving)] +#![allow(raw_pointer_derive)] extern crate core; diff --git a/src/liblog/lib.rs b/src/liblog/lib.rs index 332dd5e558e..08b01e956e1 100644 --- a/src/liblog/lib.rs +++ b/src/liblog/lib.rs @@ -163,7 +163,7 @@ html_favicon_url = "http://www.rust-lang.org/favicon.ico", html_root_url = "http://doc.rust-lang.org/nightly/", html_playground_url = "http://play.rust-lang.org/")] -#![feature(macro_rules, unboxed_closures, slicing_syntax)] +#![feature(slicing_syntax)] #![deny(missing_docs)] extern crate regex; @@ -182,8 +182,7 @@ use regex::Regex; use directive::LOG_LEVEL_NAMES; -#[cfg_attr(stage0, macro_escape)] -#[cfg_attr(not(stage0), macro_use)] +#[macro_use] pub mod macros; mod directive; diff --git a/src/librand/lib.rs b/src/librand/lib.rs index aeecd69e205..9e5fcdc8df5 100644 --- a/src/librand/lib.rs +++ b/src/librand/lib.rs @@ -23,35 +23,14 @@ html_root_url = "http://doc.rust-lang.org/nightly/", html_playground_url = "http://play.rust-lang.org/")] -#![feature(macro_rules, phase, globs)] -#![feature(unboxed_closures)] -#![feature(associated_types)] #![no_std] #![experimental] -#[cfg(stage0)] -#[phase(plugin, link)] -extern crate core; - -#[cfg(not(stage0))] #[macro_use] extern crate core; -#[cfg(all(test, stage0))] -#[phase(plugin, link)] -extern crate std; - -#[cfg(all(test, not(stage0)))] -#[macro_use] -extern crate std; - -#[cfg(all(test, stage0))] -#[phase(plugin, link)] -extern crate log; - -#[cfg(all(test, not(stage0)))] -#[macro_use] -extern crate log; +#[cfg(test)] #[macro_use] extern crate std; +#[cfg(test)] #[macro_use] extern crate log; use core::prelude::*; diff --git a/src/librbml/lib.rs b/src/librbml/lib.rs index 80a3a88f70c..a66d1dd08c1 100644 --- a/src/librbml/lib.rs +++ b/src/librbml/lib.rs @@ -24,19 +24,10 @@ html_root_url = "http://doc.rust-lang.org/nightly/", html_playground_url = "http://play.rust-lang.org/")] #![allow(unknown_features)] -#![feature(macro_rules, phase, slicing_syntax, globs)] -#![feature(unboxed_closures, associated_types)] -#![allow(missing_docs)] +#![feature(slicing_syntax)] extern crate serialize; - -#[cfg(stage0)] -#[phase(plugin, link)] -extern crate log; - -#[cfg(not(stage0))] -#[macro_use] -extern crate log; +#[macro_use] extern crate log; #[cfg(test)] extern crate test; @@ -424,263 +415,6 @@ pub mod reader { } } - #[cfg(stage0)] - impl<'doc> serialize::Decoder<Error> for Decoder<'doc> { - fn read_nil(&mut self) -> DecodeResult<()> { Ok(()) } - - fn read_u64(&mut self) -> DecodeResult<u64> { Ok(doc_as_u64(try!(self.next_doc(EsU64)))) } - fn read_u32(&mut self) -> DecodeResult<u32> { Ok(doc_as_u32(try!(self.next_doc(EsU32)))) } - fn read_u16(&mut self) -> DecodeResult<u16> { Ok(doc_as_u16(try!(self.next_doc(EsU16)))) } - fn read_u8 (&mut self) -> DecodeResult<u8 > { Ok(doc_as_u8 (try!(self.next_doc(EsU8 )))) } - fn read_uint(&mut self) -> DecodeResult<uint> { - let v = doc_as_u64(try!(self.next_doc(EsUint))); - if v > (::std::uint::MAX as u64) { - Err(IntTooBig(v as uint)) - } else { - Ok(v as uint) - } - } - - fn read_i64(&mut self) -> DecodeResult<i64> { - Ok(doc_as_u64(try!(self.next_doc(EsI64))) as i64) - } - fn read_i32(&mut self) -> DecodeResult<i32> { - Ok(doc_as_u32(try!(self.next_doc(EsI32))) as i32) - } - fn read_i16(&mut self) -> DecodeResult<i16> { - Ok(doc_as_u16(try!(self.next_doc(EsI16))) as i16) - } - fn read_i8 (&mut self) -> DecodeResult<i8> { - Ok(doc_as_u8(try!(self.next_doc(EsI8 ))) as i8) - } - fn read_int(&mut self) -> DecodeResult<int> { - let v = doc_as_u64(try!(self.next_doc(EsInt))) as i64; - if v > (int::MAX as i64) || v < (int::MIN as i64) { - debug!("FIXME \\#6122: Removing this makes this function miscompile"); - Err(IntTooBig(v as uint)) - } else { - Ok(v as int) - } - } - - fn read_bool(&mut self) -> DecodeResult<bool> { - Ok(doc_as_u8(try!(self.next_doc(EsBool))) != 0) - } - - fn read_f64(&mut self) -> DecodeResult<f64> { - let bits = doc_as_u64(try!(self.next_doc(EsF64))); - Ok(unsafe { transmute(bits) }) - } - fn read_f32(&mut self) -> DecodeResult<f32> { - let bits = doc_as_u32(try!(self.next_doc(EsF32))); - Ok(unsafe { transmute(bits) }) - } - fn read_char(&mut self) -> DecodeResult<char> { - Ok(char::from_u32(doc_as_u32(try!(self.next_doc(EsChar)))).unwrap()) - } - fn read_str(&mut self) -> DecodeResult<String> { - Ok(try!(self.next_doc(EsStr)).as_str()) - } - - // Compound types: - fn read_enum<T, F>(&mut self, name: &str, f: F) -> DecodeResult<T> where - F: FnOnce(&mut Decoder<'doc>) -> DecodeResult<T>, - { - debug!("read_enum({:?})", name); - try!(self._check_label(name)); - - let doc = try!(self.next_doc(EsEnum)); - - let (old_parent, old_pos) = (self.parent, self.pos); - self.parent = doc; - self.pos = self.parent.start; - - let result = try!(f(self)); - - self.parent = old_parent; - self.pos = old_pos; - Ok(result) - } - - fn read_enum_variant<T, F>(&mut self, _: &[&str], - mut f: F) -> DecodeResult<T> - where F: FnMut(&mut Decoder<'doc>, uint) -> DecodeResult<T>, - { - debug!("read_enum_variant()"); - let idx = try!(self._next_uint(EsEnumVid)); - debug!(" idx={:?}", idx); - - let doc = try!(self.next_doc(EsEnumBody)); - - let (old_parent, old_pos) = (self.parent, self.pos); - self.parent = doc; - self.pos = self.parent.start; - - let result = try!(f(self, idx)); - - self.parent = old_parent; - self.pos = old_pos; - Ok(result) - } - - fn read_enum_variant_arg<T, F>(&mut self, idx: uint, f: F) -> DecodeResult<T> where - F: FnOnce(&mut Decoder<'doc>) -> DecodeResult<T>, - { - debug!("read_enum_variant_arg(idx={:?})", idx); - f(self) - } - - fn read_enum_struct_variant<T, F>(&mut self, _: &[&str], - mut f: F) -> DecodeResult<T> - where F: FnMut(&mut Decoder<'doc>, uint) -> DecodeResult<T>, - { - debug!("read_enum_struct_variant()"); - let idx = try!(self._next_uint(EsEnumVid)); - debug!(" idx={:?}", idx); - - let doc = try!(self.next_doc(EsEnumBody)); - - let (old_parent, old_pos) = (self.parent, self.pos); - self.parent = doc; - self.pos = self.parent.start; - - let result = try!(f(self, idx)); - - self.parent = old_parent; - self.pos = old_pos; - Ok(result) - } - - fn read_enum_struct_variant_field<T, F>(&mut self, - name: &str, - idx: uint, - f: F) - -> DecodeResult<T> where - F: FnOnce(&mut Decoder<'doc>) -> DecodeResult<T>, - { - debug!("read_enum_struct_variant_arg(name={:?}, idx={:?})", name, idx); - f(self) - } - - fn read_struct<T, F>(&mut self, name: &str, _: uint, f: F) -> DecodeResult<T> where - F: FnOnce(&mut Decoder<'doc>) -> DecodeResult<T>, - { - debug!("read_struct(name={:?})", name); - f(self) - } - - fn read_struct_field<T, F>(&mut self, name: &str, idx: uint, f: F) -> DecodeResult<T> where - F: FnOnce(&mut Decoder<'doc>) -> DecodeResult<T>, - { - debug!("read_struct_field(name={:?}, idx={:?})", name, idx); - try!(self._check_label(name)); - f(self) - } - - fn read_tuple<T, F>(&mut self, tuple_len: uint, f: F) -> DecodeResult<T> where - F: FnOnce(&mut Decoder<'doc>) -> DecodeResult<T>, - { - debug!("read_tuple()"); - self.read_seq(move |d, len| { - if len == tuple_len { - f(d) - } else { - Err(Expected(format!("Expected tuple of length `{}`, \ - found tuple of length `{}`", tuple_len, len))) - } - }) - } - - fn read_tuple_arg<T, F>(&mut self, idx: uint, f: F) -> DecodeResult<T> where - F: FnOnce(&mut Decoder<'doc>) -> DecodeResult<T>, - { - debug!("read_tuple_arg(idx={:?})", idx); - self.read_seq_elt(idx, f) - } - - fn read_tuple_struct<T, F>(&mut self, name: &str, len: uint, f: F) -> DecodeResult<T> where - F: FnOnce(&mut Decoder<'doc>) -> DecodeResult<T>, - { - debug!("read_tuple_struct(name={:?})", name); - self.read_tuple(len, f) - } - - fn read_tuple_struct_arg<T, F>(&mut self, - idx: uint, - f: F) - -> DecodeResult<T> where - F: FnOnce(&mut Decoder<'doc>) -> DecodeResult<T>, - { - debug!("read_tuple_struct_arg(idx={:?})", idx); - self.read_tuple_arg(idx, f) - } - - fn read_option<T, F>(&mut self, mut f: F) -> DecodeResult<T> where - F: FnMut(&mut Decoder<'doc>, bool) -> DecodeResult<T>, - { - debug!("read_option()"); - self.read_enum("Option", move |this| { - this.read_enum_variant(&["None", "Some"], move |this, idx| { - match idx { - 0 => f(this, false), - 1 => f(this, true), - _ => { - Err(Expected(format!("Expected None or Some"))) - } - } - }) - }) - } - - fn read_seq<T, F>(&mut self, f: F) -> DecodeResult<T> where - F: FnOnce(&mut Decoder<'doc>, uint) -> DecodeResult<T>, - { - debug!("read_seq()"); - self.push_doc(EsVec, move |d| { - let len = try!(d._next_uint(EsVecLen)); - debug!(" len={:?}", len); - f(d, len) - }) - } - - fn read_seq_elt<T, F>(&mut self, idx: uint, f: F) -> DecodeResult<T> where - F: FnOnce(&mut Decoder<'doc>) -> DecodeResult<T>, - { - debug!("read_seq_elt(idx={:?})", idx); - self.push_doc(EsVecElt, f) - } - - fn read_map<T, F>(&mut self, f: F) -> DecodeResult<T> where - F: FnOnce(&mut Decoder<'doc>, uint) -> DecodeResult<T>, - { - debug!("read_map()"); - self.push_doc(EsMap, move |d| { - let len = try!(d._next_uint(EsMapLen)); - debug!(" len={:?}", len); - f(d, len) - }) - } - - fn read_map_elt_key<T, F>(&mut self, idx: uint, f: F) -> DecodeResult<T> where - F: FnOnce(&mut Decoder<'doc>) -> DecodeResult<T>, - { - debug!("read_map_elt_key(idx={:?})", idx); - self.push_doc(EsMapKey, f) - } - - fn read_map_elt_val<T, F>(&mut self, idx: uint, f: F) -> DecodeResult<T> where - F: FnOnce(&mut Decoder<'doc>) -> DecodeResult<T>, - { - debug!("read_map_elt_val(idx={:?})", idx); - self.push_doc(EsMapVal, f) - } - - fn error(&mut self, err: &str) -> Error { - ApplicationError(err.to_string()) - } - } - - #[cfg(not(stage0))] impl<'doc> serialize::Decoder for Decoder<'doc> { type Error = Error; fn read_nil(&mut self) -> DecodeResult<()> { Ok(()) } @@ -1137,209 +871,6 @@ pub mod writer { } } - #[cfg(stage0)] - impl<'a, W: Writer + Seek> serialize::Encoder<io::IoError> for Encoder<'a, W> { - - fn emit_nil(&mut self) -> EncodeResult { - Ok(()) - } - - fn emit_uint(&mut self, v: uint) -> EncodeResult { - self.wr_tagged_u64(EsUint as uint, v as u64) - } - fn emit_u64(&mut self, v: u64) -> EncodeResult { - self.wr_tagged_u64(EsU64 as uint, v) - } - fn emit_u32(&mut self, v: u32) -> EncodeResult { - self.wr_tagged_u32(EsU32 as uint, v) - } - fn emit_u16(&mut self, v: u16) -> EncodeResult { - self.wr_tagged_u16(EsU16 as uint, v) - } - fn emit_u8(&mut self, v: u8) -> EncodeResult { - self.wr_tagged_u8(EsU8 as uint, v) - } - - fn emit_int(&mut self, v: int) -> EncodeResult { - self.wr_tagged_i64(EsInt as uint, v as i64) - } - fn emit_i64(&mut self, v: i64) -> EncodeResult { - self.wr_tagged_i64(EsI64 as uint, v) - } - fn emit_i32(&mut self, v: i32) -> EncodeResult { - self.wr_tagged_i32(EsI32 as uint, v) - } - fn emit_i16(&mut self, v: i16) -> EncodeResult { - self.wr_tagged_i16(EsI16 as uint, v) - } - fn emit_i8(&mut self, v: i8) -> EncodeResult { - self.wr_tagged_i8(EsI8 as uint, v) - } - - fn emit_bool(&mut self, v: bool) -> EncodeResult { - self.wr_tagged_u8(EsBool as uint, v as u8) - } - - fn emit_f64(&mut self, v: f64) -> EncodeResult { - let bits = unsafe { mem::transmute(v) }; - self.wr_tagged_u64(EsF64 as uint, bits) - } - fn emit_f32(&mut self, v: f32) -> EncodeResult { - let bits = unsafe { mem::transmute(v) }; - self.wr_tagged_u32(EsF32 as uint, bits) - } - fn emit_char(&mut self, v: char) -> EncodeResult { - self.wr_tagged_u32(EsChar as uint, v as u32) - } - - fn emit_str(&mut self, v: &str) -> EncodeResult { - self.wr_tagged_str(EsStr as uint, v) - } - - fn emit_enum<F>(&mut self, name: &str, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a, W>) -> EncodeResult, - { - try!(self._emit_label(name)); - try!(self.start_tag(EsEnum as uint)); - try!(f(self)); - self.end_tag() - } - - fn emit_enum_variant<F>(&mut self, - _: &str, - v_id: uint, - _: uint, - f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a, W>) -> EncodeResult, - { - try!(self._emit_tagged_uint(EsEnumVid, v_id)); - try!(self.start_tag(EsEnumBody as uint)); - try!(f(self)); - self.end_tag() - } - - fn emit_enum_variant_arg<F>(&mut self, _: uint, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a, W>) -> EncodeResult, - { - f(self) - } - - fn emit_enum_struct_variant<F>(&mut self, - v_name: &str, - v_id: uint, - cnt: uint, - f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a, W>) -> EncodeResult, - { - self.emit_enum_variant(v_name, v_id, cnt, f) - } - - fn emit_enum_struct_variant_field<F>(&mut self, - _: &str, - idx: uint, - f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a, W>) -> EncodeResult, - { - self.emit_enum_variant_arg(idx, f) - } - - fn emit_struct<F>(&mut self, _: &str, _len: uint, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a, W>) -> EncodeResult, - { - f(self) - } - - fn emit_struct_field<F>(&mut self, name: &str, _: uint, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a, W>) -> EncodeResult, - { - try!(self._emit_label(name)); - f(self) - } - - fn emit_tuple<F>(&mut self, len: uint, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a, W>) -> EncodeResult, - { - self.emit_seq(len, f) - } - fn emit_tuple_arg<F>(&mut self, idx: uint, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a, W>) -> EncodeResult, - { - self.emit_seq_elt(idx, f) - } - - fn emit_tuple_struct<F>(&mut self, _: &str, len: uint, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a, W>) -> EncodeResult, - { - self.emit_seq(len, f) - } - fn emit_tuple_struct_arg<F>(&mut self, idx: uint, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a, W>) -> EncodeResult, - { - self.emit_seq_elt(idx, f) - } - - fn emit_option<F>(&mut self, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a, W>) -> EncodeResult, - { - self.emit_enum("Option", f) - } - fn emit_option_none(&mut self) -> EncodeResult { - self.emit_enum_variant("None", 0, 0, |_| Ok(())) - } - fn emit_option_some<F>(&mut self, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a, W>) -> EncodeResult, - { - - self.emit_enum_variant("Some", 1, 1, f) - } - - fn emit_seq<F>(&mut self, len: uint, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a, W>) -> EncodeResult, - { - - try!(self.start_tag(EsVec as uint)); - try!(self._emit_tagged_uint(EsVecLen, len)); - try!(f(self)); - self.end_tag() - } - - fn emit_seq_elt<F>(&mut self, _idx: uint, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a, W>) -> EncodeResult, - { - - try!(self.start_tag(EsVecElt as uint)); - try!(f(self)); - self.end_tag() - } - - fn emit_map<F>(&mut self, len: uint, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a, W>) -> EncodeResult, - { - - try!(self.start_tag(EsMap as uint)); - try!(self._emit_tagged_uint(EsMapLen, len)); - try!(f(self)); - self.end_tag() - } - - fn emit_map_elt_key<F>(&mut self, _idx: uint, mut f: F) -> EncodeResult where - F: FnMut(&mut Encoder<'a, W>) -> EncodeResult, - { - - try!(self.start_tag(EsMapKey as uint)); - try!(f(self)); - self.end_tag() - } - - fn emit_map_elt_val<F>(&mut self, _idx: uint, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a, W>) -> EncodeResult, - { - try!(self.start_tag(EsMapVal as uint)); - try!(f(self)); - self.end_tag() - } - } - #[cfg(not(stage0))] impl<'a, W: Writer + Seek> serialize::Encoder for Encoder<'a, W> { type Error = io::IoError; diff --git a/src/libregex/lib.rs b/src/libregex/lib.rs index 92ed048bae0..c039abc9aff 100644 --- a/src/libregex/lib.rs +++ b/src/libregex/lib.rs @@ -23,9 +23,7 @@ html_playground_url = "http://play.rust-lang.org/")] #![allow(unknown_features)] -#![feature(macro_rules, slicing_syntax, globs)] -#![feature(unboxed_closures)] -#![feature(associated_types)] +#![feature(slicing_syntax)] #![deny(missing_docs)] #[cfg(test)] diff --git a/src/librustc/lib.rs b/src/librustc/lib.rs index 3ed712b15df..0aebbf2970f 100644 --- a/src/librustc/lib.rs +++ b/src/librustc/lib.rs @@ -22,13 +22,9 @@ html_favicon_url = "http://www.rust-lang.org/favicon.ico", html_root_url = "http://doc.rust-lang.org/nightly/")] -#![allow(unknown_features)] -#![feature(default_type_params, globs, macro_rules, phase, quote)] +#![feature(quote)] #![feature(slicing_syntax, unsafe_destructor)] #![feature(rustc_diagnostic_macros)] -#![feature(unboxed_closures)] -#![feature(old_orphan_check)] -#![feature(associated_types)] extern crate arena; extern crate flate; @@ -40,22 +36,8 @@ extern crate rustc_back; extern crate serialize; extern crate rbml; extern crate collections; - -#[cfg(stage0)] -#[phase(plugin, link)] -extern crate log; - -#[cfg(not(stage0))] -#[macro_use] -extern crate log; - -#[cfg(stage0)] -#[phase(plugin, link)] -extern crate syntax; - -#[cfg(not(stage0))] -#[macro_use] -extern crate syntax; +#[macro_use] extern crate log; +#[macro_use] extern crate syntax; extern crate "serialize" as rustc_serialize; // used by deriving diff --git a/src/librustc/metadata/decoder.rs b/src/librustc/metadata/decoder.rs index 25ce8a6889d..9e71c867efa 100644 --- a/src/librustc/metadata/decoder.rs +++ b/src/librustc/metadata/decoder.rs @@ -675,14 +675,14 @@ pub fn maybe_get_item_ast<'tcx>(cdata: Cmd, tcx: &ty::ctxt<'tcx>, id: ast::NodeI debug!("Looking up item: {}", id); let item_doc = lookup_item(id, cdata.data()); let path = item_path(item_doc).init().to_vec(); - match decode_inlined_item.call_mut((cdata, tcx, path, item_doc)) { + match decode_inlined_item(cdata, tcx, path, item_doc) { Ok(ii) => csearch::found(ii), Err(path) => { match item_parent_item(item_doc) { Some(did) => { let did = translate_def_id(cdata, did); let parent_item = lookup_item(did.node, cdata.data()); - match decode_inlined_item.call_mut((cdata, tcx, path, parent_item)) { + match decode_inlined_item(cdata, tcx, path, parent_item) { Ok(ii) => csearch::found_parent(did, ii), Err(_) => csearch::not_found } diff --git a/src/librustc/metadata/encoder.rs b/src/librustc/metadata/encoder.rs index a13df5a220f..28ad36194ef 100644 --- a/src/librustc/metadata/encoder.rs +++ b/src/librustc/metadata/encoder.rs @@ -953,7 +953,7 @@ fn encode_inlined_item(ecx: &EncodeContext, ii: InlinedItemRef) { let mut eii = ecx.encode_inlined_item.borrow_mut(); let eii: &mut EncodeInlinedItem = &mut *eii; - eii.call_mut((ecx, rbml_w, ii)) + eii(ecx, rbml_w, ii) } const FN_FAMILY: char = 'f'; diff --git a/src/librustc/middle/astencode.rs b/src/librustc/middle/astencode.rs index 39e15832bb7..32ce131c57a 100644 --- a/src/librustc/middle/astencode.rs +++ b/src/librustc/middle/astencode.rs @@ -263,14 +263,6 @@ trait def_id_encoder_helpers { fn emit_def_id(&mut self, did: ast::DefId); } -#[cfg(stage0)] -impl<S:serialize::Encoder<E>, E> def_id_encoder_helpers for S { - fn emit_def_id(&mut self, did: ast::DefId) { - did.encode(self).ok().unwrap() - } -} - -#[cfg(not(stage0))] impl<S:serialize::Encoder> def_id_encoder_helpers for S { fn emit_def_id(&mut self, did: ast::DefId) { did.encode(self).ok().unwrap() @@ -283,21 +275,6 @@ trait def_id_decoder_helpers { cdata: &cstore::crate_metadata) -> ast::DefId; } -#[cfg(stage0)] -impl<D:serialize::Decoder<E>, E> def_id_decoder_helpers for D { - fn read_def_id(&mut self, dcx: &DecodeContext) -> ast::DefId { - let did: ast::DefId = Decodable::decode(self).ok().unwrap(); - did.tr(dcx) - } - - fn read_def_id_nodcx(&mut self, - cdata: &cstore::crate_metadata) -> ast::DefId { - let did: ast::DefId = Decodable::decode(self).ok().unwrap(); - decoder::translate_def_id(cdata, did) - } -} - -#[cfg(not(stage0))] impl<D:serialize::Decoder> def_id_decoder_helpers for D { fn read_def_id(&mut self, dcx: &DecodeContext) -> ast::DefId { let did: ast::DefId = Decodable::decode(self).ok().unwrap(); diff --git a/src/librustc/middle/intrinsicck.rs b/src/librustc/middle/intrinsicck.rs index 41f293cfca5..bd96a8a0f2c 100644 --- a/src/librustc/middle/intrinsicck.rs +++ b/src/librustc/middle/intrinsicck.rs @@ -205,7 +205,7 @@ impl<'a, 'tcx> IntrinsicCheckingVisitor<'a, 'tcx> { debug!("with_each_combination(substs={})", substs.repr(self.tcx)); - callback.call_mut((substs,)); + callback(substs); } Some((space, index, ¶m_ty)) => { diff --git a/src/librustc/middle/ty_fold.rs b/src/librustc/middle/ty_fold.rs index 1bc0d709458..15c2845882c 100644 --- a/src/librustc/middle/ty_fold.rs +++ b/src/librustc/middle/ty_fold.rs @@ -844,7 +844,7 @@ impl<'a, 'tcx> TypeFolder<'tcx> for RegionFolder<'a, 'tcx> _ => { debug!("RegionFolder.fold_region({}) folding free region (current_depth={})", r.repr(self.tcx()), self.current_depth); - self.fld_r.call_mut((r, self.current_depth)) + (self.fld_r)(r, self.current_depth) } } } diff --git a/src/librustc_back/lib.rs b/src/librustc_back/lib.rs index 238c84e88a9..ca39477fbdc 100644 --- a/src/librustc_back/lib.rs +++ b/src/librustc_back/lib.rs @@ -28,22 +28,11 @@ #![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png", html_favicon_url = "http://www.rust-lang.org/favicon.ico", html_root_url = "http://doc.rust-lang.org/nightly/")] - -#![allow(unknown_features)] -#![feature(globs, phase, macro_rules, slicing_syntax)] -#![feature(unboxed_closures)] -#![feature(old_orphan_check)] - -#[cfg(stage0)] -#[phase(plugin, link)] -extern crate log; - -#[cfg(not(stage0))] -#[macro_use] -extern crate log; +#![feature(slicing_syntax)] extern crate syntax; extern crate serialize; +#[macro_use] extern crate log; pub mod abi; pub mod archive; diff --git a/src/librustc_borrowck/lib.rs b/src/librustc_borrowck/lib.rs index 0600ddba018..26bcd5f4c10 100644 --- a/src/librustc_borrowck/lib.rs +++ b/src/librustc_borrowck/lib.rs @@ -17,28 +17,13 @@ html_root_url = "http://doc.rust-lang.org/nightly/")] #![allow(unknown_features)] -#![feature(default_type_params, globs, macro_rules, phase, quote)] +#![feature(quote)] #![feature(slicing_syntax, unsafe_destructor)] #![feature(rustc_diagnostic_macros)] -#![feature(unboxed_closures)] -#![feature(old_orphan_check)] #![allow(non_camel_case_types)] -#[cfg(stage0)] -#[phase(plugin, link)] -extern crate log; - -#[cfg(not(stage0))] -#[macro_use] -extern crate log; - -#[cfg(stage0)] -#[phase(plugin, link)] -extern crate syntax; - -#[cfg(not(stage0))] -#[macro_use] -extern crate syntax; +#[macro_use] extern crate log; +#[macro_use] extern crate syntax; // for "clarity", rename the graphviz crate to dot; graphviz within `borrowck` // refers to the borrowck-specific graphviz adapter traits. diff --git a/src/librustc_driver/lib.rs b/src/librustc_driver/lib.rs index 3fd5198ee4c..4164a8551cc 100644 --- a/src/librustc_driver/lib.rs +++ b/src/librustc_driver/lib.rs @@ -22,11 +22,9 @@ html_favicon_url = "http://www.rust-lang.org/favicon.ico", html_root_url = "http://doc.rust-lang.org/nightly/")] -#![feature(default_type_params, globs, macro_rules, phase, quote)] +#![feature(quote)] #![feature(slicing_syntax, unsafe_destructor)] #![feature(rustc_diagnostic_macros)] -#![feature(unboxed_closures)] -#![feature(associated_types)] extern crate arena; extern crate flate; @@ -41,22 +39,8 @@ extern crate rustc_trans; extern crate rustc_typeck; extern crate serialize; extern crate "rustc_llvm" as llvm; - -#[cfg(stage0)] -#[phase(plugin, link)] -extern crate log; - -#[cfg(not(stage0))] -#[macro_use] -extern crate log; - -#[cfg(stage0)] -#[phase(plugin, link)] -extern crate syntax; - -#[cfg(not(stage0))] -#[macro_use] -extern crate syntax; +#[macro_use] extern crate log; +#[macro_use] extern crate syntax; pub use syntax::diagnostic; diff --git a/src/librustc_llvm/lib.rs b/src/librustc_llvm/lib.rs index 8a9334be985..0bed754aa3c 100644 --- a/src/librustc_llvm/lib.rs +++ b/src/librustc_llvm/lib.rs @@ -21,10 +21,7 @@ html_favicon_url = "http://www.rust-lang.org/favicon.ico", html_root_url = "http://doc.rust-lang.org/nightly/")] -#![feature(globs)] #![feature(link_args)] -#![feature(unboxed_closures)] -#![feature(old_orphan_check)] extern crate libc; diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index dc13f2e331b..653cdb1c42c 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -16,26 +16,11 @@ html_favicon_url = "http://www.rust-lang.org/favicon.ico", html_root_url = "http://doc.rust-lang.org/nightly/")] -#![feature(globs, phase, slicing_syntax)] +#![feature(slicing_syntax)] #![feature(rustc_diagnostic_macros)] -#![feature(associated_types)] -#![feature(old_orphan_check)] -#[cfg(stage0)] -#[phase(plugin, link)] -extern crate log; - -#[cfg(not(stage0))] -#[macro_use] -extern crate log; - -#[cfg(stage0)] -#[phase(plugin, link)] -extern crate syntax; - -#[cfg(not(stage0))] -#[macro_use] -extern crate syntax; +#[macro_use] extern crate log; +#[macro_use] extern crate syntax; extern crate rustc; diff --git a/src/librustc_trans/lib.rs b/src/librustc_trans/lib.rs index 705fecf4d19..b6f90a4c2f5 100644 --- a/src/librustc_trans/lib.rs +++ b/src/librustc_trans/lib.rs @@ -22,13 +22,9 @@ html_favicon_url = "http://www.rust-lang.org/favicon.ico", html_root_url = "http://doc.rust-lang.org/nightly/")] -#![allow(unknown_features)] -#![feature(default_type_params, globs, macro_rules, phase, quote)] +#![feature(quote)] #![feature(slicing_syntax, unsafe_destructor)] #![feature(rustc_diagnostic_macros)] -#![feature(unboxed_closures)] -#![feature(old_orphan_check)] -#![feature(associated_types)] extern crate arena; extern crate flate; @@ -40,21 +36,8 @@ extern crate rustc_back; extern crate serialize; extern crate "rustc_llvm" as llvm; -#[cfg(stage0)] -#[phase(plugin, link)] -extern crate log; - -#[cfg(not(stage0))] -#[macro_use] -extern crate log; - -#[cfg(stage0)] -#[phase(plugin, link)] -extern crate syntax; - -#[cfg(not(stage0))] -#[macro_use] -extern crate syntax; +#[macro_use] extern crate log; +#[macro_use] extern crate syntax; pub use rustc::session; pub use rustc::metadata; diff --git a/src/librustc_trans/trans/mod.rs b/src/librustc_trans/trans/mod.rs index fa9cd5a698b..42134789546 100644 --- a/src/librustc_trans/trans/mod.rs +++ b/src/librustc_trans/trans/mod.rs @@ -16,8 +16,7 @@ pub use self::base::trans_crate; pub use self::context::CrateContext; pub use self::common::gensym_name; -#[cfg_attr(stage0, macro_escape)] -#[cfg_attr(not(stage0), macro_use)] +#[macro_use] mod macros; mod doc; diff --git a/src/librustc_typeck/lib.rs b/src/librustc_typeck/lib.rs index f7abd8a1cc6..ae8731dfa47 100644 --- a/src/librustc_typeck/lib.rs +++ b/src/librustc_typeck/lib.rs @@ -71,27 +71,13 @@ This API is completely unstable and subject to change. html_favicon_url = "http://www.rust-lang.org/favicon.ico", html_root_url = "http://doc.rust-lang.org/nightly/")] -#![feature(default_type_params, globs, macro_rules, phase, quote)] +#![feature(quote)] #![feature(slicing_syntax, unsafe_destructor)] #![feature(rustc_diagnostic_macros)] -#![feature(unboxed_closures)] #![allow(non_camel_case_types)] -#[cfg(stage0)] -#[phase(plugin, link)] -extern crate log; - -#[cfg(not(stage0))] -#[macro_use] -extern crate log; - -#[cfg(stage0)] -#[phase(plugin, link)] -extern crate syntax; - -#[cfg(not(stage0))] -#[macro_use] -extern crate syntax; +#[macro_use] extern crate log; +#[macro_use] extern crate syntax; extern crate arena; extern crate rustc; diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index 99c5fde47ea..6c6c7c4d378 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -16,12 +16,7 @@ html_favicon_url = "http://www.rust-lang.org/favicon.ico", html_root_url = "http://doc.rust-lang.org/nightly/", html_playground_url = "http://play.rust-lang.org/")] - -#![allow(unknown_features)] -#![feature(globs, macro_rules, phase, slicing_syntax)] -#![feature(unboxed_closures)] -#![feature(old_orphan_check)] -#![feature(associated_types)] +#![feature(slicing_syntax)] extern crate arena; extern crate getopts; @@ -32,14 +27,7 @@ extern crate rustc_driver; extern crate serialize; extern crate syntax; extern crate "test" as testing; - -#[cfg(stage0)] -#[phase(plugin, link)] -extern crate log; - -#[cfg(not(stage0))] -#[macro_use] -extern crate log; +#[macro_use] extern crate log; extern crate "serialize" as rustc_serialize; // used by deriving @@ -56,8 +44,7 @@ use rustc::session::search_paths::SearchPaths; // reexported from `clean` so it can be easily updated with the mod itself pub use clean::SCHEMA_VERSION; -#[cfg_attr(stage0, macro_escape)] -#[cfg_attr(not(stage0), macro_use)] +#[macro_use] pub mod externalfiles; pub mod clean; diff --git a/src/libserialize/collection_impls_stage0.rs b/src/libserialize/collection_impls_stage0.rs deleted file mode 100644 index f2d79b13346..00000000000 --- a/src/libserialize/collection_impls_stage0.rs +++ /dev/null @@ -1,289 +0,0 @@ -// 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. - -//! Implementations of serialization for structures found in libcollections - -use std::uint; -use std::default::Default; -use std::hash::{Hash, Hasher}; - -use {Decodable, Encodable, Decoder, Encoder}; -use std::collections::{DList, RingBuf, BTreeMap, BTreeSet, HashMap, HashSet, VecMap}; -use collections::enum_set::{EnumSet, CLike}; - -impl< - E, - S: Encoder<E>, - T: Encodable<S, E> -> Encodable<S, E> for DList<T> { - fn encode(&self, s: &mut S) -> Result<(), E> { - s.emit_seq(self.len(), |s| { - for (i, e) in self.iter().enumerate() { - try!(s.emit_seq_elt(i, |s| e.encode(s))); - } - Ok(()) - }) - } -} - -impl<E, D:Decoder<E>,T:Decodable<D, E>> Decodable<D, E> for DList<T> { - fn decode(d: &mut D) -> Result<DList<T>, E> { - d.read_seq(|d, len| { - let mut list = DList::new(); - for i in range(0u, len) { - list.push_back(try!(d.read_seq_elt(i, |d| Decodable::decode(d)))); - } - Ok(list) - }) - } -} - -impl< - E, - S: Encoder<E>, - T: Encodable<S, E> -> Encodable<S, E> for RingBuf<T> { - fn encode(&self, s: &mut S) -> Result<(), E> { - s.emit_seq(self.len(), |s| { - for (i, e) in self.iter().enumerate() { - try!(s.emit_seq_elt(i, |s| e.encode(s))); - } - Ok(()) - }) - } -} - -impl<E, D:Decoder<E>,T:Decodable<D, E>> Decodable<D, E> for RingBuf<T> { - fn decode(d: &mut D) -> Result<RingBuf<T>, E> { - d.read_seq(|d, len| { - let mut deque: RingBuf<T> = RingBuf::new(); - for i in range(0u, len) { - deque.push_back(try!(d.read_seq_elt(i, |d| Decodable::decode(d)))); - } - Ok(deque) - }) - } -} - -impl< - E, - S: Encoder<E>, - K: Encodable<S, E> + PartialEq + Ord, - V: Encodable<S, E> + PartialEq -> Encodable<S, E> for BTreeMap<K, V> { - fn encode(&self, e: &mut S) -> Result<(), E> { - e.emit_map(self.len(), |e| { - let mut i = 0; - for (key, val) in self.iter() { - try!(e.emit_map_elt_key(i, |e| key.encode(e))); - try!(e.emit_map_elt_val(i, |e| val.encode(e))); - i += 1; - } - Ok(()) - }) - } -} - -impl< - E, - D: Decoder<E>, - K: Decodable<D, E> + PartialEq + Ord, - V: Decodable<D, E> + PartialEq -> Decodable<D, E> for BTreeMap<K, V> { - fn decode(d: &mut D) -> Result<BTreeMap<K, V>, E> { - d.read_map(|d, len| { - let mut map = BTreeMap::new(); - for i in range(0u, len) { - let key = try!(d.read_map_elt_key(i, |d| Decodable::decode(d))); - let val = try!(d.read_map_elt_val(i, |d| Decodable::decode(d))); - map.insert(key, val); - } - Ok(map) - }) - } -} - -impl< - E, - S: Encoder<E>, - T: Encodable<S, E> + PartialEq + Ord -> Encodable<S, E> for BTreeSet<T> { - fn encode(&self, s: &mut S) -> Result<(), E> { - s.emit_seq(self.len(), |s| { - let mut i = 0; - for e in self.iter() { - try!(s.emit_seq_elt(i, |s| e.encode(s))); - i += 1; - } - Ok(()) - }) - } -} - -impl< - E, - D: Decoder<E>, - T: Decodable<D, E> + PartialEq + Ord -> Decodable<D, E> for BTreeSet<T> { - fn decode(d: &mut D) -> Result<BTreeSet<T>, E> { - d.read_seq(|d, len| { - let mut set = BTreeSet::new(); - for i in range(0u, len) { - set.insert(try!(d.read_seq_elt(i, |d| Decodable::decode(d)))); - } - Ok(set) - }) - } -} - -impl< - E, - S: Encoder<E>, - T: Encodable<S, E> + CLike -> Encodable<S, E> for EnumSet<T> { - fn encode(&self, s: &mut S) -> Result<(), E> { - let mut bits = 0; - for item in self.iter() { - bits |= item.to_uint(); - } - s.emit_uint(bits) - } -} - -impl< - E, - D: Decoder<E>, - T: Decodable<D, E> + CLike -> Decodable<D, E> for EnumSet<T> { - fn decode(d: &mut D) -> Result<EnumSet<T>, E> { - let bits = try!(d.read_uint()); - let mut set = EnumSet::new(); - for bit in range(0, uint::BITS) { - if bits & (1 << bit) != 0 { - set.insert(CLike::from_uint(1 << bit)); - } - } - Ok(set) - } -} - -impl< - E, - S: Encoder<E>, - K: Encodable<S, E> + Hash<X> + Eq, - V: Encodable<S, E>, - X, - H: Hasher<X> -> Encodable<S, E> for HashMap<K, V, H> { - fn encode(&self, e: &mut S) -> Result<(), E> { - e.emit_map(self.len(), |e| { - let mut i = 0; - for (key, val) in self.iter() { - try!(e.emit_map_elt_key(i, |e| key.encode(e))); - try!(e.emit_map_elt_val(i, |e| val.encode(e))); - i += 1; - } - Ok(()) - }) - } -} - -impl< - E, - D: Decoder<E>, - K: Decodable<D, E> + Hash<S> + Eq, - V: Decodable<D, E>, - S, - H: Hasher<S> + Default -> Decodable<D, E> for HashMap<K, V, H> { - fn decode(d: &mut D) -> Result<HashMap<K, V, H>, E> { - d.read_map(|d, len| { - let hasher = Default::default(); - let mut map = HashMap::with_capacity_and_hasher(len, hasher); - for i in range(0u, len) { - let key = try!(d.read_map_elt_key(i, |d| Decodable::decode(d))); - let val = try!(d.read_map_elt_val(i, |d| Decodable::decode(d))); - map.insert(key, val); - } - Ok(map) - }) - } -} - -impl< - E, - S: Encoder<E>, - T: Encodable<S, E> + Hash<X> + Eq, - X, - H: Hasher<X> -> Encodable<S, E> for HashSet<T, H> { - fn encode(&self, s: &mut S) -> Result<(), E> { - s.emit_seq(self.len(), |s| { - let mut i = 0; - for e in self.iter() { - try!(s.emit_seq_elt(i, |s| e.encode(s))); - i += 1; - } - Ok(()) - }) - } -} - -impl< - E, - D: Decoder<E>, - T: Decodable<D, E> + Hash<S> + Eq, - S, - H: Hasher<S> + Default -> Decodable<D, E> for HashSet<T, H> { - fn decode(d: &mut D) -> Result<HashSet<T, H>, E> { - d.read_seq(|d, len| { - let mut set = HashSet::with_capacity_and_hasher(len, Default::default()); - for i in range(0u, len) { - set.insert(try!(d.read_seq_elt(i, |d| Decodable::decode(d)))); - } - Ok(set) - }) - } -} - -impl< - E, - S: Encoder<E>, - V: Encodable<S, E> -> Encodable<S, E> for VecMap<V> { - fn encode(&self, e: &mut S) -> Result<(), E> { - e.emit_map(self.len(), |e| { - for (i, (key, val)) in self.iter().enumerate() { - try!(e.emit_map_elt_key(i, |e| key.encode(e))); - try!(e.emit_map_elt_val(i, |e| val.encode(e))); - } - Ok(()) - }) - } -} - -impl< - E, - D: Decoder<E>, - V: Decodable<D, E> -> Decodable<D, E> for VecMap<V> { - fn decode(d: &mut D) -> Result<VecMap<V>, E> { - d.read_map(|d, len| { - let mut map = VecMap::new(); - for i in range(0u, len) { - let key = try!(d.read_map_elt_key(i, |d| Decodable::decode(d))); - let val = try!(d.read_map_elt_val(i, |d| Decodable::decode(d))); - map.insert(key, val); - } - Ok(map) - }) - } -} diff --git a/src/libserialize/json_stage0.rs b/src/libserialize/json_stage0.rs deleted file mode 100644 index 1c2855f6745..00000000000 --- a/src/libserialize/json_stage0.rs +++ /dev/null @@ -1,3864 +0,0 @@ -// Copyright 2012-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. - -// Rust JSON serialization library -// Copyright (c) 2011 Google Inc. - -#![forbid(non_camel_case_types)] -#![allow(missing_docs)] - -//! JSON parsing and serialization -//! -//! # What is JSON? -//! -//! JSON (JavaScript Object Notation) is a way to write data in Javascript. -//! Like XML, it allows to encode structured data in a text format that can be easily read by humans -//! Its simple syntax and native compatibility with JavaScript have made it a widely used format. -//! -//! Data types that can be encoded are JavaScript types (see the `Json` enum for more details): -//! -//! * `Boolean`: equivalent to rust's `bool` -//! * `Number`: equivalent to rust's `f64` -//! * `String`: equivalent to rust's `String` -//! * `Array`: equivalent to rust's `Vec<T>`, but also allowing objects of different types in the -//! same array -//! * `Object`: equivalent to rust's `BTreeMap<String, json::Json>` -//! * `Null` -//! -//! An object is a series of string keys mapping to values, in `"key": value` format. -//! Arrays are enclosed in square brackets ([ ... ]) and objects in curly brackets ({ ... }). -//! A simple JSON document encoding a person, his/her age, address and phone numbers could look like -//! -//! ```ignore -//! { -//! "FirstName": "John", -//! "LastName": "Doe", -//! "Age": 43, -//! "Address": { -//! "Street": "Downing Street 10", -//! "City": "London", -//! "Country": "Great Britain" -//! }, -//! "PhoneNumbers": [ -//! "+44 1234567", -//! "+44 2345678" -//! ] -//! } -//! ``` -//! -//! # Rust Type-based Encoding and Decoding -//! -//! Rust provides a mechanism for low boilerplate encoding & decoding of values to and from JSON via -//! the serialization API. -//! To be able to encode a piece of data, it must implement the `serialize::RustcEncodable` trait. -//! To be able to decode a piece of data, it must implement the `serialize::RustcDecodable` trait. -//! The Rust compiler provides an annotation to automatically generate the code for these traits: -//! `#[derive(RustcDecodable, RustcEncodable)]` -//! -//! The JSON API provides an enum `json::Json` and a trait `ToJson` to encode objects. -//! The `ToJson` trait provides a `to_json` method to convert an object into a `json::Json` value. -//! A `json::Json` value can be encoded as a string or buffer using the functions described above. -//! You can also use the `json::Encoder` object, which implements the `Encoder` trait. -//! -//! When using `ToJson` the `RustcEncodable` trait implementation is not mandatory. -//! -//! # Examples of use -//! -//! ## Using Autoserialization -//! -//! Create a struct called `TestStruct` and serialize and deserialize it to and from JSON using the -//! serialization API, using the derived serialization code. -//! -//! ```notrust -//! // FIXME(#19470): this cannot be ```rust``` because it fails orphan checking at the moment -//! extern crate serialize; -//! use serialize::json; -//! -//! // Automatically generate `Decodable` and `Encodable` trait implementations -//! #[derive(RustcDecodable, RustcEncodable)] -//! pub struct TestStruct { -//! data_int: u8, -//! data_str: String, -//! data_vector: Vec<u8>, -//! } -//! -//! fn main() { -//! let object = TestStruct { -//! data_int: 1, -//! data_str: "homura".to_string(), -//! data_vector: vec![2,3,4,5], -//! }; -//! -//! // Serialize using `json::encode` -//! let encoded = json::encode(&object); -//! -//! // Deserialize using `json::decode` -//! let decoded: TestStruct = json::decode(encoded.as_slice()).unwrap(); -//! } -//! ``` -//! -//! ## Using the `ToJson` trait -//! -//! The examples above use the `ToJson` trait to generate the JSON string, which is required -//! for custom mappings. -//! -//! ### Simple example of `ToJson` usage -//! -//! ```notrust -//! // FIXME(#19470): this cannot be ```rust``` because it fails orphan checking at the moment -//! extern crate serialize; -//! use serialize::json::{mod, ToJson, Json}; -//! -//! // A custom data structure -//! struct ComplexNum { -//! a: f64, -//! b: f64, -//! } -//! -//! // JSON value representation -//! impl ToJson for ComplexNum { -//! fn to_json(&self) -> Json { -//! Json::String(format!("{}+{}i", self.a, self.b)) -//! } -//! } -//! -//! // Only generate `RustcEncodable` trait implementation -//! #[derive(Encodable)] -//! pub struct ComplexNumRecord { -//! uid: u8, -//! dsc: String, -//! val: Json, -//! } -//! -//! fn main() { -//! let num = ComplexNum { a: 0.0001, b: 12.539 }; -//! let data: String = json::encode(&ComplexNumRecord{ -//! uid: 1, -//! dsc: "test".to_string(), -//! val: num.to_json(), -//! }); -//! println!("data: {}", data); -//! // data: {"uid":1,"dsc":"test","val":"0.0001+12.539j"}; -//! } -//! ``` -//! -//! ### Verbose example of `ToJson` usage -//! -//! ```notrust -//! // FIXME(#19470): this cannot be ```rust``` because it fails orphan checking at the moment -//! extern crate serialize; -//! use std::collections::BTreeMap; -//! use serialize::json::{mod, Json, ToJson}; -//! -//! // Only generate `Decodable` trait implementation -//! #[derive(Decodable)] -//! pub struct TestStruct { -//! data_int: u8, -//! data_str: String, -//! data_vector: Vec<u8>, -//! } -//! -//! // Specify encoding method manually -//! impl ToJson for TestStruct { -//! fn to_json(&self) -> Json { -//! let mut d = BTreeMap::new(); -//! // All standard types implement `to_json()`, so use it -//! d.insert("data_int".to_string(), self.data_int.to_json()); -//! d.insert("data_str".to_string(), self.data_str.to_json()); -//! d.insert("data_vector".to_string(), self.data_vector.to_json()); -//! Json::Object(d) -//! } -//! } -//! -//! fn main() { -//! // Serialize using `ToJson` -//! let input_data = TestStruct { -//! data_int: 1, -//! data_str: "madoka".to_string(), -//! data_vector: vec![2,3,4,5], -//! }; -//! let json_obj: Json = input_data.to_json(); -//! let json_str: String = json_obj.to_string(); -//! -//! // Deserialize like before -//! let decoded: TestStruct = json::decode(json_str.as_slice()).unwrap(); -//! } -//! ``` - -use self::JsonEvent::*; -use self::StackElement::*; -use self::ErrorCode::*; -use self::ParserError::*; -use self::DecoderError::*; -use self::ParserState::*; -use self::InternalStackElement::*; - -use std; -use std::collections::{HashMap, BTreeMap}; -use std::{char, f64, fmt, io, num, str}; -use std::mem::{swap, transmute}; -use std::num::{Float, Int}; -use std::num::FpCategory as Fp; -use std::str::FromStr; -use std::string; -use std::ops; -use unicode::str as unicode_str; -use unicode::str::Utf16Item; -use std::ops::Index as IndexOp; - -use Encodable; - -/// Represents a json value -#[derive(Clone, PartialEq, PartialOrd)] -pub enum Json { - I64(i64), - U64(u64), - F64(f64), - String(string::String), - Boolean(bool), - Array(self::Array), - Object(self::Object), - Null, -} - -pub type Array = Vec<Json>; -pub type Object = BTreeMap<string::String, Json>; - -pub struct PrettyJson<'a> { inner: &'a Json } - -pub struct AsJson<'a, T: 'a> { inner: &'a T } -pub struct AsPrettyJson<'a, T: 'a> { inner: &'a T, indent: Option<uint> } - -/// The errors that can arise while parsing a JSON stream. -#[derive(Clone, Copy, PartialEq)] -pub enum ErrorCode { - InvalidSyntax, - InvalidNumber, - EOFWhileParsingObject, - EOFWhileParsingArray, - EOFWhileParsingValue, - EOFWhileParsingString, - KeyMustBeAString, - ExpectedColon, - TrailingCharacters, - TrailingComma, - InvalidEscape, - InvalidUnicodeCodePoint, - LoneLeadingSurrogateInHexEscape, - UnexpectedEndOfHexEscape, - UnrecognizedHex, - NotFourDigit, - NotUtf8, -} - -#[derive(Clone, Copy, PartialEq, Show)] -pub enum ParserError { - /// msg, line, col - SyntaxError(ErrorCode, uint, uint), - IoError(io::IoErrorKind, &'static str), -} - -// Builder and Parser have the same errors. -pub type BuilderError = ParserError; - -#[derive(Clone, PartialEq, Show)] -pub enum DecoderError { - ParseError(ParserError), - ExpectedError(string::String, string::String), - MissingFieldError(string::String), - UnknownVariantError(string::String), - ApplicationError(string::String) -} - -/// Returns a readable error string for a given error code. -pub fn error_str(error: ErrorCode) -> &'static str { - match error { - InvalidSyntax => "invalid syntax", - InvalidNumber => "invalid number", - EOFWhileParsingObject => "EOF While parsing object", - EOFWhileParsingArray => "EOF While parsing array", - EOFWhileParsingValue => "EOF While parsing value", - EOFWhileParsingString => "EOF While parsing string", - KeyMustBeAString => "key must be a string", - ExpectedColon => "expected `:`", - TrailingCharacters => "trailing characters", - TrailingComma => "trailing comma", - InvalidEscape => "invalid escape", - UnrecognizedHex => "invalid \\u{ esc}ape (unrecognized hex)", - NotFourDigit => "invalid \\u{ esc}ape (not four digits)", - NotUtf8 => "contents not utf-8", - InvalidUnicodeCodePoint => "invalid Unicode code point", - LoneLeadingSurrogateInHexEscape => "lone leading surrogate in hex escape", - UnexpectedEndOfHexEscape => "unexpected end of hex escape", - } -} - -/// Shortcut function to decode a JSON `&str` into an object -pub fn decode<T: ::Decodable<Decoder, DecoderError>>(s: &str) -> DecodeResult<T> { - let json = match from_str(s) { - Ok(x) => x, - Err(e) => return Err(ParseError(e)) - }; - - let mut decoder = Decoder::new(json); - ::Decodable::decode(&mut decoder) -} - -/// Shortcut function to encode a `T` into a JSON `String` -pub fn encode<T>(object: &T) -> string::String - where T: for<'a> Encodable<Encoder<'a>, fmt::Error> -{ - let mut s = String::new(); - { - let mut encoder = Encoder::new(&mut s); - let _ = object.encode(&mut encoder); - } - s -} - -impl fmt::Show for ErrorCode { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - error_str(*self).fmt(f) - } -} - -fn io_error_to_error(io: io::IoError) -> ParserError { - IoError(io.kind, io.desc) -} - -impl std::error::Error for DecoderError { - fn description(&self) -> &str { "decoder error" } - fn detail(&self) -> Option<std::string::String> { Some(self.to_string()) } -} - -pub type EncodeResult = fmt::Result; -pub type DecodeResult<T> = Result<T, DecoderError>; - -fn escape_str(wr: &mut fmt::Writer, v: &str) -> fmt::Result { - try!(wr.write_str("\"")); - - let mut start = 0; - - for (i, byte) in v.bytes().enumerate() { - let escaped = match byte { - b'"' => "\\\"", - b'\\' => "\\\\", - b'\x00' => "\\u0000", - b'\x01' => "\\u0001", - b'\x02' => "\\u0002", - b'\x03' => "\\u0003", - b'\x04' => "\\u0004", - b'\x05' => "\\u0005", - b'\x06' => "\\u0006", - b'\x07' => "\\u0007", - b'\x08' => "\\b", - b'\t' => "\\t", - b'\n' => "\\n", - b'\x0b' => "\\u000b", - b'\x0c' => "\\f", - b'\r' => "\\r", - b'\x0e' => "\\u000e", - b'\x0f' => "\\u000f", - b'\x10' => "\\u0010", - b'\x11' => "\\u0011", - b'\x12' => "\\u0012", - b'\x13' => "\\u0013", - b'\x14' => "\\u0014", - b'\x15' => "\\u0015", - b'\x16' => "\\u0016", - b'\x17' => "\\u0017", - b'\x18' => "\\u0018", - b'\x19' => "\\u0019", - b'\x1a' => "\\u001a", - b'\x1b' => "\\u001b", - b'\x1c' => "\\u001c", - b'\x1d' => "\\u001d", - b'\x1e' => "\\u001e", - b'\x1f' => "\\u001f", - b'\x7f' => "\\u007f", - _ => { continue; } - }; - - if start < i { - try!(wr.write_str(v.index(&(start..i)))); - } - - try!(wr.write_str(escaped)); - - start = i + 1; - } - - if start != v.len() { - try!(wr.write_str(v.index(&(start..)))); - } - - wr.write_str("\"") -} - -fn escape_char(writer: &mut fmt::Writer, v: char) -> fmt::Result { - let mut buf = [0; 4]; - let n = v.encode_utf8(&mut buf).unwrap(); - let buf = unsafe { str::from_utf8_unchecked(buf.index(&(0..n))) }; - escape_str(writer, buf) -} - -fn spaces(wr: &mut fmt::Writer, mut n: uint) -> fmt::Result { - const BUF: &'static str = " "; - - while n >= BUF.len() { - try!(wr.write_str(BUF)); - n -= BUF.len(); - } - - if n > 0 { - wr.write_str(BUF.index(&(..n))) - } else { - Ok(()) - } -} - -fn fmt_number_or_null(v: f64) -> string::String { - match v.classify() { - Fp::Nan | Fp::Infinite => string::String::from_str("null"), - _ if v.fract() != 0f64 => f64::to_str_digits(v, 6u), - _ => f64::to_str_digits(v, 6u) + ".0", - } -} - -/// A structure for implementing serialization to JSON. -pub struct Encoder<'a> { - writer: &'a mut (fmt::Writer+'a), -} - -impl<'a> Encoder<'a> { - /// Creates a new JSON encoder whose output will be written to the writer - /// specified. - pub fn new(writer: &'a mut fmt::Writer) -> Encoder<'a> { - Encoder { writer: writer } - } -} - -impl<'a> ::Encoder<fmt::Error> for Encoder<'a> { - fn emit_nil(&mut self) -> EncodeResult { write!(self.writer, "null") } - - fn emit_uint(&mut self, v: uint) -> EncodeResult { write!(self.writer, "{}", v) } - fn emit_u64(&mut self, v: u64) -> EncodeResult { write!(self.writer, "{}", v) } - fn emit_u32(&mut self, v: u32) -> EncodeResult { write!(self.writer, "{}", v) } - fn emit_u16(&mut self, v: u16) -> EncodeResult { write!(self.writer, "{}", v) } - fn emit_u8(&mut self, v: u8) -> EncodeResult { write!(self.writer, "{}", v) } - - fn emit_int(&mut self, v: int) -> EncodeResult { write!(self.writer, "{}", v) } - fn emit_i64(&mut self, v: i64) -> EncodeResult { write!(self.writer, "{}", v) } - fn emit_i32(&mut self, v: i32) -> EncodeResult { write!(self.writer, "{}", v) } - fn emit_i16(&mut self, v: i16) -> EncodeResult { write!(self.writer, "{}", v) } - fn emit_i8(&mut self, v: i8) -> EncodeResult { write!(self.writer, "{}", v) } - - fn emit_bool(&mut self, v: bool) -> EncodeResult { - if v { - write!(self.writer, "true") - } else { - write!(self.writer, "false") - } - } - - fn emit_f64(&mut self, v: f64) -> EncodeResult { - write!(self.writer, "{}", fmt_number_or_null(v)) - } - fn emit_f32(&mut self, v: f32) -> EncodeResult { - self.emit_f64(v as f64) - } - - fn emit_char(&mut self, v: char) -> EncodeResult { - escape_char(self.writer, v) - } - fn emit_str(&mut self, v: &str) -> EncodeResult { - escape_str(self.writer, v) - } - - fn emit_enum<F>(&mut self, _name: &str, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, - { - f(self) - } - - fn emit_enum_variant<F>(&mut self, - name: &str, - _id: uint, - cnt: uint, - f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, - { - // enums are encoded as strings or objects - // Bunny => "Bunny" - // Kangaroo(34,"William") => {"variant": "Kangaroo", "fields": [34,"William"]} - if cnt == 0 { - escape_str(self.writer, name) - } else { - try!(write!(self.writer, "{{\"variant\":")); - try!(escape_str(self.writer, name)); - try!(write!(self.writer, ",\"fields\":[")); - try!(f(self)); - write!(self.writer, "]}}") - } - } - - fn emit_enum_variant_arg<F>(&mut self, idx: uint, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, - { - if idx != 0 { - try!(write!(self.writer, ",")); - } - f(self) - } - - fn emit_enum_struct_variant<F>(&mut self, - name: &str, - id: uint, - cnt: uint, - f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, - { - self.emit_enum_variant(name, id, cnt, f) - } - - fn emit_enum_struct_variant_field<F>(&mut self, - _: &str, - idx: uint, - f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, - { - self.emit_enum_variant_arg(idx, f) - } - - fn emit_struct<F>(&mut self, _: &str, _: uint, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, - { - try!(write!(self.writer, "{{")); - try!(f(self)); - write!(self.writer, "}}") - } - - fn emit_struct_field<F>(&mut self, name: &str, idx: uint, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, - { - if idx != 0 { try!(write!(self.writer, ",")); } - try!(escape_str(self.writer, name)); - try!(write!(self.writer, ":")); - f(self) - } - - fn emit_tuple<F>(&mut self, len: uint, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, - { - self.emit_seq(len, f) - } - fn emit_tuple_arg<F>(&mut self, idx: uint, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, - { - self.emit_seq_elt(idx, f) - } - - fn emit_tuple_struct<F>(&mut self, _name: &str, len: uint, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, - { - self.emit_seq(len, f) - } - fn emit_tuple_struct_arg<F>(&mut self, idx: uint, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, - { - self.emit_seq_elt(idx, f) - } - - fn emit_option<F>(&mut self, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, - { - f(self) - } - fn emit_option_none(&mut self) -> EncodeResult { self.emit_nil() } - fn emit_option_some<F>(&mut self, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, - { - f(self) - } - - fn emit_seq<F>(&mut self, _len: uint, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, - { - try!(write!(self.writer, "[")); - try!(f(self)); - write!(self.writer, "]") - } - - fn emit_seq_elt<F>(&mut self, idx: uint, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, - { - if idx != 0 { - try!(write!(self.writer, ",")); - } - f(self) - } - - fn emit_map<F>(&mut self, _len: uint, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, - { - try!(write!(self.writer, "{{")); - try!(f(self)); - write!(self.writer, "}}") - } - - fn emit_map_elt_key<F>(&mut self, idx: uint, mut f: F) -> EncodeResult where - F: FnMut(&mut Encoder<'a>) -> EncodeResult, - { - if idx != 0 { try!(write!(self.writer, ",")) } - // ref #12967, make sure to wrap a key in double quotes, - // in the event that its of a type that omits them (eg numbers) - let mut buf = Vec::new(); - // FIXME(14302) remove the transmute and unsafe block. - unsafe { - let mut check_encoder = Encoder::new(&mut buf); - try!(f(transmute(&mut check_encoder))); - } - let out = str::from_utf8(buf.index(&FullRange)).unwrap(); - let needs_wrapping = out.char_at(0) != '"' && out.char_at_reverse(out.len()) != '"'; - if needs_wrapping { try!(write!(self.writer, "\"")); } - try!(f(self)); - if needs_wrapping { try!(write!(self.writer, "\"")); } - Ok(()) - } - - fn emit_map_elt_val<F>(&mut self, _idx: uint, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, - { - try!(write!(self.writer, ":")); - f(self) - } -} - -/// Another encoder for JSON, but prints out human-readable JSON instead of -/// compact data -pub struct PrettyEncoder<'a> { - writer: &'a mut (fmt::Writer+'a), - curr_indent: uint, - indent: uint, -} - -impl<'a> PrettyEncoder<'a> { - /// Creates a new encoder whose output will be written to the specified writer - pub fn new(writer: &'a mut fmt::Writer) -> PrettyEncoder<'a> { - PrettyEncoder { writer: writer, curr_indent: 0, indent: 2, } - } - - /// Set the number of spaces to indent for each level. - /// This is safe to set during encoding. - pub fn set_indent(&mut self, indent: uint) { - // self.indent very well could be 0 so we need to use checked division. - let level = self.curr_indent.checked_div(self.indent).unwrap_or(0); - self.indent = indent; - self.curr_indent = level * self.indent; - } -} - -impl<'a> ::Encoder<fmt::Error> for PrettyEncoder<'a> { - fn emit_nil(&mut self) -> EncodeResult { write!(self.writer, "null") } - - fn emit_uint(&mut self, v: uint) -> EncodeResult { write!(self.writer, "{}", v) } - fn emit_u64(&mut self, v: u64) -> EncodeResult { write!(self.writer, "{}", v) } - fn emit_u32(&mut self, v: u32) -> EncodeResult { write!(self.writer, "{}", v) } - fn emit_u16(&mut self, v: u16) -> EncodeResult { write!(self.writer, "{}", v) } - fn emit_u8(&mut self, v: u8) -> EncodeResult { write!(self.writer, "{}", v) } - - fn emit_int(&mut self, v: int) -> EncodeResult { write!(self.writer, "{}", v) } - fn emit_i64(&mut self, v: i64) -> EncodeResult { write!(self.writer, "{}", v) } - fn emit_i32(&mut self, v: i32) -> EncodeResult { write!(self.writer, "{}", v) } - fn emit_i16(&mut self, v: i16) -> EncodeResult { write!(self.writer, "{}", v) } - fn emit_i8(&mut self, v: i8) -> EncodeResult { write!(self.writer, "{}", v) } - - fn emit_bool(&mut self, v: bool) -> EncodeResult { - if v { - write!(self.writer, "true") - } else { - write!(self.writer, "false") - } - } - - fn emit_f64(&mut self, v: f64) -> EncodeResult { - write!(self.writer, "{}", fmt_number_or_null(v)) - } - fn emit_f32(&mut self, v: f32) -> EncodeResult { - self.emit_f64(v as f64) - } - - fn emit_char(&mut self, v: char) -> EncodeResult { - escape_char(self.writer, v) - } - fn emit_str(&mut self, v: &str) -> EncodeResult { - escape_str(self.writer, v) - } - - fn emit_enum<F>(&mut self, _name: &str, f: F) -> EncodeResult where - F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult, - { - f(self) - } - - fn emit_enum_variant<F>(&mut self, - name: &str, - _id: uint, - cnt: uint, - f: F) - -> EncodeResult where - F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult, - { - if cnt == 0 { - escape_str(self.writer, name) - } else { - try!(write!(self.writer, "{{\n")); - self.curr_indent += self.indent; - try!(spaces(self.writer, self.curr_indent)); - try!(write!(self.writer, "\"variant\": ")); - try!(escape_str(self.writer, name)); - try!(write!(self.writer, ",\n")); - try!(spaces(self.writer, self.curr_indent)); - try!(write!(self.writer, "\"fields\": [\n")); - self.curr_indent += self.indent; - try!(f(self)); - self.curr_indent -= self.indent; - try!(write!(self.writer, "\n")); - try!(spaces(self.writer, self.curr_indent)); - self.curr_indent -= self.indent; - try!(write!(self.writer, "]\n")); - try!(spaces(self.writer, self.curr_indent)); - write!(self.writer, "}}") - } - } - - fn emit_enum_variant_arg<F>(&mut self, idx: uint, f: F) -> EncodeResult where - F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult, - { - if idx != 0 { - try!(write!(self.writer, ",\n")); - } - try!(spaces(self.writer, self.curr_indent)); - f(self) - } - - fn emit_enum_struct_variant<F>(&mut self, - name: &str, - id: uint, - cnt: uint, - f: F) -> EncodeResult where - F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult, - { - self.emit_enum_variant(name, id, cnt, f) - } - - fn emit_enum_struct_variant_field<F>(&mut self, - _: &str, - idx: uint, - f: F) -> EncodeResult where - F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult, - { - self.emit_enum_variant_arg(idx, f) - } - - - fn emit_struct<F>(&mut self, _: &str, len: uint, f: F) -> EncodeResult where - F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult, - { - if len == 0 { - write!(self.writer, "{{}}") - } else { - try!(write!(self.writer, "{{")); - self.curr_indent += self.indent; - try!(f(self)); - self.curr_indent -= self.indent; - try!(write!(self.writer, "\n")); - try!(spaces(self.writer, self.curr_indent)); - write!(self.writer, "}}") - } - } - - fn emit_struct_field<F>(&mut self, name: &str, idx: uint, f: F) -> EncodeResult where - F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult, - { - if idx == 0 { - try!(write!(self.writer, "\n")); - } else { - try!(write!(self.writer, ",\n")); - } - try!(spaces(self.writer, self.curr_indent)); - try!(escape_str(self.writer, name)); - try!(write!(self.writer, ": ")); - f(self) - } - - fn emit_tuple<F>(&mut self, len: uint, f: F) -> EncodeResult where - F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult, - { - self.emit_seq(len, f) - } - fn emit_tuple_arg<F>(&mut self, idx: uint, f: F) -> EncodeResult where - F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult, - { - self.emit_seq_elt(idx, f) - } - - fn emit_tuple_struct<F>(&mut self, _: &str, len: uint, f: F) -> EncodeResult where - F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult, - { - self.emit_seq(len, f) - } - fn emit_tuple_struct_arg<F>(&mut self, idx: uint, f: F) -> EncodeResult where - F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult, - { - self.emit_seq_elt(idx, f) - } - - fn emit_option<F>(&mut self, f: F) -> EncodeResult where - F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult, - { - f(self) - } - fn emit_option_none(&mut self) -> EncodeResult { self.emit_nil() } - fn emit_option_some<F>(&mut self, f: F) -> EncodeResult where - F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult, - { - f(self) - } - - fn emit_seq<F>(&mut self, len: uint, f: F) -> EncodeResult where - F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult, - { - if len == 0 { - write!(self.writer, "[]") - } else { - try!(write!(self.writer, "[")); - self.curr_indent += self.indent; - try!(f(self)); - self.curr_indent -= self.indent; - try!(write!(self.writer, "\n")); - try!(spaces(self.writer, self.curr_indent)); - write!(self.writer, "]") - } - } - - fn emit_seq_elt<F>(&mut self, idx: uint, f: F) -> EncodeResult where - F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult, - { - if idx == 0 { - try!(write!(self.writer, "\n")); - } else { - try!(write!(self.writer, ",\n")); - } - try!(spaces(self.writer, self.curr_indent)); - f(self) - } - - fn emit_map<F>(&mut self, len: uint, f: F) -> EncodeResult where - F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult, - { - if len == 0 { - write!(self.writer, "{{}}") - } else { - try!(write!(self.writer, "{{")); - self.curr_indent += self.indent; - try!(f(self)); - self.curr_indent -= self.indent; - try!(write!(self.writer, "\n")); - try!(spaces(self.writer, self.curr_indent)); - write!(self.writer, "}}") - } - } - - fn emit_map_elt_key<F>(&mut self, idx: uint, mut f: F) -> EncodeResult where - F: FnMut(&mut PrettyEncoder<'a>) -> EncodeResult, - { - if idx == 0 { - try!(write!(self.writer, "\n")); - } else { - try!(write!(self.writer, ",\n")); - } - try!(spaces(self.writer, self.curr_indent)); - // ref #12967, make sure to wrap a key in double quotes, - // in the event that its of a type that omits them (eg numbers) - let mut buf = Vec::new(); - // FIXME(14302) remove the transmute and unsafe block. - unsafe { - let mut check_encoder = PrettyEncoder::new(&mut buf); - try!(f(transmute(&mut check_encoder))); - } - let out = str::from_utf8(buf.index(&FullRange)).unwrap(); - let needs_wrapping = out.char_at(0) != '"' && out.char_at_reverse(out.len()) != '"'; - if needs_wrapping { try!(write!(self.writer, "\"")); } - try!(f(self)); - if needs_wrapping { try!(write!(self.writer, "\"")); } - Ok(()) - } - - fn emit_map_elt_val<F>(&mut self, _idx: uint, f: F) -> EncodeResult where - F: FnOnce(&mut PrettyEncoder<'a>) -> EncodeResult, - { - try!(write!(self.writer, ": ")); - f(self) - } -} - -impl<E: ::Encoder<S>, S> Encodable<E, S> for Json { - fn encode(&self, e: &mut E) -> Result<(), S> { - match *self { - Json::I64(v) => v.encode(e), - Json::U64(v) => v.encode(e), - Json::F64(v) => v.encode(e), - Json::String(ref v) => v.encode(e), - Json::Boolean(v) => v.encode(e), - Json::Array(ref v) => v.encode(e), - Json::Object(ref v) => v.encode(e), - Json::Null => e.emit_nil(), - } - } -} - -/// Create an `AsJson` wrapper which can be used to print a value as JSON -/// on-the-fly via `write!` -pub fn as_json<T>(t: &T) -> AsJson<T> { - AsJson { inner: t } -} - -/// Create an `AsPrettyJson` wrapper which can be used to print a value as JSON -/// on-the-fly via `write!` -pub fn as_pretty_json<T>(t: &T) -> AsPrettyJson<T> { - AsPrettyJson { inner: t, indent: None } -} - -impl Json { - /// Borrow this json object as a pretty object to generate a pretty - /// representation for it via `Show`. - pub fn pretty(&self) -> PrettyJson { - PrettyJson { inner: self } - } - - /// If the Json value is an Object, returns the value associated with the provided key. - /// Otherwise, returns None. - pub fn find<'a>(&'a self, key: &str) -> Option<&'a Json>{ - match self { - &Json::Object(ref map) => map.get(key), - _ => None - } - } - - /// Attempts to get a nested Json Object for each key in `keys`. - /// If any key is found not to exist, find_path will return None. - /// Otherwise, it will return the Json value associated with the final key. - pub fn find_path<'a>(&'a self, keys: &[&str]) -> Option<&'a Json>{ - let mut target = self; - for key in keys.iter() { - match target.find(*key) { - Some(t) => { target = t; }, - None => return None - } - } - Some(target) - } - - /// If the Json value is an Object, performs a depth-first search until - /// a value associated with the provided key is found. If no value is found - /// or the Json value is not an Object, returns None. - pub fn search<'a>(&'a self, key: &str) -> Option<&'a Json> { - match self { - &Json::Object(ref map) => { - match map.get(key) { - Some(json_value) => Some(json_value), - None => { - for (_, v) in map.iter() { - match v.search(key) { - x if x.is_some() => return x, - _ => () - } - } - None - } - } - }, - _ => None - } - } - - /// Returns true if the Json value is an Object. Returns false otherwise. - pub fn is_object<'a>(&'a self) -> bool { - self.as_object().is_some() - } - - /// If the Json value is an Object, returns the associated BTreeMap. - /// Returns None otherwise. - pub fn as_object<'a>(&'a self) -> Option<&'a Object> { - match self { - &Json::Object(ref map) => Some(map), - _ => None - } - } - - /// Returns true if the Json value is an Array. Returns false otherwise. - pub fn is_array<'a>(&'a self) -> bool { - self.as_array().is_some() - } - - /// If the Json value is an Array, returns the associated vector. - /// Returns None otherwise. - pub fn as_array<'a>(&'a self) -> Option<&'a Array> { - match self { - &Json::Array(ref array) => Some(&*array), - _ => None - } - } - - /// Returns true if the Json value is a String. Returns false otherwise. - pub fn is_string<'a>(&'a self) -> bool { - self.as_string().is_some() - } - - /// If the Json value is a String, returns the associated str. - /// Returns None otherwise. - pub fn as_string<'a>(&'a self) -> Option<&'a str> { - match *self { - Json::String(ref s) => Some(s.index(&FullRange)), - _ => None - } - } - - /// Returns true if the Json value is a Number. Returns false otherwise. - pub fn is_number(&self) -> bool { - match *self { - Json::I64(_) | Json::U64(_) | Json::F64(_) => true, - _ => false, - } - } - - /// Returns true if the Json value is a i64. Returns false otherwise. - pub fn is_i64(&self) -> bool { - match *self { - Json::I64(_) => true, - _ => false, - } - } - - /// Returns true if the Json value is a u64. Returns false otherwise. - pub fn is_u64(&self) -> bool { - match *self { - Json::U64(_) => true, - _ => false, - } - } - - /// Returns true if the Json value is a f64. Returns false otherwise. - pub fn is_f64(&self) -> bool { - match *self { - Json::F64(_) => true, - _ => false, - } - } - - /// If the Json value is a number, return or cast it to a i64. - /// Returns None otherwise. - pub fn as_i64(&self) -> Option<i64> { - match *self { - Json::I64(n) => Some(n), - Json::U64(n) => num::cast(n), - _ => None - } - } - - /// If the Json value is a number, return or cast it to a u64. - /// Returns None otherwise. - pub fn as_u64(&self) -> Option<u64> { - match *self { - Json::I64(n) => num::cast(n), - Json::U64(n) => Some(n), - _ => None - } - } - - /// If the Json value is a number, return or cast it to a f64. - /// Returns None otherwise. - pub fn as_f64(&self) -> Option<f64> { - match *self { - Json::I64(n) => num::cast(n), - Json::U64(n) => num::cast(n), - Json::F64(n) => Some(n), - _ => None - } - } - - /// Returns true if the Json value is a Boolean. Returns false otherwise. - pub fn is_boolean(&self) -> bool { - self.as_boolean().is_some() - } - - /// If the Json value is a Boolean, returns the associated bool. - /// Returns None otherwise. - pub fn as_boolean(&self) -> Option<bool> { - match self { - &Json::Boolean(b) => Some(b), - _ => None - } - } - - /// Returns true if the Json value is a Null. Returns false otherwise. - pub fn is_null(&self) -> bool { - self.as_null().is_some() - } - - /// If the Json value is a Null, returns (). - /// Returns None otherwise. - pub fn as_null(&self) -> Option<()> { - match self { - &Json::Null => Some(()), - _ => None - } - } -} - -impl<'a> ops::Index<&'a str> for Json { - type Output = Json; - - fn index(&self, idx: & &str) -> &Json { - self.find(*idx).unwrap() - } -} - -impl ops::Index<uint> for Json { - type Output = Json; - - fn index<'a>(&'a self, idx: &uint) -> &'a Json { - match self { - &Json::Array(ref v) => v.index(idx), - _ => panic!("can only index Json with uint if it is an array") - } - } -} - -/// The output of the streaming parser. -#[derive(PartialEq, Clone, Show)] -pub enum JsonEvent { - ObjectStart, - ObjectEnd, - ArrayStart, - ArrayEnd, - BooleanValue(bool), - I64Value(i64), - U64Value(u64), - F64Value(f64), - StringValue(string::String), - NullValue, - Error(ParserError), -} - -#[derive(PartialEq, Show)] -enum ParserState { - // Parse a value in an array, true means first element. - ParseArray(bool), - // Parse ',' or ']' after an element in an array. - ParseArrayComma, - // Parse a key:value in an object, true means first element. - ParseObject(bool), - // Parse ',' or ']' after an element in an object. - ParseObjectComma, - // Initial state. - ParseStart, - // Expecting the stream to end. - ParseBeforeFinish, - // Parsing can't continue. - ParseFinished, -} - -/// A Stack represents the current position of the parser in the logical -/// structure of the JSON stream. -/// For example foo.bar[3].x -pub struct Stack { - stack: Vec<InternalStackElement>, - str_buffer: Vec<u8>, -} - -/// StackElements compose a Stack. -/// For example, Key("foo"), Key("bar"), Index(3) and Key("x") are the -/// StackElements compositing the stack that represents foo.bar[3].x -#[derive(PartialEq, Clone, Show)] -pub enum StackElement<'l> { - Index(u32), - Key(&'l str), -} - -// Internally, Key elements are stored as indices in a buffer to avoid -// allocating a string for every member of an object. -#[derive(PartialEq, Clone, Show)] -enum InternalStackElement { - InternalIndex(u32), - InternalKey(u16, u16), // start, size -} - -impl Stack { - pub fn new() -> Stack { - Stack { stack: Vec::new(), str_buffer: Vec::new() } - } - - /// Returns The number of elements in the Stack. - pub fn len(&self) -> uint { self.stack.len() } - - /// Returns true if the stack is empty. - pub fn is_empty(&self) -> bool { self.stack.is_empty() } - - /// Provides access to the StackElement at a given index. - /// lower indices are at the bottom of the stack while higher indices are - /// at the top. - pub fn get<'l>(&'l self, idx: uint) -> StackElement<'l> { - match self.stack[idx] { - InternalIndex(i) => Index(i), - InternalKey(start, size) => { - Key(str::from_utf8( - self.str_buffer.index( - &((start as uint) .. (start as uint + size as uint)))).unwrap()) - } - } - } - - /// Compares this stack with an array of StackElements. - pub fn is_equal_to(&self, rhs: &[StackElement]) -> bool { - if self.stack.len() != rhs.len() { return false; } - for i in range(0, rhs.len()) { - if self.get(i) != rhs[i] { return false; } - } - return true; - } - - /// Returns true if the bottom-most elements of this stack are the same as - /// the ones passed as parameter. - pub fn starts_with(&self, rhs: &[StackElement]) -> bool { - if self.stack.len() < rhs.len() { return false; } - for i in range(0, rhs.len()) { - if self.get(i) != rhs[i] { return false; } - } - return true; - } - - /// Returns true if the top-most elements of this stack are the same as - /// the ones passed as parameter. - pub fn ends_with(&self, rhs: &[StackElement]) -> bool { - if self.stack.len() < rhs.len() { return false; } - let offset = self.stack.len() - rhs.len(); - for i in range(0, rhs.len()) { - if self.get(i + offset) != rhs[i] { return false; } - } - return true; - } - - /// Returns the top-most element (if any). - pub fn top<'l>(&'l self) -> Option<StackElement<'l>> { - return match self.stack.last() { - None => None, - Some(&InternalIndex(i)) => Some(Index(i)), - Some(&InternalKey(start, size)) => { - Some(Key(str::from_utf8( - self.str_buffer.index(&(start as uint) .. ((start+size) as uint)) - ).unwrap())) - } - } - } - - // Used by Parser to insert Key elements at the top of the stack. - fn push_key(&mut self, key: string::String) { - self.stack.push(InternalKey(self.str_buffer.len() as u16, key.len() as u16)); - for c in key.as_bytes().iter() { - self.str_buffer.push(*c); - } - } - - // Used by Parser to insert Index elements at the top of the stack. - fn push_index(&mut self, index: u32) { - self.stack.push(InternalIndex(index)); - } - - // Used by Parser to remove the top-most element of the stack. - fn pop(&mut self) { - assert!(!self.is_empty()); - match *self.stack.last().unwrap() { - InternalKey(_, sz) => { - let new_size = self.str_buffer.len() - sz as uint; - self.str_buffer.truncate(new_size); - } - InternalIndex(_) => {} - } - self.stack.pop(); - } - - // Used by Parser to test whether the top-most element is an index. - fn last_is_index(&self) -> bool { - if self.is_empty() { return false; } - return match *self.stack.last().unwrap() { - InternalIndex(_) => true, - _ => false, - } - } - - // Used by Parser to increment the index of the top-most element. - fn bump_index(&mut self) { - let len = self.stack.len(); - let idx = match *self.stack.last().unwrap() { - InternalIndex(i) => { i + 1 } - _ => { panic!(); } - }; - self.stack[len - 1] = InternalIndex(idx); - } -} - -/// A streaming JSON parser implemented as an iterator of JsonEvent, consuming -/// an iterator of char. -pub struct Parser<T> { - rdr: T, - ch: Option<char>, - line: uint, - col: uint, - // We maintain a stack representing where we are in the logical structure - // of the JSON stream. - stack: Stack, - // A state machine is kept to make it possible to interrupt and resume parsing. - state: ParserState, -} - -impl<T: Iterator<Item=char>> Iterator for Parser<T> { - type Item = JsonEvent; - - fn next(&mut self) -> Option<JsonEvent> { - if self.state == ParseFinished { - return None; - } - - if self.state == ParseBeforeFinish { - self.parse_whitespace(); - // Make sure there is no trailing characters. - if self.eof() { - self.state = ParseFinished; - return None; - } else { - return Some(self.error_event(TrailingCharacters)); - } - } - - return Some(self.parse()); - } -} - -impl<T: Iterator<Item=char>> Parser<T> { - /// Creates the JSON parser. - pub fn new(rdr: T) -> Parser<T> { - let mut p = Parser { - rdr: rdr, - ch: Some('\x00'), - line: 1, - col: 0, - stack: Stack::new(), - state: ParseStart, - }; - p.bump(); - return p; - } - - /// Provides access to the current position in the logical structure of the - /// JSON stream. - pub fn stack<'l>(&'l self) -> &'l Stack { - return &self.stack; - } - - fn eof(&self) -> bool { self.ch.is_none() } - fn ch_or_null(&self) -> char { self.ch.unwrap_or('\x00') } - fn bump(&mut self) { - self.ch = self.rdr.next(); - - if self.ch_is('\n') { - self.line += 1u; - self.col = 1u; - } else { - self.col += 1u; - } - } - - fn next_char(&mut self) -> Option<char> { - self.bump(); - self.ch - } - fn ch_is(&self, c: char) -> bool { - self.ch == Some(c) - } - - fn error<T>(&self, reason: ErrorCode) -> Result<T, ParserError> { - Err(SyntaxError(reason, self.line, self.col)) - } - - fn parse_whitespace(&mut self) { - while self.ch_is(' ') || - self.ch_is('\n') || - self.ch_is('\t') || - self.ch_is('\r') { self.bump(); } - } - - fn parse_number(&mut self) -> JsonEvent { - let mut neg = false; - - if self.ch_is('-') { - self.bump(); - neg = true; - } - - let res = match self.parse_u64() { - Ok(res) => res, - Err(e) => { return Error(e); } - }; - - if self.ch_is('.') || self.ch_is('e') || self.ch_is('E') { - let mut res = res as f64; - - if self.ch_is('.') { - res = match self.parse_decimal(res) { - Ok(res) => res, - Err(e) => { return Error(e); } - }; - } - - if self.ch_is('e') || self.ch_is('E') { - res = match self.parse_exponent(res) { - Ok(res) => res, - Err(e) => { return Error(e); } - }; - } - - if neg { - res *= -1.0; - } - - F64Value(res) - } else { - if neg { - let res = -(res as i64); - - // Make sure we didn't underflow. - if res > 0 { - Error(SyntaxError(InvalidNumber, self.line, self.col)) - } else { - I64Value(res) - } - } else { - U64Value(res) - } - } - } - - fn parse_u64(&mut self) -> Result<u64, ParserError> { - let mut accum = 0; - let last_accum = 0; // necessary to detect overflow. - - match self.ch_or_null() { - '0' => { - self.bump(); - - // A leading '0' must be the only digit before the decimal point. - match self.ch_or_null() { - '0' ... '9' => return self.error(InvalidNumber), - _ => () - } - }, - '1' ... '9' => { - while !self.eof() { - match self.ch_or_null() { - c @ '0' ... '9' => { - accum *= 10; - accum += (c as u64) - ('0' as u64); - - // Detect overflow by comparing to the last value. - if accum <= last_accum { return self.error(InvalidNumber); } - - self.bump(); - } - _ => break, - } - } - } - _ => return self.error(InvalidNumber), - } - - Ok(accum) - } - - fn parse_decimal(&mut self, mut res: f64) -> Result<f64, ParserError> { - self.bump(); - - // Make sure a digit follows the decimal place. - match self.ch_or_null() { - '0' ... '9' => (), - _ => return self.error(InvalidNumber) - } - - let mut dec = 1.0; - while !self.eof() { - match self.ch_or_null() { - c @ '0' ... '9' => { - dec /= 10.0; - res += (((c as int) - ('0' as int)) as f64) * dec; - self.bump(); - } - _ => break, - } - } - - Ok(res) - } - - fn parse_exponent(&mut self, mut res: f64) -> Result<f64, ParserError> { - self.bump(); - - let mut exp = 0u; - let mut neg_exp = false; - - if self.ch_is('+') { - self.bump(); - } else if self.ch_is('-') { - self.bump(); - neg_exp = true; - } - - // Make sure a digit follows the exponent place. - match self.ch_or_null() { - '0' ... '9' => (), - _ => return self.error(InvalidNumber) - } - while !self.eof() { - match self.ch_or_null() { - c @ '0' ... '9' => { - exp *= 10; - exp += (c as uint) - ('0' as uint); - - self.bump(); - } - _ => break - } - } - - let exp = 10_f64.powi(exp as i32); - if neg_exp { - res /= exp; - } else { - res *= exp; - } - - Ok(res) - } - - fn decode_hex_escape(&mut self) -> Result<u16, ParserError> { - let mut i = 0u; - let mut n = 0u16; - while i < 4 && !self.eof() { - self.bump(); - n = match self.ch_or_null() { - c @ '0' ... '9' => n * 16 + ((c as u16) - ('0' as u16)), - 'a' | 'A' => n * 16 + 10, - 'b' | 'B' => n * 16 + 11, - 'c' | 'C' => n * 16 + 12, - 'd' | 'D' => n * 16 + 13, - 'e' | 'E' => n * 16 + 14, - 'f' | 'F' => n * 16 + 15, - _ => return self.error(InvalidEscape) - }; - - i += 1u; - } - - // Error out if we didn't parse 4 digits. - if i != 4 { - return self.error(InvalidEscape); - } - - Ok(n) - } - - fn parse_str(&mut self) -> Result<string::String, ParserError> { - let mut escape = false; - let mut res = string::String::new(); - - loop { - self.bump(); - if self.eof() { - return self.error(EOFWhileParsingString); - } - - if escape { - match self.ch_or_null() { - '"' => res.push('"'), - '\\' => res.push('\\'), - '/' => res.push('/'), - 'b' => res.push('\x08'), - 'f' => res.push('\x0c'), - 'n' => res.push('\n'), - 'r' => res.push('\r'), - 't' => res.push('\t'), - 'u' => match try!(self.decode_hex_escape()) { - 0xDC00 ... 0xDFFF => { - return self.error(LoneLeadingSurrogateInHexEscape) - } - - // Non-BMP characters are encoded as a sequence of - // two hex escapes, representing UTF-16 surrogates. - n1 @ 0xD800 ... 0xDBFF => { - match (self.next_char(), self.next_char()) { - (Some('\\'), Some('u')) => (), - _ => return self.error(UnexpectedEndOfHexEscape), - } - - let buf = [n1, try!(self.decode_hex_escape())]; - match unicode_str::utf16_items(&buf).next() { - Some(Utf16Item::ScalarValue(c)) => res.push(c), - _ => return self.error(LoneLeadingSurrogateInHexEscape), - } - } - - n => match char::from_u32(n as u32) { - Some(c) => res.push(c), - None => return self.error(InvalidUnicodeCodePoint), - }, - }, - _ => return self.error(InvalidEscape), - } - escape = false; - } else if self.ch_is('\\') { - escape = true; - } else { - match self.ch { - Some('"') => { - self.bump(); - return Ok(res); - }, - Some(c) => res.push(c), - None => unreachable!() - } - } - } - } - - // Invoked at each iteration, consumes the stream until it has enough - // information to return a JsonEvent. - // Manages an internal state so that parsing can be interrupted and resumed. - // Also keeps track of the position in the logical structure of the json - // stream int the form of a stack that can be queried by the user using the - // stack() method. - fn parse(&mut self) -> JsonEvent { - loop { - // The only paths where the loop can spin a new iteration - // are in the cases ParseArrayComma and ParseObjectComma if ',' - // is parsed. In these cases the state is set to (respectively) - // ParseArray(false) and ParseObject(false), which always return, - // so there is no risk of getting stuck in an infinite loop. - // All other paths return before the end of the loop's iteration. - self.parse_whitespace(); - - match self.state { - ParseStart => { - return self.parse_start(); - } - ParseArray(first) => { - return self.parse_array(first); - } - ParseArrayComma => { - match self.parse_array_comma_or_end() { - Some(evt) => { return evt; } - None => {} - } - } - ParseObject(first) => { - return self.parse_object(first); - } - ParseObjectComma => { - self.stack.pop(); - if self.ch_is(',') { - self.state = ParseObject(false); - self.bump(); - } else { - return self.parse_object_end(); - } - } - _ => { - return self.error_event(InvalidSyntax); - } - } - } - } - - fn parse_start(&mut self) -> JsonEvent { - let val = self.parse_value(); - self.state = match val { - Error(_) => ParseFinished, - ArrayStart => ParseArray(true), - ObjectStart => ParseObject(true), - _ => ParseBeforeFinish, - }; - return val; - } - - fn parse_array(&mut self, first: bool) -> JsonEvent { - if self.ch_is(']') { - if !first { - self.error_event(InvalidSyntax) - } else { - self.state = if self.stack.is_empty() { - ParseBeforeFinish - } else if self.stack.last_is_index() { - ParseArrayComma - } else { - ParseObjectComma - }; - self.bump(); - ArrayEnd - } - } else { - if first { - self.stack.push_index(0); - } - let val = self.parse_value(); - self.state = match val { - Error(_) => ParseFinished, - ArrayStart => ParseArray(true), - ObjectStart => ParseObject(true), - _ => ParseArrayComma, - }; - val - } - } - - fn parse_array_comma_or_end(&mut self) -> Option<JsonEvent> { - if self.ch_is(',') { - self.stack.bump_index(); - self.state = ParseArray(false); - self.bump(); - None - } else if self.ch_is(']') { - self.stack.pop(); - self.state = if self.stack.is_empty() { - ParseBeforeFinish - } else if self.stack.last_is_index() { - ParseArrayComma - } else { - ParseObjectComma - }; - self.bump(); - Some(ArrayEnd) - } else if self.eof() { - Some(self.error_event(EOFWhileParsingArray)) - } else { - Some(self.error_event(InvalidSyntax)) - } - } - - fn parse_object(&mut self, first: bool) -> JsonEvent { - if self.ch_is('}') { - if !first { - if self.stack.is_empty() { - return self.error_event(TrailingComma); - } else { - self.stack.pop(); - } - } - self.state = if self.stack.is_empty() { - ParseBeforeFinish - } else if self.stack.last_is_index() { - ParseArrayComma - } else { - ParseObjectComma - }; - self.bump(); - return ObjectEnd; - } - if self.eof() { - return self.error_event(EOFWhileParsingObject); - } - if !self.ch_is('"') { - return self.error_event(KeyMustBeAString); - } - let s = match self.parse_str() { - Ok(s) => s, - Err(e) => { - self.state = ParseFinished; - return Error(e); - } - }; - self.parse_whitespace(); - if self.eof() { - return self.error_event(EOFWhileParsingObject); - } else if self.ch_or_null() != ':' { - return self.error_event(ExpectedColon); - } - self.stack.push_key(s); - self.bump(); - self.parse_whitespace(); - - let val = self.parse_value(); - - self.state = match val { - Error(_) => ParseFinished, - ArrayStart => ParseArray(true), - ObjectStart => ParseObject(true), - _ => ParseObjectComma, - }; - return val; - } - - fn parse_object_end(&mut self) -> JsonEvent { - if self.ch_is('}') { - self.state = if self.stack.is_empty() { - ParseBeforeFinish - } else if self.stack.last_is_index() { - ParseArrayComma - } else { - ParseObjectComma - }; - self.bump(); - ObjectEnd - } else if self.eof() { - self.error_event(EOFWhileParsingObject) - } else { - self.error_event(InvalidSyntax) - } - } - - fn parse_value(&mut self) -> JsonEvent { - if self.eof() { return self.error_event(EOFWhileParsingValue); } - match self.ch_or_null() { - 'n' => { self.parse_ident("ull", NullValue) } - 't' => { self.parse_ident("rue", BooleanValue(true)) } - 'f' => { self.parse_ident("alse", BooleanValue(false)) } - '0' ... '9' | '-' => self.parse_number(), - '"' => match self.parse_str() { - Ok(s) => StringValue(s), - Err(e) => Error(e), - }, - '[' => { - self.bump(); - ArrayStart - } - '{' => { - self.bump(); - ObjectStart - } - _ => { self.error_event(InvalidSyntax) } - } - } - - fn parse_ident(&mut self, ident: &str, value: JsonEvent) -> JsonEvent { - if ident.chars().all(|c| Some(c) == self.next_char()) { - self.bump(); - value - } else { - Error(SyntaxError(InvalidSyntax, self.line, self.col)) - } - } - - fn error_event(&mut self, reason: ErrorCode) -> JsonEvent { - self.state = ParseFinished; - Error(SyntaxError(reason, self.line, self.col)) - } -} - -/// A Builder consumes a json::Parser to create a generic Json structure. -pub struct Builder<T> { - parser: Parser<T>, - token: Option<JsonEvent>, -} - -impl<T: Iterator<Item=char>> Builder<T> { - /// Create a JSON Builder. - pub fn new(src: T) -> Builder<T> { - Builder { parser: Parser::new(src), token: None, } - } - - // Decode a Json value from a Parser. - pub fn build(&mut self) -> Result<Json, BuilderError> { - self.bump(); - let result = self.build_value(); - self.bump(); - match self.token { - None => {} - Some(Error(e)) => { return Err(e); } - ref tok => { panic!("unexpected token {}", tok.clone()); } - } - result - } - - fn bump(&mut self) { - self.token = self.parser.next(); - } - - fn build_value(&mut self) -> Result<Json, BuilderError> { - return match self.token { - Some(NullValue) => Ok(Json::Null), - Some(I64Value(n)) => Ok(Json::I64(n)), - Some(U64Value(n)) => Ok(Json::U64(n)), - Some(F64Value(n)) => Ok(Json::F64(n)), - Some(BooleanValue(b)) => Ok(Json::Boolean(b)), - Some(StringValue(ref mut s)) => { - let mut temp = string::String::new(); - swap(s, &mut temp); - Ok(Json::String(temp)) - } - Some(Error(e)) => Err(e), - Some(ArrayStart) => self.build_array(), - Some(ObjectStart) => self.build_object(), - Some(ObjectEnd) => self.parser.error(InvalidSyntax), - Some(ArrayEnd) => self.parser.error(InvalidSyntax), - None => self.parser.error(EOFWhileParsingValue), - } - } - - fn build_array(&mut self) -> Result<Json, BuilderError> { - self.bump(); - let mut values = Vec::new(); - - loop { - if self.token == Some(ArrayEnd) { - return Ok(Json::Array(values.into_iter().collect())); - } - match self.build_value() { - Ok(v) => values.push(v), - Err(e) => { return Err(e) } - } - self.bump(); - } - } - - fn build_object(&mut self) -> Result<Json, BuilderError> { - self.bump(); - - let mut values = BTreeMap::new(); - - loop { - match self.token { - Some(ObjectEnd) => { return Ok(Json::Object(values)); } - Some(Error(e)) => { return Err(e); } - None => { break; } - _ => {} - } - let key = match self.parser.stack().top() { - Some(Key(k)) => { k.to_string() } - _ => { panic!("invalid state"); } - }; - match self.build_value() { - Ok(value) => { values.insert(key, value); } - Err(e) => { return Err(e); } - } - self.bump(); - } - return self.parser.error(EOFWhileParsingObject); - } -} - -/// Decodes a json value from an `&mut io::Reader` -pub fn from_reader(rdr: &mut io::Reader) -> Result<Json, BuilderError> { - let contents = match rdr.read_to_end() { - Ok(c) => c, - Err(e) => return Err(io_error_to_error(e)) - }; - let s = match str::from_utf8(contents.as_slice()).ok() { - Some(s) => s, - _ => return Err(SyntaxError(NotUtf8, 0, 0)) - }; - let mut builder = Builder::new(s.chars()); - builder.build() -} - -/// Decodes a json value from a string -pub fn from_str(s: &str) -> Result<Json, BuilderError> { - let mut builder = Builder::new(s.chars()); - builder.build() -} - -/// A structure to decode JSON to values in rust. -pub struct Decoder { - stack: Vec<Json>, -} - -impl Decoder { - /// Creates a new decoder instance for decoding the specified JSON value. - pub fn new(json: Json) -> Decoder { - Decoder { stack: vec![json] } - } -} - -impl Decoder { - fn pop(&mut self) -> Json { - self.stack.pop().unwrap() - } -} - -macro_rules! expect { - ($e:expr, Null) => ({ - match $e { - Json::Null => Ok(()), - other => Err(ExpectedError("Null".to_string(), - format!("{}", other))) - } - }); - ($e:expr, $t:ident) => ({ - match $e { - Json::$t(v) => Ok(v), - other => { - Err(ExpectedError(stringify!($t).to_string(), - format!("{}", other))) - } - } - }) -} - -macro_rules! read_primitive { - ($name:ident, $ty:ty) => { - fn $name(&mut self) -> DecodeResult<$ty> { - match self.pop() { - Json::I64(f) => match num::cast(f) { - Some(f) => Ok(f), - None => Err(ExpectedError("Number".to_string(), format!("{}", f))), - }, - Json::U64(f) => match num::cast(f) { - Some(f) => Ok(f), - None => Err(ExpectedError("Number".to_string(), format!("{}", f))), - }, - Json::F64(f) => Err(ExpectedError("Integer".to_string(), format!("{}", f))), - // re: #12967.. a type w/ numeric keys (ie HashMap<uint, V> etc) - // is going to have a string here, as per JSON spec. - Json::String(s) => match s.parse() { - Some(f) => Ok(f), - None => Err(ExpectedError("Number".to_string(), s)), - }, - value => Err(ExpectedError("Number".to_string(), format!("{}", value))), - } - } - } -} - -impl ::Decoder<DecoderError> for Decoder { - fn read_nil(&mut self) -> DecodeResult<()> { - expect!(self.pop(), Null) - } - - read_primitive! { read_uint, uint } - read_primitive! { read_u8, u8 } - read_primitive! { read_u16, u16 } - read_primitive! { read_u32, u32 } - read_primitive! { read_u64, u64 } - read_primitive! { read_int, int } - read_primitive! { read_i8, i8 } - read_primitive! { read_i16, i16 } - read_primitive! { read_i32, i32 } - read_primitive! { read_i64, i64 } - - fn read_f32(&mut self) -> DecodeResult<f32> { self.read_f64().map(|x| x as f32) } - - fn read_f64(&mut self) -> DecodeResult<f64> { - match self.pop() { - Json::I64(f) => Ok(f as f64), - Json::U64(f) => Ok(f as f64), - Json::F64(f) => Ok(f), - Json::String(s) => { - // re: #12967.. a type w/ numeric keys (ie HashMap<uint, V> etc) - // is going to have a string here, as per JSON spec. - match s.parse() { - Some(f) => Ok(f), - None => Err(ExpectedError("Number".to_string(), s)), - } - }, - Json::Null => Ok(f64::NAN), - value => Err(ExpectedError("Number".to_string(), format!("{}", value))) - } - } - - fn read_bool(&mut self) -> DecodeResult<bool> { - expect!(self.pop(), Boolean) - } - - fn read_char(&mut self) -> DecodeResult<char> { - let s = try!(self.read_str()); - { - let mut it = s.chars(); - match (it.next(), it.next()) { - // exactly one character - (Some(c), None) => return Ok(c), - _ => () - } - } - Err(ExpectedError("single character string".to_string(), format!("{}", s))) - } - - fn read_str(&mut self) -> DecodeResult<string::String> { - expect!(self.pop(), String) - } - - fn read_enum<T, F>(&mut self, _name: &str, f: F) -> DecodeResult<T> where - F: FnOnce(&mut Decoder) -> DecodeResult<T>, - { - f(self) - } - - fn read_enum_variant<T, F>(&mut self, names: &[&str], - mut f: F) -> DecodeResult<T> - where F: FnMut(&mut Decoder, uint) -> DecodeResult<T>, - { - let name = match self.pop() { - Json::String(s) => s, - Json::Object(mut o) => { - let n = match o.remove(&"variant".to_string()) { - Some(Json::String(s)) => s, - Some(val) => { - return Err(ExpectedError("String".to_string(), format!("{}", val))) - } - None => { - return Err(MissingFieldError("variant".to_string())) - } - }; - match o.remove(&"fields".to_string()) { - Some(Json::Array(l)) => { - for field in l.into_iter().rev() { - self.stack.push(field); - } - }, - Some(val) => { - return Err(ExpectedError("Array".to_string(), format!("{}", val))) - } - None => { - return Err(MissingFieldError("fields".to_string())) - } - } - n - } - json => { - return Err(ExpectedError("String or Object".to_string(), format!("{}", json))) - } - }; - let idx = match names.iter().position(|n| *n == name.index(&FullRange)) { - Some(idx) => idx, - None => return Err(UnknownVariantError(name)) - }; - f(self, idx) - } - - fn read_enum_variant_arg<T, F>(&mut self, _idx: uint, f: F) -> DecodeResult<T> where - F: FnOnce(&mut Decoder) -> DecodeResult<T>, - { - f(self) - } - - fn read_enum_struct_variant<T, F>(&mut self, names: &[&str], f: F) -> DecodeResult<T> where - F: FnMut(&mut Decoder, uint) -> DecodeResult<T>, - { - self.read_enum_variant(names, f) - } - - - fn read_enum_struct_variant_field<T, F>(&mut self, - _name: &str, - idx: uint, - f: F) - -> DecodeResult<T> where - F: FnOnce(&mut Decoder) -> DecodeResult<T>, - { - self.read_enum_variant_arg(idx, f) - } - - fn read_struct<T, F>(&mut self, _name: &str, _len: uint, f: F) -> DecodeResult<T> where - F: FnOnce(&mut Decoder) -> DecodeResult<T>, - { - let value = try!(f(self)); - self.pop(); - Ok(value) - } - - fn read_struct_field<T, F>(&mut self, - name: &str, - _idx: uint, - f: F) - -> DecodeResult<T> where - F: FnOnce(&mut Decoder) -> DecodeResult<T>, - { - let mut obj = try!(expect!(self.pop(), Object)); - - 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. - self.stack.push(Json::Null); - match f(self) { - Ok(x) => x, - Err(_) => return Err(MissingFieldError(name.to_string())), - } - }, - Some(json) => { - self.stack.push(json); - try!(f(self)) - } - }; - self.stack.push(Json::Object(obj)); - Ok(value) - } - - fn read_tuple<T, F>(&mut self, tuple_len: uint, f: F) -> DecodeResult<T> where - F: FnOnce(&mut Decoder) -> DecodeResult<T>, - { - self.read_seq(move |d, len| { - if len == tuple_len { - f(d) - } else { - Err(ExpectedError(format!("Tuple{}", tuple_len), format!("Tuple{}", len))) - } - }) - } - - fn read_tuple_arg<T, F>(&mut self, idx: uint, f: F) -> DecodeResult<T> where - F: FnOnce(&mut Decoder) -> DecodeResult<T>, - { - self.read_seq_elt(idx, f) - } - - fn read_tuple_struct<T, F>(&mut self, - _name: &str, - len: uint, - f: F) - -> DecodeResult<T> where - F: FnOnce(&mut Decoder) -> DecodeResult<T>, - { - self.read_tuple(len, f) - } - - fn read_tuple_struct_arg<T, F>(&mut self, - idx: uint, - f: F) - -> DecodeResult<T> where - F: FnOnce(&mut Decoder) -> DecodeResult<T>, - { - self.read_tuple_arg(idx, f) - } - - fn read_option<T, F>(&mut self, mut f: F) -> DecodeResult<T> where - F: FnMut(&mut Decoder, bool) -> DecodeResult<T>, - { - match self.pop() { - Json::Null => f(self, false), - value => { self.stack.push(value); f(self, true) } - } - } - - fn read_seq<T, F>(&mut self, f: F) -> DecodeResult<T> where - F: FnOnce(&mut Decoder, uint) -> DecodeResult<T>, - { - let array = try!(expect!(self.pop(), Array)); - let len = array.len(); - for v in array.into_iter().rev() { - self.stack.push(v); - } - f(self, len) - } - - fn read_seq_elt<T, F>(&mut self, _idx: uint, f: F) -> DecodeResult<T> where - F: FnOnce(&mut Decoder) -> DecodeResult<T>, - { - f(self) - } - - fn read_map<T, F>(&mut self, f: F) -> DecodeResult<T> where - F: FnOnce(&mut Decoder, uint) -> DecodeResult<T>, - { - let obj = try!(expect!(self.pop(), Object)); - let len = obj.len(); - for (key, value) in obj.into_iter() { - self.stack.push(value); - self.stack.push(Json::String(key)); - } - f(self, len) - } - - fn read_map_elt_key<T, F>(&mut self, _idx: uint, f: F) -> DecodeResult<T> where - F: FnOnce(&mut Decoder) -> DecodeResult<T>, - { - f(self) - } - - fn read_map_elt_val<T, F>(&mut self, _idx: uint, f: F) -> DecodeResult<T> where - F: FnOnce(&mut Decoder) -> DecodeResult<T>, - { - f(self) - } - - fn error(&mut self, err: &str) -> DecoderError { - ApplicationError(err.to_string()) - } -} - -/// A trait for converting values to JSON -pub trait ToJson { - /// Converts the value of `self` to an instance of JSON - fn to_json(&self) -> Json; -} - -macro_rules! to_json_impl_i64 { - ($($t:ty), +) => ( - $(impl ToJson for $t { - fn to_json(&self) -> Json { Json::I64(*self as i64) } - })+ - ) -} - -to_json_impl_i64! { int, i8, i16, i32, i64 } - -macro_rules! to_json_impl_u64 { - ($($t:ty), +) => ( - $(impl ToJson for $t { - fn to_json(&self) -> Json { Json::U64(*self as u64) } - })+ - ) -} - -to_json_impl_u64! { uint, u8, u16, u32, u64 } - -impl ToJson for Json { - fn to_json(&self) -> Json { self.clone() } -} - -impl ToJson for f32 { - fn to_json(&self) -> Json { (*self as f64).to_json() } -} - -impl ToJson for f64 { - fn to_json(&self) -> Json { - match self.classify() { - Fp::Nan | Fp::Infinite => Json::Null, - _ => Json::F64(*self) - } - } -} - -impl ToJson for () { - fn to_json(&self) -> Json { Json::Null } -} - -impl ToJson for bool { - fn to_json(&self) -> Json { Json::Boolean(*self) } -} - -impl ToJson for str { - fn to_json(&self) -> Json { Json::String(self.to_string()) } -} - -impl ToJson for string::String { - fn to_json(&self) -> Json { Json::String((*self).clone()) } -} - -macro_rules! tuple_impl { - // use variables to indicate the arity of the tuple - ($($tyvar:ident),* ) => { - // the trailing commas are for the 1 tuple - impl< - $( $tyvar : ToJson ),* - > ToJson for ( $( $tyvar ),* , ) { - - #[inline] - #[allow(non_snake_case)] - fn to_json(&self) -> Json { - match *self { - ($(ref $tyvar),*,) => Json::Array(vec![$($tyvar.to_json()),*]) - } - } - } - } -} - -tuple_impl!{A} -tuple_impl!{A, B} -tuple_impl!{A, B, C} -tuple_impl!{A, B, C, D} -tuple_impl!{A, B, C, D, E} -tuple_impl!{A, B, C, D, E, F} -tuple_impl!{A, B, C, D, E, F, G} -tuple_impl!{A, B, C, D, E, F, G, H} -tuple_impl!{A, B, C, D, E, F, G, H, I} -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: ToJson> ToJson for [A] { - fn to_json(&self) -> Json { Json::Array(self.iter().map(|elt| elt.to_json()).collect()) } -} - -impl<A: ToJson> ToJson for Vec<A> { - fn to_json(&self) -> Json { Json::Array(self.iter().map(|elt| elt.to_json()).collect()) } -} - -impl<A: ToJson> ToJson for BTreeMap<string::String, A> { - fn to_json(&self) -> Json { - let mut d = BTreeMap::new(); - for (key, value) in self.iter() { - d.insert((*key).clone(), value.to_json()); - } - Json::Object(d) - } -} - -impl<A: ToJson> ToJson for HashMap<string::String, A> { - fn to_json(&self) -> Json { - let mut d = BTreeMap::new(); - for (key, value) in self.iter() { - d.insert((*key).clone(), value.to_json()); - } - Json::Object(d) - } -} - -impl<A:ToJson> ToJson for Option<A> { - fn to_json(&self) -> Json { - match *self { - None => Json::Null, - Some(ref value) => value.to_json() - } - } -} - -struct FormatShim<'a, 'b: 'a> { - inner: &'a mut fmt::Formatter<'b>, -} - -impl<'a, 'b> fmt::Writer for FormatShim<'a, 'b> { - fn write_str(&mut self, s: &str) -> fmt::Result { - self.inner.write_str(s) - } -} - -impl fmt::Show for Json { - /// Encodes a json value into a string - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let mut shim = FormatShim { inner: f }; - let mut encoder = Encoder::new(&mut shim); - self.encode(&mut encoder) - } -} - -impl<'a> fmt::Show for PrettyJson<'a> { - /// Encodes a json value into a string - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let mut shim = FormatShim { inner: f }; - let mut encoder = PrettyEncoder::new(&mut shim); - self.inner.encode(&mut encoder) - } -} - -impl<'a, T> fmt::Show for AsJson<'a, T> - where T: for<'b> Encodable<Encoder<'b>, fmt::Error> -{ - /// Encodes a json value into a string - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let mut shim = FormatShim { inner: f }; - let mut encoder = Encoder::new(&mut shim); - self.inner.encode(&mut encoder) - } -} - -impl<'a, T> AsPrettyJson<'a, T> { - /// Set the indentation level for the emitted JSON - pub fn indent(mut self, indent: uint) -> AsPrettyJson<'a, T> { - self.indent = Some(indent); - self - } -} - -impl<'a, T> fmt::Show for AsPrettyJson<'a, T> - where T: for<'b> Encodable<PrettyEncoder<'b>, fmt::Error> -{ - /// Encodes a json value into a string - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let mut shim = FormatShim { inner: f }; - let mut encoder = PrettyEncoder::new(&mut shim); - match self.indent { - Some(n) => encoder.set_indent(n), - None => {} - } - self.inner.encode(&mut encoder) - } -} - -impl FromStr for Json { - fn from_str(s: &str) -> Option<Json> { - from_str(s).ok() - } -} - -#[cfg(test)] -mod tests { - extern crate test; - use self::Animal::*; - use self::DecodeEnum::*; - use self::test::Bencher; - use {Encodable, Decodable}; - use super::Json::*; - use super::ErrorCode::*; - use super::ParserError::*; - use super::DecoderError::*; - use super::JsonEvent::*; - use super::StackElement::*; - use super::{Json, from_str, DecodeResult, DecoderError, JsonEvent, Parser, - StackElement, Stack, Decoder}; - use std::{i64, u64, f32, f64}; - use std::collections::BTreeMap; - use std::num::Float; - use std::string; - - #[derive(RustcDecodable, Eq, PartialEq, Show)] - struct OptionData { - opt: Option<uint>, - } - - #[test] - fn test_decode_option_none() { - let s ="{}"; - let obj: OptionData = super::decode(s).unwrap(); - assert_eq!(obj, OptionData { opt: None }); - } - - #[test] - fn test_decode_option_some() { - let s = "{ \"opt\": 10 }"; - let obj: OptionData = super::decode(s).unwrap(); - assert_eq!(obj, OptionData { opt: Some(10u) }); - } - - #[test] - fn test_decode_option_malformed() { - check_err::<OptionData>("{ \"opt\": [] }", - ExpectedError("Number".to_string(), "[]".to_string())); - check_err::<OptionData>("{ \"opt\": false }", - ExpectedError("Number".to_string(), "false".to_string())); - } - - #[derive(PartialEq, RustcEncodable, RustcDecodable, Show)] - enum Animal { - Dog, - Frog(string::String, int) - } - - #[derive(PartialEq, RustcEncodable, RustcDecodable, Show)] - struct Inner { - a: (), - b: uint, - c: Vec<string::String>, - } - - #[derive(PartialEq, RustcEncodable, RustcDecodable, Show)] - struct Outer { - inner: Vec<Inner>, - } - - fn mk_object(items: &[(string::String, Json)]) -> Json { - let mut d = BTreeMap::new(); - - for item in items.iter() { - match *item { - (ref key, ref value) => { d.insert((*key).clone(), (*value).clone()); }, - } - }; - - Object(d) - } - - #[test] - fn test_from_str_trait() { - let s = "null"; - assert!(s.parse::<Json>().unwrap() == s.parse().unwrap()); - } - - #[test] - fn test_write_null() { - assert_eq!(Null.to_string(), "null"); - assert_eq!(Null.pretty().to_string(), "null"); - } - - #[test] - fn test_write_i64() { - assert_eq!(U64(0).to_string(), "0"); - assert_eq!(U64(0).pretty().to_string(), "0"); - - assert_eq!(U64(1234).to_string(), "1234"); - assert_eq!(U64(1234).pretty().to_string(), "1234"); - - assert_eq!(I64(-5678).to_string(), "-5678"); - assert_eq!(I64(-5678).pretty().to_string(), "-5678"); - - assert_eq!(U64(7650007200025252000).to_string(), "7650007200025252000"); - assert_eq!(U64(7650007200025252000).pretty().to_string(), "7650007200025252000"); - } - - #[test] - fn test_write_f64() { - assert_eq!(F64(3.0).to_string(), "3.0"); - assert_eq!(F64(3.0).pretty().to_string(), "3.0"); - - assert_eq!(F64(3.1).to_string(), "3.1"); - assert_eq!(F64(3.1).pretty().to_string(), "3.1"); - - assert_eq!(F64(-1.5).to_string(), "-1.5"); - assert_eq!(F64(-1.5).pretty().to_string(), "-1.5"); - - assert_eq!(F64(0.5).to_string(), "0.5"); - assert_eq!(F64(0.5).pretty().to_string(), "0.5"); - - assert_eq!(F64(f64::NAN).to_string(), "null"); - assert_eq!(F64(f64::NAN).pretty().to_string(), "null"); - - assert_eq!(F64(f64::INFINITY).to_string(), "null"); - assert_eq!(F64(f64::INFINITY).pretty().to_string(), "null"); - - assert_eq!(F64(f64::NEG_INFINITY).to_string(), "null"); - assert_eq!(F64(f64::NEG_INFINITY).pretty().to_string(), "null"); - } - - #[test] - fn test_write_str() { - assert_eq!(String("".to_string()).to_string(), "\"\""); - assert_eq!(String("".to_string()).pretty().to_string(), "\"\""); - - assert_eq!(String("homura".to_string()).to_string(), "\"homura\""); - assert_eq!(String("madoka".to_string()).pretty().to_string(), "\"madoka\""); - } - - #[test] - fn test_write_bool() { - assert_eq!(Boolean(true).to_string(), "true"); - assert_eq!(Boolean(true).pretty().to_string(), "true"); - - assert_eq!(Boolean(false).to_string(), "false"); - assert_eq!(Boolean(false).pretty().to_string(), "false"); - } - - #[test] - fn test_write_array() { - assert_eq!(Array(vec![]).to_string(), "[]"); - assert_eq!(Array(vec![]).pretty().to_string(), "[]"); - - assert_eq!(Array(vec![Boolean(true)]).to_string(), "[true]"); - assert_eq!( - Array(vec![Boolean(true)]).pretty().to_string(), - "\ - [\n \ - true\n\ - ]" - ); - - let long_test_array = Array(vec![ - Boolean(false), - Null, - Array(vec![String("foo\nbar".to_string()), F64(3.5)])]); - - assert_eq!(long_test_array.to_string(), - "[false,null,[\"foo\\nbar\",3.5]]"); - assert_eq!( - long_test_array.pretty().to_string(), - "\ - [\n \ - false,\n \ - null,\n \ - [\n \ - \"foo\\nbar\",\n \ - 3.5\n \ - ]\n\ - ]" - ); - } - - #[test] - fn test_write_object() { - assert_eq!(mk_object(&[]).to_string(), "{}"); - assert_eq!(mk_object(&[]).pretty().to_string(), "{}"); - - assert_eq!( - mk_object(&[ - ("a".to_string(), Boolean(true)) - ]).to_string(), - "{\"a\":true}" - ); - assert_eq!( - mk_object(&[("a".to_string(), Boolean(true))]).pretty().to_string(), - "\ - {\n \ - \"a\": true\n\ - }" - ); - - let complex_obj = mk_object(&[ - ("b".to_string(), Array(vec![ - mk_object(&[("c".to_string(), String("\x0c\r".to_string()))]), - mk_object(&[("d".to_string(), String("".to_string()))]) - ])) - ]); - - assert_eq!( - complex_obj.to_string(), - "{\ - \"b\":[\ - {\"c\":\"\\f\\r\"},\ - {\"d\":\"\"}\ - ]\ - }" - ); - assert_eq!( - complex_obj.pretty().to_string(), - "\ - {\n \ - \"b\": [\n \ - {\n \ - \"c\": \"\\f\\r\"\n \ - },\n \ - {\n \ - \"d\": \"\"\n \ - }\n \ - ]\n\ - }" - ); - - let a = mk_object(&[ - ("a".to_string(), Boolean(true)), - ("b".to_string(), Array(vec![ - mk_object(&[("c".to_string(), String("\x0c\r".to_string()))]), - mk_object(&[("d".to_string(), String("".to_string()))]) - ])) - ]); - - // We can't compare the strings directly because the object fields be - // printed in a different order. - assert_eq!(a.clone(), a.to_string().parse().unwrap()); - assert_eq!(a.clone(), a.pretty().to_string().parse().unwrap()); - } - - #[test] - fn test_write_enum() { - let animal = Dog; - assert_eq!( - format!("{}", super::as_json(&animal)), - "\"Dog\"" - ); - assert_eq!( - format!("{}", super::as_pretty_json(&animal)), - "\"Dog\"" - ); - - let animal = Frog("Henry".to_string(), 349); - assert_eq!( - format!("{}", super::as_json(&animal)), - "{\"variant\":\"Frog\",\"fields\":[\"Henry\",349]}" - ); - assert_eq!( - format!("{}", super::as_pretty_json(&animal)), - "{\n \ - \"variant\": \"Frog\",\n \ - \"fields\": [\n \ - \"Henry\",\n \ - 349\n \ - ]\n\ - }" - ); - } - - macro_rules! check_encoder_for_simple { - ($value:expr, $expected:expr) => ({ - let s = format!("{}", super::as_json(&$value)); - assert_eq!(s, $expected); - - let s = format!("{}", super::as_pretty_json(&$value)); - assert_eq!(s, $expected); - }) - } - - #[test] - fn test_write_some() { - check_encoder_for_simple!(Some("jodhpurs".to_string()), "\"jodhpurs\""); - } - - #[test] - fn test_write_none() { - check_encoder_for_simple!(None::<string::String>, "null"); - } - - #[test] - fn test_write_char() { - check_encoder_for_simple!('a', "\"a\""); - check_encoder_for_simple!('\t', "\"\\t\""); - check_encoder_for_simple!('\u{0000}', "\"\\u0000\""); - check_encoder_for_simple!('\u{001b}', "\"\\u001b\""); - check_encoder_for_simple!('\u{007f}', "\"\\u007f\""); - check_encoder_for_simple!('\u{00a0}', "\"\u{00a0}\""); - check_encoder_for_simple!('\u{abcd}', "\"\u{abcd}\""); - check_encoder_for_simple!('\u{10ffff}', "\"\u{10ffff}\""); - } - - #[test] - fn test_trailing_characters() { - assert_eq!(from_str("nulla"), Err(SyntaxError(TrailingCharacters, 1, 5))); - assert_eq!(from_str("truea"), Err(SyntaxError(TrailingCharacters, 1, 5))); - assert_eq!(from_str("falsea"), Err(SyntaxError(TrailingCharacters, 1, 6))); - assert_eq!(from_str("1a"), Err(SyntaxError(TrailingCharacters, 1, 2))); - assert_eq!(from_str("[]a"), Err(SyntaxError(TrailingCharacters, 1, 3))); - assert_eq!(from_str("{}a"), Err(SyntaxError(TrailingCharacters, 1, 3))); - } - - #[test] - fn test_read_identifiers() { - assert_eq!(from_str("n"), Err(SyntaxError(InvalidSyntax, 1, 2))); - assert_eq!(from_str("nul"), Err(SyntaxError(InvalidSyntax, 1, 4))); - assert_eq!(from_str("t"), Err(SyntaxError(InvalidSyntax, 1, 2))); - assert_eq!(from_str("truz"), Err(SyntaxError(InvalidSyntax, 1, 4))); - assert_eq!(from_str("f"), Err(SyntaxError(InvalidSyntax, 1, 2))); - assert_eq!(from_str("faz"), Err(SyntaxError(InvalidSyntax, 1, 3))); - - assert_eq!(from_str("null"), Ok(Null)); - assert_eq!(from_str("true"), Ok(Boolean(true))); - assert_eq!(from_str("false"), Ok(Boolean(false))); - assert_eq!(from_str(" null "), Ok(Null)); - assert_eq!(from_str(" true "), Ok(Boolean(true))); - assert_eq!(from_str(" false "), Ok(Boolean(false))); - } - - #[test] - fn test_decode_identifiers() { - let v: () = super::decode("null").unwrap(); - assert_eq!(v, ()); - - let v: bool = super::decode("true").unwrap(); - assert_eq!(v, true); - - let v: bool = super::decode("false").unwrap(); - assert_eq!(v, false); - } - - #[test] - fn test_read_number() { - assert_eq!(from_str("+"), Err(SyntaxError(InvalidSyntax, 1, 1))); - assert_eq!(from_str("."), Err(SyntaxError(InvalidSyntax, 1, 1))); - assert_eq!(from_str("NaN"), Err(SyntaxError(InvalidSyntax, 1, 1))); - assert_eq!(from_str("-"), Err(SyntaxError(InvalidNumber, 1, 2))); - assert_eq!(from_str("00"), Err(SyntaxError(InvalidNumber, 1, 2))); - assert_eq!(from_str("1."), Err(SyntaxError(InvalidNumber, 1, 3))); - assert_eq!(from_str("1e"), Err(SyntaxError(InvalidNumber, 1, 3))); - assert_eq!(from_str("1e+"), Err(SyntaxError(InvalidNumber, 1, 4))); - - assert_eq!(from_str("18446744073709551616"), Err(SyntaxError(InvalidNumber, 1, 20))); - assert_eq!(from_str("-9223372036854775809"), Err(SyntaxError(InvalidNumber, 1, 21))); - - assert_eq!(from_str("3"), Ok(U64(3))); - assert_eq!(from_str("3.1"), Ok(F64(3.1))); - assert_eq!(from_str("-1.2"), Ok(F64(-1.2))); - assert_eq!(from_str("0.4"), Ok(F64(0.4))); - assert_eq!(from_str("0.4e5"), Ok(F64(0.4e5))); - assert_eq!(from_str("0.4e+15"), Ok(F64(0.4e15))); - assert_eq!(from_str("0.4e-01"), Ok(F64(0.4e-01))); - assert_eq!(from_str(" 3 "), Ok(U64(3))); - - assert_eq!(from_str("-9223372036854775808"), Ok(I64(i64::MIN))); - assert_eq!(from_str("9223372036854775807"), Ok(U64(i64::MAX as u64))); - assert_eq!(from_str("18446744073709551615"), Ok(U64(u64::MAX))); - } - - #[test] - fn test_decode_numbers() { - let v: f64 = super::decode("3").unwrap(); - assert_eq!(v, 3.0); - - let v: f64 = super::decode("3.1").unwrap(); - assert_eq!(v, 3.1); - - let v: f64 = super::decode("-1.2").unwrap(); - assert_eq!(v, -1.2); - - let v: f64 = super::decode("0.4").unwrap(); - assert_eq!(v, 0.4); - - let v: f64 = super::decode("0.4e5").unwrap(); - assert_eq!(v, 0.4e5); - - let v: f64 = super::decode("0.4e15").unwrap(); - assert_eq!(v, 0.4e15); - - let v: f64 = super::decode("0.4e-01").unwrap(); - assert_eq!(v, 0.4e-01); - - let v: u64 = super::decode("0").unwrap(); - assert_eq!(v, 0); - - let v: u64 = super::decode("18446744073709551615").unwrap(); - assert_eq!(v, u64::MAX); - - let v: i64 = super::decode("-9223372036854775808").unwrap(); - assert_eq!(v, i64::MIN); - - let v: i64 = super::decode("9223372036854775807").unwrap(); - assert_eq!(v, i64::MAX); - - let res: DecodeResult<i64> = super::decode("765.25252"); - assert_eq!(res, Err(ExpectedError("Integer".to_string(), "765.25252".to_string()))); - } - - #[test] - fn test_read_str() { - assert_eq!(from_str("\""), Err(SyntaxError(EOFWhileParsingString, 1, 2))); - assert_eq!(from_str("\"lol"), Err(SyntaxError(EOFWhileParsingString, 1, 5))); - - assert_eq!(from_str("\"\""), Ok(String("".to_string()))); - assert_eq!(from_str("\"foo\""), Ok(String("foo".to_string()))); - assert_eq!(from_str("\"\\\"\""), Ok(String("\"".to_string()))); - assert_eq!(from_str("\"\\b\""), Ok(String("\x08".to_string()))); - assert_eq!(from_str("\"\\n\""), Ok(String("\n".to_string()))); - assert_eq!(from_str("\"\\r\""), Ok(String("\r".to_string()))); - assert_eq!(from_str("\"\\t\""), Ok(String("\t".to_string()))); - assert_eq!(from_str(" \"foo\" "), Ok(String("foo".to_string()))); - assert_eq!(from_str("\"\\u12ab\""), Ok(String("\u{12ab}".to_string()))); - assert_eq!(from_str("\"\\uAB12\""), Ok(String("\u{AB12}".to_string()))); - } - - #[test] - fn test_decode_str() { - let s = [("\"\"", ""), - ("\"foo\"", "foo"), - ("\"\\\"\"", "\""), - ("\"\\b\"", "\x08"), - ("\"\\n\"", "\n"), - ("\"\\r\"", "\r"), - ("\"\\t\"", "\t"), - ("\"\\u12ab\"", "\u{12ab}"), - ("\"\\uAB12\"", "\u{AB12}")]; - - for &(i, o) in s.iter() { - let v: string::String = super::decode(i).unwrap(); - assert_eq!(v, o); - } - } - - #[test] - fn test_read_array() { - assert_eq!(from_str("["), Err(SyntaxError(EOFWhileParsingValue, 1, 2))); - assert_eq!(from_str("[1"), Err(SyntaxError(EOFWhileParsingArray, 1, 3))); - assert_eq!(from_str("[1,"), Err(SyntaxError(EOFWhileParsingValue, 1, 4))); - assert_eq!(from_str("[1,]"), Err(SyntaxError(InvalidSyntax, 1, 4))); - assert_eq!(from_str("[6 7]"), Err(SyntaxError(InvalidSyntax, 1, 4))); - - assert_eq!(from_str("[]"), Ok(Array(vec![]))); - assert_eq!(from_str("[ ]"), Ok(Array(vec![]))); - assert_eq!(from_str("[true]"), Ok(Array(vec![Boolean(true)]))); - assert_eq!(from_str("[ false ]"), Ok(Array(vec![Boolean(false)]))); - assert_eq!(from_str("[null]"), Ok(Array(vec![Null]))); - assert_eq!(from_str("[3, 1]"), - Ok(Array(vec![U64(3), U64(1)]))); - assert_eq!(from_str("\n[3, 2]\n"), - Ok(Array(vec![U64(3), U64(2)]))); - assert_eq!(from_str("[2, [4, 1]]"), - Ok(Array(vec![U64(2), Array(vec![U64(4), U64(1)])]))); - } - - #[test] - fn test_decode_array() { - let v: Vec<()> = super::decode("[]").unwrap(); - assert_eq!(v, vec![]); - - let v: Vec<()> = super::decode("[null]").unwrap(); - assert_eq!(v, vec![()]); - - let v: Vec<bool> = super::decode("[true]").unwrap(); - assert_eq!(v, vec![true]); - - let v: Vec<int> = super::decode("[3, 1]").unwrap(); - assert_eq!(v, vec![3, 1]); - - let v: Vec<Vec<uint>> = super::decode("[[3], [1, 2]]").unwrap(); - assert_eq!(v, vec![vec![3], vec![1, 2]]); - } - - #[test] - fn test_decode_tuple() { - let t: (uint, uint, uint) = super::decode("[1, 2, 3]").unwrap(); - assert_eq!(t, (1u, 2, 3)); - - let t: (uint, string::String) = super::decode("[1, \"two\"]").unwrap(); - assert_eq!(t, (1u, "two".to_string())); - } - - #[test] - fn test_decode_tuple_malformed_types() { - assert!(super::decode::<(uint, string::String)>("[1, 2]").is_err()); - } - - #[test] - fn test_decode_tuple_malformed_length() { - assert!(super::decode::<(uint, uint)>("[1, 2, 3]").is_err()); - } - - #[test] - fn test_read_object() { - assert_eq!(from_str("{"), Err(SyntaxError(EOFWhileParsingObject, 1, 2))); - assert_eq!(from_str("{ "), Err(SyntaxError(EOFWhileParsingObject, 1, 3))); - assert_eq!(from_str("{1"), Err(SyntaxError(KeyMustBeAString, 1, 2))); - assert_eq!(from_str("{ \"a\""), Err(SyntaxError(EOFWhileParsingObject, 1, 6))); - assert_eq!(from_str("{\"a\""), Err(SyntaxError(EOFWhileParsingObject, 1, 5))); - assert_eq!(from_str("{\"a\" "), Err(SyntaxError(EOFWhileParsingObject, 1, 6))); - - assert_eq!(from_str("{\"a\" 1"), Err(SyntaxError(ExpectedColon, 1, 6))); - assert_eq!(from_str("{\"a\":"), Err(SyntaxError(EOFWhileParsingValue, 1, 6))); - assert_eq!(from_str("{\"a\":1"), Err(SyntaxError(EOFWhileParsingObject, 1, 7))); - assert_eq!(from_str("{\"a\":1 1"), Err(SyntaxError(InvalidSyntax, 1, 8))); - assert_eq!(from_str("{\"a\":1,"), Err(SyntaxError(EOFWhileParsingObject, 1, 8))); - - assert_eq!(from_str("{}").unwrap(), mk_object(&[])); - assert_eq!(from_str("{\"a\": 3}").unwrap(), - mk_object(&[("a".to_string(), U64(3))])); - - assert_eq!(from_str( - "{ \"a\": null, \"b\" : true }").unwrap(), - mk_object(&[ - ("a".to_string(), Null), - ("b".to_string(), Boolean(true))])); - assert_eq!(from_str("\n{ \"a\": null, \"b\" : true }\n").unwrap(), - mk_object(&[ - ("a".to_string(), Null), - ("b".to_string(), Boolean(true))])); - assert_eq!(from_str( - "{\"a\" : 1.0 ,\"b\": [ true ]}").unwrap(), - mk_object(&[ - ("a".to_string(), F64(1.0)), - ("b".to_string(), Array(vec![Boolean(true)])) - ])); - assert_eq!(from_str( - "{\ - \"a\": 1.0, \ - \"b\": [\ - true,\ - \"foo\\nbar\", \ - { \"c\": {\"d\": null} } \ - ]\ - }").unwrap(), - mk_object(&[ - ("a".to_string(), F64(1.0)), - ("b".to_string(), Array(vec![ - Boolean(true), - String("foo\nbar".to_string()), - mk_object(&[ - ("c".to_string(), mk_object(&[("d".to_string(), Null)])) - ]) - ])) - ])); - } - - #[test] - fn test_decode_struct() { - let s = "{ - \"inner\": [ - { \"a\": null, \"b\": 2, \"c\": [\"abc\", \"xyz\"] } - ] - }"; - - let v: Outer = super::decode(s).unwrap(); - assert_eq!( - v, - Outer { - inner: vec![ - Inner { a: (), b: 2, c: vec!["abc".to_string(), "xyz".to_string()] } - ] - } - ); - } - - #[derive(RustcDecodable)] - struct FloatStruct { - f: f64, - a: Vec<f64> - } - #[test] - fn test_decode_struct_with_nan() { - let s = "{\"f\":null,\"a\":[null,123]}"; - let obj: FloatStruct = super::decode(s).unwrap(); - assert!(obj.f.is_nan()); - assert!(obj.a[0].is_nan()); - assert_eq!(obj.a[1], 123f64); - } - - #[test] - fn test_decode_option() { - let value: Option<string::String> = super::decode("null").unwrap(); - assert_eq!(value, None); - - let value: Option<string::String> = super::decode("\"jodhpurs\"").unwrap(); - assert_eq!(value, Some("jodhpurs".to_string())); - } - - #[test] - fn test_decode_enum() { - let value: Animal = super::decode("\"Dog\"").unwrap(); - assert_eq!(value, Dog); - - let s = "{\"variant\":\"Frog\",\"fields\":[\"Henry\",349]}"; - let value: Animal = super::decode(s).unwrap(); - assert_eq!(value, Frog("Henry".to_string(), 349)); - } - - #[test] - fn test_decode_map() { - let s = "{\"a\": \"Dog\", \"b\": {\"variant\":\"Frog\",\ - \"fields\":[\"Henry\", 349]}}"; - let mut map: BTreeMap<string::String, Animal> = super::decode(s).unwrap(); - - assert_eq!(map.remove(&"a".to_string()), Some(Dog)); - assert_eq!(map.remove(&"b".to_string()), Some(Frog("Henry".to_string(), 349))); - } - - #[test] - fn test_multiline_errors() { - assert_eq!(from_str("{\n \"foo\":\n \"bar\""), - Err(SyntaxError(EOFWhileParsingObject, 3u, 8u))); - } - - #[derive(RustcDecodable)] - #[allow(dead_code)] - struct DecodeStruct { - x: f64, - y: bool, - z: string::String, - w: Vec<DecodeStruct> - } - #[derive(RustcDecodable)] - enum DecodeEnum { - A(f64), - B(string::String) - } - fn check_err<T: Decodable<Decoder, DecoderError>>(to_parse: &'static str, - expected: DecoderError) { - let res: DecodeResult<T> = match from_str(to_parse) { - Err(e) => Err(ParseError(e)), - Ok(json) => Decodable::decode(&mut Decoder::new(json)) - }; - match res { - Ok(_) => panic!("`{}` parsed & decoded ok, expecting error `{}`", - to_parse, expected), - Err(ParseError(e)) => panic!("`{}` is not valid json: {}", - to_parse, e), - Err(e) => { - assert_eq!(e, expected); - } - } - } - #[test] - fn test_decode_errors_struct() { - check_err::<DecodeStruct>("[]", ExpectedError("Object".to_string(), "[]".to_string())); - check_err::<DecodeStruct>("{\"x\": true, \"y\": true, \"z\": \"\", \"w\": []}", - ExpectedError("Number".to_string(), "true".to_string())); - check_err::<DecodeStruct>("{\"x\": 1, \"y\": [], \"z\": \"\", \"w\": []}", - ExpectedError("Boolean".to_string(), "[]".to_string())); - check_err::<DecodeStruct>("{\"x\": 1, \"y\": true, \"z\": {}, \"w\": []}", - ExpectedError("String".to_string(), "{}".to_string())); - check_err::<DecodeStruct>("{\"x\": 1, \"y\": true, \"z\": \"\", \"w\": null}", - ExpectedError("Array".to_string(), "null".to_string())); - check_err::<DecodeStruct>("{\"x\": 1, \"y\": true, \"z\": \"\"}", - MissingFieldError("w".to_string())); - } - #[test] - fn test_decode_errors_enum() { - check_err::<DecodeEnum>("{}", - MissingFieldError("variant".to_string())); - check_err::<DecodeEnum>("{\"variant\": 1}", - ExpectedError("String".to_string(), "1".to_string())); - check_err::<DecodeEnum>("{\"variant\": \"A\"}", - MissingFieldError("fields".to_string())); - check_err::<DecodeEnum>("{\"variant\": \"A\", \"fields\": null}", - ExpectedError("Array".to_string(), "null".to_string())); - check_err::<DecodeEnum>("{\"variant\": \"C\", \"fields\": []}", - UnknownVariantError("C".to_string())); - } - - #[test] - fn test_find(){ - let json_value = from_str("{\"dog\" : \"cat\"}").unwrap(); - let found_str = json_value.find("dog"); - assert!(found_str.unwrap().as_string().unwrap() == "cat"); - } - - #[test] - fn test_find_path(){ - let json_value = from_str("{\"dog\":{\"cat\": {\"mouse\" : \"cheese\"}}}").unwrap(); - let found_str = json_value.find_path(&["dog", "cat", "mouse"]); - assert!(found_str.unwrap().as_string().unwrap() == "cheese"); - } - - #[test] - fn test_search(){ - let json_value = from_str("{\"dog\":{\"cat\": {\"mouse\" : \"cheese\"}}}").unwrap(); - let found_str = json_value.search("mouse").and_then(|j| j.as_string()); - assert!(found_str.unwrap() == "cheese"); - } - - #[test] - fn test_index(){ - let json_value = from_str("{\"animals\":[\"dog\",\"cat\",\"mouse\"]}").unwrap(); - let ref array = json_value["animals"]; - assert_eq!(array[0].as_string().unwrap(), "dog"); - assert_eq!(array[1].as_string().unwrap(), "cat"); - assert_eq!(array[2].as_string().unwrap(), "mouse"); - } - - #[test] - fn test_is_object(){ - let json_value = from_str("{}").unwrap(); - assert!(json_value.is_object()); - } - - #[test] - fn test_as_object(){ - let json_value = from_str("{}").unwrap(); - let json_object = json_value.as_object(); - assert!(json_object.is_some()); - } - - #[test] - fn test_is_array(){ - let json_value = from_str("[1, 2, 3]").unwrap(); - assert!(json_value.is_array()); - } - - #[test] - fn test_as_array(){ - let json_value = from_str("[1, 2, 3]").unwrap(); - let json_array = json_value.as_array(); - let expected_length = 3; - assert!(json_array.is_some() && json_array.unwrap().len() == expected_length); - } - - #[test] - fn test_is_string(){ - let json_value = from_str("\"dog\"").unwrap(); - assert!(json_value.is_string()); - } - - #[test] - fn test_as_string(){ - let json_value = from_str("\"dog\"").unwrap(); - let json_str = json_value.as_string(); - let expected_str = "dog"; - assert_eq!(json_str, Some(expected_str)); - } - - #[test] - fn test_is_number(){ - let json_value = from_str("12").unwrap(); - assert!(json_value.is_number()); - } - - #[test] - fn test_is_i64(){ - let json_value = from_str("-12").unwrap(); - assert!(json_value.is_i64()); - - let json_value = from_str("12").unwrap(); - assert!(!json_value.is_i64()); - - let json_value = from_str("12.0").unwrap(); - assert!(!json_value.is_i64()); - } - - #[test] - fn test_is_u64(){ - let json_value = from_str("12").unwrap(); - assert!(json_value.is_u64()); - - let json_value = from_str("-12").unwrap(); - assert!(!json_value.is_u64()); - - let json_value = from_str("12.0").unwrap(); - assert!(!json_value.is_u64()); - } - - #[test] - fn test_is_f64(){ - let json_value = from_str("12").unwrap(); - assert!(!json_value.is_f64()); - - let json_value = from_str("-12").unwrap(); - assert!(!json_value.is_f64()); - - let json_value = from_str("12.0").unwrap(); - assert!(json_value.is_f64()); - - let json_value = from_str("-12.0").unwrap(); - assert!(json_value.is_f64()); - } - - #[test] - fn test_as_i64(){ - let json_value = from_str("-12").unwrap(); - let json_num = json_value.as_i64(); - assert_eq!(json_num, Some(-12)); - } - - #[test] - fn test_as_u64(){ - let json_value = from_str("12").unwrap(); - let json_num = json_value.as_u64(); - assert_eq!(json_num, Some(12)); - } - - #[test] - fn test_as_f64(){ - let json_value = from_str("12.0").unwrap(); - let json_num = json_value.as_f64(); - assert_eq!(json_num, Some(12f64)); - } - - #[test] - fn test_is_boolean(){ - let json_value = from_str("false").unwrap(); - assert!(json_value.is_boolean()); - } - - #[test] - fn test_as_boolean(){ - let json_value = from_str("false").unwrap(); - let json_bool = json_value.as_boolean(); - let expected_bool = false; - assert!(json_bool.is_some() && json_bool.unwrap() == expected_bool); - } - - #[test] - fn test_is_null(){ - let json_value = from_str("null").unwrap(); - assert!(json_value.is_null()); - } - - #[test] - fn test_as_null(){ - let json_value = from_str("null").unwrap(); - let json_null = json_value.as_null(); - let expected_null = (); - assert!(json_null.is_some() && json_null.unwrap() == expected_null); - } - - #[test] - fn test_encode_hashmap_with_numeric_key() { - use std::str::from_utf8; - use std::io::Writer; - use std::collections::HashMap; - let mut hm: HashMap<uint, bool> = HashMap::new(); - hm.insert(1, true); - let mut mem_buf = Vec::new(); - write!(&mut mem_buf, "{}", super::as_pretty_json(&hm)).unwrap(); - let json_str = from_utf8(&mem_buf.index(&FullRange)).unwrap(); - match from_str(json_str) { - Err(_) => panic!("Unable to parse json_str: {}", json_str), - _ => {} // it parsed and we are good to go - } - } - - #[test] - fn test_prettyencode_hashmap_with_numeric_key() { - use std::str::from_utf8; - use std::io::Writer; - use std::collections::HashMap; - let mut hm: HashMap<uint, bool> = HashMap::new(); - hm.insert(1, true); - let mut mem_buf = Vec::new(); - write!(&mut mem_buf, "{}", super::as_pretty_json(&hm)).unwrap(); - let json_str = from_utf8(&mem_buf.index(&FullRange)).unwrap(); - match from_str(json_str) { - Err(_) => panic!("Unable to parse json_str: {}", json_str), - _ => {} // it parsed and we are good to go - } - } - - #[test] - fn test_prettyencoder_indent_level_param() { - use std::str::from_utf8; - use std::collections::BTreeMap; - - let mut tree = BTreeMap::new(); - - tree.insert("hello".to_string(), String("guten tag".to_string())); - tree.insert("goodbye".to_string(), String("sayonara".to_string())); - - let json = Array( - // The following layout below should look a lot like - // the pretty-printed JSON (indent * x) - vec! - ( // 0x - String("greetings".to_string()), // 1x - Object(tree), // 1x + 2x + 2x + 1x - ) // 0x - // End JSON array (7 lines) - ); - - // Helper function for counting indents - fn indents(source: &str) -> uint { - let trimmed = source.trim_left_matches(' '); - source.len() - trimmed.len() - } - - // Test up to 4 spaces of indents (more?) - for i in range(0, 4u) { - let mut writer = Vec::new(); - write!(&mut writer, "{}", - super::as_pretty_json(&json).indent(i)).unwrap(); - - let printed = from_utf8(&writer.index(&FullRange)).unwrap(); - - // Check for indents at each line - let lines: Vec<&str> = printed.lines().collect(); - assert_eq!(lines.len(), 7); // JSON should be 7 lines - - assert_eq!(indents(lines[0]), 0 * i); // [ - assert_eq!(indents(lines[1]), 1 * i); // "greetings", - assert_eq!(indents(lines[2]), 1 * i); // { - assert_eq!(indents(lines[3]), 2 * i); // "hello": "guten tag", - assert_eq!(indents(lines[4]), 2 * i); // "goodbye": "sayonara" - assert_eq!(indents(lines[5]), 1 * i); // }, - assert_eq!(indents(lines[6]), 0 * i); // ] - - // Finally, test that the pretty-printed JSON is valid - from_str(printed).ok().expect("Pretty-printed JSON is invalid!"); - } - } - - #[test] - fn test_hashmap_with_numeric_key_can_handle_double_quote_delimited_key() { - use std::collections::HashMap; - use Decodable; - let json_str = "{\"1\":true}"; - let json_obj = match from_str(json_str) { - Err(_) => panic!("Unable to parse json_str: {}", json_str), - Ok(o) => o - }; - let mut decoder = Decoder::new(json_obj); - let _hm: HashMap<uint, bool> = Decodable::decode(&mut decoder).unwrap(); - } - - #[test] - fn test_hashmap_with_numeric_key_will_error_with_string_keys() { - use std::collections::HashMap; - use Decodable; - let json_str = "{\"a\":true}"; - let json_obj = match from_str(json_str) { - Err(_) => panic!("Unable to parse json_str: {}", json_str), - Ok(o) => o - }; - let mut decoder = Decoder::new(json_obj); - let result: Result<HashMap<uint, bool>, DecoderError> = Decodable::decode(&mut decoder); - assert_eq!(result, Err(ExpectedError("Number".to_string(), "a".to_string()))); - } - - fn assert_stream_equal(src: &str, - expected: Vec<(JsonEvent, Vec<StackElement>)>) { - let mut parser = Parser::new(src.chars()); - let mut i = 0; - loop { - let evt = match parser.next() { - Some(e) => e, - None => { break; } - }; - let (ref expected_evt, ref expected_stack) = expected[i]; - if !parser.stack().is_equal_to(expected_stack.as_slice()) { - panic!("Parser stack is not equal to {}", expected_stack); - } - assert_eq!(&evt, expected_evt); - i+=1; - } - } - #[test] - #[cfg_attr(target_word_size = "32", ignore)] // FIXME(#14064) - fn test_streaming_parser() { - assert_stream_equal( - r#"{ "foo":"bar", "array" : [0, 1, 2, 3, 4, 5], "idents":[null,true,false]}"#, - vec![ - (ObjectStart, vec![]), - (StringValue("bar".to_string()), vec![Key("foo")]), - (ArrayStart, vec![Key("array")]), - (U64Value(0), vec![Key("array"), Index(0)]), - (U64Value(1), vec![Key("array"), Index(1)]), - (U64Value(2), vec![Key("array"), Index(2)]), - (U64Value(3), vec![Key("array"), Index(3)]), - (U64Value(4), vec![Key("array"), Index(4)]), - (U64Value(5), vec![Key("array"), Index(5)]), - (ArrayEnd, vec![Key("array")]), - (ArrayStart, vec![Key("idents")]), - (NullValue, vec![Key("idents"), Index(0)]), - (BooleanValue(true), vec![Key("idents"), Index(1)]), - (BooleanValue(false), vec![Key("idents"), Index(2)]), - (ArrayEnd, vec![Key("idents")]), - (ObjectEnd, vec![]), - ] - ); - } - fn last_event(src: &str) -> JsonEvent { - let mut parser = Parser::new(src.chars()); - let mut evt = NullValue; - loop { - evt = match parser.next() { - Some(e) => e, - None => return evt, - } - } - } - - #[test] - #[cfg_attr(target_word_size = "32", ignore)] // FIXME(#14064) - fn test_read_object_streaming() { - assert_eq!(last_event("{ "), Error(SyntaxError(EOFWhileParsingObject, 1, 3))); - assert_eq!(last_event("{1"), Error(SyntaxError(KeyMustBeAString, 1, 2))); - assert_eq!(last_event("{ \"a\""), Error(SyntaxError(EOFWhileParsingObject, 1, 6))); - assert_eq!(last_event("{\"a\""), Error(SyntaxError(EOFWhileParsingObject, 1, 5))); - assert_eq!(last_event("{\"a\" "), Error(SyntaxError(EOFWhileParsingObject, 1, 6))); - - assert_eq!(last_event("{\"a\" 1"), Error(SyntaxError(ExpectedColon, 1, 6))); - assert_eq!(last_event("{\"a\":"), Error(SyntaxError(EOFWhileParsingValue, 1, 6))); - assert_eq!(last_event("{\"a\":1"), Error(SyntaxError(EOFWhileParsingObject, 1, 7))); - assert_eq!(last_event("{\"a\":1 1"), Error(SyntaxError(InvalidSyntax, 1, 8))); - assert_eq!(last_event("{\"a\":1,"), Error(SyntaxError(EOFWhileParsingObject, 1, 8))); - assert_eq!(last_event("{\"a\":1,}"), Error(SyntaxError(TrailingComma, 1, 8))); - - assert_stream_equal( - "{}", - vec![(ObjectStart, vec![]), (ObjectEnd, vec![])] - ); - assert_stream_equal( - "{\"a\": 3}", - vec![ - (ObjectStart, vec![]), - (U64Value(3), vec![Key("a")]), - (ObjectEnd, vec![]), - ] - ); - assert_stream_equal( - "{ \"a\": null, \"b\" : true }", - vec![ - (ObjectStart, vec![]), - (NullValue, vec![Key("a")]), - (BooleanValue(true), vec![Key("b")]), - (ObjectEnd, vec![]), - ] - ); - assert_stream_equal( - "{\"a\" : 1.0 ,\"b\": [ true ]}", - vec![ - (ObjectStart, vec![]), - (F64Value(1.0), vec![Key("a")]), - (ArrayStart, vec![Key("b")]), - (BooleanValue(true),vec![Key("b"), Index(0)]), - (ArrayEnd, vec![Key("b")]), - (ObjectEnd, vec![]), - ] - ); - assert_stream_equal( - r#"{ - "a": 1.0, - "b": [ - true, - "foo\nbar", - { "c": {"d": null} } - ] - }"#, - vec![ - (ObjectStart, vec![]), - (F64Value(1.0), vec![Key("a")]), - (ArrayStart, vec![Key("b")]), - (BooleanValue(true), vec![Key("b"), Index(0)]), - (StringValue("foo\nbar".to_string()), vec![Key("b"), Index(1)]), - (ObjectStart, vec![Key("b"), Index(2)]), - (ObjectStart, vec![Key("b"), Index(2), Key("c")]), - (NullValue, vec![Key("b"), Index(2), Key("c"), Key("d")]), - (ObjectEnd, vec![Key("b"), Index(2), Key("c")]), - (ObjectEnd, vec![Key("b"), Index(2)]), - (ArrayEnd, vec![Key("b")]), - (ObjectEnd, vec![]), - ] - ); - } - #[test] - #[cfg_attr(target_word_size = "32", ignore)] // FIXME(#14064) - fn test_read_array_streaming() { - assert_stream_equal( - "[]", - vec![ - (ArrayStart, vec![]), - (ArrayEnd, vec![]), - ] - ); - assert_stream_equal( - "[ ]", - vec![ - (ArrayStart, vec![]), - (ArrayEnd, vec![]), - ] - ); - assert_stream_equal( - "[true]", - vec![ - (ArrayStart, vec![]), - (BooleanValue(true), vec![Index(0)]), - (ArrayEnd, vec![]), - ] - ); - assert_stream_equal( - "[ false ]", - vec![ - (ArrayStart, vec![]), - (BooleanValue(false), vec![Index(0)]), - (ArrayEnd, vec![]), - ] - ); - assert_stream_equal( - "[null]", - vec![ - (ArrayStart, vec![]), - (NullValue, vec![Index(0)]), - (ArrayEnd, vec![]), - ] - ); - assert_stream_equal( - "[3, 1]", - vec![ - (ArrayStart, vec![]), - (U64Value(3), vec![Index(0)]), - (U64Value(1), vec![Index(1)]), - (ArrayEnd, vec![]), - ] - ); - assert_stream_equal( - "\n[3, 2]\n", - vec![ - (ArrayStart, vec![]), - (U64Value(3), vec![Index(0)]), - (U64Value(2), vec![Index(1)]), - (ArrayEnd, vec![]), - ] - ); - assert_stream_equal( - "[2, [4, 1]]", - vec![ - (ArrayStart, vec![]), - (U64Value(2), vec![Index(0)]), - (ArrayStart, vec![Index(1)]), - (U64Value(4), vec![Index(1), Index(0)]), - (U64Value(1), vec![Index(1), Index(1)]), - (ArrayEnd, vec![Index(1)]), - (ArrayEnd, vec![]), - ] - ); - - assert_eq!(last_event("["), Error(SyntaxError(EOFWhileParsingValue, 1, 2))); - - assert_eq!(from_str("["), Err(SyntaxError(EOFWhileParsingValue, 1, 2))); - assert_eq!(from_str("[1"), Err(SyntaxError(EOFWhileParsingArray, 1, 3))); - assert_eq!(from_str("[1,"), Err(SyntaxError(EOFWhileParsingValue, 1, 4))); - assert_eq!(from_str("[1,]"), Err(SyntaxError(InvalidSyntax, 1, 4))); - assert_eq!(from_str("[6 7]"), Err(SyntaxError(InvalidSyntax, 1, 4))); - - } - #[test] - fn test_trailing_characters_streaming() { - assert_eq!(last_event("nulla"), Error(SyntaxError(TrailingCharacters, 1, 5))); - assert_eq!(last_event("truea"), Error(SyntaxError(TrailingCharacters, 1, 5))); - assert_eq!(last_event("falsea"), Error(SyntaxError(TrailingCharacters, 1, 6))); - assert_eq!(last_event("1a"), Error(SyntaxError(TrailingCharacters, 1, 2))); - assert_eq!(last_event("[]a"), Error(SyntaxError(TrailingCharacters, 1, 3))); - assert_eq!(last_event("{}a"), Error(SyntaxError(TrailingCharacters, 1, 3))); - } - #[test] - fn test_read_identifiers_streaming() { - assert_eq!(Parser::new("null".chars()).next(), Some(NullValue)); - assert_eq!(Parser::new("true".chars()).next(), Some(BooleanValue(true))); - assert_eq!(Parser::new("false".chars()).next(), Some(BooleanValue(false))); - - assert_eq!(last_event("n"), Error(SyntaxError(InvalidSyntax, 1, 2))); - assert_eq!(last_event("nul"), Error(SyntaxError(InvalidSyntax, 1, 4))); - assert_eq!(last_event("t"), Error(SyntaxError(InvalidSyntax, 1, 2))); - assert_eq!(last_event("truz"), Error(SyntaxError(InvalidSyntax, 1, 4))); - assert_eq!(last_event("f"), Error(SyntaxError(InvalidSyntax, 1, 2))); - assert_eq!(last_event("faz"), Error(SyntaxError(InvalidSyntax, 1, 3))); - } - - #[test] - fn test_stack() { - let mut stack = Stack::new(); - - assert!(stack.is_empty()); - assert!(stack.len() == 0); - assert!(!stack.last_is_index()); - - stack.push_index(0); - stack.bump_index(); - - assert!(stack.len() == 1); - assert!(stack.is_equal_to(&[Index(1)])); - assert!(stack.starts_with(&[Index(1)])); - assert!(stack.ends_with(&[Index(1)])); - assert!(stack.last_is_index()); - assert!(stack.get(0) == Index(1)); - - stack.push_key("foo".to_string()); - - assert!(stack.len() == 2); - assert!(stack.is_equal_to(&[Index(1), Key("foo")])); - assert!(stack.starts_with(&[Index(1), Key("foo")])); - assert!(stack.starts_with(&[Index(1)])); - assert!(stack.ends_with(&[Index(1), Key("foo")])); - assert!(stack.ends_with(&[Key("foo")])); - assert!(!stack.last_is_index()); - assert!(stack.get(0) == Index(1)); - assert!(stack.get(1) == Key("foo")); - - stack.push_key("bar".to_string()); - - assert!(stack.len() == 3); - assert!(stack.is_equal_to(&[Index(1), Key("foo"), Key("bar")])); - assert!(stack.starts_with(&[Index(1)])); - assert!(stack.starts_with(&[Index(1), Key("foo")])); - assert!(stack.starts_with(&[Index(1), Key("foo"), Key("bar")])); - assert!(stack.ends_with(&[Key("bar")])); - assert!(stack.ends_with(&[Key("foo"), Key("bar")])); - assert!(stack.ends_with(&[Index(1), Key("foo"), Key("bar")])); - assert!(!stack.last_is_index()); - assert!(stack.get(0) == Index(1)); - assert!(stack.get(1) == Key("foo")); - assert!(stack.get(2) == Key("bar")); - - stack.pop(); - - assert!(stack.len() == 2); - assert!(stack.is_equal_to(&[Index(1), Key("foo")])); - assert!(stack.starts_with(&[Index(1), Key("foo")])); - assert!(stack.starts_with(&[Index(1)])); - assert!(stack.ends_with(&[Index(1), Key("foo")])); - assert!(stack.ends_with(&[Key("foo")])); - assert!(!stack.last_is_index()); - assert!(stack.get(0) == Index(1)); - assert!(stack.get(1) == Key("foo")); - } - - #[test] - fn test_to_json() { - use std::collections::{HashMap,BTreeMap}; - use super::ToJson; - - let array2 = Array(vec!(U64(1), U64(2))); - let array3 = Array(vec!(U64(1), U64(2), U64(3))); - let object = { - let mut tree_map = BTreeMap::new(); - tree_map.insert("a".to_string(), U64(1)); - tree_map.insert("b".to_string(), U64(2)); - Object(tree_map) - }; - - assert_eq!(array2.to_json(), array2); - assert_eq!(object.to_json(), object); - assert_eq!(3_i.to_json(), I64(3)); - assert_eq!(4_i8.to_json(), I64(4)); - assert_eq!(5_i16.to_json(), I64(5)); - assert_eq!(6_i32.to_json(), I64(6)); - assert_eq!(7_i64.to_json(), I64(7)); - assert_eq!(8_u.to_json(), U64(8)); - assert_eq!(9_u8.to_json(), U64(9)); - assert_eq!(10_u16.to_json(), U64(10)); - assert_eq!(11_u32.to_json(), U64(11)); - assert_eq!(12_u64.to_json(), U64(12)); - assert_eq!(13.0_f32.to_json(), F64(13.0_f64)); - assert_eq!(14.0_f64.to_json(), F64(14.0_f64)); - assert_eq!(().to_json(), Null); - assert_eq!(f32::INFINITY.to_json(), Null); - assert_eq!(f64::NAN.to_json(), Null); - assert_eq!(true.to_json(), Boolean(true)); - assert_eq!(false.to_json(), Boolean(false)); - assert_eq!("abc".to_json(), String("abc".to_string())); - assert_eq!("abc".to_string().to_json(), String("abc".to_string())); - assert_eq!((1u, 2u).to_json(), array2); - assert_eq!((1u, 2u, 3u).to_json(), array3); - assert_eq!([1u, 2].to_json(), array2); - assert_eq!((&[1u, 2, 3]).to_json(), array3); - assert_eq!((vec![1u, 2]).to_json(), array2); - assert_eq!(vec!(1u, 2, 3).to_json(), array3); - let mut tree_map = BTreeMap::new(); - tree_map.insert("a".to_string(), 1u); - tree_map.insert("b".to_string(), 2); - assert_eq!(tree_map.to_json(), object); - let mut hash_map = HashMap::new(); - hash_map.insert("a".to_string(), 1u); - hash_map.insert("b".to_string(), 2); - assert_eq!(hash_map.to_json(), object); - assert_eq!(Some(15i).to_json(), I64(15)); - assert_eq!(Some(15u).to_json(), U64(15)); - assert_eq!(None::<int>.to_json(), Null); - } - - #[bench] - fn bench_streaming_small(b: &mut Bencher) { - b.iter( || { - let mut parser = Parser::new( - r#"{ - "a": 1.0, - "b": [ - true, - "foo\nbar", - { "c": {"d": null} } - ] - }"#.chars() - ); - loop { - match parser.next() { - None => return, - _ => {} - } - } - }); - } - #[bench] - fn bench_small(b: &mut Bencher) { - b.iter( || { - let _ = from_str(r#"{ - "a": 1.0, - "b": [ - true, - "foo\nbar", - { "c": {"d": null} } - ] - }"#); - }); - } - - fn big_json() -> string::String { - let mut src = "[\n".to_string(); - for _ in range(0i, 500) { - src.push_str(r#"{ "a": true, "b": null, "c":3.1415, "d": "Hello world", "e": \ - [1,2,3]},"#); - } - src.push_str("{}]"); - return src; - } - - #[bench] - fn bench_streaming_large(b: &mut Bencher) { - let src = big_json(); - b.iter( || { - let mut parser = Parser::new(src.chars()); - loop { - match parser.next() { - None => return, - _ => {} - } - } - }); - } - #[bench] - fn bench_large(b: &mut Bencher) { - let src = big_json(); - b.iter( || { let _ = from_str(src.as_slice()); }); - } -} diff --git a/src/libserialize/lib.rs b/src/libserialize/lib.rs index 8fe15f00ded..3dfed466e60 100644 --- a/src/libserialize/lib.rs +++ b/src/libserialize/lib.rs @@ -23,48 +23,23 @@ Core encoding and decoding interfaces. html_root_url = "http://doc.rust-lang.org/nightly/", html_playground_url = "http://play.rust-lang.org/")] #![allow(unknown_features)] -#![feature(macro_rules, default_type_params, phase, slicing_syntax, globs)] -#![feature(unboxed_closures)] -#![feature(associated_types)] +#![feature(slicing_syntax)] // test harness access -#[cfg(test)] -extern crate test; - -#[cfg(stage0)] -#[phase(plugin, link)] -extern crate log; - -#[cfg(not(stage0))] -#[macro_use] -extern crate log; +#[cfg(test)] extern crate test; +#[macro_use] extern crate log; extern crate unicode; - extern crate collections; pub use self::serialize::{Decoder, Encoder, Decodable, Encodable, DecoderHelpers, EncoderHelpers}; -#[cfg(stage0)] -#[path = "serialize_stage0.rs"] -mod serialize; -#[cfg(not(stage0))] mod serialize; - -#[cfg(stage0)] -#[path = "collection_impls_stage0.rs"] -mod collection_impls; -#[cfg(not(stage0))] mod collection_impls; pub mod base64; pub mod hex; - -#[cfg(stage0)] -#[path = "json_stage0.rs"] -pub mod json; -#[cfg(not(stage0))] pub mod json; mod rustc_serialize { diff --git a/src/libserialize/serialize_stage0.rs b/src/libserialize/serialize_stage0.rs deleted file mode 100644 index 87ff5922c1a..00000000000 --- a/src/libserialize/serialize_stage0.rs +++ /dev/null @@ -1,630 +0,0 @@ -// Copyright 2012-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. - -//! Support code for encoding and decoding types. - -/* -Core encoding and decoding interfaces. -*/ - -use std::ops::FullRange; -use std::path; -use std::rc::Rc; -use std::cell::{Cell, RefCell}; -use std::sync::Arc; - -pub trait Encoder<E> { - // Primitive types: - fn emit_nil(&mut self) -> Result<(), E>; - fn emit_uint(&mut self, v: uint) -> Result<(), E>; - fn emit_u64(&mut self, v: u64) -> Result<(), E>; - fn emit_u32(&mut self, v: u32) -> Result<(), E>; - fn emit_u16(&mut self, v: u16) -> Result<(), E>; - fn emit_u8(&mut self, v: u8) -> Result<(), E>; - fn emit_int(&mut self, v: int) -> Result<(), E>; - fn emit_i64(&mut self, v: i64) -> Result<(), E>; - fn emit_i32(&mut self, v: i32) -> Result<(), E>; - fn emit_i16(&mut self, v: i16) -> Result<(), E>; - fn emit_i8(&mut self, v: i8) -> Result<(), E>; - fn emit_bool(&mut self, v: bool) -> Result<(), E>; - fn emit_f64(&mut self, v: f64) -> Result<(), E>; - fn emit_f32(&mut self, v: f32) -> Result<(), E>; - fn emit_char(&mut self, v: char) -> Result<(), E>; - fn emit_str(&mut self, v: &str) -> Result<(), E>; - - // Compound types: - fn emit_enum<F>(&mut self, name: &str, f: F) -> Result<(), E> where - F: FnOnce(&mut Self) -> Result<(), E>; - - fn emit_enum_variant<F>(&mut self, v_name: &str, - v_id: uint, - len: uint, - f: F) -> Result<(), E> where - F: FnOnce(&mut Self) -> Result<(), E>; - fn emit_enum_variant_arg<F>(&mut self, a_idx: uint, f: F) -> Result<(), E> where - F: FnOnce(&mut Self) -> Result<(), E>; - - fn emit_enum_struct_variant<F>(&mut self, v_name: &str, - v_id: uint, - len: uint, - f: F) -> Result<(), E> where - F: FnOnce(&mut Self) -> Result<(), E>; - fn emit_enum_struct_variant_field<F>(&mut self, - f_name: &str, - f_idx: uint, - f: F) -> Result<(), E> where - F: FnOnce(&mut Self) -> Result<(), E>; - - fn emit_struct<F>(&mut self, name: &str, len: uint, f: F) -> Result<(), E> where - F: FnOnce(&mut Self) -> Result<(), E>; - fn emit_struct_field<F>(&mut self, f_name: &str, f_idx: uint, f: F) -> Result<(), E> where - F: FnOnce(&mut Self) -> Result<(), E>; - - fn emit_tuple<F>(&mut self, len: uint, f: F) -> Result<(), E> where - F: FnOnce(&mut Self) -> Result<(), E>; - fn emit_tuple_arg<F>(&mut self, idx: uint, f: F) -> Result<(), E> where - F: FnOnce(&mut Self) -> Result<(), E>; - - fn emit_tuple_struct<F>(&mut self, name: &str, len: uint, f: F) -> Result<(), E> where - F: FnOnce(&mut Self) -> Result<(), E>; - fn emit_tuple_struct_arg<F>(&mut self, f_idx: uint, f: F) -> Result<(), E> where - F: FnOnce(&mut Self) -> Result<(), E>; - - // Specialized types: - fn emit_option<F>(&mut self, f: F) -> Result<(), E> where - F: FnOnce(&mut Self) -> Result<(), E>; - fn emit_option_none(&mut self) -> Result<(), E>; - fn emit_option_some<F>(&mut self, f: F) -> Result<(), E> where - F: FnOnce(&mut Self) -> Result<(), E>; - - fn emit_seq<F>(&mut self, len: uint, f: F) -> Result<(), E> where - F: FnOnce(&mut Self) -> Result<(), E>; - fn emit_seq_elt<F>(&mut self, idx: uint, f: F) -> Result<(), E> where - F: FnOnce(&mut Self) -> Result<(), E>; - - fn emit_map<F>(&mut self, len: uint, f: F) -> Result<(), E> where - F: FnOnce(&mut Self) -> Result<(), E>; - fn emit_map_elt_key<F>(&mut self, idx: uint, f: F) -> Result<(), E> where - F: FnMut(&mut Self) -> Result<(), E>; - fn emit_map_elt_val<F>(&mut self, idx: uint, f: F) -> Result<(), E> where - F: FnOnce(&mut Self) -> Result<(), E>; -} - -pub trait Decoder<E> { - // Primitive types: - fn read_nil(&mut self) -> Result<(), E>; - fn read_uint(&mut self) -> Result<uint, E>; - fn read_u64(&mut self) -> Result<u64, E>; - fn read_u32(&mut self) -> Result<u32, E>; - fn read_u16(&mut self) -> Result<u16, E>; - fn read_u8(&mut self) -> Result<u8, E>; - fn read_int(&mut self) -> Result<int, E>; - fn read_i64(&mut self) -> Result<i64, E>; - fn read_i32(&mut self) -> Result<i32, E>; - fn read_i16(&mut self) -> Result<i16, E>; - fn read_i8(&mut self) -> Result<i8, E>; - fn read_bool(&mut self) -> Result<bool, E>; - fn read_f64(&mut self) -> Result<f64, E>; - fn read_f32(&mut self) -> Result<f32, E>; - fn read_char(&mut self) -> Result<char, E>; - fn read_str(&mut self) -> Result<String, E>; - - // Compound types: - fn read_enum<T, F>(&mut self, name: &str, f: F) -> Result<T, E> where - F: FnOnce(&mut Self) -> Result<T, E>; - - fn read_enum_variant<T, F>(&mut self, names: &[&str], f: F) -> Result<T, E> where - F: FnMut(&mut Self, uint) -> Result<T, E>; - fn read_enum_variant_arg<T, F>(&mut self, a_idx: uint, f: F) -> Result<T, E> where - F: FnOnce(&mut Self) -> Result<T, E>; - - fn read_enum_struct_variant<T, F>(&mut self, names: &[&str], f: F) -> Result<T, E> where - F: FnMut(&mut Self, uint) -> Result<T, E>; - fn read_enum_struct_variant_field<T, F>(&mut self, - &f_name: &str, - f_idx: uint, - f: F) - -> Result<T, E> where - F: FnOnce(&mut Self) -> Result<T, E>; - - fn read_struct<T, F>(&mut self, s_name: &str, len: uint, f: F) -> Result<T, E> where - F: FnOnce(&mut Self) -> Result<T, E>; - fn read_struct_field<T, F>(&mut self, - f_name: &str, - f_idx: uint, - f: F) - -> Result<T, E> where - F: FnOnce(&mut Self) -> Result<T, E>; - - fn read_tuple<T, F>(&mut self, len: uint, f: F) -> Result<T, E> where - F: FnOnce(&mut Self) -> Result<T, E>; - fn read_tuple_arg<T, F>(&mut self, a_idx: uint, f: F) -> Result<T, E> where - F: FnOnce(&mut Self) -> Result<T, E>; - - fn read_tuple_struct<T, F>(&mut self, s_name: &str, len: uint, f: F) -> Result<T, E> where - F: FnOnce(&mut Self) -> Result<T, E>; - fn read_tuple_struct_arg<T, F>(&mut self, a_idx: uint, f: F) -> Result<T, E> where - F: FnOnce(&mut Self) -> Result<T, E>; - - // Specialized types: - fn read_option<T, F>(&mut self, f: F) -> Result<T, E> where - F: FnMut(&mut Self, bool) -> Result<T, E>; - - fn read_seq<T, F>(&mut self, f: F) -> Result<T, E> where - F: FnOnce(&mut Self, uint) -> Result<T, E>; - fn read_seq_elt<T, F>(&mut self, idx: uint, f: F) -> Result<T, E> where - F: FnOnce(&mut Self) -> Result<T, E>; - - fn read_map<T, F>(&mut self, f: F) -> Result<T, E> where - F: FnOnce(&mut Self, uint) -> Result<T, E>; - fn read_map_elt_key<T, F>(&mut self, idx: uint, f: F) -> Result<T, E> where - F: FnOnce(&mut Self) -> Result<T, E>; - fn read_map_elt_val<T, F>(&mut self, idx: uint, f: F) -> Result<T, E> where - F: FnOnce(&mut Self) -> Result<T, E>; - - // Failure - fn error(&mut self, err: &str) -> E; -} - -pub trait Encodable<S:Encoder<E>, E> { - fn encode(&self, s: &mut S) -> Result<(), E>; -} - -pub trait Decodable<D:Decoder<E>, E> { - fn decode(d: &mut D) -> Result<Self, E>; -} - -impl<E, S:Encoder<E>> Encodable<S, E> for uint { - fn encode(&self, s: &mut S) -> Result<(), E> { - s.emit_uint(*self) - } -} - -impl<E, D:Decoder<E>> Decodable<D, E> for uint { - fn decode(d: &mut D) -> Result<uint, E> { - d.read_uint() - } -} - -impl<E, S:Encoder<E>> Encodable<S, E> for u8 { - fn encode(&self, s: &mut S) -> Result<(), E> { - s.emit_u8(*self) - } -} - -impl<E, D:Decoder<E>> Decodable<D, E> for u8 { - fn decode(d: &mut D) -> Result<u8, E> { - d.read_u8() - } -} - -impl<E, S:Encoder<E>> Encodable<S, E> for u16 { - fn encode(&self, s: &mut S) -> Result<(), E> { - s.emit_u16(*self) - } -} - -impl<E, D:Decoder<E>> Decodable<D, E> for u16 { - fn decode(d: &mut D) -> Result<u16, E> { - d.read_u16() - } -} - -impl<E, S:Encoder<E>> Encodable<S, E> for u32 { - fn encode(&self, s: &mut S) -> Result<(), E> { - s.emit_u32(*self) - } -} - -impl<E, D:Decoder<E>> Decodable<D, E> for u32 { - fn decode(d: &mut D) -> Result<u32, E> { - d.read_u32() - } -} - -impl<E, S:Encoder<E>> Encodable<S, E> for u64 { - fn encode(&self, s: &mut S) -> Result<(), E> { - s.emit_u64(*self) - } -} - -impl<E, D:Decoder<E>> Decodable<D, E> for u64 { - fn decode(d: &mut D) -> Result<u64, E> { - d.read_u64() - } -} - -impl<E, S:Encoder<E>> Encodable<S, E> for int { - fn encode(&self, s: &mut S) -> Result<(), E> { - s.emit_int(*self) - } -} - -impl<E, D:Decoder<E>> Decodable<D, E> for int { - fn decode(d: &mut D) -> Result<int, E> { - d.read_int() - } -} - -impl<E, S:Encoder<E>> Encodable<S, E> for i8 { - fn encode(&self, s: &mut S) -> Result<(), E> { - s.emit_i8(*self) - } -} - -impl<E, D:Decoder<E>> Decodable<D, E> for i8 { - fn decode(d: &mut D) -> Result<i8, E> { - d.read_i8() - } -} - -impl<E, S:Encoder<E>> Encodable<S, E> for i16 { - fn encode(&self, s: &mut S) -> Result<(), E> { - s.emit_i16(*self) - } -} - -impl<E, D:Decoder<E>> Decodable<D, E> for i16 { - fn decode(d: &mut D) -> Result<i16, E> { - d.read_i16() - } -} - -impl<E, S:Encoder<E>> Encodable<S, E> for i32 { - fn encode(&self, s: &mut S) -> Result<(), E> { - s.emit_i32(*self) - } -} - -impl<E, D:Decoder<E>> Decodable<D, E> for i32 { - fn decode(d: &mut D) -> Result<i32, E> { - d.read_i32() - } -} - -impl<E, S:Encoder<E>> Encodable<S, E> for i64 { - fn encode(&self, s: &mut S) -> Result<(), E> { - s.emit_i64(*self) - } -} - -impl<E, D:Decoder<E>> Decodable<D, E> for i64 { - fn decode(d: &mut D) -> Result<i64, E> { - d.read_i64() - } -} - -impl<E, S:Encoder<E>> Encodable<S, E> for str { - fn encode(&self, s: &mut S) -> Result<(), E> { - s.emit_str(self) - } -} - -impl<E, S:Encoder<E>> Encodable<S, E> for String { - fn encode(&self, s: &mut S) -> Result<(), E> { - s.emit_str(self.index(&FullRange)) - } -} - -impl<E, D:Decoder<E>> Decodable<D, E> for String { - fn decode(d: &mut D) -> Result<String, E> { - d.read_str() - } -} - -impl<E, S:Encoder<E>> Encodable<S, E> for f32 { - fn encode(&self, s: &mut S) -> Result<(), E> { - s.emit_f32(*self) - } -} - -impl<E, D:Decoder<E>> Decodable<D, E> for f32 { - fn decode(d: &mut D) -> Result<f32, E> { - d.read_f32() - } -} - -impl<E, S:Encoder<E>> Encodable<S, E> for f64 { - fn encode(&self, s: &mut S) -> Result<(), E> { - s.emit_f64(*self) - } -} - -impl<E, D:Decoder<E>> Decodable<D, E> for f64 { - fn decode(d: &mut D) -> Result<f64, E> { - d.read_f64() - } -} - -impl<E, S:Encoder<E>> Encodable<S, E> for bool { - fn encode(&self, s: &mut S) -> Result<(), E> { - s.emit_bool(*self) - } -} - -impl<E, D:Decoder<E>> Decodable<D, E> for bool { - fn decode(d: &mut D) -> Result<bool, E> { - d.read_bool() - } -} - -impl<E, S:Encoder<E>> Encodable<S, E> for char { - fn encode(&self, s: &mut S) -> Result<(), E> { - s.emit_char(*self) - } -} - -impl<E, D:Decoder<E>> Decodable<D, E> for char { - fn decode(d: &mut D) -> Result<char, E> { - d.read_char() - } -} - -impl<E, S:Encoder<E>> Encodable<S, E> for () { - fn encode(&self, s: &mut S) -> Result<(), E> { - s.emit_nil() - } -} - -impl<E, D:Decoder<E>> Decodable<D, E> for () { - fn decode(d: &mut D) -> Result<(), E> { - d.read_nil() - } -} - -impl<'a, E, S: Encoder<E>, T: ?Sized + 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: ?Sized + 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> { - 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> { - (**self).encode(s) - } -} - -impl<E, D:Decoder<E>,T:Decodable<D, E>> Decodable<D, E> for Rc<T> { - #[inline] - fn decode(d: &mut D) -> Result<Rc<T>, E> { - Ok(Rc::new(try!(Decodable::decode(d)))) - } -} - -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() { - try!(s.emit_seq_elt(i, |s| e.encode(s))) - } - Ok(()) - }) - } -} - -impl<E, S:Encoder<E>,T:Encodable<S, E>> Encodable<S, E> for Vec<T> { - fn encode(&self, s: &mut S) -> Result<(), E> { - s.emit_seq(self.len(), |s| { - for (i, e) in self.iter().enumerate() { - try!(s.emit_seq_elt(i, |s| e.encode(s))) - } - Ok(()) - }) - } -} - -impl<E, D:Decoder<E>,T:Decodable<D, E>> Decodable<D, E> for Vec<T> { - fn decode(d: &mut D) -> Result<Vec<T>, E> { - d.read_seq(|d, len| { - let mut v = Vec::with_capacity(len); - for i in range(0, len) { - v.push(try!(d.read_seq_elt(i, |d| Decodable::decode(d)))); - } - Ok(v) - }) - } -} - -impl<E, S:Encoder<E>,T:Encodable<S, E>> Encodable<S, E> for Option<T> { - fn encode(&self, s: &mut S) -> Result<(), E> { - s.emit_option(|s| { - match *self { - None => s.emit_option_none(), - Some(ref v) => s.emit_option_some(|s| v.encode(s)), - } - }) - } -} - -impl<E, D:Decoder<E>,T:Decodable<D, E>> Decodable<D, E> for Option<T> { - fn decode(d: &mut D) -> Result<Option<T>, E> { - d.read_option(|d, b| { - if b { - Ok(Some(try!(Decodable::decode(d)))) - } else { - Ok(None) - } - }) - } -} - -macro_rules! peel { - ($name:ident, $($other:ident,)*) => (tuple! { $($other,)* }) -} - -/// Evaluates to the number of identifiers passed to it, for example: `count_idents!(a, b, c) == 3 -macro_rules! count_idents { - () => { 0u }; - ($_i:ident $(, $rest:ident)*) => { 1 + count_idents!($($rest),*) } -} - -macro_rules! tuple { - () => (); - ( $($name:ident,)+ ) => ( - impl<E, D:Decoder<E>,$($name:Decodable<D, E>),*> Decodable<D,E> for ($($name,)*) { - #[allow(non_snake_case)] - fn decode(d: &mut D) -> Result<($($name,)*), E> { - let len: uint = count_idents!($($name),*); - d.read_tuple(len, |d| { - let mut i = 0; - let ret = ($(try!(d.read_tuple_arg({ i+=1; i-1 }, |d| -> Result<$name,E> { - Decodable::decode(d) - })),)*); - return Ok(ret); - }) - } - } - impl<E, S:Encoder<E>,$($name:Encodable<S, E>),*> Encodable<S, E> for ($($name,)*) { - #[allow(non_snake_case)] - fn encode(&self, s: &mut S) -> Result<(), E> { - let ($(ref $name,)*) = *self; - let mut n = 0; - $(let $name = $name; n += 1;)* - s.emit_tuple(n, |s| { - let mut i = 0; - $(try!(s.emit_tuple_arg({ i+=1; i-1 }, |s| $name.encode(s)));)* - Ok(()) - }) - } - } - peel! { $($name,)* } - ) -} - -tuple! { T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, } - -impl<E, S: Encoder<E>> Encodable<S, E> for path::posix::Path { - fn encode(&self, e: &mut S) -> Result<(), E> { - self.as_vec().encode(e) - } -} - -impl<E, D: Decoder<E>> Decodable<D, E> for path::posix::Path { - fn decode(d: &mut D) -> Result<path::posix::Path, E> { - let bytes: Vec<u8> = try!(Decodable::decode(d)); - Ok(path::posix::Path::new(bytes)) - } -} - -impl<E, S: Encoder<E>> Encodable<S, E> for path::windows::Path { - fn encode(&self, e: &mut S) -> Result<(), E> { - self.as_vec().encode(e) - } -} - -impl<E, D: Decoder<E>> Decodable<D, E> for path::windows::Path { - fn decode(d: &mut D) -> Result<path::windows::Path, E> { - let bytes: Vec<u8> = try!(Decodable::decode(d)); - Ok(path::windows::Path::new(bytes)) - } -} - -impl<E, S: Encoder<E>, T: Encodable<S, E> + Copy> Encodable<S, E> for Cell<T> { - fn encode(&self, s: &mut S) -> Result<(), E> { - self.get().encode(s) - } -} - -impl<E, D: Decoder<E>, T: Decodable<D, E> + Copy> Decodable<D, E> for Cell<T> { - fn decode(d: &mut D) -> Result<Cell<T>, E> { - Ok(Cell::new(try!(Decodable::decode(d)))) - } -} - -// FIXME: #15036 -// Should use `try_borrow`, returning a -// `encoder.error("attempting to Encode borrowed RefCell")` -// from `encode` when `try_borrow` returns `None`. - -impl<E, S: Encoder<E>, T: Encodable<S, E>> Encodable<S, E> for RefCell<T> { - fn encode(&self, s: &mut S) -> Result<(), E> { - self.borrow().encode(s) - } -} - -impl<E, D: Decoder<E>, T: Decodable<D, E>> Decodable<D, E> for RefCell<T> { - fn decode(d: &mut D) -> Result<RefCell<T>, E> { - Ok(RefCell::new(try!(Decodable::decode(d)))) - } -} - -impl<E, S:Encoder<E>, T:Encodable<S, E>> Encodable<S, E> for Arc<T> { - fn encode(&self, s: &mut S) -> Result<(), E> { - (**self).encode(s) - } -} - -impl<E, D:Decoder<E>,T:Decodable<D, E>+Send+Sync> Decodable<D, E> for Arc<T> { - fn decode(d: &mut D) -> Result<Arc<T>, E> { - Ok(Arc::new(try!(Decodable::decode(d)))) - } -} - -// ___________________________________________________________________________ -// Helper routines - -pub trait EncoderHelpers<E> { - fn emit_from_vec<T, F>(&mut self, v: &[T], f: F) -> Result<(), E> where - F: FnMut(&mut Self, &T) -> Result<(), E>; -} - -impl<E, S:Encoder<E>> EncoderHelpers<E> for S { - fn emit_from_vec<T, F>(&mut self, v: &[T], mut f: F) -> Result<(), E> where - F: FnMut(&mut S, &T) -> Result<(), E>, - { - self.emit_seq(v.len(), |this| { - for (i, e) in v.iter().enumerate() { - try!(this.emit_seq_elt(i, |this| { - f(this, e) - })); - } - Ok(()) - }) - } -} - -pub trait DecoderHelpers<E> { - fn read_to_vec<T, F>(&mut self, f: F) -> Result<Vec<T>, E> where - F: FnMut(&mut Self) -> Result<T, E>; -} - -impl<E, D:Decoder<E>> DecoderHelpers<E> for D { - fn read_to_vec<T, F>(&mut self, mut f: F) -> Result<Vec<T>, E> where F: - FnMut(&mut D) -> Result<T, E>, - { - self.read_seq(|this, len| { - let mut v = Vec::with_capacity(len); - for i in range(0, len) { - v.push(try!(this.read_seq_elt(i, |this| f(this)))); - } - Ok(v) - }) - } -} diff --git a/src/libstd/collections/hash/set.rs b/src/libstd/collections/hash/set.rs index 10e8ca6c622..e598923f11c 100644 --- a/src/libstd/collections/hash/set.rs +++ b/src/libstd/collections/hash/set.rs @@ -22,9 +22,6 @@ use iter::{Iterator, IteratorExt, FromIterator, Map, Chain, Extend}; use ops::{BitOr, BitAnd, BitXor, Sub}; use option::Option::{Some, None, self}; -// NOTE: for old macros; remove after the next snapshot -#[cfg(stage0)] use result::Result::{Ok, Err}; - use super::map::{self, HashMap, Keys, INITIAL_CAPACITY}; // Future Optimization (FIXME!) diff --git a/src/libstd/io/buffered.rs b/src/libstd/io/buffered.rs index c54a5ffa665..74c503e6f2b 100644 --- a/src/libstd/io/buffered.rs +++ b/src/libstd/io/buffered.rs @@ -23,9 +23,6 @@ use slice::{SliceExt}; use slice; use vec::Vec; -// NOTE: for old macros; remove after the next snapshot -#[cfg(stage0)] use result::Result::Err; - /// Wraps a Reader and buffers input from it /// /// It can be excessively inefficient to work directly with a `Reader`. For diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index 010cb814732..5de66c02b53 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -285,8 +285,7 @@ pub mod stdio; pub mod timer; pub mod util; -#[cfg_attr(stage0, macro_escape)] -#[cfg_attr(not(stage0), macro_use)] +#[macro_use] pub mod test; /// The default buffer size for various I/O operations diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index b9f226c5aca..25cc574bb5d 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -104,42 +104,24 @@ html_playground_url = "http://play.rust-lang.org/")] #![allow(unknown_features)] -#![feature(macro_rules, globs, linkage, thread_local, asm)] -#![feature(default_type_params, phase, lang_items, unsafe_destructor)] +#![feature(linkage, thread_local, asm)] +#![feature(phase, lang_items, unsafe_destructor)] #![feature(slicing_syntax, unboxed_closures)] -#![feature(old_orphan_check)] -#![feature(associated_types)] // Don't link to std. We are std. #![no_std] #![deny(missing_docs)] -#![reexport_test_harness_main = "test_main"] - -#[cfg(all(test, stage0))] -#[phase(plugin, link)] -extern crate log; - -#[cfg(all(test, not(stage0)))] +#[cfg(test)] #[macro_use] extern crate log; -#[cfg(stage0)] -#[phase(plugin, link)] -extern crate core; - -#[cfg(not(stage0))] #[macro_use] #[macro_reexport(assert, assert_eq, debug_assert, debug_assert_eq, - unreachable, unimplemented, write, writeln)] + unreachable, unimplemented, write, writeln)] extern crate core; -#[cfg(stage0)] -#[phase(plugin, link)] -extern crate "collections" as core_collections; - -#[cfg(not(stage0))] #[macro_use] #[macro_reexport(vec)] extern crate "collections" as core_collections; @@ -190,18 +172,10 @@ pub use unicode::char; /* Exported macros */ -#[cfg(stage0)] -#[cfg_attr(stage0, macro_escape)] -#[cfg_attr(not(stage0), macro_use)] -pub mod macros_stage0; - -#[cfg(not(stage0))] -#[cfg_attr(stage0, macro_escape)] -#[cfg_attr(not(stage0), macro_use)] +#[macro_use] pub mod macros; -#[cfg_attr(stage0, macro_escape)] -#[cfg_attr(not(stage0), macro_use)] +#[macro_use] pub mod bitflags; mod rtdeps; @@ -214,18 +188,15 @@ pub mod prelude; /* Primitive types */ #[path = "num/float_macros.rs"] -#[cfg_attr(stage0, macro_escape)] -#[cfg_attr(not(stage0), macro_use)] +#[macro_use] mod float_macros; #[path = "num/int_macros.rs"] -#[cfg_attr(stage0, macro_escape)] -#[cfg_attr(not(stage0), macro_use)] +#[macro_use] mod int_macros; #[path = "num/uint_macros.rs"] -#[cfg_attr(stage0, macro_escape)] -#[cfg_attr(not(stage0), macro_use)] +#[macro_use] mod uint_macros; #[path = "num/int.rs"] pub mod int; @@ -253,8 +224,7 @@ pub mod num; /* Runtime and platform support */ -#[cfg_attr(stage0, macro_escape)] -#[cfg_attr(not(stage0), macro_use)] +#[macro_use] pub mod thread_local; pub mod dynamic_lib; diff --git a/src/libstd/macros_stage0.rs b/src/libstd/macros_stage0.rs deleted file mode 100644 index 48d62e73e13..00000000000 --- a/src/libstd/macros_stage0.rs +++ /dev/null @@ -1,648 +0,0 @@ -// 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. - -//! Standard library macros -//! -//! This modules contains a set of macros which are exported from the standard -//! library. Each macro is available for use when linking against the standard -//! library. - -#![experimental] - -/// The entry point for panic of Rust tasks. -/// -/// This macro is used to inject panic into a Rust task, causing the task to -/// unwind and panic entirely. Each task's panic can be reaped as the -/// `Box<Any>` type, and the single-argument form of the `panic!` macro will be -/// the value which is transmitted. -/// -/// The multi-argument form of this macro panics with a string and has the -/// `format!` syntax for building a string. -/// -/// # Example -/// -/// ```should_fail -/// # #![allow(unreachable_code)] -/// panic!(); -/// panic!("this is a terrible mistake!"); -/// panic!(4i); // panic with the value of 4 to be collected elsewhere -/// panic!("this is a {} {message}", "fancy", message = "message"); -/// ``` -#[macro_export] -macro_rules! panic { - () => ({ - panic!("explicit panic") - }); - ($msg:expr) => ({ - // static requires less code at runtime, more constant data - static _FILE_LINE: (&'static str, uint) = (file!(), line!()); - ::std::rt::begin_unwind($msg, &_FILE_LINE) - }); - ($fmt:expr, $($arg:tt)*) => ({ - // The leading _'s are to avoid dead code warnings if this is - // used inside a dead function. Just `#[allow(dead_code)]` is - // insufficient, since the user may have - // `#[forbid(dead_code)]` and which cannot be overridden. - static _FILE_LINE: (&'static str, uint) = (file!(), line!()); - ::std::rt::begin_unwind_fmt(format_args!($fmt, $($arg)*), &_FILE_LINE) - - }); -} - -/// Ensure that a boolean expression is `true` at runtime. -/// -/// This will invoke the `panic!` macro if the provided expression cannot be -/// evaluated to `true` at runtime. -/// -/// # Example -/// -/// ``` -/// // the panic message for these assertions is the stringified value of the -/// // expression given. -/// assert!(true); -/// # fn some_computation() -> bool { true } -/// assert!(some_computation()); -/// -/// // assert with a custom message -/// # let x = true; -/// assert!(x, "x wasn't true!"); -/// # let a = 3i; let b = 27i; -/// assert!(a + b == 30, "a = {}, b = {}", a, b); -/// ``` -#[macro_export] -macro_rules! assert { - ($cond:expr) => ( - if !$cond { - panic!(concat!("assertion failed: ", stringify!($cond))) - } - ); - ($cond:expr, $($arg:expr),+) => ( - if !$cond { - panic!($($arg),+) - } - ); -} - -/// Asserts that two expressions are equal to each other, testing equality in -/// both directions. -/// -/// On panic, this macro will print the values of the expressions. -/// -/// # Example -/// -/// ``` -/// let a = 3i; -/// let b = 1i + 2i; -/// assert_eq!(a, b); -/// ``` -#[macro_export] -macro_rules! assert_eq { - ($left:expr , $right:expr) => ({ - match (&($left), &($right)) { - (left_val, right_val) => { - // check both directions of equality.... - if !((*left_val == *right_val) && - (*right_val == *left_val)) { - panic!("assertion failed: `(left == right) && (right == left)` \ - (left: `{}`, right: `{}`)", *left_val, *right_val) - } - } - } - }) -} - -/// Ensure that a boolean expression is `true` at runtime. -/// -/// This will invoke the `panic!` macro if the provided expression cannot be -/// evaluated to `true` at runtime. -/// -/// Unlike `assert!`, `debug_assert!` statements can be disabled by passing -/// `--cfg ndebug` to the compiler. This makes `debug_assert!` useful for -/// checks that are too expensive to be present in a release build but may be -/// helpful during development. -/// -/// # Example -/// -/// ``` -/// // the panic message for these assertions is the stringified value of the -/// // expression given. -/// debug_assert!(true); -/// # fn some_expensive_computation() -> bool { true } -/// debug_assert!(some_expensive_computation()); -/// -/// // assert with a custom message -/// # let x = true; -/// debug_assert!(x, "x wasn't true!"); -/// # let a = 3i; let b = 27i; -/// debug_assert!(a + b == 30, "a = {}, b = {}", a, b); -/// ``` -#[macro_export] -macro_rules! debug_assert { - ($($arg:tt)*) => (if cfg!(not(ndebug)) { assert!($($arg)*); }) -} - -/// Asserts that two expressions are equal to each other, testing equality in -/// both directions. -/// -/// On panic, this macro will print the values of the expressions. -/// -/// Unlike `assert_eq!`, `debug_assert_eq!` statements can be disabled by -/// passing `--cfg ndebug` to the compiler. This makes `debug_assert_eq!` -/// useful for checks that are too expensive to be present in a release build -/// but may be helpful during development. -/// -/// # Example -/// -/// ``` -/// let a = 3i; -/// let b = 1i + 2i; -/// debug_assert_eq!(a, b); -/// ``` -#[macro_export] -macro_rules! debug_assert_eq { - ($($arg:tt)*) => (if cfg!(not(ndebug)) { assert_eq!($($arg)*); }) -} - -/// A utility macro for indicating unreachable code. -/// -/// This is useful any time that the compiler can't determine that some code is unreachable. For -/// example: -/// -/// * Match arms with guard conditions. -/// * Loops that dynamically terminate. -/// * Iterators that dynamically terminate. -/// -/// # Panics -/// -/// This will always panic. -/// -/// # Examples -/// -/// Match arms: -/// -/// ```rust -/// fn foo(x: Option<int>) { -/// match x { -/// Some(n) if n >= 0 => println!("Some(Non-negative)"), -/// Some(n) if n < 0 => println!("Some(Negative)"), -/// Some(_) => unreachable!(), // compile error if commented out -/// None => println!("None") -/// } -/// } -/// ``` -/// -/// Iterators: -/// -/// ```rust -/// fn divide_by_three(x: u32) -> u32 { // one of the poorest implementations of x/3 -/// for i in std::iter::count(0_u32, 1) { -/// if 3*i < i { panic!("u32 overflow"); } -/// if x < 3*i { return i-1; } -/// } -/// unreachable!(); -/// } -/// ``` -#[macro_export] -macro_rules! unreachable { - () => ({ - panic!("internal error: entered unreachable code") - }); - ($msg:expr) => ({ - unreachable!("{}", $msg) - }); - ($fmt:expr, $($arg:tt)*) => ({ - panic!(concat!("internal error: entered unreachable code: ", $fmt), $($arg)*) - }); -} - -/// A standardised placeholder for marking unfinished code. It panics with the -/// message `"not yet implemented"` when executed. -#[macro_export] -macro_rules! unimplemented { - () => (panic!("not yet implemented")) -} - -/// Use the syntax described in `std::fmt` to create a value of type `String`. -/// See `std::fmt` for more information. -/// -/// # Example -/// -/// ``` -/// format!("test"); -/// format!("hello {}", "world!"); -/// format!("x = {}, y = {y}", 10i, y = 30i); -/// ``` -#[macro_export] -#[stable] -macro_rules! format { - ($($arg:tt)*) => (::std::fmt::format(format_args!($($arg)*))) -} - -/// Use the `format!` syntax to write data into a buffer of type `&mut Writer`. -/// See `std::fmt` for more information. -/// -/// # Example -/// -/// ``` -/// # #![allow(unused_must_use)] -/// -/// let mut w = Vec::new(); -/// write!(&mut w, "test"); -/// write!(&mut w, "formatted {}", "arguments"); -/// ``` -#[macro_export] -#[stable] -macro_rules! write { - ($dst:expr, $($arg:tt)*) => ((&mut *$dst).write_fmt(format_args!($($arg)*))) -} - -/// Equivalent to the `write!` macro, except that a newline is appended after -/// the message is written. -#[macro_export] -#[stable] -macro_rules! writeln { - ($dst:expr, $fmt:expr $($arg:tt)*) => ( - write!($dst, concat!($fmt, "\n") $($arg)*) - ) -} - -/// Equivalent to the `println!` macro except that a newline is not printed at -/// the end of the message. -#[macro_export] -#[stable] -macro_rules! print { - ($($arg:tt)*) => (::std::io::stdio::print_args(format_args!($($arg)*))) -} - -/// Macro for printing to a task's stdout handle. -/// -/// Each task can override its stdout handle via `std::io::stdio::set_stdout`. -/// The syntax of this macro is the same as that used for `format!`. For more -/// information, see `std::fmt` and `std::io::stdio`. -/// -/// # Example -/// -/// ``` -/// println!("hello there!"); -/// println!("format {} arguments", "some"); -/// ``` -#[macro_export] -#[stable] -macro_rules! println { - ($($arg:tt)*) => (::std::io::stdio::println_args(format_args!($($arg)*))) -} - -/// Helper macro for unwrapping `Result` values while returning early with an -/// error if the value of the expression is `Err`. For more information, see -/// `std::io`. -#[macro_export] -macro_rules! try { - ($expr:expr) => ({ - match $expr { - Ok(val) => val, - Err(err) => return Err(::std::error::FromError::from_error(err)) - } - }) -} - -/// Create a `std::vec::Vec` containing the arguments. -#[macro_export] -macro_rules! vec { - ($($x:expr),*) => ({ - let xs: ::std::boxed::Box<[_]> = box [$($x),*]; - ::std::slice::SliceExt::into_vec(xs) - }); - ($($x:expr,)*) => (vec![$($x),*]) -} - -/// A macro to select an event from a number of receivers. -/// -/// This macro is used to wait for the first event to occur on a number of -/// receivers. It places no restrictions on the types of receivers given to -/// this macro, this can be viewed as a heterogeneous select. -/// -/// # Example -/// -/// ``` -/// use std::thread::Thread; -/// use std::sync::mpsc::channel; -/// -/// let (tx1, rx1) = channel(); -/// let (tx2, rx2) = channel(); -/// # fn long_running_task() {} -/// # fn calculate_the_answer() -> int { 42i } -/// -/// Thread::spawn(move|| { long_running_task(); tx1.send(()) }).detach(); -/// Thread::spawn(move|| { tx2.send(calculate_the_answer()) }).detach(); -/// -/// select! ( -/// _ = rx1.recv() => println!("the long running task finished first"), -/// answer = rx2.recv() => { -/// println!("the answer was: {}", answer.unwrap()); -/// } -/// ) -/// ``` -/// -/// For more information about select, see the `std::sync::mpsc::Select` structure. -#[macro_export] -#[experimental] -macro_rules! select { - ( - $($name:pat = $rx:ident.$meth:ident() => $code:expr),+ - ) => ({ - use std::sync::mpsc::Select; - let sel = Select::new(); - $( let mut $rx = sel.handle(&$rx); )+ - unsafe { - $( $rx.add(); )+ - } - let ret = sel.wait(); - $( if ret == $rx.id() { let $name = $rx.$meth(); $code } else )+ - { unreachable!() } - }) -} - -// When testing the standard library, we link to the liblog crate to get the -// logging macros. In doing so, the liblog crate was linked against the real -// version of libstd, and uses a different std::fmt module than the test crate -// uses. To get around this difference, we redefine the log!() macro here to be -// just a dumb version of what it should be. -#[cfg(test)] -macro_rules! log { - ($lvl:expr, $($args:tt)*) => ( - if log_enabled!($lvl) { println!($($args)*) } - ) -} - -/// Built-in macros to the compiler itself. -/// -/// These macros do not have any corresponding definition with a `macro_rules!` -/// macro, but are documented here. Their implementations can be found hardcoded -/// into libsyntax itself. -#[cfg(dox)] -pub mod builtin { - /// The core macro for formatted string creation & output. - /// - /// This macro produces a value of type `fmt::Arguments`. This value can be - /// passed to the functions in `std::fmt` for performing useful functions. - /// All other formatting macros (`format!`, `write!`, `println!`, etc) are - /// proxied through this one. - /// - /// For more information, see the documentation in `std::fmt`. - /// - /// # Example - /// - /// ```rust - /// use std::fmt; - /// - /// let s = fmt::format(format_args!("hello {}", "world")); - /// assert_eq!(s, format!("hello {}", "world")); - /// - /// ``` - #[macro_export] - macro_rules! format_args { ($fmt:expr $($args:tt)*) => ({ - /* compiler built-in */ - }) } - - /// Inspect an environment variable at compile time. - /// - /// This macro will expand to the value of the named environment variable at - /// compile time, yielding an expression of type `&'static str`. - /// - /// If the environment variable is not defined, then a compilation error - /// will be emitted. To not emit a compile error, use the `option_env!` - /// macro instead. - /// - /// # Example - /// - /// ```rust - /// let path: &'static str = env!("PATH"); - /// println!("the $PATH variable at the time of compiling was: {}", path); - /// ``` - #[macro_export] - macro_rules! env { ($name:expr) => ({ /* compiler built-in */ }) } - - /// Optionally inspect an environment variable at compile time. - /// - /// If the named environment variable is present at compile time, this will - /// expand into an expression of type `Option<&'static str>` whose value is - /// `Some` of the value of the environment variable. If the environment - /// variable is not present, then this will expand to `None`. - /// - /// A compile time error is never emitted when using this macro regardless - /// of whether the environment variable is present or not. - /// - /// # Example - /// - /// ```rust - /// let key: Option<&'static str> = option_env!("SECRET_KEY"); - /// println!("the secret key might be: {}", key); - /// ``` - #[macro_export] - macro_rules! option_env { ($name:expr) => ({ /* compiler built-in */ }) } - - /// Concatenate literals into a static byte slice. - /// - /// This macro takes any number of comma-separated literal expressions, - /// yielding an expression of type `&'static [u8]` which is the - /// concatenation (left to right) of all the literals in their byte format. - /// - /// This extension currently only supports string literals, character - /// literals, and integers less than 256. The byte slice returned is the - /// utf8-encoding of strings and characters. - /// - /// # Example - /// - /// ``` - /// let rust = bytes!("r", 'u', "st", 255); - /// assert_eq!(rust[1], b'u'); - /// assert_eq!(rust[4], 255); - /// ``` - #[macro_export] - macro_rules! bytes { ($($e:expr),*) => ({ /* compiler built-in */ }) } - - /// Concatenate identifiers into one identifier. - /// - /// This macro takes any number of comma-separated identifiers, and - /// concatenates them all into one, yielding an expression which is a new - /// identifier. Note that hygiene makes it such that this macro cannot - /// capture local variables, and macros are only allowed in item, - /// statement or expression position, meaning this macro may be difficult to - /// use in some situations. - /// - /// # Example - /// - /// ``` - /// #![feature(concat_idents)] - /// - /// # fn main() { - /// fn foobar() -> int { 23 } - /// - /// let f = concat_idents!(foo, bar); - /// println!("{}", f()); - /// # } - /// ``` - #[macro_export] - macro_rules! concat_idents { - ($($e:ident),*) => ({ /* compiler built-in */ }) - } - - /// Concatenates literals into a static string slice. - /// - /// This macro takes any number of comma-separated literals, yielding an - /// expression of type `&'static str` which represents all of the literals - /// concatenated left-to-right. - /// - /// Integer and floating point literals are stringified in order to be - /// concatenated. - /// - /// # Example - /// - /// ``` - /// let s = concat!("test", 10i, 'b', true); - /// assert_eq!(s, "test10btrue"); - /// ``` - #[macro_export] - macro_rules! concat { ($($e:expr),*) => ({ /* compiler built-in */ }) } - - /// A macro which expands to the line number on which it was invoked. - /// - /// The expanded expression has type `uint`, and the returned line is not - /// the invocation of the `line!()` macro itself, but rather the first macro - /// invocation leading up to the invocation of the `line!()` macro. - /// - /// # Example - /// - /// ``` - /// let current_line = line!(); - /// println!("defined on line: {}", current_line); - /// ``` - #[macro_export] - macro_rules! line { () => ({ /* compiler built-in */ }) } - - /// A macro which expands to the column number on which it was invoked. - /// - /// The expanded expression has type `uint`, and the returned column is not - /// the invocation of the `column!()` macro itself, but rather the first macro - /// invocation leading up to the invocation of the `column!()` macro. - /// - /// # Example - /// - /// ``` - /// let current_col = column!(); - /// println!("defined on column: {}", current_col); - /// ``` - #[macro_export] - macro_rules! column { () => ({ /* compiler built-in */ }) } - - /// A macro which expands to the file name from which it was invoked. - /// - /// The expanded expression has type `&'static str`, and the returned file - /// is not the invocation of the `file!()` macro itself, but rather the - /// first macro invocation leading up to the invocation of the `file!()` - /// macro. - /// - /// # Example - /// - /// ``` - /// let this_file = file!(); - /// println!("defined in file: {}", this_file); - /// ``` - #[macro_export] - macro_rules! file { () => ({ /* compiler built-in */ }) } - - /// A macro which stringifies its argument. - /// - /// This macro will yield an expression of type `&'static str` which is the - /// stringification of all the tokens passed to the macro. No restrictions - /// are placed on the syntax of the macro invocation itself. - /// - /// # Example - /// - /// ``` - /// let one_plus_one = stringify!(1 + 1); - /// assert_eq!(one_plus_one, "1 + 1"); - /// ``` - #[macro_export] - macro_rules! stringify { ($t:tt) => ({ /* compiler built-in */ }) } - - /// Includes a utf8-encoded file as a string. - /// - /// This macro will yield an expression of type `&'static str` which is the - /// contents of the filename specified. The file is located relative to the - /// current file (similarly to how modules are found), - /// - /// # Example - /// - /// ```rust,ignore - /// let secret_key = include_str!("secret-key.ascii"); - /// ``` - #[macro_export] - macro_rules! include_str { ($file:expr) => ({ /* compiler built-in */ }) } - - /// Includes a file as a byte slice. - /// - /// This macro will yield an expression of type `&'static [u8]` which is - /// the contents of the filename specified. The file is located relative to - /// the current file (similarly to how modules are found), - /// - /// # Example - /// - /// ```rust,ignore - /// let secret_key = include_bytes!("secret-key.bin"); - /// ``` - #[macro_export] - macro_rules! include_bytes { ($file:expr) => ({ /* compiler built-in */ }) } - - /// Deprecated alias for `include_bytes!()`. - #[macro_export] - macro_rules! include_bin { ($file:expr) => ({ /* compiler built-in */}) } - - /// Expands to a string that represents the current module path. - /// - /// The current module path can be thought of as the hierarchy of modules - /// leading back up to the crate root. The first component of the path - /// returned is the name of the crate currently being compiled. - /// - /// # Example - /// - /// ```rust - /// mod test { - /// pub fn foo() { - /// assert!(module_path!().ends_with("test")); - /// } - /// } - /// - /// test::foo(); - /// ``` - #[macro_export] - macro_rules! module_path { () => ({ /* compiler built-in */ }) } - - /// Boolean evaluation of configuration flags. - /// - /// In addition to the `#[cfg]` attribute, this macro is provided to allow - /// boolean expression evaluation of configuration flags. This frequently - /// leads to less duplicated code. - /// - /// The syntax given to this macro is the same syntax as the `cfg` - /// attribute. - /// - /// # Example - /// - /// ```rust - /// let my_directory = if cfg!(windows) { - /// "windows-specific-directory" - /// } else { - /// "unix-directory" - /// }; - /// ``` - #[macro_export] - macro_rules! cfg { ($cfg:tt) => ({ /* compiler built-in */ }) } -} diff --git a/src/libstd/rand/os.rs b/src/libstd/rand/os.rs index a79a6e35ebc..b64da403c8b 100644 --- a/src/libstd/rand/os.rs +++ b/src/libstd/rand/os.rs @@ -28,9 +28,6 @@ mod imp { use mem; use os::errno; - // NOTE: for old macros; remove after the next snapshot - #[cfg(stage0)] use result::Result::Err; - #[cfg(all(target_os = "linux", any(target_arch = "x86_64", target_arch = "x86", diff --git a/src/libstd/rt/mod.rs b/src/libstd/rt/mod.rs index e556888a470..2d54fc36ca8 100644 --- a/src/libstd/rt/mod.rs +++ b/src/libstd/rt/mod.rs @@ -39,8 +39,7 @@ pub use alloc::heap; pub mod backtrace; // Internals -#[cfg_attr(stage0, macro_escape)] -#[cfg_attr(not(stage0), macro_use)] +#[macro_use] mod macros; // These should be refactored/moved/made private over time diff --git a/src/libstd/thread_local/mod.rs b/src/libstd/thread_local/mod.rs index e0cbaa8ca50..c11162c09bc 100644 --- a/src/libstd/thread_local/mod.rs +++ b/src/libstd/thread_local/mod.rs @@ -40,8 +40,7 @@ use prelude::v1::*; use cell::UnsafeCell; -#[cfg_attr(stage0, macro_escape)] -#[cfg_attr(not(stage0), macro_use)] +#[macro_use] pub mod scoped; // Sure wish we had macro hygiene, no? diff --git a/src/libstd/time/duration.rs b/src/libstd/time/duration.rs index 9bd6f78300e..a651d927c14 100644 --- a/src/libstd/time/duration.rs +++ b/src/libstd/time/duration.rs @@ -19,9 +19,6 @@ use option::Option::{Some, None}; use num::Int; use result::Result::Ok; -// NOTE: for old macros; remove after the next snapshot -#[cfg(stage0)] use result::Result::Err; - /// The number of nanoseconds in a microsecond. const NANOS_PER_MICRO: i32 = 1000; /// The number of nanoseconds in a millisecond. diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 10cdea791b8..45233dd5225 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -195,28 +195,12 @@ impl Name { /// A mark represents a unique id associated with a macro expansion pub type Mrk = u32; -#[cfg(stage0)] -impl<S: Encoder<E>, E> Encodable<S, E> for Ident { - fn encode(&self, s: &mut S) -> Result<(), E> { - s.emit_str(token::get_ident(*self).get()) - } -} - -#[cfg(not(stage0))] impl Encodable for Ident { fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { s.emit_str(token::get_ident(*self).get()) } } -#[cfg(stage0)] -impl<D: Decoder<E>, E> Decodable<D, E> for Ident { - fn decode(d: &mut D) -> Result<Ident, E> { - Ok(str_to_ident(try!(d.read_str()).index(&FullRange))) - } -} - -#[cfg(not(stage0))] impl Decodable for Ident { fn decode<D: Decoder>(d: &mut D) -> Result<Ident, D::Error> { Ok(str_to_ident(try!(d.read_str()).index(&FullRange))) diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index 04753bdf652..31fe23847d9 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -120,15 +120,6 @@ impl PartialEq for Span { impl Eq for Span {} -#[cfg(stage0)] -impl<S:Encoder<E>, E> Encodable<S, E> for Span { - /* Note #1972 -- spans are encoded but not decoded */ - fn encode(&self, s: &mut S) -> Result<(), E> { - s.emit_nil() - } -} - -#[cfg(not(stage0))] impl Encodable for Span { /* Note #1972 -- spans are encoded but not decoded */ fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { @@ -136,14 +127,6 @@ impl Encodable for Span { } } -#[cfg(stage0)] -impl<D:Decoder<E>, E> Decodable<D, E> for Span { - fn decode(_d: &mut D) -> Result<Span, E> { - Ok(DUMMY_SP) - } -} - -#[cfg(not(stage0))] impl Decodable for Span { fn decode<D: Decoder>(_d: &mut D) -> Result<Span, D::Error> { Ok(DUMMY_SP) diff --git a/src/libsyntax/ext/deriving/generic/mod.rs b/src/libsyntax/ext/deriving/generic/mod.rs index 8f13a4475b7..50b3559f369 100644 --- a/src/libsyntax/ext/deriving/generic/mod.rs +++ b/src/libsyntax/ext/deriving/generic/mod.rs @@ -602,7 +602,7 @@ impl<'a> MethodDef<'a> { }; let mut f = self.combine_substructure.borrow_mut(); let f: &mut CombineSubstructureFunc = &mut *f; - f.call_mut((cx, trait_.span, &substructure)) + f(cx, trait_.span, &substructure) } fn get_ret_ty(&self, @@ -1365,8 +1365,8 @@ pub fn cs_fold<F>(use_foldl: bool, } }, EnumNonMatchingCollapsed(ref all_args, _, tuple) => - enum_nonmatch_f.call_mut((cx, trait_span, (all_args.index(&FullRange), tuple), - substructure.nonself_args)), + enum_nonmatch_f(cx, trait_span, (all_args.index(&FullRange), tuple), + substructure.nonself_args), StaticEnum(..) | StaticStruct(..) => { cx.span_bug(trait_span, "static function in `derive`") } @@ -1405,8 +1405,8 @@ pub fn cs_same_method<F>(f: F, f(cx, trait_span, called) }, EnumNonMatchingCollapsed(ref all_self_args, _, tuple) => - enum_nonmatch_f.call_mut((cx, trait_span, (all_self_args.index(&FullRange), tuple), - substructure.nonself_args)), + enum_nonmatch_f(cx, trait_span, (all_self_args.index(&FullRange), tuple), + substructure.nonself_args), StaticEnum(..) | StaticStruct(..) => { cx.span_bug(trait_span, "static function in `derive`") } diff --git a/src/libsyntax/ext/deriving/mod.rs b/src/libsyntax/ext/deriving/mod.rs index b44aa9dbd9f..43a0e0606f8 100644 --- a/src/libsyntax/ext/deriving/mod.rs +++ b/src/libsyntax/ext/deriving/mod.rs @@ -73,7 +73,7 @@ pub fn expand_meta_derive(cx: &mut ExtCtxt, MetaWord(ref tname) => { macro_rules! expand { ($func:path) => ($func(cx, titem.span, &**titem, item, - |i| push.call_mut((i,)))) + |i| push(i))) } match tname.get() { diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index 2c36a02d44f..38a369bdb57 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -44,7 +44,7 @@ static KNOWN_FEATURES: &'static [(&'static str, Status)] = &[ ("non_ascii_idents", Active), ("thread_local", Active), ("link_args", Active), - ("phase", Active), // NOTE(stage0): switch to Removed after next snapshot + ("phase", Removed), ("plugin_registrar", Active), ("log_syntax", Active), ("trace_macros", Active), diff --git a/src/libsyntax/lib.rs b/src/libsyntax/lib.rs index b7bfd346d50..9e14f9dd1ea 100644 --- a/src/libsyntax/lib.rs +++ b/src/libsyntax/lib.rs @@ -23,25 +23,15 @@ html_root_url = "http://doc.rust-lang.org/nightly/")] #![allow(unknown_features)] -#![feature(macro_rules, globs, default_type_params, phase, slicing_syntax)] +#![feature(slicing_syntax)] #![feature(quote, unsafe_destructor)] -#![feature(unboxed_closures)] -#![feature(old_orphan_check)] -#![feature(associated_types)] extern crate arena; extern crate fmt_macros; extern crate serialize; extern crate term; extern crate libc; - -#[cfg(stage0)] -#[phase(plugin, link)] -extern crate log; - -#[cfg(not(stage0))] -#[macro_use] -extern crate log; +#[macro_use] extern crate log; extern crate "serialize" as rustc_serialize; // used by deriving diff --git a/src/libsyntax/owned_slice.rs b/src/libsyntax/owned_slice.rs index 2a27431a086..707e540a17b 100644 --- a/src/libsyntax/owned_slice.rs +++ b/src/libsyntax/owned_slice.rs @@ -82,31 +82,12 @@ impl<T> FromIterator<T> for OwnedSlice<T> { } } -#[cfg(stage0)] -impl<S: Encoder<E>, T: Encodable<S, E>, E> Encodable<S, E> for OwnedSlice<T> { - fn encode(&self, s: &mut S) -> Result<(), E> { - self.as_slice().encode(s) - } -} - -#[cfg(not(stage0))] impl<T: Encodable> Encodable for OwnedSlice<T> { fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { self.as_slice().encode(s) } } -#[cfg(stage0)] -impl<D: Decoder<E>, T: Decodable<D, E>, E> Decodable<D, E> for OwnedSlice<T> { - fn decode(d: &mut D) -> Result<OwnedSlice<T>, E> { - Ok(OwnedSlice::from_vec(match Decodable::decode(d) { - Ok(t) => t, - Err(e) => return Err(e) - })) - } -} - -#[cfg(not(stage0))] impl<T: Decodable> Decodable for OwnedSlice<T> { fn decode<D: Decoder>(d: &mut D) -> Result<OwnedSlice<T>, D::Error> { Ok(OwnedSlice::from_vec(match Decodable::decode(d) { diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index 82e2c8136a4..3335566801a 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -24,8 +24,7 @@ use std::num::Int; use std::str; use std::iter; -#[cfg_attr(stage0, macro_escape)] -#[cfg_attr(not(stage0), macro_use)] +#[macro_use] pub mod parser; pub mod lexer; diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index 306ab303411..bdf96104697 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -684,29 +684,12 @@ impl<'a> PartialEq<InternedString > for &'a str { } } -#[cfg(stage0)] -impl<D:Decoder<E>, E> Decodable<D, E> for InternedString { - fn decode(d: &mut D) -> Result<InternedString, E> { - Ok(get_name(get_ident_interner().intern( - try!(d.read_str()).index(&FullRange)))) - } -} - -#[cfg(not(stage0))] impl Decodable for InternedString { fn decode<D: Decoder>(d: &mut D) -> Result<InternedString, D::Error> { Ok(get_name(get_ident_interner().intern(try!(d.read_str()).index(&FullRange)))) } } -#[cfg(stage0)] -impl<S:Encoder<E>, E> Encodable<S, E> for InternedString { - fn encode(&self, s: &mut S) -> Result<(), E> { - s.emit_str(self.string.index(&FullRange)) - } -} - -#[cfg(not(stage0))] impl Encodable for InternedString { fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { s.emit_str(self.string.index(&FullRange)) diff --git a/src/libsyntax/ptr.rs b/src/libsyntax/ptr.rs index 13eda7bb88f..8abb46011e6 100644 --- a/src/libsyntax/ptr.rs +++ b/src/libsyntax/ptr.rs @@ -111,28 +111,12 @@ impl<S, T: Hash<S>> Hash<S> for P<T> { } } -#[cfg(stage0)] -impl<E, D: Decoder<E>, T: 'static + Decodable<D, E>> Decodable<D, E> for P<T> { - fn decode(d: &mut D) -> Result<P<T>, E> { - Decodable::decode(d).map(P) - } -} - -#[cfg(not(stage0))] impl<T: 'static + Decodable> Decodable for P<T> { fn decode<D: Decoder>(d: &mut D) -> Result<P<T>, D::Error> { Decodable::decode(d).map(P) } } -#[cfg(stage0)] -impl<E, S: Encoder<E>, T: Encodable<S, E>> Encodable<S, E> for P<T> { - fn encode(&self, s: &mut S) -> Result<(), E> { - (**self).encode(s) - } -} - -#[cfg(not(stage0))] impl<T: Encodable> Encodable for P<T> { fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { (**self).encode(s) diff --git a/src/libterm/lib.rs b/src/libterm/lib.rs index dd42bede13a..c953f591d80 100644 --- a/src/libterm/lib.rs +++ b/src/libterm/lib.rs @@ -48,17 +48,10 @@ html_playground_url = "http://play.rust-lang.org/")] #![allow(unknown_features)] -#![feature(macro_rules, phase, slicing_syntax, globs)] - +#![feature(slicing_syntax)] #![deny(missing_docs)] -#[cfg(stage0)] -#[phase(plugin, link)] -extern crate log; - -#[cfg(not(stage0))] -#[macro_use] -extern crate log; +#[macro_use] extern crate log; pub use terminfo::TerminfoTerminal; #[cfg(windows)] diff --git a/src/libtest/lib.rs b/src/libtest/lib.rs index 3ee2b1b2224..a7912216630 100644 --- a/src/libtest/lib.rs +++ b/src/libtest/lib.rs @@ -30,11 +30,7 @@ #![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png", html_favicon_url = "http://www.rust-lang.org/favicon.ico", html_root_url = "http://doc.rust-lang.org/nightly/")] - -#![allow(unknown_features)] -#![feature(asm, globs, slicing_syntax)] -#![feature(unboxed_closures, default_type_params)] -#![feature(old_orphan_check)] +#![feature(asm, slicing_syntax)] extern crate getopts; extern crate regex; diff --git a/src/libunicode/lib.rs b/src/libunicode/lib.rs index a3884d0c86e..db98b429e40 100644 --- a/src/libunicode/lib.rs +++ b/src/libunicode/lib.rs @@ -28,8 +28,7 @@ html_root_url = "http://doc.rust-lang.org/nightly/", html_playground_url = "http://play.rust-lang.org/")] #![no_std] -#![feature(globs, macro_rules, slicing_syntax, unboxed_closures)] -#![feature(associated_types)] +#![feature(slicing_syntax)] extern crate core; diff --git a/src/snapshots.txt b/src/snapshots.txt index 5c21a8a8abf..d6134898cbd 100644 --- a/src/snapshots.txt +++ b/src/snapshots.txt @@ -1,3 +1,12 @@ +S 2015-01-06 340ac04 + freebsd-x86_64 5413b8931d7076e90c873e0cc7a43e0793c2b17a + linux-i386 cacb8e3ad15937916e455d8f63e740c30a807b10 + linux-x86_64 e4a7f73959130671a5eb9107d593d37b43e571b0 + macos-i386 da6b0b7d12964e815175eb1fe5fc495098ca3125 + macos-x86_64 edf8a109e175be6e5bc44484261d2248946f1dd1 + winnt-i386 34fc7d9905d5845cdc7901f661d43a463534a20d + winnt-x86_64 99580840f5763f517f47cd55ff373e4d116b018d + S 2015-01-04 b2085d9 freebsd-x86_64 50ccb6bf9c0645d0746a5167493a39b2be40c2d4 linux-i386 b880b98d832c9a049b8ef6a50df50061e363de5a |
