Fix serde2_macros and benchmarks, and silence some warnings

This commit is contained in:
Erick Tryzelaar 2015-03-03 11:07:33 -08:00
parent dd4f7537ea
commit 9904d32ff9
5 changed files with 54 additions and 47 deletions

View File

@ -10,12 +10,10 @@ use std::io::{self, ReadExt, WriteExt};
use std::num::FromPrimitive;
use test::Bencher;
use serde2::de::{Deserialize, Deserializer};
use serde2::de;
use serde2::de::{self, Deserialize, Deserializer};
use serde2::json::ser::escape_str;
use serde2::json;
use serde2::ser::{Serialize, Serializer};
use serde2::ser;
use serde2::ser::{self, Serialize, Serializer};
use rustc_serialize::Encodable;
@ -70,7 +68,7 @@ impl de::Deserialize for HttpProtocol {
fn deserialize<
S: Deserializer,
>(state: &mut S) -> Result<HttpProtocol, S::Error> {
state.visit(&mut de::PrimitiveVisitor)
state.visit(&mut de::PrimitiveVisitor::new())
}
}
@ -118,7 +116,7 @@ impl de::Deserialize for HttpMethod {
fn deserialize<
S: de::Deserializer,
>(state: &mut S) -> Result<HttpMethod, S::Error> {
state.visit(&mut de::PrimitiveVisitor)
state.visit(&mut de::PrimitiveVisitor::new())
}
}
@ -159,7 +157,7 @@ impl de::Deserialize for CacheStatus {
fn deserialize<
S: de::Deserializer,
>(state: &mut S) -> Result<CacheStatus, S::Error> {
state.visit(&mut de::PrimitiveVisitor)
state.visit(&mut de::PrimitiveVisitor::new())
}
}
@ -209,7 +207,7 @@ impl de::Deserialize for OriginProtocol {
fn deserialize<
S: de::Deserializer,
>(state: &mut S) -> Result<OriginProtocol, S::Error> {
state.visit(&mut de::PrimitiveVisitor)
state.visit(&mut de::PrimitiveVisitor::new())
}
}
@ -251,7 +249,7 @@ impl de::Deserialize for ZonePlan {
fn deserialize<
S: de::Deserializer,
>(state: &mut S) -> Result<ZonePlan, S::Error> {
state.visit(&mut de::PrimitiveVisitor)
state.visit(&mut de::PrimitiveVisitor::new())
}
}
@ -544,7 +542,7 @@ impl de::Deserialize for Country {
fn deserialize<
S: de::Deserializer,
>(state: &mut S) -> Result<Country, S::Error> {
state.visit(&mut de::PrimitiveVisitor)
state.visit(&mut de::PrimitiveVisitor::new())
}
}
@ -677,7 +675,7 @@ fn push_all_bytes(dst: &mut Vec<u8>, src: &[u8]) {
// we would have failed if `reserve` overflowed.
dst.set_len(dst_len + src_len);
::std::ptr::copy_nonoverlapping_memory(
::std::ptr::copy_nonoverlapping(
dst.as_mut_ptr().offset(dst_len as isize),
src.as_ptr(),
src_len);
@ -712,7 +710,7 @@ fn test_encoder() {
log.encode(&mut encoder).unwrap();
}
assert_eq!(&wr[], JSON_STR);
assert_eq!(&wr, &JSON_STR);
}
#[bench]
@ -758,11 +756,11 @@ fn bench_serializer(b: &mut Bencher) {
fn test_serializer_vec() {
let log = Log::new();
let wr = Vec::with_capacity(1024);
let mut serializer = json::Writer::new(wr);
let mut serializer = json::Serializer::new(wr);
serializer.visit(&log).unwrap();
let json = serializer.into_inner();
assert_eq!(&json[], JSON_STR.as_bytes());
assert_eq!(&json, &JSON_STR.as_bytes());
}
#[bench]
@ -776,7 +774,7 @@ fn bench_serializer_vec(b: &mut Bencher) {
b.iter(|| {
wr.clear();
let mut serializer = json::Writer::new(wr.by_ref());
let mut serializer = json::Serializer::new(wr.by_ref());
serializer.visit(&log).unwrap();
let _json = serializer.into_inner();
});
@ -792,9 +790,9 @@ fn bench_serializer_slice(b: &mut Bencher) {
b.iter(|| {
for item in buf.iter_mut(){ *item = 0; }
let mut wr = &mut buf[];
let mut wr = &mut buf[..];
let mut serializer = json::Writer::new(wr.by_ref());
let mut serializer = json::Serializer::new(wr.by_ref());
serializer.visit(&log).unwrap();
let _json = serializer.into_inner();
});
@ -807,12 +805,12 @@ fn test_serializer_my_mem_writer0() {
let mut wr = MyMemWriter0::with_capacity(1024);
{
let mut serializer = json::Writer::new(wr.by_ref());
let mut serializer = json::Serializer::new(wr.by_ref());
serializer.visit(&log).unwrap();
let _json = serializer.into_inner();
}
assert_eq!(&wr.buf[], JSON_STR.as_bytes());
assert_eq!(&wr.buf, &JSON_STR.as_bytes());
}
#[bench]
@ -826,7 +824,7 @@ fn bench_serializer_my_mem_writer0(b: &mut Bencher) {
b.iter(|| {
wr.buf.clear();
let mut serializer = json::Writer::new(wr.by_ref());
let mut serializer = json::Serializer::new(wr.by_ref());
serializer.visit(&log).unwrap();
let _json = serializer.into_inner();
});
@ -839,12 +837,12 @@ fn test_serializer_my_mem_writer1() {
let mut wr = MyMemWriter1::with_capacity(1024);
{
let mut serializer = json::Writer::new(wr.by_ref());
let mut serializer = json::Serializer::new(wr.by_ref());
serializer.visit(&log).unwrap();
let _json = serializer.into_inner();
}
assert_eq!(&wr.buf[], JSON_STR.as_bytes());
assert_eq!(&wr.buf, &JSON_STR.as_bytes());
}
#[bench]
@ -858,7 +856,7 @@ fn bench_serializer_my_mem_writer1(b: &mut Bencher) {
b.iter(|| {
wr.buf.clear();
let mut serializer = json::Writer::new(wr.by_ref());
let mut serializer = json::Serializer::new(wr.by_ref());
serializer.visit(&log).unwrap();
let _json = serializer.into_inner();
});
@ -1044,7 +1042,7 @@ fn test_manual_serialize_vec_no_escape() {
manual_serialize_no_escape(&mut wr, &log);
let json = String::from_utf8(wr).unwrap();
assert_eq!(JSON_STR, &json[]);
assert_eq!(&JSON_STR, &json);
}
#[bench]
@ -1069,7 +1067,7 @@ fn test_manual_serialize_vec_escape() {
manual_serialize_escape(&mut wr, &log);
let json = String::from_utf8(wr).unwrap();
assert_eq!(JSON_STR, &json[]);
assert_eq!(&JSON_STR, &json);
}
#[bench]
@ -1095,7 +1093,7 @@ fn test_manual_serialize_my_mem_writer0_no_escape() {
manual_serialize_no_escape(&mut wr, &log);
let json = String::from_utf8(wr.buf).unwrap();
assert_eq!(JSON_STR, &json[]);
assert_eq!(&JSON_STR, &json);
}
#[bench]
@ -1121,7 +1119,7 @@ fn test_manual_serialize_my_mem_writer0_escape() {
manual_serialize_escape(&mut wr, &log);
let json = String::from_utf8(wr.buf).unwrap();
assert_eq!(JSON_STR, &json[]);
assert_eq!(&JSON_STR, &json);
}
#[bench]
@ -1147,7 +1145,7 @@ fn test_manual_serialize_my_mem_writer1_no_escape() {
manual_serialize_no_escape(&mut wr, &log);
let json = String::from_utf8(wr.buf).unwrap();
assert_eq!(JSON_STR, &json[]);
assert_eq!(&JSON_STR, &json);
}
#[bench]
@ -1173,7 +1171,7 @@ fn test_manual_serialize_my_mem_writer1_escape() {
manual_serialize_escape(&mut wr, &log);
let json = String::from_utf8(wr.buf).unwrap();
assert_eq!(JSON_STR, &json[]);
assert_eq!(&JSON_STR, &json);
}
#[bench]

View File

@ -140,7 +140,7 @@ fn serialize_substructure(cx: &ExtCtxt,
visitor,
substr.type_ident,
variant,
&fields[],
&fields,
generics)
}
@ -510,7 +510,7 @@ fn serialize_variant(
),
);
let impl_ident = ast_util::impl_pretty_name(&opt_trait_ref, &self_ty);
let impl_ident = ast_util::impl_pretty_name(&opt_trait_ref, Some(&self_ty));
let methods = vec![
ast::MethodImplItem(
@ -674,7 +674,7 @@ fn deserialize_struct(
type_ident,
struct_ident,
struct_path,
&fields[],
&fields,
state)
}
}
@ -685,7 +685,7 @@ fn deserialize_struct(
type_ident,
struct_ident,
struct_path,
&fields[],
&fields,
state,
struct_def)
}
@ -752,7 +752,7 @@ fn deserialize_struct_unnamed_fields(
cx,
span,
struct_path,
&field_names[],
&field_names,
);
quote_expr!(cx, {
@ -835,7 +835,7 @@ fn deserialize_struct_named_fields(
let field_deserializer = declare_map_field_deserializer(
cx,
span,
&field_names[],
&field_names,
fields,
struct_def,
);
@ -844,7 +844,7 @@ fn deserialize_struct_named_fields(
cx,
span,
struct_path,
&field_names[],
&field_names,
fields,
struct_def
);
@ -1177,7 +1177,7 @@ fn deserialize_enum_variant(
cx,
span,
variant_path,
&field_names[],
&field_names,
);
quote_expr!(cx, {
@ -1206,7 +1206,7 @@ fn deserialize_enum_variant(
let field_deserializer = declare_map_field_deserializer(
cx,
span,
&field_names[],
&field_names,
fields,
match variant_ptr.node.kind {
ast::VariantKind::StructVariantKind(ref sd) => &*sd,
@ -1218,7 +1218,7 @@ fn deserialize_enum_variant(
cx,
span,
variant_path,
&field_names[],
&field_names,
fields,
match variant_ptr.node.kind {
ast::VariantKind::StructVariantKind(ref sd) => &*sd,

View File

@ -361,6 +361,15 @@ pub struct PrimitiveVisitor<T> {
marker: PhantomData<T>,
}
impl<T> PrimitiveVisitor<T> {
#[inline]
pub fn new() -> Self {
PrimitiveVisitor {
marker: PhantomData,
}
}
}
impl<
T: Deserialize + FromPrimitive
> self::Visitor for PrimitiveVisitor<T> {
@ -387,7 +396,7 @@ macro_rules! impl_deserialize_num {
fn deserialize<
D: Deserializer,
>(deserializer: &mut D) -> Result<$ty, D::Error> {
deserializer.visit(&mut PrimitiveVisitor { marker: PhantomData })
deserializer.visit(&mut PrimitiveVisitor::new())
}
}
}

View File

@ -1,6 +1,6 @@
use std::error;
use std::fmt;
use std::old_io;
use std::io;
use de;
@ -81,7 +81,7 @@ impl fmt::Debug for ErrorCode {
pub enum Error {
/// msg, line, col
SyntaxError(ErrorCode, usize, usize),
IoError(old_io::IoError),
IoError(io::Error),
/*
ExpectedError(String, String),
*/
@ -139,8 +139,8 @@ impl fmt::Display for Error {
}
}
impl error::FromError<old_io::IoError> for Error {
fn from_error(error: old_io::IoError) -> Error {
impl error::FromError<io::Error> for Error {
fn from_error(error: io::Error) -> Error {
Error::IoError(error)
}
}

View File

@ -1108,14 +1108,14 @@ mod tests {
false => vec![Token::Bool(false)],
}
test_isizes {
0is => vec![Token::Isize(0)],
0isize => vec![Token::Isize(0)],
0i8 => vec![Token::I8(0)],
0i16 => vec![Token::I16(0)],
0i32 => vec![Token::I32(0)],
0i64 => vec![Token::I64(0)],
}
test_usizes {
0us => vec![Token::Usize(0)],
0usize => vec![Token::Usize(0)],
0u8 => vec![Token::U8(0)],
0u16 => vec![Token::U16(0)],
0u32 => vec![Token::U32(0)],
@ -1144,7 +1144,7 @@ mod tests {
Token::SeqStart(0),
Token::SeqEnd,
],
&[1, 2, 3][] => vec![
&[1, 2, 3][..] => vec![
Token::SeqStart(3),
Token::SeqSep(true),
Token::I32(1),