Merge ser::Serializer and ser::Visitor
This commit is contained in:
parent
fbd6d1974a
commit
3f62b6d2bf
@ -56,10 +56,10 @@ impl rustc_serialize::Decodable for HttpProtocol {
|
||||
|
||||
impl ser::Serialize for HttpProtocol {
|
||||
#[inline]
|
||||
fn visit<
|
||||
V: ser::Visitor,
|
||||
>(&self, visitor: &mut V) -> Result<(), V::Error> {
|
||||
visitor.visit_u8(*self as u8)
|
||||
fn visit<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: ser::Serializer,
|
||||
{
|
||||
serializer.visit_u8(*self as u8)
|
||||
}
|
||||
}
|
||||
|
||||
@ -104,10 +104,10 @@ impl rustc_serialize::Decodable for HttpMethod {
|
||||
|
||||
impl ser::Serialize for HttpMethod {
|
||||
#[inline]
|
||||
fn visit<
|
||||
V: ser::Visitor,
|
||||
>(&self, visitor: &mut V) -> Result<(), V::Error> {
|
||||
visitor.visit_u8(*self as u8)
|
||||
fn visit<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: ser::Serializer,
|
||||
{
|
||||
serializer.visit_u8(*self as u8)
|
||||
}
|
||||
}
|
||||
|
||||
@ -145,10 +145,10 @@ impl rustc_serialize::Decodable for CacheStatus {
|
||||
|
||||
impl ser::Serialize for CacheStatus {
|
||||
#[inline]
|
||||
fn visit<
|
||||
V: ser::Visitor,
|
||||
>(&self, visitor: &mut V) -> Result<(), V::Error> {
|
||||
visitor.visit_u8(*self as u8)
|
||||
fn visit<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: ser::Serializer,
|
||||
{
|
||||
serializer.visit_u8(*self as u8)
|
||||
}
|
||||
}
|
||||
|
||||
@ -195,10 +195,10 @@ impl rustc_serialize::Decodable for OriginProtocol {
|
||||
|
||||
impl ser::Serialize for OriginProtocol {
|
||||
#[inline]
|
||||
fn visit<
|
||||
V: ser::Visitor,
|
||||
>(&self, visitor: &mut V) -> Result<(), V::Error> {
|
||||
visitor.visit_u8(*self as u8)
|
||||
fn visit<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: ser::Serializer,
|
||||
{
|
||||
serializer.visit_u8(*self as u8)
|
||||
}
|
||||
}
|
||||
|
||||
@ -237,10 +237,10 @@ impl rustc_serialize::Decodable for ZonePlan {
|
||||
|
||||
impl ser::Serialize for ZonePlan {
|
||||
#[inline]
|
||||
fn visit<
|
||||
V: ser::Visitor,
|
||||
>(&self, visitor: &mut V) -> Result<(), V::Error> {
|
||||
visitor.visit_u8(*self as u8)
|
||||
fn visit<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: ser::Serializer,
|
||||
{
|
||||
serializer.visit_u8(*self as u8)
|
||||
}
|
||||
}
|
||||
|
||||
@ -530,10 +530,10 @@ impl rustc_serialize::Decodable for Country {
|
||||
|
||||
impl ser::Serialize for Country {
|
||||
#[inline]
|
||||
fn visit<
|
||||
V: ser::Visitor,
|
||||
>(&self, visitor: &mut V) -> Result<(), V::Error> {
|
||||
visitor.visit_u8(*self as u8)
|
||||
fn visit<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: ser::Serializer,
|
||||
{
|
||||
serializer.visit_u8(*self as u8)
|
||||
}
|
||||
}
|
||||
|
||||
@ -757,7 +757,7 @@ fn test_serializer_vec() {
|
||||
let log = Log::new();
|
||||
let wr = Vec::with_capacity(1024);
|
||||
let mut serializer = json::Serializer::new(wr);
|
||||
serializer.visit(&log).unwrap();
|
||||
log.visit(&mut serializer).unwrap();
|
||||
|
||||
let json = serializer.into_inner();
|
||||
assert_eq!(&json, &JSON_STR.as_bytes());
|
||||
@ -775,7 +775,7 @@ fn bench_serializer_vec(b: &mut Bencher) {
|
||||
wr.clear();
|
||||
|
||||
let mut serializer = json::Serializer::new(wr.by_ref());
|
||||
serializer.visit(&log).unwrap();
|
||||
log.visit(&mut serializer).unwrap();
|
||||
let _json = serializer.into_inner();
|
||||
});
|
||||
}
|
||||
@ -793,7 +793,7 @@ fn bench_serializer_slice(b: &mut Bencher) {
|
||||
let mut wr = &mut buf[..];
|
||||
|
||||
let mut serializer = json::Serializer::new(wr.by_ref());
|
||||
serializer.visit(&log).unwrap();
|
||||
log.visit(&mut serializer).unwrap();
|
||||
let _json = serializer.into_inner();
|
||||
});
|
||||
}
|
||||
@ -806,7 +806,7 @@ fn test_serializer_my_mem_writer0() {
|
||||
|
||||
{
|
||||
let mut serializer = json::Serializer::new(wr.by_ref());
|
||||
serializer.visit(&log).unwrap();
|
||||
log.visit(&mut serializer).unwrap();
|
||||
let _json = serializer.into_inner();
|
||||
}
|
||||
|
||||
@ -825,7 +825,7 @@ fn bench_serializer_my_mem_writer0(b: &mut Bencher) {
|
||||
wr.buf.clear();
|
||||
|
||||
let mut serializer = json::Serializer::new(wr.by_ref());
|
||||
serializer.visit(&log).unwrap();
|
||||
log.visit(&mut serializer).unwrap();
|
||||
let _json = serializer.into_inner();
|
||||
});
|
||||
}
|
||||
@ -838,7 +838,7 @@ fn test_serializer_my_mem_writer1() {
|
||||
|
||||
{
|
||||
let mut serializer = json::Serializer::new(wr.by_ref());
|
||||
serializer.visit(&log).unwrap();
|
||||
log.visit(&mut serializer).unwrap();
|
||||
let _json = serializer.into_inner();
|
||||
}
|
||||
|
||||
@ -857,7 +857,7 @@ fn bench_serializer_my_mem_writer1(b: &mut Bencher) {
|
||||
wr.buf.clear();
|
||||
|
||||
let mut serializer = json::Serializer::new(wr.by_ref());
|
||||
serializer.visit(&log).unwrap();
|
||||
log.visit(&mut serializer).unwrap();
|
||||
let _json = serializer.into_inner();
|
||||
});
|
||||
}
|
||||
|
@ -82,13 +82,13 @@ fn expand_derive_serialize(
|
||||
generics: LifetimeBounds {
|
||||
lifetimes: Vec::new(),
|
||||
bounds: vec![
|
||||
("__V", vec![Path::new(vec!["serde", "ser", "Visitor"])]),
|
||||
("__S", vec![Path::new(vec!["serde", "ser", "Serializer"])]),
|
||||
]
|
||||
},
|
||||
explicit_self: borrowed_explicit_self(),
|
||||
args: vec![
|
||||
Ty::Ptr(
|
||||
Box::new(Ty::Literal(Path::new_local("__V"))),
|
||||
Box::new(Ty::Literal(Path::new_local("__S"))),
|
||||
Borrowed(None, MutMutable),
|
||||
),
|
||||
],
|
||||
@ -98,7 +98,7 @@ fn expand_derive_serialize(
|
||||
None,
|
||||
vec![
|
||||
Box::new(Ty::Tuple(vec![])),
|
||||
Box::new(Ty::Literal(Path::new_(vec!["__V", "Error"],
|
||||
Box::new(Ty::Literal(Path::new_(vec!["__S", "Error"],
|
||||
None,
|
||||
vec![],
|
||||
false))),
|
||||
@ -125,7 +125,7 @@ fn serialize_substructure(
|
||||
) -> P<Expr> {
|
||||
let builder = aster::AstBuilder::new().span(span);
|
||||
|
||||
let visitor = substr.nonself_args[0].clone();
|
||||
let serializer = substr.nonself_args[0].clone();
|
||||
|
||||
match (&item.node, &*substr.fields) {
|
||||
(&ast::ItemStruct(ref struct_def, ref generics), &Struct(ref fields)) => {
|
||||
@ -144,7 +144,7 @@ fn serialize_substructure(
|
||||
serialize_unit_struct(
|
||||
cx,
|
||||
&builder,
|
||||
visitor,
|
||||
serializer,
|
||||
substr.type_ident,
|
||||
)
|
||||
}
|
||||
@ -152,7 +152,7 @@ fn serialize_substructure(
|
||||
serialize_tuple_struct(
|
||||
cx,
|
||||
&builder,
|
||||
visitor,
|
||||
serializer,
|
||||
substr.type_ident,
|
||||
&unnamed_fields,
|
||||
generics,
|
||||
@ -162,7 +162,7 @@ fn serialize_substructure(
|
||||
serialize_struct(
|
||||
cx,
|
||||
&builder,
|
||||
visitor,
|
||||
serializer,
|
||||
substr.type_ident,
|
||||
&named_fields,
|
||||
struct_def,
|
||||
@ -180,7 +180,7 @@ fn serialize_substructure(
|
||||
cx,
|
||||
span,
|
||||
&builder,
|
||||
visitor,
|
||||
serializer,
|
||||
substr.type_ident,
|
||||
variant,
|
||||
&fields,
|
||||
@ -195,18 +195,18 @@ fn serialize_substructure(
|
||||
fn serialize_unit_struct(
|
||||
cx: &ExtCtxt,
|
||||
builder: &aster::AstBuilder,
|
||||
visitor: P<Expr>,
|
||||
serializer: P<Expr>,
|
||||
type_ident: Ident
|
||||
) -> P<Expr> {
|
||||
let type_name = builder.expr().str(type_ident);
|
||||
|
||||
quote_expr!(cx, $visitor.visit_named_unit($type_name))
|
||||
quote_expr!(cx, $serializer.visit_named_unit($type_name))
|
||||
}
|
||||
|
||||
fn serialize_tuple_struct(
|
||||
cx: &ExtCtxt,
|
||||
builder: &aster::AstBuilder,
|
||||
visitor: P<Expr>,
|
||||
serializer: P<Expr>,
|
||||
type_ident: Ident,
|
||||
fields: &[Span],
|
||||
generics: &ast::Generics
|
||||
@ -224,7 +224,7 @@ fn serialize_tuple_struct(
|
||||
quote_arm!(cx,
|
||||
$i => {
|
||||
self.state += 1;
|
||||
let v = try!(visitor.visit_seq_elt($first, &$expr));
|
||||
let v = try!(serializer.visit_seq_elt($first, &$expr));
|
||||
Ok(Some(v))
|
||||
}
|
||||
)
|
||||
@ -255,8 +255,8 @@ fn serialize_tuple_struct(
|
||||
|
||||
impl $visitor_impl_generics ::serde::ser::SeqVisitor for Visitor $visitor_generics {
|
||||
#[inline]
|
||||
fn visit<V>(&mut self, visitor: &mut V) -> Result<Option<()>, V::Error>
|
||||
where V: ::serde::ser::Visitor,
|
||||
fn visit<S>(&mut self, serializer: &mut S) -> Result<Option<()>, S::Error>
|
||||
where S: ::serde::ser::Serializer,
|
||||
{
|
||||
match self.state {
|
||||
$arms
|
||||
@ -271,7 +271,7 @@ fn serialize_tuple_struct(
|
||||
}
|
||||
}
|
||||
|
||||
$visitor.visit_named_seq($type_name, Visitor {
|
||||
$serializer.visit_named_seq($type_name, Visitor {
|
||||
value: self,
|
||||
state: 0,
|
||||
})
|
||||
@ -281,7 +281,7 @@ fn serialize_tuple_struct(
|
||||
fn serialize_struct(
|
||||
cx: &ExtCtxt,
|
||||
builder: &aster::AstBuilder,
|
||||
visitor: P<Expr>,
|
||||
serializer: P<Expr>,
|
||||
type_ident: Ident,
|
||||
fields: &[(Ident, Span)],
|
||||
struct_def: &StructDef,
|
||||
@ -308,7 +308,7 @@ fn serialize_struct(
|
||||
quote_arm!(cx,
|
||||
$i => {
|
||||
self.state += 1;
|
||||
let v = try!(visitor.visit_map_elt($first, $expr, &self.value.$name));
|
||||
let v = try!(serializer.visit_map_elt($first, $expr, &self.value.$name));
|
||||
Ok(Some(v))
|
||||
}
|
||||
)
|
||||
@ -339,8 +339,8 @@ fn serialize_struct(
|
||||
|
||||
impl $visitor_impl_generics ::serde::ser::MapVisitor for Visitor $visitor_generics {
|
||||
#[inline]
|
||||
fn visit<V>(&mut self, visitor: &mut V) -> Result<Option<()>, V::Error>
|
||||
where V: ::serde::ser::Visitor,
|
||||
fn visit<S>(&mut self, serializer: &mut S) -> Result<Option<()>, S::Error>
|
||||
where S: ::serde::ser::Serializer,
|
||||
{
|
||||
match self.state {
|
||||
$arms
|
||||
@ -355,7 +355,7 @@ fn serialize_struct(
|
||||
}
|
||||
}
|
||||
|
||||
$visitor.visit_named_map($type_name, Visitor {
|
||||
$serializer.visit_named_map($type_name, Visitor {
|
||||
value: self,
|
||||
state: 0,
|
||||
})
|
||||
@ -366,7 +366,7 @@ fn serialize_enum(
|
||||
cx: &ExtCtxt,
|
||||
span: Span,
|
||||
builder: &aster::AstBuilder,
|
||||
visitor: P<Expr>,
|
||||
serializer: P<Expr>,
|
||||
type_ident: Ident,
|
||||
variant: &ast::Variant,
|
||||
fields: &[FieldInfo],
|
||||
@ -377,8 +377,8 @@ fn serialize_enum(
|
||||
|
||||
if fields.is_empty() {
|
||||
quote_expr!(cx,
|
||||
::serde::ser::Visitor::visit_enum_unit(
|
||||
$visitor,
|
||||
::serde::ser::Serializer::visit_enum_unit(
|
||||
$serializer,
|
||||
$type_name,
|
||||
$variant_name)
|
||||
)
|
||||
@ -387,7 +387,7 @@ fn serialize_enum(
|
||||
cx,
|
||||
span,
|
||||
builder,
|
||||
visitor,
|
||||
serializer,
|
||||
type_name,
|
||||
variant_name,
|
||||
generics,
|
||||
@ -400,7 +400,7 @@ fn serialize_variant(
|
||||
cx: &ExtCtxt,
|
||||
span: Span,
|
||||
builder: &aster::AstBuilder,
|
||||
visitor: P<ast::Expr>,
|
||||
serializer: P<ast::Expr>,
|
||||
type_name: P<ast::Expr>,
|
||||
variant_name: P<ast::Expr>,
|
||||
generics: &ast::Generics,
|
||||
@ -486,8 +486,8 @@ fn serialize_variant(
|
||||
let real_name = builder.expr().str(real_name);
|
||||
|
||||
quote_expr!(cx,
|
||||
::serde::ser::Visitor::visit_map_elt(
|
||||
visitor,
|
||||
::serde::ser::Serializer::visit_map_elt(
|
||||
serializer,
|
||||
$first,
|
||||
$real_name,
|
||||
$field_expr,
|
||||
@ -496,8 +496,8 @@ fn serialize_variant(
|
||||
}
|
||||
None => {
|
||||
quote_expr!(cx,
|
||||
::serde::ser::Visitor::visit_seq_elt(
|
||||
visitor,
|
||||
::serde::ser::Serializer::visit_seq_elt(
|
||||
serializer,
|
||||
$first,
|
||||
$field_expr,
|
||||
)
|
||||
@ -537,8 +537,8 @@ fn serialize_variant(
|
||||
let methods = vec![
|
||||
ast::MethodImplItem(
|
||||
quote_method!(cx,
|
||||
fn visit<V>(&mut self, visitor: &mut V) -> Result<Option<()>, V::Error>
|
||||
where V: ::serde::ser::Visitor,
|
||||
fn visit<V>(&mut self, serializer: &mut V) -> Result<Option<()>, V::Error>
|
||||
where V: ::serde::ser::Serializer,
|
||||
{
|
||||
match self.state {
|
||||
$visitor_arms
|
||||
@ -575,8 +575,8 @@ fn serialize_variant(
|
||||
$visitor_struct
|
||||
$visitor_impl
|
||||
|
||||
::serde::ser::Visitor::$visitor_method_name(
|
||||
$visitor,
|
||||
::serde::ser::Serializer::$visitor_method_name(
|
||||
$serializer,
|
||||
$type_name,
|
||||
$variant_name,
|
||||
$visitor_expr,
|
||||
@ -967,7 +967,7 @@ fn deserialize_struct_named_fields(
|
||||
.segment(type_ident).with_generics(generics.clone()).build()
|
||||
.build();
|
||||
|
||||
let field_deserializer = declare_map_field_deserializer(
|
||||
let field_devisitor = declare_map_field_devisitor(
|
||||
cx,
|
||||
span,
|
||||
builder,
|
||||
@ -986,7 +986,7 @@ fn deserialize_struct_named_fields(
|
||||
);
|
||||
|
||||
quote_expr!(cx, {
|
||||
$field_deserializer
|
||||
$field_devisitor
|
||||
|
||||
$visitor_struct;
|
||||
|
||||
@ -1045,7 +1045,7 @@ fn field_alias(field: &ast::StructField) -> Option<&ast::Lit> {
|
||||
})
|
||||
}
|
||||
|
||||
fn declare_map_field_deserializer(
|
||||
fn declare_map_field_devisitor(
|
||||
cx: &ExtCtxt,
|
||||
span: Span,
|
||||
_builder: &aster::AstBuilder,
|
||||
@ -1396,7 +1396,7 @@ fn deserialize_enum_variant(
|
||||
.map(|i| token::str_to_ident(&format!("__field{}", i)))
|
||||
.collect();
|
||||
|
||||
let field_deserializer = declare_map_field_deserializer(
|
||||
let field_devisitor = declare_map_field_devisitor(
|
||||
cx,
|
||||
span,
|
||||
builder,
|
||||
@ -1421,7 +1421,7 @@ fn deserialize_enum_variant(
|
||||
);
|
||||
|
||||
quote_expr!(cx, {
|
||||
$field_deserializer
|
||||
$field_devisitor
|
||||
|
||||
impl $visitor_impl_generics ::serde::de::EnumMapVisitor for $visitor_ty {
|
||||
type Value = $value_ty;
|
||||
|
@ -49,32 +49,7 @@ impl<W: io::Write> Serializer<W> {
|
||||
pub fn into_inner(self) -> W {
|
||||
self.writer
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: io::Write> ser::Serializer for Serializer<W> {
|
||||
type Error = io::Error;
|
||||
|
||||
#[inline]
|
||||
fn visit<T>(&mut self, value: &T) -> io::Result<()>
|
||||
where T: ser::Serialize,
|
||||
{
|
||||
value.visit(&mut Visitor {
|
||||
writer: &mut self.writer,
|
||||
format: self.format,
|
||||
current_indent: self.current_indent,
|
||||
indent: self.indent,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
struct Visitor<'a, W: 'a> {
|
||||
writer: &'a mut W,
|
||||
format: Format,
|
||||
current_indent: usize,
|
||||
indent: usize,
|
||||
}
|
||||
|
||||
impl<'a, W> Visitor<'a, W> where W: io::Write, {
|
||||
fn serialize_sep(&mut self, first: bool) -> io::Result<()> {
|
||||
match self.format {
|
||||
Format::Compact => {
|
||||
@ -115,7 +90,9 @@ impl<'a, W> Visitor<'a, W> where W: io::Write, {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, W> ser::Visitor for Visitor<'a, W> where W: io::Write, {
|
||||
impl<W> ser::Serializer for Serializer<W>
|
||||
where W: io::Write,
|
||||
{
|
||||
type Error = io::Error;
|
||||
|
||||
#[inline]
|
||||
@ -129,72 +106,72 @@ impl<'a, W> ser::Visitor for Visitor<'a, W> where W: io::Write, {
|
||||
|
||||
#[inline]
|
||||
fn visit_isize(&mut self, value: isize) -> io::Result<()> {
|
||||
write!(self.writer, "{}", value)
|
||||
write!(&mut self.writer, "{}", value)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_i8(&mut self, value: i8) -> io::Result<()> {
|
||||
write!(self.writer, "{}", value)
|
||||
write!(&mut self.writer, "{}", value)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_i16(&mut self, value: i16) -> io::Result<()> {
|
||||
write!(self.writer, "{}", value)
|
||||
write!(&mut self.writer, "{}", value)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_i32(&mut self, value: i32) -> io::Result<()> {
|
||||
write!(self.writer, "{}", value)
|
||||
write!(&mut self.writer, "{}", value)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_i64(&mut self, value: i64) -> io::Result<()> {
|
||||
write!(self.writer, "{}", value)
|
||||
write!(&mut self.writer, "{}", value)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_usize(&mut self, value: usize) -> io::Result<()> {
|
||||
write!(self.writer, "{}", value)
|
||||
write!(&mut self.writer, "{}", value)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_u8(&mut self, value: u8) -> io::Result<()> {
|
||||
write!(self.writer, "{}", value)
|
||||
write!(&mut self.writer, "{}", value)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_u16(&mut self, value: u16) -> io::Result<()> {
|
||||
write!(self.writer, "{}", value)
|
||||
write!(&mut self.writer, "{}", value)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_u32(&mut self, value: u32) -> io::Result<()> {
|
||||
write!(self.writer, "{}", value)
|
||||
write!(&mut self.writer, "{}", value)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_u64(&mut self, value: u64) -> io::Result<()> {
|
||||
write!(self.writer, "{}", value)
|
||||
write!(&mut self.writer, "{}", value)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_f32(&mut self, value: f32) -> io::Result<()> {
|
||||
fmt_f32_or_null(self.writer, value)
|
||||
fmt_f32_or_null(&mut self.writer, value)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_f64(&mut self, value: f64) -> io::Result<()> {
|
||||
fmt_f64_or_null(self.writer, value)
|
||||
fmt_f64_or_null(&mut self.writer, value)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_char(&mut self, value: char) -> io::Result<()> {
|
||||
escape_char(self.writer, value)
|
||||
escape_char(&mut self.writer, value)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_str(&mut self, value: &str) -> io::Result<()> {
|
||||
escape_str(self.writer, value)
|
||||
escape_str(&mut self.writer, value)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
@ -379,7 +356,7 @@ pub fn to_writer<W, T>(writer: &mut W, value: &T) -> io::Result<()>
|
||||
T: ser::Serialize,
|
||||
{
|
||||
let mut ser = Serializer::new(writer);
|
||||
try!(ser::Serializer::visit(&mut ser, value));
|
||||
try!(value.visit(&mut ser));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -390,7 +367,7 @@ pub fn to_writer_pretty<W, T>(writer: &mut W, value: &T) -> io::Result<()>
|
||||
T: ser::Serialize,
|
||||
{
|
||||
let mut ser = Serializer::new_pretty(writer);
|
||||
try!(ser::Serializer::visit(&mut ser, value));
|
||||
try!(value.visit(&mut ser));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -21,17 +21,17 @@ pub enum Value {
|
||||
|
||||
impl ser::Serialize for Value {
|
||||
#[inline]
|
||||
fn visit<V>(&self, visitor: &mut V) -> Result<(), V::Error>
|
||||
where V: ser::Visitor,
|
||||
fn visit<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: ser::Serializer,
|
||||
{
|
||||
match *self {
|
||||
Value::Null => visitor.visit_unit(),
|
||||
Value::Bool(v) => visitor.visit_bool(v),
|
||||
Value::I64(v) => visitor.visit_i64(v),
|
||||
Value::F64(v) => visitor.visit_f64(v),
|
||||
Value::String(ref v) => visitor.visit_str(&v),
|
||||
Value::Array(ref v) => v.visit(visitor),
|
||||
Value::Object(ref v) => v.visit(visitor),
|
||||
Value::Null => serializer.visit_unit(),
|
||||
Value::Bool(v) => serializer.visit_bool(v),
|
||||
Value::I64(v) => serializer.visit_i64(v),
|
||||
Value::F64(v) => serializer.visit_f64(v),
|
||||
Value::String(ref v) => serializer.visit_str(&v),
|
||||
Value::Array(ref v) => v.visit(serializer),
|
||||
Value::Object(ref v) => v.visit(serializer),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -165,18 +165,6 @@ impl Serializer {
|
||||
impl ser::Serializer for Serializer {
|
||||
type Error = ();
|
||||
|
||||
#[inline]
|
||||
fn visit<T>(&mut self, value: &T) -> Result<(), ()>
|
||||
where T: ser::Serialize,
|
||||
{
|
||||
try!(value.visit(self));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl ser::Visitor for Serializer {
|
||||
type Error = ();
|
||||
|
||||
#[inline]
|
||||
fn visit_bool(&mut self, value: bool) -> Result<(), ()> {
|
||||
self.state.push(State::Value(Value::Bool(value)));
|
||||
@ -589,7 +577,7 @@ pub fn to_value<T>(value: &T) -> Value
|
||||
where T: ser::Serialize
|
||||
{
|
||||
let mut ser = Serializer::new();
|
||||
ser::Serializer::visit(&mut ser, value).ok().unwrap();
|
||||
value.visit(&mut ser).ok().unwrap();
|
||||
ser.unwrap()
|
||||
}
|
||||
|
||||
|
161
src/ser.rs
161
src/ser.rs
@ -9,8 +9,8 @@ use std::sync::Arc;
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
pub trait Serialize {
|
||||
fn visit<V>(&self, visitor: &mut V) -> Result<(), V::Error>
|
||||
where V: Visitor;
|
||||
fn visit<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
@ -18,15 +18,6 @@ pub trait Serialize {
|
||||
pub trait Serializer {
|
||||
type Error;
|
||||
|
||||
fn visit<T>(&mut self, value: &T) -> Result<(), Self::Error>
|
||||
where T: Serialize;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
pub trait Visitor {
|
||||
type Error;
|
||||
|
||||
fn visit_bool(&mut self, v: bool) -> Result<(), Self::Error>;
|
||||
|
||||
#[inline]
|
||||
@ -169,8 +160,8 @@ pub trait Visitor {
|
||||
}
|
||||
|
||||
pub trait SeqVisitor {
|
||||
fn visit<V>(&mut self, visitor: &mut V) -> Result<Option<()>, V::Error>
|
||||
where V: Visitor;
|
||||
fn visit<S>(&mut self, serializer: &mut S) -> Result<Option<()>, S::Error>
|
||||
where S: Serializer;
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
@ -179,8 +170,8 @@ pub trait SeqVisitor {
|
||||
}
|
||||
|
||||
pub trait MapVisitor {
|
||||
fn visit<V>(&mut self, visitor: &mut V) -> Result<Option<()>, V::Error>
|
||||
where V: Visitor;
|
||||
fn visit<S>(&mut self, serializer: &mut S) -> Result<Option<()>, S::Error>
|
||||
where S: Serializer;
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
@ -194,10 +185,10 @@ macro_rules! impl_visit {
|
||||
($ty:ty, $method:ident) => {
|
||||
impl Serialize for $ty {
|
||||
#[inline]
|
||||
fn visit<V>(&self, visitor: &mut V) -> Result<(), V::Error>
|
||||
where V: Visitor,
|
||||
fn visit<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
visitor.$method(*self)
|
||||
serializer.$method(*self)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -222,19 +213,19 @@ impl_visit!(char, visit_char);
|
||||
|
||||
impl<'a> Serialize for &'a str {
|
||||
#[inline]
|
||||
fn visit<V>(&self, visitor: &mut V) -> Result<(), V::Error>
|
||||
where V: Visitor,
|
||||
fn visit<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
visitor.visit_str(*self)
|
||||
serializer.visit_str(*self)
|
||||
}
|
||||
}
|
||||
|
||||
impl Serialize for String {
|
||||
#[inline]
|
||||
fn visit<V>(&self, visitor: &mut V) -> Result<(), V::Error>
|
||||
where V: Visitor,
|
||||
fn visit<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
(&self[..]).visit(visitor)
|
||||
(&self[..]).visit(serializer)
|
||||
}
|
||||
}
|
||||
|
||||
@ -242,12 +233,12 @@ impl Serialize for String {
|
||||
|
||||
impl<T> Serialize for Option<T> where T: Serialize {
|
||||
#[inline]
|
||||
fn visit<V>(&self, visitor: &mut V) -> Result<(), V::Error>
|
||||
where V: Visitor,
|
||||
fn visit<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
match *self {
|
||||
Some(ref value) => visitor.visit_some(value),
|
||||
None => visitor.visit_none(),
|
||||
Some(ref value) => serializer.visit_some(value),
|
||||
None => serializer.visit_none(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -276,15 +267,15 @@ impl<T, Iter> SeqVisitor for SeqIteratorVisitor<Iter>
|
||||
Iter: Iterator<Item=T>,
|
||||
{
|
||||
#[inline]
|
||||
fn visit<V>(&mut self, visitor: &mut V) -> Result<Option<()>, V::Error>
|
||||
where V: Visitor,
|
||||
fn visit<S>(&mut self, serializer: &mut S) -> Result<Option<()>, S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
let first = self.first;
|
||||
self.first = false;
|
||||
|
||||
match self.iter.next() {
|
||||
Some(value) => {
|
||||
let value = try!(visitor.visit_seq_elt(first, value));
|
||||
let value = try!(serializer.visit_seq_elt(first, value));
|
||||
Ok(Some(value))
|
||||
}
|
||||
None => Ok(None),
|
||||
@ -303,38 +294,40 @@ impl<'a, T> Serialize for &'a [T]
|
||||
where T: Serialize,
|
||||
{
|
||||
#[inline]
|
||||
fn visit<V>(&self, visitor: &mut V) -> Result<(), V::Error>
|
||||
where V: Visitor,
|
||||
fn visit<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
visitor.visit_seq(SeqIteratorVisitor::new(self.iter()))
|
||||
serializer.visit_seq(SeqIteratorVisitor::new(self.iter()))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Serialize for Vec<T> where T: Serialize {
|
||||
#[inline]
|
||||
fn visit<V>(&self, visitor: &mut V) -> Result<(), V::Error>
|
||||
where V: Visitor,
|
||||
fn visit<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
(&self[..]).visit(visitor)
|
||||
(&self[..]).visit(serializer)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Serialize for BTreeSet<T> where T: Serialize {
|
||||
#[inline]
|
||||
fn visit<V>(&self, visitor: &mut V) -> Result<(), V::Error>
|
||||
where V: Visitor,
|
||||
fn visit<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
visitor.visit_seq(SeqIteratorVisitor::new(self.iter()))
|
||||
serializer.visit_seq(SeqIteratorVisitor::new(self.iter()))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> Serialize for HashSet<T, S>
|
||||
impl<T, H> Serialize for HashSet<T, H>
|
||||
where T: Serialize + Eq + Hash,
|
||||
S: HashState,
|
||||
H: HashState,
|
||||
{
|
||||
#[inline]
|
||||
fn visit<V: Visitor>(&self, visitor: &mut V) -> Result<(), V::Error> {
|
||||
visitor.visit_seq(SeqIteratorVisitor::new(self.iter()))
|
||||
fn visit<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
serializer.visit_seq(SeqIteratorVisitor::new(self.iter()))
|
||||
}
|
||||
}
|
||||
|
||||
@ -342,10 +335,10 @@ impl<T, S> Serialize for HashSet<T, S>
|
||||
|
||||
impl Serialize for () {
|
||||
#[inline]
|
||||
fn visit<V>(&self, visitor: &mut V) -> Result<(), V::Error>
|
||||
where V: Visitor,
|
||||
fn visit<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
visitor.visit_unit()
|
||||
serializer.visit_unit()
|
||||
}
|
||||
}
|
||||
|
||||
@ -382,8 +375,8 @@ macro_rules! tuple_impls {
|
||||
impl<'a, $($T),+> SeqVisitor for $TupleVisitor<'a, $($T),+>
|
||||
where $($T: Serialize),+
|
||||
{
|
||||
fn visit<V>(&mut self, visitor: &mut V) -> Result<Option<()>, V::Error>
|
||||
where V: Visitor,
|
||||
fn visit<S>(&mut self, serializer: &mut S) -> Result<Option<()>, S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
let first = self.first;
|
||||
self.first = false;
|
||||
@ -392,7 +385,7 @@ macro_rules! tuple_impls {
|
||||
$(
|
||||
$state => {
|
||||
self.state += 1;
|
||||
Ok(Some(try!(visitor.visit_seq_elt(first, &e!(self.tuple.$idx)))))
|
||||
Ok(Some(try!(serializer.visit_seq_elt(first, &e!(self.tuple.$idx)))))
|
||||
}
|
||||
)+
|
||||
_ => {
|
||||
@ -410,8 +403,8 @@ macro_rules! tuple_impls {
|
||||
where $($T: Serialize),+
|
||||
{
|
||||
#[inline]
|
||||
fn visit<V: Visitor>(&self, visitor: &mut V) -> Result<(), V::Error> {
|
||||
visitor.visit_seq($TupleVisitor::new(self))
|
||||
fn visit<S: Serializer>(&self, serializer: &mut S) -> Result<(), S::Error> {
|
||||
serializer.visit_seq($TupleVisitor::new(self))
|
||||
}
|
||||
}
|
||||
)+
|
||||
@ -548,15 +541,15 @@ impl<K, V, I> MapVisitor for MapIteratorVisitor<I>
|
||||
I: Iterator<Item=(K, V)>,
|
||||
{
|
||||
#[inline]
|
||||
fn visit<V_>(&mut self, visitor: &mut V_) -> Result<Option<()>, V_::Error>
|
||||
where V_: Visitor,
|
||||
fn visit<S>(&mut self, serializer: &mut S) -> Result<Option<()>, S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
let first = self.first;
|
||||
self.first = false;
|
||||
|
||||
match self.iter.next() {
|
||||
Some((key, value)) => {
|
||||
let value = try!(visitor.visit_map_elt(first, key, value));
|
||||
let value = try!(serializer.visit_map_elt(first, key, value));
|
||||
Ok(Some(value))
|
||||
}
|
||||
None => Ok(None)
|
||||
@ -576,19 +569,23 @@ impl<K, V> Serialize for BTreeMap<K, V>
|
||||
V: Serialize,
|
||||
{
|
||||
#[inline]
|
||||
fn visit<V_: Visitor>(&self, visitor: &mut V_) -> Result<(), V_::Error> {
|
||||
visitor.visit_map(MapIteratorVisitor::new(self.iter()))
|
||||
fn visit<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
serializer.visit_map(MapIteratorVisitor::new(self.iter()))
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V, S> Serialize for HashMap<K, V, S>
|
||||
impl<K, V, H> Serialize for HashMap<K, V, H>
|
||||
where K: Serialize + Eq + Hash,
|
||||
V: Serialize,
|
||||
S: HashState,
|
||||
H: HashState,
|
||||
{
|
||||
#[inline]
|
||||
fn visit<V_: Visitor>(&self, visitor: &mut V_) -> Result<(), V_::Error> {
|
||||
visitor.visit_map(MapIteratorVisitor::new(self.iter()))
|
||||
fn visit<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
serializer.visit_map(MapIteratorVisitor::new(self.iter()))
|
||||
}
|
||||
}
|
||||
|
||||
@ -596,63 +593,63 @@ impl<K, V, S> Serialize for HashMap<K, V, S>
|
||||
|
||||
impl<'a, T> Serialize for &'a T where T: Serialize {
|
||||
#[inline]
|
||||
fn visit<V>(&self, visitor: &mut V) -> Result<(), V::Error>
|
||||
where V: Visitor,
|
||||
fn visit<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
(**self).visit(visitor)
|
||||
(**self).visit(serializer)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> Serialize for &'a mut T where T: Serialize {
|
||||
#[inline]
|
||||
fn visit<V>(&self, visitor: &mut V) -> Result<(), V::Error>
|
||||
where V: Visitor,
|
||||
fn visit<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
(**self).visit(visitor)
|
||||
(**self).visit(serializer)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Serialize for Box<T> where T: Serialize {
|
||||
#[inline]
|
||||
fn visit<V>(&self, visitor: &mut V) -> Result<(), V::Error>
|
||||
where V: Visitor,
|
||||
fn visit<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
(**self).visit(visitor)
|
||||
(**self).visit(serializer)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Serialize for Rc<T> where T: Serialize, {
|
||||
#[inline]
|
||||
fn visit<V>(&self, visitor: &mut V) -> Result<(), V::Error>
|
||||
where V: Visitor,
|
||||
fn visit<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
(**self).visit(visitor)
|
||||
(**self).visit(serializer)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Serialize for Arc<T> where T: Serialize, {
|
||||
#[inline]
|
||||
fn visit<V>(&self, visitor: &mut V) -> Result<(), V::Error>
|
||||
where V: Visitor,
|
||||
fn visit<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
(**self).visit(visitor)
|
||||
(**self).visit(serializer)
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
impl Serialize for path::Path {
|
||||
fn visit<V>(&self, visitor: &mut V) -> Result<(), V::Error>
|
||||
where V: Visitor,
|
||||
fn visit<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
self.to_str().unwrap().visit(visitor)
|
||||
self.to_str().unwrap().visit(serializer)
|
||||
}
|
||||
}
|
||||
|
||||
impl Serialize for path::PathBuf {
|
||||
fn visit<V>(&self, visitor: &mut V) -> Result<(), V::Error>
|
||||
where V: Visitor,
|
||||
fn visit<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: Serializer,
|
||||
{
|
||||
self.to_str().unwrap().visit(visitor)
|
||||
self.to_str().unwrap().visit(serializer)
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ extern crate serde;
|
||||
use std::vec;
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use serde::ser::{Serialize, Serializer, Visitor, SeqVisitor, MapVisitor};
|
||||
use serde::ser::{Serialize, Serializer, SeqVisitor, MapVisitor};
|
||||
|
||||
#[derive(Clone, PartialEq, Debug)]
|
||||
pub enum Token<'a> {
|
||||
@ -81,14 +81,6 @@ impl<'a> AssertSerializer<'a> {
|
||||
impl<'a> Serializer for AssertSerializer<'a> {
|
||||
type Error = ();
|
||||
|
||||
fn visit<T: Serialize>(&mut self, value: &T) -> Result<(), ()> {
|
||||
value.visit(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Visitor for AssertSerializer<'a> {
|
||||
type Error = ();
|
||||
|
||||
fn visit_unit(&mut self) -> Result<(), ()> {
|
||||
assert_eq!(self.iter.next(), Some(Token::Unit));
|
||||
Ok(())
|
||||
@ -322,7 +314,7 @@ macro_rules! declare_test {
|
||||
fn $name() {
|
||||
$(
|
||||
let mut ser = AssertSerializer::new($tokens);
|
||||
assert_eq!(ser.visit(&$value), Ok(()));
|
||||
assert_eq!($value.visit(&mut ser), Ok(()));
|
||||
)+
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user