@@ -72,17 +72,25 @@ impl std::fmt::Debug for RawEvent {
72
72
}
73
73
}
74
74
75
- /// Functions that take an input stream, consume an object, and output the serialized bytes.
76
- trait TypeSegmenterFn : Fn ( & mut & [ u8 ] , & mut Vec < u8 > ) -> Result < ( ) , Error > + Send { }
77
- impl < T > TypeSegmenterFn for T where
78
- T : Fn ( & mut & [ u8 ] , & mut Vec < u8 > ) -> Result < ( ) , Error > + Send
75
+ trait TypeSegmenter : Send {
76
+ /// Consumes an object from an input stream, and output the serialized bytes.
77
+ fn segment ( & self , input : & mut & [ u8 ] , output : & mut Vec < u8 > ) -> Result < ( ) , Error > ;
78
+ }
79
+
80
+ impl < T > TypeSegmenter for PhantomData < T >
81
+ where
82
+ T : Codec + Send ,
79
83
{
84
+ fn segment ( & self , input : & mut & [ u8 ] , output : & mut Vec < u8 > ) -> Result < ( ) , Error > {
85
+ T :: decode ( input) . map_err ( Error :: from) ?. encode_to ( output) ;
86
+ Ok ( ( ) )
87
+ }
80
88
}
81
89
82
90
/// Events decoder.
83
91
pub struct EventsDecoder < T > {
84
92
metadata : Metadata ,
85
- type_segmenters : HashMap < String , Box < dyn TypeSegmenterFn > > ,
93
+ type_segmenters : HashMap < String , Box < dyn TypeSegmenter > > ,
86
94
marker : PhantomData < fn ( ) -> T > ,
87
95
}
88
96
@@ -141,15 +149,8 @@ impl<T: System> EventsDecoder<T> {
141
149
let size = U :: default ( ) . encode ( ) . len ( ) ;
142
150
// A segmenter decodes a type from an input stream (&mut &[u8]) and returns the serialized
143
151
// type to the output stream (&mut Vec<u8>).
144
- self . type_segmenters . insert (
145
- name. to_string ( ) ,
146
- Box :: new (
147
- |input : & mut & [ u8 ] , output : & mut Vec < u8 > | -> Result < ( ) , Error > {
148
- U :: decode ( input) . map_err ( Error :: from) ?. encode_to ( output) ;
149
- Ok ( ( ) )
150
- } ,
151
- ) ,
152
- ) ;
152
+ self . type_segmenters
153
+ . insert ( name. to_string ( ) , Box :: new ( PhantomData :: < U > ) ) ;
153
154
size
154
155
}
155
156
@@ -221,7 +222,7 @@ impl<T: System> EventsDecoder<T> {
221
222
_ => {
222
223
if let Some ( seg) = self . type_segmenters . get ( name) {
223
224
let mut buf = Vec :: < u8 > :: new ( ) ;
224
- seg ( input, & mut buf) ?;
225
+ seg. segment ( input, & mut buf) ?;
225
226
output. write ( & buf) ;
226
227
Ok ( ( ) )
227
228
} else {
0 commit comments