@@ -3,6 +3,7 @@ use serde;
33use std:: fmt;
44use std:: io:: Write ;
55
6+ use rmp:: Marker ;
67use rmp:: encode:: {
78 write_nil,
89 write_bool,
@@ -58,6 +59,30 @@ impl From<ValueWriteError> for Error {
5859 }
5960}
6061
62+ pub trait VariantWriter {
63+ fn write_struct_len < W > ( & self , wr : & mut W , len : u32 ) -> Result < Marker , ValueWriteError > where W : Write ;
64+ fn write_field_name < W > ( & self , wr : & mut W , _key : & str ) -> Result < ( ) , ValueWriteError > where W : Write ;
65+ }
66+
67+ /// Writes struct as MessagePack array with no field names
68+ pub struct StructArrayWriter ;
69+
70+ impl VariantWriter for StructArrayWriter {
71+ fn write_struct_len < W > ( & self , wr : & mut W , len : u32 ) -> Result < Marker , ValueWriteError >
72+ where W : Write
73+ {
74+ write_array_len ( wr, len)
75+ }
76+
77+ /// This implementation does not write field names
78+ #[ allow( unused_variables) ]
79+ fn write_field_name < W > ( & self , wr : & mut W , _key : & str ) -> Result < ( ) , ValueWriteError >
80+ where W : Write
81+ {
82+ Ok ( ( ) )
83+ }
84+ }
85+
6186/// Represents MessagePack serialization implementation.
6287///
6388/// # Note
@@ -70,20 +95,32 @@ impl From<ValueWriteError> for Error {
7095/// All instances of `ErrorKind::Interrupted` are handled by this function and the underlying
7196/// operation is retried.
7297// TODO: Docs. Examples.
73- pub struct Serializer < ' a > {
98+ pub struct Serializer < ' a , W : VariantWriter > {
7499 wr : & ' a mut Write ,
100+ vw : W ,
75101}
76102
77- impl < ' a > Serializer < ' a > {
103+ impl < ' a > Serializer < ' a , StructArrayWriter > {
78104 /// Creates a new MessagePack encoder whose output will be written to the writer specified.
79- pub fn new ( wr : & ' a mut Write ) -> Serializer < ' a > {
105+ pub fn new ( wr : & ' a mut Write ) -> Serializer < ' a , StructArrayWriter > {
80106 Serializer {
81107 wr : wr,
108+ vw : StructArrayWriter ,
82109 }
83110 }
84111}
85112
86- impl < ' a > serde:: Serializer for Serializer < ' a > {
113+ impl < ' a , W : VariantWriter > Serializer < ' a , W > {
114+ /// Creates a new MessagePack encoder whose output will be written to the writer specified.
115+ pub fn with ( wr : & ' a mut Write , vw : W ) -> Serializer < ' a , W > {
116+ Serializer {
117+ wr : wr,
118+ vw : vw,
119+ }
120+ }
121+ }
122+
123+ impl < ' a , W : VariantWriter > serde:: Serializer for Serializer < ' a , W > {
87124 type Error = Error ;
88125
89126 fn visit_unit ( & mut self ) -> Result < ( ) , Error > {
@@ -276,7 +313,7 @@ impl<'a> serde::Serializer for Serializer<'a> {
276313 None => panic ! ( "do not know how to serialize a sequence with no length" ) ,
277314 } ;
278315
279- try!( write_array_len ( & mut self . wr , len as u32 ) ) ;
316+ try!( self . vw . write_struct_len ( & mut self . wr , len as u32 ) ) ;
280317
281318 while let Some ( ( ) ) = try!( visitor. visit ( self ) ) { }
282319
@@ -286,6 +323,7 @@ impl<'a> serde::Serializer for Serializer<'a> {
286323 fn visit_struct_elt < V > ( & mut self , _key : & str , value : V ) -> Result < ( ) , Error >
287324 where V : serde:: Serialize ,
288325 {
326+ try!( self . vw . write_field_name ( & mut self . wr , _key) ) ;
289327 value. serialize ( self )
290328 }
291329
0 commit comments