diff --git a/benches/bench_log.rs b/benches/bench_log.rs index 25e0bd7a..f284221c 100644 --- a/benches/bench_log.rs +++ b/benches/bench_log.rs @@ -9,7 +9,7 @@ extern crate serialize; extern crate test; use std::io; -use std::io::{MemWriter, ByRefWriter}; +use std::io::ByRefWriter; use test::Bencher; use serde::de; @@ -622,10 +622,10 @@ fn bench_serializer_mem_writer(b: &mut Bencher) { b.iter(|| { //let _json = json::to_str(&log).unwrap(); - let wr = MemWriter::with_capacity(1024); + let wr = Vec::with_capacity(1024); let mut serializer = json::Serializer::new(wr); log.serialize(&mut serializer).unwrap(); - let _json = serializer.unwrap().unwrap(); + let _json = serializer.unwrap(); }); } @@ -838,14 +838,13 @@ fn bench_manual_mem_writer_no_escape(b: &mut Bencher) { let log = Log::new(); let _s = r#"{"timestamp":2837513946597,"zone_id":123456,"zone_plan":1,"http":{"protocol":2,"status":200,"host_status":503,"up_status":520,"method":1,"content_type":"text/html","user_agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36","referer":"https://www.cloudflare.com/","request_uri":"/cdn-cgi/trace"},"origin":{"ip":"1.2.3.4","port":8000,"hostname":"www.example.com","protocol":2},"country":238,"cache_status":3,"server_ip":"192.168.1.1","server_name":"metal.cloudflare.com","remote_ip":"10.1.2.3","bytes_dlv":123456,"ray_id":"10c73629cce30078-LAX"}"#; - let mut wr = MemWriter::with_capacity(1024); + let mut wr = Vec::with_capacity(1024); manual_no_escape(wr.by_ref(), &log); - b.bytes = wr.unwrap().len() as u64; + b.bytes = wr.len() as u64; b.iter(|| { - let mut wr = MemWriter::with_capacity(1024); + let mut wr = Vec::with_capacity(1024); manual_no_escape(wr.by_ref(), &log); - let _json = wr.unwrap(); //let _json = String::from_utf8(_json).unwrap(); //assert_eq!(_s, _json.as_slice()); @@ -857,14 +856,13 @@ fn bench_manual_mem_writer_escape(b: &mut Bencher) { let log = Log::new(); let _s = r#"{"timestamp":2837513946597,"zone_id":123456,"zone_plan":1,"http":{"protocol":2,"status":200,"host_status":503,"up_status":520,"method":1,"content_type":"text/html","user_agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36","referer":"https://www.cloudflare.com/","request_uri":"/cdn-cgi/trace"},"origin":{"ip":"1.2.3.4","port":8000,"hostname":"www.example.com","protocol":2},"country":238,"cache_status":3,"server_ip":"192.168.1.1","server_name":"metal.cloudflare.com","remote_ip":"10.1.2.3","bytes_dlv":123456,"ray_id":"10c73629cce30078-LAX"}"#; - let mut wr = MemWriter::with_capacity(1024); + let mut wr = Vec::with_capacity(1024); manual_escape(wr.by_ref(), &log); - b.bytes = wr.unwrap().len() as u64; + b.bytes = wr.len() as u64; b.iter(|| { - let mut wr = MemWriter::with_capacity(1024); + let mut wr = Vec::with_capacity(1024); manual_escape(wr.by_ref(), &log); - let _json = wr.unwrap(); //let _json = String::from_utf8(_json).unwrap(); //assert_eq!(_s, _json.as_slice()); @@ -898,9 +896,9 @@ fn bench_manual_my_mem_writer0_escape(b: &mut Bencher) { let log = Log::new(); let _s = r#"{"timestamp":2837513946597,"zone_id":123456,"zone_plan":"FREE","http":{"protocol":"HTTP11","status":200,"host_status":503,"up_status":520,"method":"GET","content_type":"text/html","user_agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36","referer":"https://www.cloudflare.com/","request_uri":"/cdn-cgi/trace"},"origin":{"ip":"1.2.3.4","port":8000,"hostname":"www.example.com","protocol":"HTTPS"},"country":"US","cache_status":"Hit","server_ip":"192.168.1.1","server_name":"metal.cloudflare.com","remote_ip":"10.1.2.3","bytes_dlv":123456,"ray_id":"10c73629cce30078-LAX"}"#; - let mut wr = MemWriter::with_capacity(1024); + let mut wr = Vec::with_capacity(1024); manual_escape(wr.by_ref(), &log); - b.bytes = wr.unwrap().len() as u64; + b.bytes = wr.len() as u64; b.iter(|| { let mut wr = MyMemWriter0::with_capacity(1024); @@ -1001,14 +999,13 @@ fn bench_direct_mem_writer(b: &mut Bencher) { let log = Log::new(); let _s = r#"{"timestamp":2837513946597,"zone_id":123456,"zone_plan":"FREE","http":{"protocol":"HTTP11","status":200,"host_status":503,"up_status":520,"method":"GET","content_type":"text/html","user_agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36","referer":"https://www.cloudflare.com/","request_uri":"/cdn-cgi/trace"},"origin":{"ip":"1.2.3.4","port":8000,"hostname":"www.example.com","protocol":"HTTPS"},"country":"US","cache_status":"Hit","server_ip":"192.168.1.1","server_name":"metal.cloudflare.com","remote_ip":"10.1.2.3","bytes_dlv":123456,"ray_id":"10c73629cce30078-LAX"}"#; - let mut wr = MemWriter::with_capacity(1024); + let mut wr = Vec::with_capacity(1024); direct(wr.by_ref(), &log); - b.bytes = wr.unwrap().len() as u64; + b.bytes = wr.len() as u64; b.iter(|| { - let mut wr = MemWriter::with_capacity(1024); + let mut wr = Vec::with_capacity(1024); direct(wr.by_ref(), &log); - let _json = wr.unwrap(); //let _json = String::from_utf8(wr.unwrap()).unwrap(); /* diff --git a/src/de.rs b/src/de.rs index d250c339..1b87148e 100644 --- a/src/de.rs +++ b/src/de.rs @@ -510,21 +510,23 @@ pub trait Deserializer: Iterator> { #[inline] fn expect_seq< - 'a, T: Deserialize, C: FromIterator - >(&'a mut self, token: Token) -> Result { + >(&mut self, token: Token) -> Result { let len = try!(self.expect_seq_start(token)); + let mut err = None; - let mut d: SeqDeserializer<'a, Self, E> = SeqDeserializer { - d: self, - len: len, - err: None, + let collection: C = { + let d = SeqDeserializer { + d: self, + len: len, + err: &mut err, + }; + + d.collect() }; - let collection: C = d.collect(); - - match d.err { + match err { Some(err) => Err(err), None => Ok(collection), } @@ -560,22 +562,24 @@ pub trait Deserializer: Iterator> { #[inline] fn expect_map< - 'a, K: Deserialize, V: Deserialize, C: FromIterator<(K, V)> - >(&'a mut self, token: Token) -> Result { + >(&mut self, token: Token) -> Result { let len = try!(self.expect_map_start(token)); + let mut err = None; - let mut d: MapDeserializer<'a, Self, E> = MapDeserializer { - d: self, - len: len, - err: None, + let collection: C = { + let d = MapDeserializer { + d: self, + len: len, + err: &mut err, + }; + + d.collect() }; - let collection: C = d.collect(); - - match d.err { + match err { Some(err) => Err(err), None => Ok(collection), } @@ -584,10 +588,10 @@ pub trait Deserializer: Iterator> { ////////////////////////////////////////////////////////////////////////////// -struct SeqDeserializer<'a, D: 'a, E> { +struct SeqDeserializer<'a, D: 'a, E: 'a> { d: &'a mut D, len: uint, - err: option::Option, + err: &'a mut Option, } impl< @@ -599,9 +603,12 @@ impl< #[inline] fn next(&mut self) -> option::Option { match self.d.expect_seq_elt_or_end() { - Ok(next) => next, + Ok(next) => { + self.len -= 1; + next + } Err(err) => { - self.err = Some(err); + *self.err = Some(err); None } } @@ -615,10 +622,10 @@ impl< ////////////////////////////////////////////////////////////////////////////// -struct MapDeserializer<'a, D:'a, E> { +struct MapDeserializer<'a, D:'a, E: 'a> { d: &'a mut D, len: uint, - err: option::Option, + err: &'a mut option::Option, } impl< @@ -633,7 +640,7 @@ impl< match self.d.expect_map_elt_or_end() { Ok(next) => next, Err(err) => { - self.err = Some(err); + *self.err = Some(err); None } } diff --git a/src/json/mod.rs b/src/json/mod.rs index 6226c30a..e9c8cef7 100644 --- a/src/json/mod.rs +++ b/src/json/mod.rs @@ -425,7 +425,7 @@ mod tests { } fn test_encode_ok< - T: PartialEq + Show + ToJson + ser::Serialize, io::IoError> + T: PartialEq + Show + ToJson + ser::Serialize>, io::IoError> >(errors: &[(T, &str)]) { for &(ref value, out) in errors.iter() { let out = out.to_string(); @@ -439,7 +439,7 @@ mod tests { } fn test_pretty_encode_ok< - T: PartialEq + Show + ToJson + ser::Serialize, io::IoError> + T: PartialEq + Show + ToJson + ser::Serialize>, io::IoError> >(errors: &[(T, &str)]) { for &(ref value, out) in errors.iter() { let out = out.to_string(); @@ -1370,7 +1370,6 @@ mod tests { #[test] fn test_encode_hashmap_with_numeric_key() { use std::str::from_utf8; - use std::io::MemWriter; use std::collections::HashMap; let mut hm: HashMap = HashMap::new(); hm.insert(1, true); @@ -1386,7 +1385,6 @@ mod tests { #[test] fn test_prettyencode_hashmap_with_numeric_key() { use std::str::from_utf8; - use std::io::MemWriter; use std::collections::HashMap; let mut hm: HashMap = HashMap::new(); hm.insert(1, true); diff --git a/src/json/ser.rs b/src/json/ser.rs index 009c709f..65414929 100644 --- a/src/json/ser.rs +++ b/src/json/ser.rs @@ -1,7 +1,7 @@ use std::f32; use std::f64; use std::num::{Float, FPNaN, FPInfinite}; -use std::io::{IoError, IoResult, MemWriter}; +use std::io::{IoError, IoResult}; use ser::Serialize; use ser; @@ -587,18 +587,18 @@ pub fn to_writer< /// Encode the specified struct into a json `[u8]` buffer. #[inline] pub fn to_vec< - T: Serialize, IoError> + T: Serialize>, IoError> >(value: &T) -> Vec { // We are writing to a Vec, which doesn't fail. So we can ignore // the error. - let writer = MemWriter::with_capacity(128); - to_writer(writer, value).unwrap().unwrap() + let writer = Vec::with_capacity(128); + to_writer(writer, value).unwrap() } /// Encode the specified struct into a json `String` buffer. #[inline] pub fn to_string< - T: Serialize, IoError> + T: Serialize>, IoError> >(value: &T) -> Result> { let buf = to_vec(value); String::from_utf8(buf) @@ -617,17 +617,17 @@ pub fn to_pretty_writer< /// Encode the specified struct into a json `[u8]` buffer. pub fn to_pretty_vec< - T: Serialize, IoError> + T: Serialize>, IoError> >(value: &T) -> Vec { // We are writing to a Vec, which doesn't fail. So we can ignore // the error. - let writer = MemWriter::with_capacity(128); - to_pretty_writer(writer, value).unwrap().unwrap() + let writer = Vec::with_capacity(128); + to_pretty_writer(writer, value).unwrap() } /// Encode the specified struct into a json `String` buffer. pub fn to_pretty_string< - T: Serialize, IoError> + T: Serialize>, IoError> >(value: &T) -> Result> { let buf = to_pretty_vec(value); String::from_utf8(buf) diff --git a/src/json/value.rs b/src/json/value.rs index a2408c37..7f37f8c8 100644 --- a/src/json/value.rs +++ b/src/json/value.rs @@ -1,6 +1,6 @@ use std::collections::{HashMap, TreeMap, tree_map}; use std::fmt; -use std::io::{ByRefWriter, IoResult, MemWriter}; +use std::io::{ByRefWriter, IoResult}; use std::io; use std::str; use std::string; @@ -41,9 +41,9 @@ impl Value { /// Serializes a json value into a string pub fn to_pretty_string(&self) -> string::String { - let mut wr = MemWriter::new(); + let mut wr = Vec::new(); self.to_pretty_writer(wr.by_ref()).unwrap(); - str::from_utf8(wr.unwrap().as_slice()).unwrap().to_string() + str::from_utf8(wr.as_slice()).unwrap().to_string() } /// If the Json value is an Object, returns the value associated with the provided key.