@@ -6,6 +6,7 @@ use crate::io::prelude::*;
6
6
use crate :: alloc:: Allocator ;
7
7
use crate :: cmp;
8
8
use crate :: io:: { self , BorrowedCursor , ErrorKind , IoSlice , IoSliceMut , SeekFrom } ;
9
+ use core:: alloc;
9
10
10
11
/// A `Cursor` wraps an in-memory buffer and provides it with a
11
12
/// [`Seek`] implementation.
@@ -397,11 +398,12 @@ fn slice_write_vectored(
397
398
}
398
399
399
400
/// Reserves the required space, and pads the vec with 0s if necessary.
400
- fn reserve_and_pad < A : Allocator > (
401
+ fn reserve_and_pad < A : Allocator , const COOP_PREFERRED : bool > (
401
402
pos_mut : & mut u64 ,
402
- vec : & mut Vec < u8 , A > ,
403
+ vec : & mut Vec < u8 , A , COOP_PREFERRED > ,
403
404
buf_len : usize ,
404
- ) -> io:: Result < usize > {
405
+ ) -> io:: Result < usize >
406
+ where [ ( ) ; alloc:: co_alloc_metadata_num_slots_with_preference :: < A > ( COOP_PREFERRED ) ] : {
405
407
let pos: usize = ( * pos_mut) . try_into ( ) . map_err ( |_| {
406
408
io:: const_io_error!(
407
409
ErrorKind :: InvalidInput ,
@@ -440,9 +442,10 @@ fn reserve_and_pad<A: Allocator>(
440
442
441
443
/// Writes the slice to the vec without allocating
442
444
/// # Safety: vec must have buf.len() spare capacity
443
- unsafe fn vec_write_unchecked < A > ( pos : usize , vec : & mut Vec < u8 , A > , buf : & [ u8 ] ) -> usize
445
+ unsafe fn vec_write_unchecked < A , const COOP_PREFERRED : bool > ( pos : usize , vec : & mut Vec < u8 , A , COOP_PREFERRED > , buf : & [ u8 ] ) -> usize
444
446
where
445
447
A : Allocator ,
448
+ [ ( ) ; alloc:: co_alloc_metadata_num_slots_with_preference :: < A > ( COOP_PREFERRED ) ] :
446
449
{
447
450
debug_assert ! ( vec. capacity( ) >= pos + buf. len( ) ) ;
448
451
vec. as_mut_ptr ( ) . add ( pos) . copy_from ( buf. as_ptr ( ) , buf. len ( ) ) ;
@@ -458,9 +461,10 @@ where
458
461
/// This also allows for the vec body to be empty, but with a position of N.
459
462
/// This means that [`Write`] will pad the vec with 0 initially,
460
463
/// before writing anything from that point
461
- fn vec_write < A > ( pos_mut : & mut u64 , vec : & mut Vec < u8 , A > , buf : & [ u8 ] ) -> io:: Result < usize >
464
+ fn vec_write < A , const COOP_PREFERRED : bool > ( pos_mut : & mut u64 , vec : & mut Vec < u8 , A , COOP_PREFERRED > , buf : & [ u8 ] ) -> io:: Result < usize >
462
465
where
463
466
A : Allocator ,
467
+ [ ( ) ; alloc:: co_alloc_metadata_num_slots_with_preference :: < A > ( COOP_PREFERRED ) ] :
464
468
{
465
469
let buf_len = buf. len ( ) ;
466
470
let mut pos = reserve_and_pad ( pos_mut, vec, buf_len) ?;
@@ -489,13 +493,14 @@ where
489
493
/// This also allows for the vec body to be empty, but with a position of N.
490
494
/// This means that [`Write`] will pad the vec with 0 initially,
491
495
/// before writing anything from that point
492
- fn vec_write_vectored < A > (
496
+ fn vec_write_vectored < A , const COOP_PREFERRED : bool > (
493
497
pos_mut : & mut u64 ,
494
- vec : & mut Vec < u8 , A > ,
498
+ vec : & mut Vec < u8 , A , COOP_PREFERRED > ,
495
499
bufs : & [ IoSlice < ' _ > ] ,
496
500
) -> io:: Result < usize >
497
501
where
498
502
A : Allocator ,
503
+ [ ( ) ; alloc:: co_alloc_metadata_num_slots_with_preference :: < A > ( COOP_PREFERRED ) ] :
499
504
{
500
505
// For safety reasons, we don't want this sum to overflow ever.
501
506
// If this saturates, the reserve should panic to avoid any unsound writing.
@@ -543,9 +548,10 @@ impl Write for Cursor<&mut [u8]> {
543
548
}
544
549
545
550
#[ stable( feature = "cursor_mut_vec" , since = "1.25.0" ) ]
546
- impl < A > Write for Cursor < & mut Vec < u8 , A > >
551
+ impl < A , const COOP_PREFERRED : bool > Write for Cursor < & mut Vec < u8 , A , COOP_PREFERRED > >
547
552
where
548
553
A : Allocator ,
554
+ [ ( ) ; alloc:: co_alloc_metadata_num_slots_with_preference :: < A > ( COOP_PREFERRED ) ] :
549
555
{
550
556
fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > {
551
557
vec_write ( & mut self . pos , self . inner , buf)
@@ -567,9 +573,10 @@ where
567
573
}
568
574
569
575
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
570
- impl < A > Write for Cursor < Vec < u8 , A > >
576
+ impl < A , const COOP_PREFERRED : bool > Write for Cursor < Vec < u8 , A , COOP_PREFERRED > >
571
577
where
572
578
A : Allocator ,
579
+ [ ( ) ; alloc:: co_alloc_metadata_num_slots_with_preference :: < A > ( COOP_PREFERRED ) ] :
573
580
{
574
581
fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > {
575
582
vec_write ( & mut self . pos , & mut self . inner , buf)
0 commit comments