diff --git a/benches/bench_log.rs b/benches/bench_log.rs index 76d73683..b075deca 100644 --- a/benches/bench_log.rs +++ b/benches/bench_log.rs @@ -18,6 +18,8 @@ use serde::json; use serde::ser::Serialize; use serde::ser; +use serialize::Encodable; + #[deriving(Show, PartialEq, Encodable, Decodable)] #[deriving_serialize] #[deriving_deserialize] @@ -611,12 +613,6 @@ impl MyMemWriter0 { buf: Vec::with_capacity(cap) } } - - #[inline] - pub fn clear(&mut self) { self.buf.clear() } - - #[inline] - pub fn unwrap(self) -> Vec { self.buf } } @@ -638,12 +634,6 @@ impl MyMemWriter1 { buf: Vec::with_capacity(cap) } } - - #[inline] - pub fn clear(&mut self) { self.buf.clear() } - - #[inline] - pub fn unwrap(self) -> Vec { self.buf } } // LLVM isn't yet able to lower `Vec::push_all` into a memcpy, so this helps @@ -685,7 +675,7 @@ fn test_encoder() { let mut wr = Vec::with_capacity(1024); { - let mut encoder = serialize::json::Encoder::new(&mut wr as &mut io::Writer); + let mut encoder = serialize::json::Encoder::new(&mut wr as &mut Writer); log.encode(&mut encoder).unwrap(); } @@ -695,11 +685,21 @@ fn test_encoder() { #[bench] fn bench_encoder(b: &mut Bencher) { let log = Log::new(); - let json = serialize::json::encode(&log); - b.bytes = json.len() as u64; + + let mut wr = Vec::with_capacity(1024); + + { + let mut encoder = serialize::json::Encoder::new(&mut wr as &mut Writer); + log.encode(&mut encoder).unwrap(); + } + + b.bytes = wr.len() as u64; b.iter(|| { - let _ = serialize::json::encode(&log); + wr.clear(); + + let mut encoder = serialize::json::Encoder::new(&mut wr as &mut Writer); + log.encode(&mut encoder).unwrap(); }); } @@ -707,7 +707,7 @@ fn bench_encoder(b: &mut Bencher) { fn test_serializer() { let log = Log::new(); let json = json::to_vec(&log); - assert_eq!(json.as_slice(), JSON_STR.as_bytes()); + assert_eq!(json, JSON_STR.as_bytes()); } #[bench] @@ -717,7 +717,7 @@ fn bench_serializer(b: &mut Bencher) { b.bytes = json.len() as u64; b.iter(|| { - let _json = json::to_vec(&log); + let _ = json::to_vec(&log); }); } @@ -760,7 +760,7 @@ fn test_serializer_my_mem_writer0() { log.serialize(&mut serializer).unwrap(); } - assert_eq!(wr.unwrap().as_slice(), JSON_STR.as_bytes()); + assert_eq!(wr.buf.as_slice(), JSON_STR.as_bytes()); } #[bench] @@ -772,7 +772,7 @@ fn bench_serializer_my_mem_writer0(b: &mut Bencher) { let mut wr = MyMemWriter0::with_capacity(1024); b.iter(|| { - wr.clear(); + wr.buf.clear(); let mut serializer = json::Serializer::new(wr.by_ref()); log.serialize(&mut serializer).unwrap(); @@ -790,7 +790,7 @@ fn test_serializer_my_mem_writer1() { log.serialize(&mut serializer).unwrap(); } - assert_eq!(wr.unwrap().as_slice(), JSON_STR.as_bytes()); + assert_eq!(wr.buf.as_slice(), JSON_STR.as_bytes()); } #[bench] @@ -802,7 +802,7 @@ fn bench_serializer_my_mem_writer1(b: &mut Bencher) { let mut wr = MyMemWriter1::with_capacity(1024); b.iter(|| { - wr.clear(); + wr.buf.clear(); let mut serializer = json::Serializer::new(wr.by_ref()); log.serialize(&mut serializer).unwrap(); @@ -819,165 +819,165 @@ fn bench_copy(b: &mut Bencher) { }); } -fn manual_no_escape(mut wr: W, log: &Log) { +fn manual_no_escape(wr: &mut W, log: &Log) { wr.write_str("{\"timestamp\":").unwrap(); - (write!(&mut wr, "{}", log.timestamp)).unwrap(); + (write!(wr, "{}", log.timestamp)).unwrap(); wr.write_str(",\"zone_id\":").unwrap(); - (write!(&mut wr, "{}", log.zone_id)).unwrap(); + (write!(wr, "{}", log.zone_id)).unwrap(); wr.write_str(",\"zone_plan\":").unwrap(); - (write!(&mut wr, "{}", log.zone_plan as uint)).unwrap(); + (write!(wr, "{}", log.zone_plan as uint)).unwrap(); wr.write_str(",\"http\":{\"protocol\":").unwrap(); - (write!(&mut wr, "{}", log.http.protocol as uint)).unwrap(); + (write!(wr, "{}", log.http.protocol as uint)).unwrap(); wr.write_str(",\"status\":").unwrap(); - (write!(&mut wr, "{}", log.http.status)).unwrap(); + (write!(wr, "{}", log.http.status)).unwrap(); wr.write_str(",\"host_status\":").unwrap(); - (write!(&mut wr, "{}", log.http.host_status)).unwrap(); + (write!(wr, "{}", log.http.host_status)).unwrap(); wr.write_str(",\"up_status\":").unwrap(); - (write!(&mut wr, "{}", log.http.up_status)).unwrap(); + (write!(wr, "{}", log.http.up_status)).unwrap(); wr.write_str(",\"method\":").unwrap(); - (write!(&mut wr, "{}", log.http.method as uint)).unwrap(); + (write!(wr, "{}", log.http.method as uint)).unwrap(); wr.write_str(",\"content_type\":").unwrap(); - (write!(&mut wr, "\"{}\"", log.http.content_type)).unwrap(); + (write!(wr, "\"{}\"", log.http.content_type)).unwrap(); wr.write_str(",\"user_agent\":").unwrap(); - (write!(&mut wr, "\"{}\"", log.http.user_agent)).unwrap(); + (write!(wr, "\"{}\"", log.http.user_agent)).unwrap(); wr.write_str(",\"referer\":").unwrap(); - (write!(&mut wr, "\"{}\"", log.http.referer)).unwrap(); + (write!(wr, "\"{}\"", log.http.referer)).unwrap(); wr.write_str(",\"request_uri\":").unwrap(); - (write!(&mut wr, "\"{}\"", log.http.request_uri)).unwrap(); + (write!(wr, "\"{}\"", log.http.request_uri)).unwrap(); wr.write_str("},\"origin\":{").unwrap(); wr.write_str("\"ip\":").unwrap(); - (write!(&mut wr, "\"{}\"", log.origin.ip)).unwrap(); + (write!(wr, "\"{}\"", log.origin.ip)).unwrap(); wr.write_str(",\"port\":").unwrap(); - (write!(&mut wr, "{}", log.origin.port)).unwrap(); + (write!(wr, "{}", log.origin.port)).unwrap(); wr.write_str(",\"hostname\":").unwrap(); - (write!(&mut wr, "\"{}\"", log.origin.hostname)).unwrap(); + (write!(wr, "\"{}\"", log.origin.hostname)).unwrap(); wr.write_str(",\"protocol\":").unwrap(); - (write!(&mut wr, "{}", log.origin.protocol as uint)).unwrap(); + (write!(wr, "{}", log.origin.protocol as uint)).unwrap(); wr.write_str("},\"country\":").unwrap(); - (write!(&mut wr, "{}", log.country as uint)).unwrap(); + (write!(wr, "{}", log.country as uint)).unwrap(); wr.write_str(",\"cache_status\":").unwrap(); - (write!(&mut wr, "{}", log.cache_status as uint)).unwrap(); + (write!(wr, "{}", log.cache_status as uint)).unwrap(); wr.write_str(",\"server_ip\":").unwrap(); - (write!(&mut wr, "\"{}\"", log.server_ip)).unwrap(); + (write!(wr, "\"{}\"", log.server_ip)).unwrap(); wr.write_str(",\"server_name\":").unwrap(); - (write!(&mut wr, "\"{}\"", log.server_name)).unwrap(); + (write!(wr, "\"{}\"", log.server_name)).unwrap(); wr.write_str(",\"remote_ip\":").unwrap(); - (write!(&mut wr, "\"{}\"", log.remote_ip)).unwrap(); + (write!(wr, "\"{}\"", log.remote_ip)).unwrap(); wr.write_str(",\"bytes_dlv\":").unwrap(); - (write!(&mut wr, "{}", log.bytes_dlv)).unwrap(); + (write!(wr, "{}", log.bytes_dlv)).unwrap(); wr.write_str(",\"ray_id\":").unwrap(); - (write!(&mut wr, "\"{}\"", log.ray_id)).unwrap(); + (write!(wr, "\"{}\"", log.ray_id)).unwrap(); wr.write_str("}").unwrap(); } -fn manual_escape(mut wr: W, log: &Log) { +fn manual_escape(wr: &mut W, log: &Log) { wr.write_str("{").unwrap(); - escape_str(&mut wr, "timestamp").unwrap(); + escape_str(wr, "timestamp").unwrap(); wr.write_str(":").unwrap(); - (write!(&mut wr, "{}", log.timestamp)).unwrap(); + (write!(wr, "{}", log.timestamp)).unwrap(); wr.write_str(",").unwrap(); - escape_str(&mut wr, "zone_id").unwrap(); + escape_str(wr, "zone_id").unwrap(); wr.write_str(":").unwrap(); - (write!(&mut wr, "{}", log.zone_id)).unwrap(); + (write!(wr, "{}", log.zone_id)).unwrap(); wr.write_str(",").unwrap(); - escape_str(&mut wr, "zone_plan").unwrap(); + escape_str(wr, "zone_plan").unwrap(); wr.write_str(":").unwrap(); - (write!(&mut wr, "{}", log.zone_plan as int)).unwrap(); + (write!(wr, "{}", log.zone_plan as int)).unwrap(); wr.write_str(",").unwrap(); - escape_str(&mut wr, "http").unwrap(); + escape_str(wr, "http").unwrap(); wr.write_str(":{").unwrap(); - escape_str(&mut wr, "protocol").unwrap(); + escape_str(wr, "protocol").unwrap(); wr.write_str(":").unwrap(); - (write!(&mut wr, "{}", log.http.protocol as uint)).unwrap(); + (write!(wr, "{}", log.http.protocol as uint)).unwrap(); wr.write_str(",").unwrap(); - escape_str(&mut wr, "status").unwrap(); + escape_str(wr, "status").unwrap(); wr.write_str(":").unwrap(); - (write!(&mut wr, "{}", log.http.status)).unwrap(); + (write!(wr, "{}", log.http.status)).unwrap(); wr.write_str(",").unwrap(); - escape_str(&mut wr, "host_status").unwrap(); + escape_str(wr, "host_status").unwrap(); wr.write_str(":").unwrap(); - (write!(&mut wr, "{}", log.http.host_status)).unwrap(); + (write!(wr, "{}", log.http.host_status)).unwrap(); wr.write_str(",").unwrap(); - escape_str(&mut wr, "up_status").unwrap(); + escape_str(wr, "up_status").unwrap(); wr.write_str(":").unwrap(); - (write!(&mut wr, "{}", log.http.up_status)).unwrap(); + (write!(wr, "{}", log.http.up_status)).unwrap(); wr.write_str(",").unwrap(); - escape_str(&mut wr, "method").unwrap(); + escape_str(wr, "method").unwrap(); wr.write_str(":").unwrap(); - (write!(&mut wr, "{}", log.http.method as uint)).unwrap(); + (write!(wr, "{}", log.http.method as uint)).unwrap(); wr.write_str(",").unwrap(); - escape_str(&mut wr, "content_type").unwrap(); + escape_str(wr, "content_type").unwrap(); wr.write_str(":").unwrap(); - escape_str(&mut wr, log.http.content_type.as_slice()).unwrap(); + escape_str(wr, log.http.content_type.as_slice()).unwrap(); wr.write_str(",").unwrap(); - escape_str(&mut wr, "user_agent").unwrap(); + escape_str(wr, "user_agent").unwrap(); wr.write_str(":").unwrap(); - escape_str(&mut wr, log.http.user_agent.as_slice()).unwrap(); + escape_str(wr, log.http.user_agent.as_slice()).unwrap(); wr.write_str(",").unwrap(); - escape_str(&mut wr, "referer").unwrap(); + escape_str(wr, "referer").unwrap(); wr.write_str(":").unwrap(); - escape_str(&mut wr, log.http.referer.as_slice()).unwrap(); + escape_str(wr, log.http.referer.as_slice()).unwrap(); wr.write_str(",").unwrap(); - escape_str(&mut wr, "request_uri").unwrap(); + escape_str(wr, "request_uri").unwrap(); wr.write_str(":").unwrap(); - escape_str(&mut wr, log.http.request_uri.as_slice()).unwrap(); + escape_str(wr, log.http.request_uri.as_slice()).unwrap(); wr.write_str("},").unwrap(); - escape_str(&mut wr, "origin").unwrap(); + escape_str(wr, "origin").unwrap(); wr.write_str(":{").unwrap(); - escape_str(&mut wr, "ip").unwrap(); + escape_str(wr, "ip").unwrap(); wr.write_str(":").unwrap(); - escape_str(&mut wr, log.origin.ip.as_slice()).unwrap(); + escape_str(wr, log.origin.ip.as_slice()).unwrap(); wr.write_str(",").unwrap(); - escape_str(&mut wr, "port").unwrap(); + escape_str(wr, "port").unwrap(); wr.write_str(":").unwrap(); - (write!(&mut wr, "{}", log.origin.port)).unwrap(); + (write!(wr, "{}", log.origin.port)).unwrap(); wr.write_str(",").unwrap(); - escape_str(&mut wr, "hostname").unwrap(); + escape_str(wr, "hostname").unwrap(); wr.write_str(":").unwrap(); - escape_str(&mut wr, log.origin.hostname.as_slice()).unwrap(); + escape_str(wr, log.origin.hostname.as_slice()).unwrap(); wr.write_str(",").unwrap(); - escape_str(&mut wr, "protocol").unwrap(); + escape_str(wr, "protocol").unwrap(); wr.write_str(":").unwrap(); - (write!(&mut wr, "{}", log.origin.protocol as uint)).unwrap(); + (write!(wr, "{}", log.origin.protocol as uint)).unwrap(); wr.write_str("},").unwrap(); - escape_str(&mut wr, "country").unwrap(); + escape_str(wr, "country").unwrap(); wr.write_str(":").unwrap(); - (write!(&mut wr, "{}", log.country as uint)).unwrap(); + (write!(wr, "{}", log.country as uint)).unwrap(); wr.write_str(",").unwrap(); - escape_str(&mut wr, "cache_status").unwrap(); + escape_str(wr, "cache_status").unwrap(); wr.write_str(":").unwrap(); - (write!(&mut wr, "{}", log.cache_status as uint)).unwrap(); + (write!(wr, "{}", log.cache_status as uint)).unwrap(); wr.write_str(",").unwrap(); - escape_str(&mut wr, "server_ip").unwrap(); + escape_str(wr, "server_ip").unwrap(); wr.write_str(":").unwrap(); - escape_str(&mut wr, log.server_ip.as_slice()).unwrap(); + escape_str(wr, log.server_ip.as_slice()).unwrap(); wr.write_str(",").unwrap(); - escape_str(&mut wr, "server_name").unwrap(); + escape_str(wr, "server_name").unwrap(); wr.write_str(":").unwrap(); - escape_str(&mut wr, log.server_name.as_slice()).unwrap(); + escape_str(wr, log.server_name.as_slice()).unwrap(); wr.write_str(",").unwrap(); - escape_str(&mut wr, "remote_ip").unwrap(); + escape_str(wr, "remote_ip").unwrap(); wr.write_str(":").unwrap(); - escape_str(&mut wr, log.remote_ip.as_slice()).unwrap(); + escape_str(wr, log.remote_ip.as_slice()).unwrap(); wr.write_str(",").unwrap(); - escape_str(&mut wr, "bytes_dlv").unwrap(); + escape_str(wr, "bytes_dlv").unwrap(); wr.write_str(":").unwrap(); - (write!(&mut wr, "{}", log.bytes_dlv)).unwrap(); + (write!(wr, "{}", log.bytes_dlv)).unwrap(); wr.write_str(",").unwrap(); - escape_str(&mut wr, "ray_id").unwrap(); + escape_str(wr, "ray_id").unwrap(); wr.write_str(":").unwrap(); - escape_str(&mut wr, log.ray_id.as_slice()).unwrap(); + escape_str(wr, log.ray_id.as_slice()).unwrap(); wr.write_str("}").unwrap(); } @@ -986,7 +986,7 @@ fn test_manual_vec_no_escape() { let log = Log::new(); let mut wr = Vec::with_capacity(1024); - manual_no_escape(wr.by_ref(), &log); + manual_no_escape(&mut wr, &log); let json = String::from_utf8(wr).unwrap(); assert_eq!(JSON_STR, json.as_slice()); @@ -997,12 +997,12 @@ fn bench_manual_vec_no_escape(b: &mut Bencher) { let log = Log::new(); let mut wr = Vec::with_capacity(1024); - manual_no_escape(wr.by_ref(), &log); + manual_no_escape(&mut wr, &log); b.bytes = wr.len() as u64; b.iter(|| { wr.clear(); - manual_no_escape(wr.by_ref(), &log); + manual_no_escape(&mut wr, &log); }); } @@ -1011,7 +1011,7 @@ fn test_manual_vec_escape() { let log = Log::new(); let mut wr = Vec::with_capacity(1024); - manual_escape(wr.by_ref(), &log); + manual_escape(&mut wr, &log); let json = String::from_utf8(wr).unwrap(); assert_eq!(JSON_STR, json.as_slice()); @@ -1022,13 +1022,13 @@ fn bench_manual_vec_escape(b: &mut Bencher) { let log = Log::new(); let mut wr = Vec::with_capacity(1024); - manual_escape(wr.by_ref(), &log); + manual_escape(&mut wr, &log); b.bytes = wr.len() as u64; b.iter(|| { wr.clear(); - manual_escape(wr.by_ref(), &log); + manual_escape(&mut wr, &log); }); } @@ -1037,9 +1037,9 @@ fn test_manual_my_mem_writer0_no_escape() { let log = Log::new(); let mut wr = MyMemWriter0::with_capacity(1000); - manual_no_escape(wr.by_ref(), &log); + manual_no_escape(&mut wr, &log); - let json = String::from_utf8(wr.unwrap()).unwrap(); + let json = String::from_utf8(wr.buf).unwrap(); assert_eq!(JSON_STR, json.as_slice()); } @@ -1047,14 +1047,14 @@ fn test_manual_my_mem_writer0_no_escape() { fn bench_manual_my_mem_writer0_no_escape(b: &mut Bencher) { let log = Log::new(); - let mut wr = MyMemWriter0::with_capacity(1000); - manual_no_escape(wr.by_ref(), &log); + let mut wr = MyMemWriter0::with_capacity(1024); + manual_no_escape(&mut wr, &log); b.bytes = wr.buf.len() as u64; b.iter(|| { - wr.clear(); + wr.buf.clear(); - manual_no_escape(wr.by_ref(), &log); + manual_no_escape(&mut wr, &log); }); } @@ -1063,9 +1063,9 @@ fn test_manual_my_mem_writer0_escape() { let log = Log::new(); let mut wr = MyMemWriter0::with_capacity(1024); - manual_escape(wr.by_ref(), &log); + manual_escape(&mut wr, &log); - let json = String::from_utf8(wr.unwrap()).unwrap(); + let json = String::from_utf8(wr.buf).unwrap(); assert_eq!(JSON_STR, json.as_slice()); } @@ -1074,13 +1074,13 @@ fn bench_manual_my_mem_writer0_escape(b: &mut Bencher) { let log = Log::new(); let mut wr = MyMemWriter0::with_capacity(1024); - manual_escape(wr.by_ref(), &log); + manual_escape(&mut wr, &log); b.bytes = wr.buf.len() as u64; b.iter(|| { - wr.clear(); + wr.buf.clear(); - manual_escape(wr.by_ref(), &log); + manual_escape(&mut wr, &log); }); } @@ -1089,9 +1089,9 @@ fn test_manual_my_mem_writer1_no_escape() { let log = Log::new(); let mut wr = MyMemWriter1::with_capacity(1024); - manual_no_escape(wr.by_ref(), &log); + manual_no_escape(&mut wr, &log); - let json = String::from_utf8(wr.unwrap()).unwrap(); + let json = String::from_utf8(wr.buf).unwrap(); assert_eq!(JSON_STR, json.as_slice()); } @@ -1099,14 +1099,14 @@ fn test_manual_my_mem_writer1_no_escape() { fn bench_manual_my_mem_writer1_no_escape(b: &mut Bencher) { let log = Log::new(); - let mut wr = MyMemWriter1::with_capacity(1000); - manual_no_escape(wr.by_ref(), &log); + let mut wr = MyMemWriter1::with_capacity(1024); + manual_no_escape(&mut wr, &log); b.bytes = wr.buf.len() as u64; b.iter(|| { - wr.clear(); + wr.buf.clear(); - manual_no_escape(wr.by_ref(), &log); + manual_no_escape(&mut wr, &log); }); } @@ -1115,9 +1115,9 @@ fn test_manual_my_mem_writer1_escape() { let log = Log::new(); let mut wr = MyMemWriter1::with_capacity(1024); - manual_escape(wr.by_ref(), &log); + manual_escape(&mut wr, &log); - let json = String::from_utf8(wr.unwrap()).unwrap(); + let json = String::from_utf8(wr.buf).unwrap(); assert_eq!(JSON_STR, json.as_slice()); } @@ -1126,20 +1126,20 @@ fn bench_manual_my_mem_writer1_escape(b: &mut Bencher) { let log = Log::new(); let mut wr = MyMemWriter1::with_capacity(1024); - manual_escape(wr.by_ref(), &log); - b.bytes = wr.unwrap().len() as u64; + manual_escape(&mut wr, &log); + b.bytes = wr.buf.len() as u64; b.iter(|| { - let mut wr = MyMemWriter1::with_capacity(1024); - manual_escape(wr.by_ref(), &log); - let _json = wr.unwrap(); + wr.buf.clear(); + + manual_escape(&mut wr, &log); }); } -fn direct(wr: W, log: &Log) { +fn direct(wr: &mut W, log: &Log) { use serde::ser::Serializer; - let mut serializer = json::Serializer::new(wr); + let mut serializer = json::Serializer::new(wr.by_ref()); serializer.serialize_struct_start("Log", 12).unwrap(); @@ -1164,7 +1164,7 @@ fn test_direct_vec() { let log = Log::new(); let mut wr = Vec::with_capacity(1024); - direct(wr.by_ref(), &log); + direct(&mut wr, &log); let json = String::from_utf8(wr).unwrap(); assert_eq!(JSON_STR, json.as_slice()); @@ -1175,12 +1175,12 @@ fn bench_direct_vec(b: &mut Bencher) { let log = Log::new(); let mut wr = Vec::with_capacity(1024); - direct(wr.by_ref(), &log); + direct(&mut wr, &log); b.bytes = wr.len() as u64; b.iter(|| { let mut wr = Vec::with_capacity(1024); - direct(wr.by_ref(), &log); + direct(&mut wr, &log); }); } @@ -1189,9 +1189,9 @@ fn test_direct_my_mem_writer0() { let log = Log::new(); let mut wr = MyMemWriter0::with_capacity(1024); - direct(wr.by_ref(), &log); + direct(&mut wr, &log); - let json = String::from_utf8(wr.unwrap()).unwrap(); + let json = String::from_utf8(wr.buf).unwrap(); assert_eq!(JSON_STR, json.as_slice()); } @@ -1200,13 +1200,13 @@ fn bench_direct_my_mem_writer0(b: &mut Bencher) { let log = Log::new(); let mut wr = MyMemWriter0::with_capacity(1024); - direct(wr.by_ref(), &log); + direct(&mut wr, &log); b.bytes = wr.buf.len() as u64; b.iter(|| { - wr.clear(); + wr.buf.clear(); - direct(wr.by_ref(), &log); + direct(&mut wr, &log); }); } @@ -1215,9 +1215,9 @@ fn test_direct_my_mem_writer1() { let log = Log::new(); let mut wr = MyMemWriter1::with_capacity(1024); - direct(wr.by_ref(), &log); + direct(&mut wr, &log); - let json = String::from_utf8(wr.unwrap()).unwrap(); + let json = String::from_utf8(wr.buf).unwrap(); assert_eq!(JSON_STR, json.as_slice()); } @@ -1226,13 +1226,13 @@ fn bench_direct_my_mem_writer1(b: &mut Bencher) { let log = Log::new(); let mut wr = MyMemWriter1::with_capacity(1024); - direct(wr.by_ref(), &log); + direct(&mut wr, &log); b.bytes = wr.buf.len() as u64; b.iter(|| { - wr.clear(); + wr.buf.clear(); - direct(wr.by_ref(), &log); + direct(&mut wr, &log); }); } diff --git a/serde2/benches/bench_log.rs b/serde2/benches/bench_log.rs index 8a76320b..67649d18 100644 --- a/serde2/benches/bench_log.rs +++ b/serde2/benches/bench_log.rs @@ -9,14 +9,17 @@ extern crate serialize; extern crate test; use std::io; -use std::io::{ByRefWriter, MemWriter}; +use std::io::ByRefWriter; use test::Bencher; -//use serde2::de; +use serde2::json::ser::escape_str; use serde2::json; +use serde2::ser::{Serialize, Serializer}; use serde2::ser; -#[deriving(Encodable, Decodable)] +use serialize::Encodable; + +#[deriving(Show, PartialEq, Encodable, Decodable)] #[deriving_serialize] //#[deriving_deserialize] struct Http { @@ -31,13 +34,28 @@ struct Http { request_uri: String, } -#[deriving(Show, Encodable, Decodable, FromPrimitive)] +#[deriving(Show, PartialEq, FromPrimitive)] enum HttpProtocol { HTTP_PROTOCOL_UNKNOWN, HTTP10, HTTP11, } +impl, E> serialize::Encodable for HttpProtocol { + fn encode(&self, s: &mut S) -> Result<(), E> { + (*self as uint).encode(s) + } +} + +impl, E> serialize::Decodable for HttpProtocol { + fn decode(d: &mut D) -> Result { + match FromPrimitive::from_uint(try!(d.read_uint())) { + Some(value) => Ok(value), + None => Err(d.error("cannot convert from uint")), + } + } +} + impl ser::Serialize for HttpProtocol { #[inline] fn visit< @@ -59,7 +77,7 @@ impl, E> de::Deserialize for HttpProtocol { } */ -#[deriving(Show, Encodable, Decodable, FromPrimitive)] +#[deriving(Show, PartialEq, FromPrimitive)] enum HttpMethod { METHOD_UNKNOWN, GET, @@ -74,6 +92,21 @@ enum HttpMethod { PATCH, } +impl, E> serialize::Encodable for HttpMethod { + fn encode(&self, s: &mut S) -> Result<(), E> { + (*self as uint).encode(s) + } +} + +impl, E> serialize::Decodable for HttpMethod { + fn decode(d: &mut D) -> Result { + match FromPrimitive::from_uint(try!(d.read_uint())) { + Some(value) => Ok(value), + None => Err(d.error("cannot convert from uint")), + } + } +} + impl ser::Serialize for HttpMethod { #[inline] fn visit< @@ -95,7 +128,7 @@ impl, E> de::Deserialize for HttpMethod { } */ -#[deriving(Show, Encodable, Decodable, FromPrimitive)] +#[deriving(Show, PartialEq, FromPrimitive)] enum CacheStatus { CACHESTATUS_UNKNOWN, Miss, @@ -103,6 +136,21 @@ enum CacheStatus { Hit, } +impl, E> serialize::Encodable for CacheStatus { + fn encode(&self, s: &mut S) -> Result<(), E> { + (*self as uint).encode(s) + } +} + +impl, E> serialize::Decodable for CacheStatus { + fn decode(d: &mut D) -> Result { + match FromPrimitive::from_uint(try!(d.read_uint())) { + Some(value) => Ok(value), + None => Err(d.error("cannot convert from uint")), + } + } +} + impl ser::Serialize for CacheStatus { #[inline] fn visit< @@ -124,7 +172,7 @@ impl, E> de::Deserialize for CacheStatus { } */ -#[deriving(Encodable, Decodable)] +#[deriving(Show, PartialEq, Encodable, Decodable)] #[deriving_serialize] //#[deriving_deserialize] struct Origin { @@ -134,13 +182,28 @@ struct Origin { protocol: OriginProtocol, } -#[deriving(Show, Encodable, Decodable, FromPrimitive)] +#[deriving(Show, PartialEq, FromPrimitive)] enum OriginProtocol { ORIGIN_PROTOCOL_UNKNOWN, HTTP, HTTPS, } +impl, E> serialize::Encodable for OriginProtocol { + fn encode(&self, s: &mut S) -> Result<(), E> { + (*self as uint).encode(s) + } +} + +impl, E> serialize::Decodable for OriginProtocol { + fn decode(d: &mut D) -> Result { + match FromPrimitive::from_uint(try!(d.read_uint())) { + Some(value) => Ok(value), + None => Err(d.error("cannot convert from uint")), + } + } +} + impl ser::Serialize for OriginProtocol { #[inline] fn visit< @@ -162,7 +225,7 @@ impl, E> de::Deserialize for OriginProtocol { } */ -#[deriving(Show, Encodable, Decodable, FromPrimitive)] +#[deriving(Show, PartialEq, FromPrimitive)] enum ZonePlan { ZONEPLAN_UNKNOWN, FREE, @@ -171,6 +234,21 @@ enum ZonePlan { ENT, } +impl, E> serialize::Encodable for ZonePlan { + fn encode(&self, s: &mut S) -> Result<(), E> { + (*self as uint).encode(s) + } +} + +impl, E> serialize::Decodable for ZonePlan { + fn decode(d: &mut D) -> Result { + match FromPrimitive::from_uint(try!(d.read_uint())) { + Some(value) => Ok(value), + None => Err(d.error("cannot convert from uint")), + } + } +} + impl ser::Serialize for ZonePlan { #[inline] fn visit< @@ -192,7 +270,7 @@ impl, E> de::Deserialize for ZonePlan { } */ -#[deriving(Show, Encodable, Decodable, FromPrimitive)] +#[deriving(Show, PartialEq, FromPrimitive)] enum Country { UNKNOWN, A1, @@ -452,6 +530,21 @@ enum Country { ZW, } +impl, E> serialize::Encodable for Country { + fn encode(&self, s: &mut S) -> Result<(), E> { + (*self as uint).encode(s) + } +} + +impl, E> serialize::Decodable for Country { + fn decode(d: &mut D) -> Result { + match FromPrimitive::from_uint(try!(d.read_uint())) { + Some(value) => Ok(value), + None => Err(d.error("cannot convert from uint")), + } + } +} + impl ser::Serialize for Country { #[inline] fn visit< @@ -473,7 +566,7 @@ impl, E> de::Deserialize for Country { } */ -#[deriving(Encodable, Decodable)] +#[deriving(Show, PartialEq, Encodable, Decodable)] #[deriving_serialize] //#[deriving_deserialize] struct Log { @@ -496,13 +589,13 @@ impl Log { Log { timestamp: 2837513946597, zone_id: 123456, - zone_plan: FREE, + zone_plan: ZonePlan::FREE, http: Http { - protocol: HTTP11, + protocol: HttpProtocol::HTTP11, status: 200, host_status: 503, up_status: 520, - method: GET, + method: HttpMethod::GET, content_type: "text/html".to_string(), user_agent: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36".to_string(), referer: "https://www.cloudflare.com/".to_string(), @@ -512,10 +605,10 @@ impl Log { ip: "1.2.3.4".to_string(), port: 8000, hostname: "www.example.com".to_string(), - protocol: HTTPS, + protocol: OriginProtocol::HTTPS, }, - country: US, - cache_status: Hit, + country: Country::US, + cache_status: CacheStatus::Hit, server_ip: "192.168.1.1".to_string(), server_name: "metal.cloudflare.com".to_string(), remote_ip: "10.1.2.3".to_string(), @@ -525,7 +618,6 @@ impl Log { } } - macro_rules! likely( ($val:expr) => { { @@ -557,20 +649,11 @@ struct MyMemWriter0 { } impl MyMemWriter0 { - /* - pub fn new() -> MyMemWriter0 { - MyMemWriter0::with_capacity(128) - } - */ - pub fn with_capacity(cap: uint) -> MyMemWriter0 { MyMemWriter0 { buf: Vec::with_capacity(cap) } } - - #[inline] - pub fn unwrap(self) -> Vec { self.buf } } @@ -587,20 +670,11 @@ struct MyMemWriter1 { } impl MyMemWriter1 { - /* - pub fn new() -> MyMemWriter1 { - MyMemWriter1::with_capacity(128) - } - */ - pub fn with_capacity(cap: uint) -> MyMemWriter1 { MyMemWriter1 { buf: Vec::with_capacity(cap) } } - - #[inline] - pub fn unwrap(self) -> Vec { self.buf } } // LLVM isn't yet able to lower `Vec::push_all` into a memcpy, so this helps @@ -631,19 +705,52 @@ impl Writer for MyMemWriter1 { } } +const JSON_STR: &'static str = 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"}"#; + +#[test] +fn test_encoder() { + use serialize::Encodable; + + let log = Log::new(); + + let mut wr = Vec::with_capacity(1024); + + { + let mut encoder = serialize::json::Encoder::new(&mut wr as &mut Writer); + log.encode(&mut encoder).unwrap(); + } + + assert_eq!(wr.as_slice(), JSON_STR.as_bytes()); +} + #[bench] fn bench_encoder(b: &mut Bencher) { let log = Log::new(); - let json = serialize::json::encode(&log); - b.bytes = json.len() as u64; + + let mut wr = Vec::with_capacity(1024); + + { + let mut encoder = serialize::json::Encoder::new(&mut wr as &mut Writer); + log.encode(&mut encoder).unwrap(); + } + + b.bytes = wr.len() as u64; b.iter(|| { - //for _ in range(0u, 1000) { - let _json = serialize::json::encode(&log); - //} + wr.clear(); + + let mut encoder = serialize::json::Encoder::new(&mut wr as &mut Writer); + log.encode(&mut encoder).unwrap(); }); } +#[test] +fn test_serializer() { + let log = Log::new(); + let json = json::to_vec(&log).unwrap(); + assert_eq!(json, JSON_STR.as_bytes()); +} + #[bench] fn bench_serializer(b: &mut Bencher) { let log = Log::new(); @@ -651,25 +758,50 @@ fn bench_serializer(b: &mut Bencher) { b.bytes = json.len() as u64; b.iter(|| { - //for _ in range(0u, 1000) { - let _json = json::to_vec(&log).unwrap(); - //} + let _ = json::to_vec(&log); }); } +#[test] +fn test_serializer_vec() { + let log = Log::new(); + let wr = Vec::with_capacity(1024); + let mut serializer = json::Writer::new(wr); + serializer.visit(&log).unwrap(); + + let json = serializer.unwrap(); + assert_eq!(json.as_slice(), JSON_STR.as_bytes()); +} + #[bench] -fn bench_copy(b: &mut Bencher) { - 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 json = s.as_bytes().to_vec(); +fn bench_serializer_vec(b: &mut Bencher) { + let log = Log::new(); + let json = json::to_vec(&log).unwrap(); b.bytes = json.len() as u64; + let mut wr = Vec::with_capacity(1024); + b.iter(|| { - let _json = s.as_bytes().to_vec(); + wr.clear(); + + let mut serializer = json::Writer::new(wr.by_ref()); + serializer.visit(&log).unwrap(); + let _json = serializer.unwrap(); }); } -fn manual_no_escape(mut wr: W, log: &Log) { + +#[bench] +fn bench_copy(b: &mut Bencher) { + let json = JSON_STR.as_bytes().to_vec(); + b.bytes = json.len() as u64; + + b.iter(|| { + let _json = JSON_STR.as_bytes().to_vec(); + }); +} + +fn manual_no_escape(wr: &mut W, log: &Log) { wr.write_str("{\"timestamp\":").unwrap(); (write!(wr, "{}", log.timestamp)).unwrap(); wr.write_str(",\"zone_id\":").unwrap(); @@ -726,336 +858,262 @@ fn manual_no_escape(mut wr: W, log: &Log) { wr.write_str("}").unwrap(); } -fn manual_escape(mut wr: W, log: &Log) { +fn manual_escape(wr: &mut W, log: &Log) { wr.write_str("{").unwrap(); - json::escape_str(&mut wr, "timestamp").unwrap(); + escape_str(wr, "timestamp").unwrap(); wr.write_str(":").unwrap(); (write!(wr, "{}", log.timestamp)).unwrap(); wr.write_str(",").unwrap(); - json::escape_str(&mut wr, "zone_id").unwrap(); + escape_str(wr, "zone_id").unwrap(); wr.write_str(":").unwrap(); (write!(wr, "{}", log.zone_id)).unwrap(); wr.write_str(",").unwrap(); - json::escape_str(&mut wr, "zone_plan").unwrap(); + escape_str(wr, "zone_plan").unwrap(); wr.write_str(":").unwrap(); (write!(wr, "{}", log.zone_plan as int)).unwrap(); wr.write_str(",").unwrap(); - json::escape_str(&mut wr, "http").unwrap(); + escape_str(wr, "http").unwrap(); wr.write_str(":{").unwrap(); - json::escape_str(&mut wr, "protocol").unwrap(); + escape_str(wr, "protocol").unwrap(); wr.write_str(":").unwrap(); (write!(wr, "{}", log.http.protocol as uint)).unwrap(); wr.write_str(",").unwrap(); - json::escape_str(&mut wr, "status").unwrap(); + escape_str(wr, "status").unwrap(); wr.write_str(":").unwrap(); (write!(wr, "{}", log.http.status)).unwrap(); wr.write_str(",").unwrap(); - json::escape_str(&mut wr, "host_status").unwrap(); + escape_str(wr, "host_status").unwrap(); wr.write_str(":").unwrap(); (write!(wr, "{}", log.http.host_status)).unwrap(); wr.write_str(",").unwrap(); - json::escape_str(&mut wr, "up_status").unwrap(); + escape_str(wr, "up_status").unwrap(); wr.write_str(":").unwrap(); (write!(wr, "{}", log.http.up_status)).unwrap(); wr.write_str(",").unwrap(); - json::escape_str(&mut wr, "method").unwrap(); + escape_str(wr, "method").unwrap(); wr.write_str(":").unwrap(); (write!(wr, "{}", log.http.method as uint)).unwrap(); wr.write_str(",").unwrap(); - json::escape_str(&mut wr, "content_type").unwrap(); + escape_str(wr, "content_type").unwrap(); wr.write_str(":").unwrap(); - json::escape_str(&mut wr, log.http.content_type.as_slice()).unwrap(); + escape_str(wr, log.http.content_type.as_slice()).unwrap(); wr.write_str(",").unwrap(); - json::escape_str(&mut wr, "user_agent").unwrap(); + escape_str(wr, "user_agent").unwrap(); wr.write_str(":").unwrap(); - json::escape_str(&mut wr, log.http.user_agent.as_slice()).unwrap(); + escape_str(wr, log.http.user_agent.as_slice()).unwrap(); wr.write_str(",").unwrap(); - json::escape_str(&mut wr, "referer").unwrap(); + escape_str(wr, "referer").unwrap(); wr.write_str(":").unwrap(); - json::escape_str(&mut wr, log.http.referer.as_slice()).unwrap(); + escape_str(wr, log.http.referer.as_slice()).unwrap(); wr.write_str(",").unwrap(); - json::escape_str(&mut wr, "request_uri").unwrap(); + escape_str(wr, "request_uri").unwrap(); wr.write_str(":").unwrap(); - json::escape_str(&mut wr, log.http.request_uri.as_slice()).unwrap(); + escape_str(wr, log.http.request_uri.as_slice()).unwrap(); wr.write_str("},").unwrap(); - json::escape_str(&mut wr, "origin").unwrap(); + escape_str(wr, "origin").unwrap(); wr.write_str(":{").unwrap(); - json::escape_str(&mut wr, "ip").unwrap(); + escape_str(wr, "ip").unwrap(); wr.write_str(":").unwrap(); - json::escape_str(&mut wr, log.origin.ip.as_slice()).unwrap(); + escape_str(wr, log.origin.ip.as_slice()).unwrap(); wr.write_str(",").unwrap(); - json::escape_str(&mut wr, "port").unwrap(); + escape_str(wr, "port").unwrap(); wr.write_str(":").unwrap(); (write!(wr, "{}", log.origin.port)).unwrap(); wr.write_str(",").unwrap(); - json::escape_str(&mut wr, "hostname").unwrap(); + escape_str(wr, "hostname").unwrap(); wr.write_str(":").unwrap(); - json::escape_str(&mut wr, log.origin.hostname.as_slice()).unwrap(); + escape_str(wr, log.origin.hostname.as_slice()).unwrap(); wr.write_str(",").unwrap(); - json::escape_str(&mut wr, "protocol").unwrap(); + escape_str(wr, "protocol").unwrap(); wr.write_str(":").unwrap(); (write!(wr, "{}", log.origin.protocol as uint)).unwrap(); wr.write_str("},").unwrap(); - json::escape_str(&mut wr, "country").unwrap(); + escape_str(wr, "country").unwrap(); wr.write_str(":").unwrap(); (write!(wr, "{}", log.country as uint)).unwrap(); wr.write_str(",").unwrap(); - json::escape_str(&mut wr, "cache_status").unwrap(); + escape_str(wr, "cache_status").unwrap(); wr.write_str(":").unwrap(); (write!(wr, "{}", log.cache_status as uint)).unwrap(); wr.write_str(",").unwrap(); - json::escape_str(&mut wr, "server_ip").unwrap(); + escape_str(wr, "server_ip").unwrap(); wr.write_str(":").unwrap(); - json::escape_str(&mut wr, log.server_ip.as_slice()).unwrap(); + escape_str(wr, log.server_ip.as_slice()).unwrap(); wr.write_str(",").unwrap(); - json::escape_str(&mut wr, "server_name").unwrap(); + escape_str(wr, "server_name").unwrap(); wr.write_str(":").unwrap(); - json::escape_str(&mut wr, log.server_name.as_slice()).unwrap(); + escape_str(wr, log.server_name.as_slice()).unwrap(); wr.write_str(",").unwrap(); - json::escape_str(&mut wr, "remote_ip").unwrap(); + escape_str(wr, "remote_ip").unwrap(); wr.write_str(":").unwrap(); - json::escape_str(&mut wr, log.remote_ip.as_slice()).unwrap(); + escape_str(wr, log.remote_ip.as_slice()).unwrap(); wr.write_str(",").unwrap(); - json::escape_str(&mut wr, "bytes_dlv").unwrap(); + escape_str(wr, "bytes_dlv").unwrap(); wr.write_str(":").unwrap(); (write!(wr, "{}", log.bytes_dlv)).unwrap(); wr.write_str(",").unwrap(); - json::escape_str(&mut wr, "ray_id").unwrap(); + escape_str(wr, "ray_id").unwrap(); wr.write_str(":").unwrap(); - json::escape_str(&mut wr, log.ray_id.as_slice()).unwrap(); + escape_str(wr, log.ray_id.as_slice()).unwrap(); wr.write_str("}").unwrap(); } -#[bench] -fn bench_manual_mem_writer_no_escape(b: &mut Bencher) { +#[test] +fn test_manual_vec_no_escape() { 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); - manual_no_escape(wr.by_ref(), &log); - b.bytes = wr.unwrap().len() as u64; + let mut wr = Vec::with_capacity(1024); + manual_no_escape(&mut wr, &log); - b.iter(|| { - let mut wr = MemWriter::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()); - }); + let json = String::from_utf8(wr).unwrap(); + assert_eq!(JSON_STR, json.as_slice()); } #[bench] -fn bench_manual_mem_writer_escape(b: &mut Bencher) { +fn bench_manual_vec_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); - manual_escape(wr.by_ref(), &log); - b.bytes = wr.unwrap().len() as u64; + let mut wr = Vec::with_capacity(1024); + manual_no_escape(&mut wr, &log); + b.bytes = wr.len() as u64; b.iter(|| { - let mut wr = MemWriter::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()); + wr.clear(); + manual_no_escape(&mut wr, &log); }); } +#[test] +fn test_manual_vec_escape() { + let log = Log::new(); + + let mut wr = Vec::with_capacity(1024); + manual_escape(&mut wr, &log); + + let json = String::from_utf8(wr).unwrap(); + assert_eq!(JSON_STR, json.as_slice()); +} + +#[bench] +fn bench_manual_vec_escape(b: &mut Bencher) { + let log = Log::new(); + + let mut wr = Vec::with_capacity(1024); + manual_escape(&mut wr, &log); + b.bytes = wr.len() as u64; + + b.iter(|| { + wr.clear(); + + manual_escape(&mut wr, &log); + }); +} + +#[test] +fn test_manual_my_mem_writer0_no_escape() { + let log = Log::new(); + + let mut wr = MyMemWriter0::with_capacity(1000); + manual_no_escape(&mut wr, &log); + + let json = String::from_utf8(wr.buf).unwrap(); + assert_eq!(JSON_STR, json.as_slice()); +} + #[bench] fn bench_manual_my_mem_writer0_no_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 = MyMemWriter0::with_capacity(1000); - manual_no_escape(wr.by_ref(), &log); - b.bytes = wr.unwrap().len() as u64; + let mut wr = MyMemWriter0::with_capacity(1024); + manual_no_escape(&mut wr, &log); + b.bytes = wr.buf.len() as u64; b.iter(|| { - let mut wr = MyMemWriter0::with_capacity(1024); - manual_no_escape(wr.by_ref(), &log); + wr.buf.clear(); - let _json = wr.unwrap(); - - //let _json = String::from_utf8(wr.unwrap()).unwrap(); - /* - assert_eq!(_s, _json.as_slice()); - */ + manual_no_escape(&mut wr, &log); }); } +#[test] +fn test_manual_my_mem_writer0_escape() { + let log = Log::new(); + + let mut wr = MyMemWriter0::with_capacity(1024); + manual_escape(&mut wr, &log); + + let json = String::from_utf8(wr.buf).unwrap(); + assert_eq!(JSON_STR, json.as_slice()); +} + #[bench] 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); - manual_escape(wr.by_ref(), &log); - b.bytes = wr.unwrap().len() as u64; + let mut wr = MyMemWriter0::with_capacity(1024); + manual_escape(&mut wr, &log); + b.bytes = wr.buf.len() as u64; b.iter(|| { - let mut wr = MyMemWriter0::with_capacity(1024); - manual_escape(wr.by_ref(), &log); - let _json = wr.unwrap(); + wr.buf.clear(); - //let _json = String::from_utf8(wr.unwrap()).unwrap(); - /* - assert_eq!(_s, _json.as_slice()); - */ + manual_escape(&mut wr, &log); }); } +#[test] +fn test_manual_my_mem_writer1_no_escape() { + let log = Log::new(); + + let mut wr = MyMemWriter1::with_capacity(1024); + manual_no_escape(&mut wr, &log); + + let json = String::from_utf8(wr.buf).unwrap(); + assert_eq!(JSON_STR, json.as_slice()); +} + #[bench] fn bench_manual_my_mem_writer1_no_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 = MyMemWriter1::with_capacity(1000); - manual_no_escape(wr.by_ref(), &log); - b.bytes = wr.unwrap().len() as u64; + let mut wr = MyMemWriter1::with_capacity(1024); + manual_no_escape(&mut wr, &log); + b.bytes = wr.buf.len() as u64; b.iter(|| { - let mut wr = MyMemWriter1::with_capacity(1024); - manual_no_escape(wr.by_ref(), &log); + wr.buf.clear(); - let _json = wr.unwrap(); - - //let _json = String::from_utf8(wr.unwrap()).unwrap(); - /* - assert_eq!(_s, _json.as_slice()); - */ + manual_no_escape(&mut wr, &log); }); } +#[test] +fn test_manual_my_mem_writer1_escape() { + let log = Log::new(); + + let mut wr = MyMemWriter1::with_capacity(1024); + manual_escape(&mut wr, &log); + + let json = String::from_utf8(wr.buf).unwrap(); + assert_eq!(JSON_STR, json.as_slice()); +} + #[bench] fn bench_manual_my_mem_writer1_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 = MyMemWriter1::with_capacity(1024); - manual_escape(wr.by_ref(), &log); - b.bytes = wr.unwrap().len() as u64; + manual_escape(&mut wr, &log); + b.bytes = wr.buf.len() as u64; b.iter(|| { - let mut wr = MyMemWriter1::with_capacity(1024); - manual_escape(wr.by_ref(), &log); - let _json = wr.unwrap(); + wr.buf.clear(); - //let _json = String::from_utf8(wr.unwrap()).unwrap(); - /* - assert_eq!(_s, _json.as_slice()); - */ + manual_escape(&mut wr, &log); }); } - -/* -fn direct(wr: W, log: &Log) { - use serde2::ser::VisitorState; - - let mut serializer = json::Writer::new(wr); - serializer.serialize_struct_start("Log", 12).unwrap(); - - serializer.serialize_struct_elt("timestamp", &log.timestamp).unwrap(); - serializer.serialize_struct_elt("zone_id", &log.zone_id).unwrap(); - serializer.serialize_struct_elt("zone_plan", &(log.zone_plan as uint)).unwrap(); - - serializer.serialize_struct_start("Http", 9).unwrap(); - serializer.serialize_struct_elt("protocol", &(log.http.protocol as uint)).unwrap(); - serializer.serialize_struct_elt("status", &log.http.status).unwrap(); - serializer.serialize_struct_elt("host_status", &log.http.host_status).unwrap(); - serializer.serialize_struct_elt("up_status", &log.http.up_status).unwrap(); - serializer.serialize_struct_elt("method", &(log.http.method as uint)).unwrap(); - serializer.serialize_struct_elt("content_type", &log.http.content_type).unwrap(); - serializer.serialize_struct_elt("user_agent", &log.http.user_agent).unwrap(); - serializer.serialize_struct_elt("referer", &log.http.referer.as_slice()).unwrap(); - serializer.serialize_struct_elt("request_uri", &log.http.request_uri.as_slice()).unwrap(); - serializer.serialize_struct_end().unwrap(); - - serializer.serialize_struct_start("Origin", 3).unwrap(); - serializer.serialize_struct_elt("port", &log.origin.port).unwrap(); - serializer.serialize_struct_elt("hostname", &log.origin.hostname.as_slice()).unwrap(); - serializer.serialize_struct_elt("protocol", &(log.origin.protocol as uint)).unwrap(); - serializer.serialize_struct_end().unwrap(); - - serializer.serialize_struct_elt("country", &(log.country as uint)).unwrap(); - serializer.serialize_struct_elt("cache_status", &(log.cache_status as uint)).unwrap(); - serializer.serialize_struct_elt("server_ip", &log.server_ip.as_slice()).unwrap(); - serializer.serialize_struct_elt("server_name", &log.server_name.as_slice()).unwrap(); - serializer.serialize_struct_elt("remote_ip", &log.remote_ip.as_slice()).unwrap(); - serializer.serialize_struct_elt("bytes_dlv", &log.bytes_dlv).unwrap(); - serializer.serialize_struct_elt("ray_id", &log.ray_id.as_slice()).unwrap(); - - serializer.serialize_struct_end().unwrap(); -} - -#[bench] -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); - direct(wr.by_ref(), &log); - b.bytes = wr.unwrap().len() as u64; - - b.iter(|| { - let mut wr = MemWriter::with_capacity(1024); - direct(wr.by_ref(), &log); - let _json = wr.unwrap(); - - //let _json = String::from_utf8(_json).unwrap(); - //assert_eq!(_s, _json.as_slice()); - }); -} - -#[bench] -fn bench_direct_my_mem_writer0(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 = MyMemWriter0::with_capacity(1024); - direct(wr.by_ref(), &log); - b.bytes = wr.unwrap().len() as u64; - - b.iter(|| { - let mut wr = MyMemWriter0::with_capacity(1024); - direct(wr.by_ref(), &log); - let _json = wr.unwrap(); - - //let _json = String::from_utf8(_json).unwrap(); - //assert_eq!(_s, _json.as_slice()); - }); -} -*/ - -/* -#[bench] -fn bench_decoder(b: &mut Bencher) { - 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"}"#; - - b.bytes = s.len() as u64; - - b.iter(|| { - let json = serialize::json::from_str(s).unwrap(); - let mut decoder = serialize::json::Decoder::new(json); - let _log: Log = serialize::Decodable::decode(&mut decoder).unwrap(); - }); -} - -#[bench] -fn bench_deserializer(b: &mut Bencher) { - let s = r#"{"timestamp":25469139677502,"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"}"#; - - b.bytes = s.len() as u64; - - b.iter(|| { - let _log: Log = json::from_str(s).unwrap(); - }); -} -*/ diff --git a/serde2/src/bin.rs b/serde2/src/bin.rs index 8a49216e..1feadae4 100644 --- a/serde2/src/bin.rs +++ b/serde2/src/bin.rs @@ -64,27 +64,27 @@ impl> Deserializer for MyDeserializer { V: de::Visitor, R, Error>, >(&mut self, visitor: &mut V) -> Result { match self.next() { - Some(Null) => { + Some(Token::Null) => { visitor.visit_null(self) } - Some(Int(v)) => { + Some(Token::Int(v)) => { visitor.visit_int(self, v) } - Some(String(v)) => { + Some(Token::String(v)) => { visitor.visit_string(self, v) } - Some(Option(is_some)) => { + Some(Token::Option(is_some)) => { visitor.visit_option(self, MyOptionVisitor { is_some: is_some, }) } - Some(SeqStart(len)) => { + Some(Token::SeqStart(len)) => { visitor.visit_seq(self, MySeqVisitor { len: len }) } - Some(MapStart(len)) => { + Some(Token::MapStart(len)) => { visitor.visit_map(self, MyMapVisitor { len: len }) } - Some(End) => { + Some(Token::End) => { Err(self.syntax_error()) } None => { @@ -98,13 +98,13 @@ impl> Deserializer for MyDeserializer { V: de::Visitor, R, Error>, >(&mut self, visitor: &mut V) -> Result { match self.peek() { - Some(&Null) => { + Some(&Token::Null) => { self.next(); visitor.visit_option(self, MyOptionVisitor { is_some: false, }) } - Some(&Option(is_some)) => { + Some(&Token::Option(is_some)) => { self.next(); visitor.visit_option(self, MyOptionVisitor { is_some: is_some, @@ -119,11 +119,11 @@ impl> Deserializer for MyDeserializer { } fn syntax_error(&mut self) -> Error { - SyntaxError + Error::SyntaxError } fn end_of_stream_error(&mut self) -> Error { - EndOfStreamError + Error::EndOfStreamError } } @@ -158,7 +158,7 @@ impl< T: Deserialize, Error> >(&mut self, d: &mut MyDeserializer) -> Result, Error> { match d.peek() { - Some(&End) => { + Some(&Token::End) => { d.next(); Ok(None) } @@ -175,7 +175,7 @@ impl< fn end(&mut self, d: &mut MyDeserializer) -> Result<(), Error> { match d.next() { - Some(End) => Ok(()), + Some(Token::End) => Ok(()), Some(_) => Err(d.syntax_error()), None => Err(d.end_of_stream_error()), } @@ -198,7 +198,7 @@ impl< V: Deserialize, Error>, >(&mut self, d: &mut MyDeserializer) -> Result, Error> { match d.peek() { - Some(&End) => { + Some(&Token::End) => { d.next(); Ok(None) } @@ -218,7 +218,7 @@ impl< fn end(&mut self, d: &mut MyDeserializer) -> Result<(), Error> { match d.next() { - Some(End) => Ok(()), + Some(Token::End) => Ok(()), Some(_) => Err(d.syntax_error()), None => Err(d.end_of_stream_error()), } @@ -257,16 +257,16 @@ mod json { E, > de::Visitor for Visitor { fn visit_null(&mut self, _d: &mut D) -> Result { - Ok(Null) + Ok(Value::Null) } fn visit_int(&mut self, _d: &mut D, v: int) -> Result { - Ok(Int(v)) + Ok(Value::Int(v)) } /* fn visit_string(&mut self, _d: &mut D, v: String) -> Result { - Ok(String(v)) + Ok(Value::String(v)) } */ @@ -275,7 +275,7 @@ mod json { >(&mut self, d: &mut D, mut visitor: Visitor) -> Result { match try!(visitor.visit(d)) { Some(value) => Ok(value), - None => Ok(Null), + None => Ok(Value::Null), } } @@ -296,7 +296,7 @@ mod json { } } - Ok(List(values)) + Ok(Value::List(values)) } fn visit_map< @@ -315,7 +315,7 @@ mod json { } } - Ok(Map(values)) + Ok(Value::Map(values)) } } @@ -328,10 +328,10 @@ mod json { fn main() { let tokens = vec!( - SeqStart(2), - Int(1), - Int(2), - End, + Token::SeqStart(2), + Token::Int(1), + Token::Int(2), + Token::End, ); let mut state = MyDeserializer::new(tokens.into_iter()); @@ -341,10 +341,10 @@ fn main() { //// let tokens = vec!( - SeqStart(2), - Int(3), - Int(4), - End, + Token::SeqStart(2), + Token::Int(3), + Token::Int(4), + Token::End, ); let mut state = MyDeserializer::new(tokens.into_iter()); @@ -354,10 +354,10 @@ fn main() { //// let tokens = vec!( - SeqStart(2), - Int(5), - Int(6), - End, + Token::SeqStart(2), + Token::Int(5), + Token::Int(6), + Token::End, ); let mut state = MyDeserializer::new(tokens.into_iter()); @@ -367,8 +367,8 @@ fn main() { //// let tokens = vec!( - Option(true), - Int(7), + Token::Option(true), + Token::Int(7), ); let mut state = MyDeserializer::new(tokens.into_iter()); @@ -378,7 +378,7 @@ fn main() { //// let tokens = vec!( - Option(false), + Token::Option(false), ); let mut state = MyDeserializer::new(tokens.into_iter()); @@ -388,8 +388,8 @@ fn main() { //// let tokens = vec!( - Option(true), - Int(8), + Token::Option(true), + Token::Int(8), ); let mut state = MyDeserializer::new(tokens.into_iter()); @@ -399,7 +399,7 @@ fn main() { //// let tokens = vec!( - Option(false), + Token::Option(false), ); let mut state = MyDeserializer::new(tokens.into_iter()); @@ -409,7 +409,7 @@ fn main() { //// let tokens = vec!( - Int(9), + Token::Int(9), ); let mut state = MyDeserializer::new(tokens.into_iter()); @@ -419,7 +419,7 @@ fn main() { //// let tokens = vec!( - Null, + Token::Null, ); let mut state = MyDeserializer::new(tokens.into_iter()); @@ -429,7 +429,7 @@ fn main() { //// let tokens = vec!( - Int(10), + Token::Int(10), ); let mut state = MyDeserializer::new(tokens.into_iter()); @@ -439,7 +439,7 @@ fn main() { //// let tokens = vec!( - Null, + Token::Null, ); let mut state = MyDeserializer::new(tokens.into_iter()); @@ -449,12 +449,12 @@ fn main() { //// let tokens = vec!( - MapStart(2), - String("a".to_string()), - Int(1), - String("b".to_string()), - Int(2), - End + Token::MapStart(2), + Token::String("a".to_string()), + Token::Int(1), + Token::String("b".to_string()), + Token::Int(2), + Token::End ); let mut state = MyDeserializer::new(tokens.into_iter()); @@ -464,12 +464,12 @@ fn main() { //// let tokens = vec!( - MapStart(2), - String("a".to_string()), - Int(1), - String("b".to_string()), - Int(2), - End + Token::MapStart(2), + Token::String("a".to_string()), + Token::Int(1), + Token::String("b".to_string()), + Token::Int(2), + Token::End ); let mut state = MyDeserializer::new(tokens.into_iter()); diff --git a/serde2/src/json/de.rs b/serde2/src/json/de.rs index b82f7cc4..69a01a8f 100644 --- a/serde2/src/json/de.rs +++ b/serde2/src/json/de.rs @@ -46,28 +46,28 @@ pub enum SyntaxExpectation { impl fmt::Show for ErrorCode { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { - EOFWhileParsingList => "EOF While parsing list".fmt(f), - EOFWhileParsingObject => "EOF While parsing object".fmt(f), - EOFWhileParsingString => "EOF While parsing string".fmt(f), - EOFWhileParsingValue => "EOF While parsing value".fmt(f), - ExpectedColon => "expected `:`".fmt(f), - InvalidEscape => "invalid escape".fmt(f), - InvalidNumber => "invalid number".fmt(f), - InvalidSyntax(expect) => { + ErrorCode::EOFWhileParsingList => "EOF While parsing list".fmt(f), + ErrorCode::EOFWhileParsingObject => "EOF While parsing object".fmt(f), + ErrorCode::EOFWhileParsingString => "EOF While parsing string".fmt(f), + ErrorCode::EOFWhileParsingValue => "EOF While parsing value".fmt(f), + ErrorCode::ExpectedColon => "expected `:`".fmt(f), + ErrorCode::InvalidEscape => "invalid escape".fmt(f), + ErrorCode::InvalidNumber => "invalid number".fmt(f), + ErrorCode::InvalidSyntax(expect) => { write!(f, "invalid syntax, expected: {}", expect) } - InvalidUnicodeCodePoint => "invalid unicode code point".fmt(f), - KeyMustBeAString => "key must be a string".fmt(f), - LoneLeadingSurrogateInHexEscape => "lone leading surrogate in hex escape".fmt(f), - MissingField(field) => { + ErrorCode::InvalidUnicodeCodePoint => "invalid unicode code point".fmt(f), + ErrorCode::KeyMustBeAString => "key must be a string".fmt(f), + ErrorCode::LoneLeadingSurrogateInHexEscape => "lone leading surrogate in hex escape".fmt(f), + ErrorCode::MissingField(field) => { write!(f, "missing field \"{}\"", field) } - NotFourDigit => "invalid \\u escape (not four digits)".fmt(f), - NotUtf8 => "contents not utf-8".fmt(f), - TrailingCharacters => "trailing characters".fmt(f), - UnexpectedEndOfHexEscape => "unexpected end of hex escape".fmt(f), - UnknownVariant => "unknown variant".fmt(f), - UnrecognizedHex => "invalid \\u escape (unrecognized hex)".fmt(f), + ErrorCode::NotFourDigit => "invalid \\u escape (not four digits)".fmt(f), + ErrorCode::NotUtf8 => "contents not utf-8".fmt(f), + ErrorCode::TrailingCharacters => "trailing characters".fmt(f), + ErrorCode::UnexpectedEndOfHexEscape => "unexpected end of hex escape".fmt(f), + ErrorCode::UnknownVariant => "unknown variant".fmt(f), + ErrorCode::UnrecognizedHex => "invalid \\u escape (unrecognized hex)".fmt(f), } } } @@ -103,7 +103,7 @@ impl< if self.eof() { Ok(()) } else { - Err(self.error(TrailingCharacters)) + Err(self.error(ErrorCode::TrailingCharacters)) } } @@ -140,7 +140,7 @@ impl< fn error(&mut self, reason: ErrorCode) -> Error { //self.state_stack.clear(); - SyntaxError(reason, self.line, self.col) + Error::SyntaxError(reason, self.line, self.col) } fn parse_value< @@ -150,7 +150,7 @@ impl< self.parse_whitespace(); if self.eof() { - return Err(self.error(EOFWhileParsingValue)); + return Err(self.error(ErrorCode::EOFWhileParsingValue)); } match self.ch_or_null() { @@ -180,7 +180,7 @@ impl< visitor.visit_map(self, MapVisitor { first: true }) } _ => { - Err(self.error(InvalidSyntax(SomeValue))) + Err(self.error(ErrorCode::InvalidSyntax(SyntaxExpectation::SomeValue))) } } } @@ -190,7 +190,7 @@ impl< self.bump(); Ok(()) } else { - Err(self.error(InvalidSyntax(SomeIdent))) + Err(self.error(ErrorCode::InvalidSyntax(SyntaxExpectation::SomeIdent))) } } @@ -235,7 +235,7 @@ impl< // There can be only one leading '0'. match self.ch_or_null() { '0' ... '9' => { - return Err(self.error(InvalidNumber)); + return Err(self.error(ErrorCode::InvalidNumber)); } _ => () } @@ -253,7 +253,7 @@ impl< } } _ => { - return Err(self.error(InvalidNumber)); + return Err(self.error(ErrorCode::InvalidNumber)); } } @@ -267,7 +267,7 @@ impl< match self.ch_or_null() { '0' ... '9' => (), _ => { - return Err(self.error(InvalidNumber)); + return Err(self.error(ErrorCode::InvalidNumber)); } } @@ -304,7 +304,7 @@ impl< match self.ch_or_null() { '0' ... '9' => (), _ => { - return Err(self.error(InvalidNumber)); + return Err(self.error(ErrorCode::InvalidNumber)); } } while !self.eof() { @@ -343,7 +343,7 @@ impl< 'e' | 'E' => n * 16_u16 + 14_u16, 'f' | 'F' => n * 16_u16 + 15_u16, _ => { - return Err(self.error(InvalidEscape)); + return Err(self.error(ErrorCode::InvalidEscape)); } }; @@ -352,7 +352,7 @@ impl< // Error out if we didn't parse 4 digits. if i != 4u { - return Err(self.error(InvalidEscape)); + return Err(self.error(ErrorCode::InvalidEscape)); } Ok(n) @@ -365,7 +365,7 @@ impl< loop { self.bump(); if self.eof() { - return Err(self.error(EOFWhileParsingString)); + return Err(self.error(ErrorCode::EOFWhileParsingString)); } if escape { @@ -380,7 +380,7 @@ impl< 't' => res.push('\t'), 'u' => match try!(self.decode_hex_escape()) { 0xDC00 ... 0xDFFF => { - return Err(self.error(LoneLeadingSurrogateInHexEscape)); + return Err(self.error(ErrorCode::LoneLeadingSurrogateInHexEscape)); } // Non-BMP characters are encoded as a sequence of @@ -391,7 +391,7 @@ impl< match (c1, c2) { (Some('\\'), Some('u')) => (), _ => { - return Err(self.error(UnexpectedEndOfHexEscape)); + return Err(self.error(ErrorCode::UnexpectedEndOfHexEscape)); } } @@ -399,7 +399,7 @@ impl< match str::utf16_items(buf.as_slice()).next() { Some(ScalarValue(c)) => res.push(c), _ => { - return Err(self.error(LoneLeadingSurrogateInHexEscape)); + return Err(self.error(ErrorCode::LoneLeadingSurrogateInHexEscape)); } } } @@ -407,12 +407,12 @@ impl< n => match char::from_u32(n as u32) { Some(c) => res.push(c), None => { - return Err(self.error(InvalidUnicodeCodePoint)); + return Err(self.error(ErrorCode::InvalidUnicodeCodePoint)); } }, }, _ => { - return Err(self.error(InvalidEscape)); + return Err(self.error(ErrorCode::InvalidEscape)); } } escape = false; @@ -442,11 +442,11 @@ impl> Deserializer for Parser { } fn syntax_error(&mut self) -> Error { - SyntaxError(InvalidSyntax(SomeValue), self.line, self.col) + Error::SyntaxError(ErrorCode::InvalidSyntax(SyntaxExpectation::SomeValue), self.line, self.col) } fn end_of_stream_error(&mut self) -> Error { - SyntaxError(EOFWhileParsingValue, self.line, self.col) + Error::SyntaxError(ErrorCode::EOFWhileParsingValue, self.line, self.col) } } @@ -471,9 +471,9 @@ impl> de::SeqVisitor, Error> for SeqVisitor { if d.ch_is(',') { d.bump(); } else if d.eof() { - return Err(d.error(EOFWhileParsingList)); + return Err(d.error(ErrorCode::EOFWhileParsingList)); } else { - return Err(d.error(InvalidSyntax(ListCommaOrEnd))); + return Err(d.error(ErrorCode::InvalidSyntax(SyntaxExpectation::ListCommaOrEnd))); } } @@ -486,9 +486,9 @@ impl> de::SeqVisitor, Error> for SeqVisitor { d.bump(); Ok(()) } else if d.eof() { - Err(d.error(EOFWhileParsingList)) + Err(d.error(ErrorCode::EOFWhileParsingList)) } else { - Err(d.error(TrailingCharacters)) + Err(d.error(ErrorCode::TrailingCharacters)) } } } @@ -516,18 +516,18 @@ impl> de::MapVisitor, Error> for MapVisitor { d.bump(); d.parse_whitespace(); } else if d.eof() { - return Err(d.error(EOFWhileParsingObject)); + return Err(d.error(ErrorCode::EOFWhileParsingObject)); } else { - return Err(d.error(InvalidSyntax(ObjectCommaOrEnd))); + return Err(d.error(ErrorCode::InvalidSyntax(SyntaxExpectation::ObjectCommaOrEnd))); } } if d.eof() { - return Err(d.error(EOFWhileParsingValue)); + return Err(d.error(ErrorCode::EOFWhileParsingValue)); } if !d.ch_is('"') { - return Err(d.error(KeyMustBeAString)); + return Err(d.error(ErrorCode::KeyMustBeAString)); } let key = try!(de::Deserialize::deserialize(d)); @@ -537,9 +537,9 @@ impl> de::MapVisitor, Error> for MapVisitor { if d.ch_is(':') { d.bump(); } else if d.eof() { - return Err(d.error(EOFWhileParsingObject)); + return Err(d.error(ErrorCode::EOFWhileParsingObject)); } else { - return Err(d.error(ExpectedColon)); + return Err(d.error(ErrorCode::ExpectedColon)); } d.parse_whitespace(); @@ -554,9 +554,9 @@ impl> de::MapVisitor, Error> for MapVisitor { d.bump(); Ok(()) } else if d.eof() { - Err(d.error(EOFWhileParsingList)) + Err(d.error(ErrorCode::EOFWhileParsingList)) } else { - Err(d.error(TrailingCharacters)) + Err(d.error(ErrorCode::TrailingCharacters)) } } } @@ -590,25 +590,7 @@ mod tests { use std::collections::TreeMap; use de::Deserialize; - use super::{Parser, Error, from_str}; - use super::{ - ListCommaOrEnd, - ObjectCommaOrEnd, - SomeIdent, - SomeValue, - }; - use super::{ - EOFWhileParsingList, - EOFWhileParsingObject, - EOFWhileParsingString, - EOFWhileParsingValue, - ExpectedColon, - InvalidNumber, - InvalidSyntax, - KeyMustBeAString, - TrailingCharacters, - }; - use super::SyntaxError; + use super::{Parser, Error, ErrorCode, SyntaxExpectation, from_str}; macro_rules! treemap { ($($k:expr => $v:expr),*) => ({ @@ -653,12 +635,12 @@ mod tests { #[test] fn test_parse_bool() { test_parse_err::(vec![ - ("t", SyntaxError(InvalidSyntax(SomeIdent), 1, 2)), - ("truz", SyntaxError(InvalidSyntax(SomeIdent), 1, 4)), - ("f", SyntaxError(InvalidSyntax(SomeIdent), 1, 2)), - ("faz", SyntaxError(InvalidSyntax(SomeIdent), 1, 3)), - ("truea", SyntaxError(TrailingCharacters, 1, 5)), - ("falsea", SyntaxError(TrailingCharacters, 1, 6)), + ("t", Error::SyntaxError(ErrorCode::InvalidSyntax(SyntaxExpectation::SomeIdent), 1, 2)), + ("truz", Error::SyntaxError(ErrorCode::InvalidSyntax(SyntaxExpectation::SomeIdent), 1, 4)), + ("f", Error::SyntaxError(ErrorCode::InvalidSyntax(SyntaxExpectation::SomeIdent), 1, 2)), + ("faz", Error::SyntaxError(ErrorCode::InvalidSyntax(SyntaxExpectation::SomeIdent), 1, 3)), + ("truea", Error::SyntaxError(ErrorCode::TrailingCharacters, 1, 5)), + ("falsea", Error::SyntaxError(ErrorCode::TrailingCharacters, 1, 6)), ]); test_parse_ok(vec![ @@ -670,14 +652,14 @@ mod tests { #[test] fn test_parse_numbers() { test_parse_err::(vec![ - ("+", SyntaxError(InvalidSyntax(SomeValue), 1, 1)), - (".", SyntaxError(InvalidSyntax(SomeValue), 1, 1)), - ("-", SyntaxError(InvalidNumber, 1, 2)), - ("00", SyntaxError(InvalidNumber, 1, 2)), - ("1.", SyntaxError(InvalidNumber, 1, 3)), - ("1e", SyntaxError(InvalidNumber, 1, 3)), - ("1e+", SyntaxError(InvalidNumber, 1, 4)), - ("1a", SyntaxError(TrailingCharacters, 1, 2)), + ("+", Error::SyntaxError(ErrorCode::InvalidSyntax(SyntaxExpectation::SomeValue), 1, 1)), + (".", Error::SyntaxError(ErrorCode::InvalidSyntax(SyntaxExpectation::SomeValue), 1, 1)), + ("-", Error::SyntaxError(ErrorCode::InvalidNumber, 1, 2)), + ("00", Error::SyntaxError(ErrorCode::InvalidNumber, 1, 2)), + ("1.", Error::SyntaxError(ErrorCode::InvalidNumber, 1, 3)), + ("1e", Error::SyntaxError(ErrorCode::InvalidNumber, 1, 3)), + ("1e+", Error::SyntaxError(ErrorCode::InvalidNumber, 1, 4)), + ("1a", Error::SyntaxError(ErrorCode::TrailingCharacters, 1, 2)), ]); test_parse_ok(vec![ @@ -700,9 +682,9 @@ mod tests { #[test] fn test_parse_string() { test_parse_err::(vec![ - ("\"", SyntaxError(EOFWhileParsingString, 1, 2)), - ("\"lol", SyntaxError(EOFWhileParsingString, 1, 5)), - ("\"lol\"a", SyntaxError(TrailingCharacters, 1, 6)), + ("\"", Error::SyntaxError(ErrorCode::EOFWhileParsingString, 1, 2)), + ("\"lol", Error::SyntaxError(ErrorCode::EOFWhileParsingString, 1, 5)), + ("\"lol\"a", Error::SyntaxError(ErrorCode::TrailingCharacters, 1, 6)), ]); test_parse_ok(vec![ @@ -721,13 +703,13 @@ mod tests { #[test] fn test_parse_list() { test_parse_err::>(vec![ - ("[", SyntaxError(EOFWhileParsingValue, 1, 2)), - ("[ ", SyntaxError(EOFWhileParsingValue, 1, 3)), - ("[1", SyntaxError(EOFWhileParsingList, 1, 3)), - ("[1,", SyntaxError(EOFWhileParsingValue, 1, 4)), - ("[1,]", SyntaxError(InvalidSyntax(SomeValue), 1, 4)), - ("[1 2]", SyntaxError(InvalidSyntax(ListCommaOrEnd), 1, 4)), - ("[]a", SyntaxError(TrailingCharacters, 1, 3)), + ("[", Error::SyntaxError(ErrorCode::EOFWhileParsingValue, 1, 2)), + ("[ ", Error::SyntaxError(ErrorCode::EOFWhileParsingValue, 1, 3)), + ("[1", Error::SyntaxError(ErrorCode::EOFWhileParsingList, 1, 3)), + ("[1,", Error::SyntaxError(ErrorCode::EOFWhileParsingValue, 1, 4)), + ("[1,]", Error::SyntaxError(ErrorCode::InvalidSyntax(SyntaxExpectation::SomeValue), 1, 4)), + ("[1 2]", Error::SyntaxError(ErrorCode::InvalidSyntax(SyntaxExpectation::ListCommaOrEnd), 1, 4)), + ("[]a", Error::SyntaxError(ErrorCode::TrailingCharacters, 1, 3)), ]); test_parse_ok(vec![ @@ -770,18 +752,18 @@ mod tests { #[test] fn test_parse_object() { test_parse_err::>(vec![ - ("{", SyntaxError(EOFWhileParsingValue, 1, 2)), - ("{ ", SyntaxError(EOFWhileParsingValue, 1, 3)), - ("{1", SyntaxError(KeyMustBeAString, 1, 2)), - ("{ \"a\"", SyntaxError(EOFWhileParsingObject, 1, 6)), - ("{\"a\"", SyntaxError(EOFWhileParsingObject, 1, 5)), - ("{\"a\" ", SyntaxError(EOFWhileParsingObject, 1, 6)), - ("{\"a\" 1", SyntaxError(ExpectedColon, 1, 6)), - ("{\"a\":", SyntaxError(EOFWhileParsingValue, 1, 6)), - ("{\"a\":1", SyntaxError(EOFWhileParsingObject, 1, 7)), - ("{\"a\":1 1", SyntaxError(InvalidSyntax(ObjectCommaOrEnd), 1, 8)), - ("{\"a\":1,", SyntaxError(EOFWhileParsingValue, 1, 8)), - ("{}a", SyntaxError(TrailingCharacters, 1, 3)), + ("{", Error::SyntaxError(ErrorCode::EOFWhileParsingValue, 1, 2)), + ("{ ", Error::SyntaxError(ErrorCode::EOFWhileParsingValue, 1, 3)), + ("{1", Error::SyntaxError(ErrorCode::KeyMustBeAString, 1, 2)), + ("{ \"a\"", Error::SyntaxError(ErrorCode::EOFWhileParsingObject, 1, 6)), + ("{\"a\"", Error::SyntaxError(ErrorCode::EOFWhileParsingObject, 1, 5)), + ("{\"a\" ", Error::SyntaxError(ErrorCode::EOFWhileParsingObject, 1, 6)), + ("{\"a\" 1", Error::SyntaxError(ErrorCode::ExpectedColon, 1, 6)), + ("{\"a\":", Error::SyntaxError(ErrorCode::EOFWhileParsingValue, 1, 6)), + ("{\"a\":1", Error::SyntaxError(ErrorCode::EOFWhileParsingObject, 1, 7)), + ("{\"a\":1 1", Error::SyntaxError(ErrorCode::InvalidSyntax(SyntaxExpectation::ObjectCommaOrEnd), 1, 8)), + ("{\"a\":1,", Error::SyntaxError(ErrorCode::EOFWhileParsingValue, 1, 8)), + ("{}a", Error::SyntaxError(ErrorCode::TrailingCharacters, 1, 3)), ]); test_parse_ok(vec![ diff --git a/serde2/src/json/ser.rs b/serde2/src/json/ser.rs index 17ad634e..00326879 100644 --- a/serde2/src/json/ser.rs +++ b/serde2/src/json/ser.rs @@ -218,7 +218,7 @@ pub fn escape_str(wr: &mut W, value: &str) -> Result<(), IoError> #[inline] pub fn escape_char(wr: &mut W, value: char) -> Result<(), IoError> { - let mut buf = [0, .. 4]; + let mut buf = &mut [0, .. 4]; value.encode_utf8(buf); escape_bytes(wr, buf) }