@@ -1092,6 +1092,36 @@ where
10921092 }
10931093}
10941094
1095+ /// Implement `PinInit` and `Init` for closures.
1096+ ///
1097+ /// It is unsafe to create this type, since the closure needs to fulfill the same safety
1098+ /// requirement as the `__pinned_init`/`__init` functions.
1099+ struct InitClosure < F , T : ?Sized > ( F , __internal:: PhantomInvariant < T > ) ;
1100+
1101+ // SAFETY: While constructing the `InitClosure`, the user promised that it upholds the
1102+ // `__init` invariants.
1103+ unsafe impl < T : ?Sized , F , E > Init < T , E > for InitClosure < F , T >
1104+ where
1105+ F : FnOnce ( * mut T ) -> Result < ( ) , E > ,
1106+ {
1107+ #[ inline]
1108+ unsafe fn __init ( self , slot : * mut T ) -> Result < ( ) , E > {
1109+ ( self . 0 ) ( slot)
1110+ }
1111+ }
1112+
1113+ // SAFETY: While constructing the `InitClosure`, the user promised that it upholds the
1114+ // `__pinned_init` invariants.
1115+ unsafe impl < T : ?Sized , F , E > PinInit < T , E > for InitClosure < F , T >
1116+ where
1117+ F : FnOnce ( * mut T ) -> Result < ( ) , E > ,
1118+ {
1119+ #[ inline]
1120+ unsafe fn __pinned_init ( self , slot : * mut T ) -> Result < ( ) , E > {
1121+ ( self . 0 ) ( slot)
1122+ }
1123+ }
1124+
10951125/// Creates a new [`PinInit<T, E>`] from the given closure.
10961126///
10971127/// # Safety
@@ -1108,7 +1138,7 @@ where
11081138pub const unsafe fn pin_init_from_closure < T : ?Sized , E > (
11091139 f : impl FnOnce ( * mut T ) -> Result < ( ) , E > ,
11101140) -> impl PinInit < T , E > {
1111- __internal :: InitClosure ( f, __internal:: PhantomInvariant :: new ( ) )
1141+ InitClosure ( f, __internal:: PhantomInvariant :: new ( ) )
11121142}
11131143
11141144/// Creates a new [`Init<T, E>`] from the given closure.
@@ -1127,7 +1157,7 @@ pub const unsafe fn pin_init_from_closure<T: ?Sized, E>(
11271157pub const unsafe fn init_from_closure < T : ?Sized , E > (
11281158 f : impl FnOnce ( * mut T ) -> Result < ( ) , E > ,
11291159) -> impl Init < T , E > {
1130- __internal :: InitClosure ( f, __internal:: PhantomInvariant :: new ( ) )
1160+ InitClosure ( f, __internal:: PhantomInvariant :: new ( ) )
11311161}
11321162
11331163/// Changes the to be initialized type.
0 commit comments