File tree Expand file tree Collapse file tree 1 file changed +26
-4
lines changed Expand file tree Collapse file tree 1 file changed +26
-4
lines changed Original file line number Diff line number Diff line change @@ -1219,9 +1219,9 @@ pub fn to_vec<T>(val: &T) -> Result<Vec<u8>, Error>
12191219where
12201220 T : Serialize + ?Sized ,
12211221{
1222- let mut wr = Vec :: with_capacity ( 128 ) ;
1222+ let mut wr = FallibleWriter ( Vec :: new ( ) ) ;
12231223 write ( & mut wr, val) ?;
1224- Ok ( wr)
1224+ Ok ( wr. 0 )
12251225}
12261226
12271227/// Serializes data structure into byte vector as a map
@@ -1235,7 +1235,29 @@ pub fn to_vec_named<T>(val: &T) -> Result<Vec<u8>, Error>
12351235where
12361236 T : Serialize + ?Sized ,
12371237{
1238- let mut wr = Vec :: with_capacity ( 128 ) ;
1238+ let mut wr = FallibleWriter ( Vec :: new ( ) ) ;
12391239 write_named ( & mut wr, val) ?;
1240- Ok ( wr)
1240+ Ok ( wr. 0 )
1241+ }
1242+
1243+ #[ repr( transparent) ]
1244+ struct FallibleWriter ( Vec < u8 > ) ;
1245+
1246+ impl Write for FallibleWriter {
1247+ #[ inline( always) ]
1248+ fn write ( & mut self , buf : & [ u8 ] ) -> std:: io:: Result < usize > {
1249+ self . write_all ( buf) ?;
1250+ Ok ( buf. len ( ) )
1251+ }
1252+
1253+ #[ inline]
1254+ fn write_all ( & mut self , buf : & [ u8 ] ) -> std:: io:: Result < ( ) > {
1255+ self . 0 . try_reserve ( buf. len ( ) ) . map_err ( |_| std:: io:: ErrorKind :: OutOfMemory ) ?;
1256+ self . 0 . extend_from_slice ( buf) ;
1257+ Ok ( ( ) )
1258+ }
1259+
1260+ fn flush ( & mut self ) -> std:: io:: Result < ( ) > {
1261+ Ok ( ( ) )
1262+ }
12411263}
You can’t perform that action at this time.
0 commit comments