@@ -336,7 +336,7 @@ macro_rules! pin_init {
336
336
let init = move |slot: * mut $ty| -> :: core:: result:: Result <( ) , _> {
337
337
init( slot) . map( |__InitOk| ( ) )
338
338
} ;
339
- let init: $crate:: init:: PinInitClosure <_ , $t $( <$( $generics) ,* >) ?, _> = unsafe { $crate :: init :: PinInitClosure :: from_closure ( init) } ;
339
+ let init = unsafe { $crate:: init:: pin_init_from_closure :: < $t $( <$( $generics) ,* >) ?, _>( init) } ;
340
340
init
341
341
} }
342
342
}
@@ -509,7 +509,7 @@ macro_rules! init {
509
509
let init = move |slot: * mut $t $( <$( $generics) ,* >) ?| -> :: core:: result:: Result <( ) , _> {
510
510
init( slot) . map( |__InitOk| ( ) )
511
511
} ;
512
- let init: $crate:: init:: InitClosure <_ , $t $( <$( $generics) ,* >) ?, _> = unsafe { $crate :: init :: InitClosure :: from_closure ( init) } ;
512
+ let init = unsafe { $crate:: init:: init_from_closure :: < $t $( <$( $generics) ,* >) ?, _>( init) } ;
513
513
init
514
514
} }
515
515
}
@@ -569,27 +569,39 @@ pub unsafe trait Init<T, E = Infallible>: PinInit<T, E> {
569
569
570
570
type Invariant < T > = PhantomData < fn ( T ) -> T > ;
571
571
572
- /// A closure initializer.
573
- pub struct InitClosure < F , T , E > ( F , Invariant < ( T , E ) > ) ;
572
+ struct InitClosure < F , T , E > ( F , Invariant < ( T , E ) > ) ;
574
573
575
- impl < T , E , F > InitClosure < F , T , E >
576
- where
577
- F : FnOnce ( * mut T ) -> Result < ( ) , E > ,
578
- {
579
- /// Creates a new Init from the given closure
580
- ///
581
- /// # Safety
582
- ///
583
- /// The closure
584
- /// - returns `Ok(())` iff it initialized every field of slot,
585
- /// - returns `Err(err)` iff it encountered an error and then cleaned slot, this means:
586
- /// - slot can be deallocated without UB ocurring,
587
- /// - slot does not need to be dropped,
588
- /// - slot is not partially initialized.
589
- /// - slot may move after initialization
590
- pub const unsafe fn from_closure ( f : F ) -> Self {
591
- Self ( f, PhantomData )
592
- }
574
+ /// Creates a new Init from the given closure
575
+ ///
576
+ /// # Safety
577
+ ///
578
+ /// The closure
579
+ /// - returns `Ok(())` iff it initialized every field of slot,
580
+ /// - returns `Err(err)` iff it encountered an error and then cleaned slot, this means:
581
+ /// - slot can be deallocated without UB ocurring,
582
+ /// - slot does not need to be dropped,
583
+ /// - slot is not partially initialized.
584
+ /// - slot may move after initialization
585
+ pub const unsafe fn init_from_closure < T , E > (
586
+ f : impl FnOnce ( * mut T ) -> Result < ( ) , E > ,
587
+ ) -> impl Init < T , E > {
588
+ InitClosure ( f, PhantomData )
589
+ }
590
+ /// Creates a new PinInit from the given closure
591
+ ///
592
+ /// # Safety
593
+ ///
594
+ /// The closure
595
+ /// - returns `Ok(())` iff it initialized every field of slot,
596
+ /// - returns `Err(err)` iff it encountered an error and then cleaned slot, this means:
597
+ /// - slot can be deallocated without UB ocurring,
598
+ /// - slot does not need to be dropped,
599
+ /// - slot is not partially initialized.
600
+ /// - may assume that the slot does not move if `T: !Unpin`
601
+ pub const unsafe fn pin_init_from_closure < T , E > (
602
+ f : impl FnOnce ( * mut T ) -> Result < ( ) , E > ,
603
+ ) -> impl PinInit < T , E > {
604
+ InitClosure ( f, PhantomData )
593
605
}
594
606
595
607
unsafe impl < T , F , E > PinInit < T , E > for InitClosure < F , T , E >
@@ -610,37 +622,6 @@ where
610
622
}
611
623
}
612
624
613
- /// A closure initializer for pinned data.
614
- pub struct PinInitClosure < F , T , E > ( F , Invariant < ( T , E ) > ) ;
615
-
616
- impl < T , E , F > PinInitClosure < F , T , E >
617
- where
618
- F : FnOnce ( * mut T ) -> Result < ( ) , E > ,
619
- {
620
- /// Creates a new Init from the given closure
621
- ///
622
- /// # Safety
623
- ///
624
- /// The closure
625
- /// - returns `Ok(())` iff it initialized every field of slot,
626
- /// - returns `Err(err)` iff it encountered an error and then cleaned slot, this means:
627
- /// - slot can be deallocated without UB ocurring,
628
- /// - slot does not need to be dropped,
629
- /// - slot is not partially initialized.
630
- pub const unsafe fn from_closure ( f : F ) -> Self {
631
- Self ( f, PhantomData )
632
- }
633
- }
634
-
635
- unsafe impl < T , F , E > PinInit < T , E > for PinInitClosure < F , T , E >
636
- where
637
- F : FnOnce ( * mut T ) -> Result < ( ) , E > ,
638
- {
639
- unsafe fn __pinned_init ( self , slot : * mut T ) -> Result < ( ) , E > {
640
- ( self . 0 ) ( slot)
641
- }
642
- }
643
-
644
625
/// Trait facilitating pinned destruction.
645
626
///
646
627
/// Use [`pinned_drop`] to implement this trait safely:
@@ -756,7 +737,7 @@ pub unsafe trait Zeroable {}
756
737
/// Create a new zeroed T
757
738
pub fn zeroed < T : Zeroable + Unpin > ( ) -> impl Init < T > {
758
739
unsafe {
759
- InitClosure :: from_closure ( |slot : * mut T | {
740
+ init_from_closure ( |slot : * mut T | {
760
741
slot. write_bytes ( 0 , 1 ) ;
761
742
Ok ( ( ) )
762
743
} )
@@ -765,13 +746,13 @@ pub fn zeroed<T: Zeroable + Unpin>() -> impl Init<T> {
765
746
766
747
/// An initializer that leaves the memory uninitialized.
767
748
pub fn uninit < T > ( ) -> impl Init < MaybeUninit < T > > {
768
- unsafe { InitClosure :: from_closure ( |_| Ok ( ( ) ) ) }
749
+ unsafe { init_from_closure ( |_| Ok ( ( ) ) ) }
769
750
}
770
751
771
752
/// Convert a value into an initializer
772
753
pub fn from_value < T > ( value : T ) -> impl Init < T > {
773
754
unsafe {
774
- InitClosure :: from_closure ( move |slot : * mut T | {
755
+ init_from_closure ( move |slot : * mut T | {
775
756
slot. write ( value) ;
776
757
Ok ( ( ) )
777
758
} )
0 commit comments