@@ -383,26 +383,21 @@ impl HyperionCore {
383
383
/// A scratch buffer for intermediate operations. This will return an empty [`Vec`] when calling [`Scratch::obtain`].
384
384
#[ derive( Debug ) ]
385
385
pub struct Scratch < A : Allocator = std:: alloc:: Global > {
386
- inner : Vec < u8 , A > ,
386
+ inner : Box < [ u8 ] , A > ,
387
387
}
388
388
389
389
impl Default for Scratch < std:: alloc:: Global > {
390
390
fn default ( ) -> Self {
391
- let inner = Vec :: with_capacity ( MAX_PACKET_SIZE ) ;
392
- Self { inner }
391
+ std:: alloc:: Global . into ( )
393
392
}
394
393
}
395
394
396
395
/// Nice for getting a buffer that can be used for intermediate work
397
- ///
398
- /// # Safety
399
- /// - every single time [`ScratchBuffer::obtain`] is called, the buffer will be cleared before returning
400
- /// - the buffer has capacity of at least `MAX_PACKET_SIZE`
401
- pub unsafe trait ScratchBuffer : sealed:: Sealed + Debug {
396
+ pub trait ScratchBuffer : sealed:: Sealed + Debug {
402
397
/// The type of the allocator the [`Vec`] uses.
403
398
type Allocator : Allocator ;
404
- /// Obtains a buffer that can be used for intermediate work.
405
- fn obtain ( & mut self ) -> & mut Vec < u8 , Self :: Allocator > ;
399
+ /// Obtains a buffer that can be used for intermediate work. The contents are unspecified.
400
+ fn obtain ( & mut self ) -> & mut [ u8 ] ;
406
401
}
407
402
408
403
mod sealed {
@@ -411,20 +406,23 @@ mod sealed {
411
406
412
407
impl < A : Allocator + Debug > sealed:: Sealed for Scratch < A > { }
413
408
414
- unsafe impl < A : Allocator + Debug > ScratchBuffer for Scratch < A > {
409
+ impl < A : Allocator + Debug > ScratchBuffer for Scratch < A > {
415
410
type Allocator = A ;
416
411
417
- fn obtain ( & mut self ) -> & mut Vec < u8 , Self :: Allocator > {
418
- self . inner . clear ( ) ;
412
+ fn obtain ( & mut self ) -> & mut [ u8 ] {
419
413
& mut self . inner
420
414
}
421
415
}
422
416
423
417
impl < A : Allocator > From < A > for Scratch < A > {
424
418
fn from ( allocator : A ) -> Self {
425
- Self {
426
- inner : Vec :: with_capacity_in ( MAX_PACKET_SIZE , allocator) ,
427
- }
419
+ // A zeroed slice is allocated to avoid reading from uninitialized memory, which is UB.
420
+ // Allocating zeroed memory is usually very cheap, so there are minimal performance
421
+ // penalties from this.
422
+ let inner = Box :: new_zeroed_slice_in ( MAX_PACKET_SIZE , allocator) ;
423
+ // SAFETY: The box was initialized to zero, and u8 can be represented by zero
424
+ let inner = unsafe { inner. assume_init ( ) } ;
425
+ Self { inner }
428
426
}
429
427
}
430
428
0 commit comments