@@ -265,6 +265,7 @@ impl<'a> CodedOutputStream<'a> {
265
265
pub fn write_raw_varint32 ( & mut self , value : u32 ) -> crate :: Result < ( ) > {
266
266
if self . buffer . unfilled_len ( ) >= 5 {
267
267
// fast path
268
+ // SAFETY: we've just checked that there's enough space in the buffer.
268
269
unsafe {
269
270
let len = encode_varint32 ( value, self . buffer . unfilled ( ) ) ;
270
271
self . buffer . advance ( len) ;
@@ -284,6 +285,7 @@ impl<'a> CodedOutputStream<'a> {
284
285
pub fn write_raw_varint64 ( & mut self , value : u64 ) -> crate :: Result < ( ) > {
285
286
if self . buffer . unfilled_len ( ) >= MAX_VARINT_ENCODED_LEN {
286
287
// fast path
288
+ // SAFETY: we've just checked that there's enough space in the buffer.
287
289
unsafe {
288
290
let len = encode_varint64 ( value, self . buffer . unfilled ( ) ) ;
289
291
self . buffer . advance ( len) ;
@@ -301,12 +303,38 @@ impl<'a> CodedOutputStream<'a> {
301
303
302
304
/// Write 32-bit integer little endian
303
305
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
+ }
305
320
}
306
321
307
322
/// Write 64-bit integer little endian
308
323
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
+ }
310
338
}
311
339
312
340
/// Write `float`
0 commit comments