@@ -32,14 +32,6 @@ pub unsafe trait BlockFn: private::Sealed<Self::Args, Self::Output> {
3232
3333 /// The return type of the block.
3434 type Output : EncodeReturn ;
35-
36- /// Calls the given invoke function with the block and arguments.
37- #[ doc( hidden) ]
38- unsafe fn __call_block (
39- invoke : unsafe extern "C-unwind" fn ( ) ,
40- block : * mut Block < Self > ,
41- args : Self :: Args ,
42- ) -> Self :: Output ;
4335}
4436
4537/// Types that may be converted into a block.
6658}
6759
6860macro_rules! impl_traits {
69- ( $( $a: ident: $t: ident) ,* ) => (
61+ ( $num_args : literal ; $ ( $a: ident: $t: ident) ,* ) => (
7062 impl <$( $t: EncodeArgument , ) * R : EncodeReturn , Closure > private:: Sealed <( $( $t, ) * ) , R > for Closure
7163 where
7264 Closure : ?Sized + Fn ( $( $t) ,* ) -> R ,
@@ -76,20 +68,6 @@ macro_rules! impl_traits {
7668 unsafe impl <$( $t: EncodeArgument , ) * R : EncodeReturn > BlockFn for dyn Fn ( $( $t) ,* ) -> R + ' _ {
7769 type Args = ( $( $t, ) * ) ;
7870 type Output = R ;
79-
80- #[ inline]
81- unsafe fn __call_block(
82- invoke: unsafe extern "C-unwind" fn ( ) ,
83- block: * mut Block <Self >,
84- ( $( $a, ) * ) : Self :: Args ,
85- ) -> Self :: Output {
86- // Very similar to `MessageArguments::__invoke`
87- let invoke: unsafe extern "C-unwind" fn ( * mut Block <Self > $( , $t) * ) -> R = unsafe {
88- mem:: transmute( invoke)
89- } ;
90-
91- unsafe { invoke( block $( , $a) * ) }
92- }
9371 }
9472
9573 unsafe impl <' f, $( $t, ) * R , Closure > IntoBlock <' f, ( $( $t, ) * ) , R > for Closure
@@ -121,22 +99,41 @@ macro_rules! impl_traits {
12199 }
122100 }
123101 }
102+
103+ impl <' f, $( $t: EncodeArgument , ) * R : EncodeReturn > Block <dyn Fn ( $( $t) ,* ) -> R + ' f> {
104+ #[ doc = concat!( "Call the block with " , $num_args, " arguments." ) ]
105+ ///
106+ /// The return value is the output of the block.
107+ #[ doc( alias = "invoke" ) ]
108+ #[ inline]
109+ #[ allow( clippy:: too_many_arguments) ]
110+ pub fn call( & self , $( $a: $t) ,* ) -> R {
111+ // Very similar to `MessageArguments::__invoke`
112+ let invoke: unsafe extern "C-unwind" fn ( & Self $( , $t) * ) -> R = unsafe {
113+ mem:: transmute( self . invoke_ptr( ) )
114+ } ;
115+
116+ // SAFETY: The closure is an `Fn`, and as such is safe to call
117+ // from an immutable reference.
118+ unsafe { invoke( self $( , $a) * ) }
119+ }
120+ }
124121 ) ;
125122}
126123
127- impl_traits ! ( ) ;
128- impl_traits ! ( t0: T0 ) ;
129- impl_traits ! ( t0: T0 , t1: T1 ) ;
130- impl_traits ! ( t0: T0 , t1: T1 , t2: T2 ) ;
131- impl_traits ! ( t0: T0 , t1: T1 , t2: T2 , t3: T3 ) ;
132- impl_traits ! ( t0: T0 , t1: T1 , t2: T2 , t3: T3 , t4: T4 ) ;
133- impl_traits ! ( t0: T0 , t1: T1 , t2: T2 , t3: T3 , t4: T4 , t5: T5 ) ;
134- impl_traits ! ( t0: T0 , t1: T1 , t2: T2 , t3: T3 , t4: T4 , t5: T5 , t6: T6 ) ;
135- impl_traits ! ( t0: T0 , t1: T1 , t2: T2 , t3: T3 , t4: T4 , t5: T5 , t6: T6 , t7: T7 ) ;
136- impl_traits ! ( t0: T0 , t1: T1 , t2: T2 , t3: T3 , t4: T4 , t5: T5 , t6: T6 , t7: T7 , t8: T8 ) ;
137- impl_traits ! ( t0: T0 , t1: T1 , t2: T2 , t3: T3 , t4: T4 , t5: T5 , t6: T6 , t7: T7 , t8: T8 , t9: T9 ) ;
138- impl_traits ! ( t0: T0 , t1: T1 , t2: T2 , t3: T3 , t4: T4 , t5: T5 , t6: T6 , t7: T7 , t8: T8 , t9: T9 , t10: T10 ) ;
139- impl_traits ! ( t0: T0 , t1: T1 , t2: T2 , t3: T3 , t4: T4 , t5: T5 , t6: T6 , t7: T7 , t8: T8 , t9: T9 , t10: T10 , t11: T11 ) ;
124+ impl_traits ! ( 0 ; ) ;
125+ impl_traits ! ( 1 ; t0: T0 ) ;
126+ impl_traits ! ( 2 ; t0: T0 , t1: T1 ) ;
127+ impl_traits ! ( 3 ; t0: T0 , t1: T1 , t2: T2 ) ;
128+ impl_traits ! ( 4 ; t0: T0 , t1: T1 , t2: T2 , t3: T3 ) ;
129+ impl_traits ! ( 5 ; t0: T0 , t1: T1 , t2: T2 , t3: T3 , t4: T4 ) ;
130+ impl_traits ! ( 6 ; t0: T0 , t1: T1 , t2: T2 , t3: T3 , t4: T4 , t5: T5 ) ;
131+ impl_traits ! ( 7 ; t0: T0 , t1: T1 , t2: T2 , t3: T3 , t4: T4 , t5: T5 , t6: T6 ) ;
132+ impl_traits ! ( 8 ; t0: T0 , t1: T1 , t2: T2 , t3: T3 , t4: T4 , t5: T5 , t6: T6 , t7: T7 ) ;
133+ impl_traits ! ( 9 ; t0: T0 , t1: T1 , t2: T2 , t3: T3 , t4: T4 , t5: T5 , t6: T6 , t7: T7 , t8: T8 ) ;
134+ impl_traits ! ( 10 ; t0: T0 , t1: T1 , t2: T2 , t3: T3 , t4: T4 , t5: T5 , t6: T6 , t7: T7 , t8: T8 , t9: T9 ) ;
135+ impl_traits ! ( 11 ; t0: T0 , t1: T1 , t2: T2 , t3: T3 , t4: T4 , t5: T5 , t6: T6 , t7: T7 , t8: T8 , t9: T9 , t10: T10 ) ;
136+ impl_traits ! ( 12 ; t0: T0 , t1: T1 , t2: T2 , t3: T3 , t4: T4 , t5: T5 , t6: T6 , t7: T7 , t8: T8 , t9: T9 , t10: T10 , t11: T11 ) ;
140137
141138/// Interim abstraction to manually provide block encodings for use at compile
142139/// time with [`StackBlock::with_encoding`] and [`RcBlock::with_encoding`].
0 commit comments