@@ -265,6 +265,7 @@ impl<'a> CodedOutputStream<'a> {
265265 pub fn write_raw_varint32 ( & mut self , value : u32 ) -> crate :: Result < ( ) > {
266266 if self . buffer . unfilled_len ( ) >= 5 {
267267 // fast path
268+ // SAFETY: we've just checked that there's enough space in the buffer.
268269 unsafe {
269270 let len = encode_varint32 ( value, self . buffer . unfilled ( ) ) ;
270271 self . buffer . advance ( len) ;
@@ -284,6 +285,7 @@ impl<'a> CodedOutputStream<'a> {
284285 pub fn write_raw_varint64 ( & mut self , value : u64 ) -> crate :: Result < ( ) > {
285286 if self . buffer . unfilled_len ( ) >= MAX_VARINT_ENCODED_LEN {
286287 // fast path
288+ // SAFETY: we've just checked that there's enough space in the buffer.
287289 unsafe {
288290 let len = encode_varint64 ( value, self . buffer . unfilled ( ) ) ;
289291 self . buffer . advance ( len) ;
@@ -301,12 +303,38 @@ impl<'a> CodedOutputStream<'a> {
301303
302304 /// Write 32-bit integer little endian
303305 pub fn write_raw_little_endian32 ( & mut self , value : u32 ) -> crate :: Result < ( ) > {
304- self . write_raw_bytes ( & value. to_le_bytes ( ) )
306+ if self . buffer . unfilled_len ( ) >= 4 {
307+ // fast path
308+ // SAFETY: we've just checked that there's enough space in the buffer.
309+ unsafe {
310+ let buf = self . buffer . unfilled ( ) ;
311+ let bytes = value. to_le_bytes ( ) ;
312+ ptr:: copy_nonoverlapping ( bytes. as_ptr ( ) , buf. as_mut_ptr ( ) as * mut u8 , 4 ) ;
313+ self . buffer . advance ( 4 ) ;
314+ } ;
315+ Ok ( ( ) )
316+ } else {
317+ // slow path
318+ self . write_raw_bytes ( & value. to_le_bytes ( ) )
319+ }
305320 }
306321
307322 /// Write 64-bit integer little endian
308323 pub fn write_raw_little_endian64 ( & mut self , value : u64 ) -> crate :: Result < ( ) > {
309- self . write_raw_bytes ( & value. to_le_bytes ( ) )
324+ if self . buffer . unfilled_len ( ) >= 8 {
325+ // fast path
326+ // SAFETY: we've just checked that there's enough space in the buffer.
327+ unsafe {
328+ let buf = self . buffer . unfilled ( ) ;
329+ let bytes = value. to_le_bytes ( ) ;
330+ ptr:: copy_nonoverlapping ( bytes. as_ptr ( ) , buf. as_mut_ptr ( ) as * mut u8 , 8 ) ;
331+ self . buffer . advance ( 8 ) ;
332+ } ;
333+ Ok ( ( ) )
334+ } else {
335+ // slow path
336+ self . write_raw_bytes ( & value. to_le_bytes ( ) )
337+ }
310338 }
311339
312340 /// Write `float`
0 commit comments