@@ -55,34 +55,16 @@ extern crate std;
5555#[ cfg( test) ]
5656mod test_utils;
5757
58- use alloc:: boxed:: Box ;
5958use core:: ffi:: c_void;
6059use core:: marker:: PhantomData ;
6160use core:: mem;
6261use core:: ops:: { Deref , DerefMut } ;
6362use core:: ptr;
6463use std:: os:: raw:: { c_int, c_ulong} ;
6564
65+ pub use objc2_block_sys as ffi;
6666use objc2_encode:: { Encode , EncodeArguments , Encoding , RefEncode } ;
6767
68- // TODO: Replace with `objc2::Class`
69- #[ repr( C ) ]
70- struct ClassInternal {
71- _priv : [ u8 ; 0 ] ,
72- }
73-
74- #[ cfg_attr( target_vendor = "apple" , link( name = "System" , kind = "dylib" ) ) ]
75- #[ cfg_attr(
76- not( target_vendor = "apple" ) ,
77- link( name = "BlocksRuntime" , kind = "dylib" )
78- ) ]
79- extern "C" {
80- static _NSConcreteStackBlock: ClassInternal ;
81-
82- fn _Block_copy ( block : * const c_void ) -> * mut c_void ;
83- fn _Block_release ( block : * const c_void ) ;
84- }
85-
8668/// Types that may be used as the arguments to an Objective-C block.
8769pub trait BlockArguments : Sized {
8870 /// Calls the given `Block` with self as the arguments.
@@ -151,7 +133,7 @@ block_args_impl!(
151133
152134#[ repr( C ) ]
153135struct BlockBase < A , R > {
154- isa : * const ClassInternal ,
136+ isa : * const ffi :: Class ,
155137 flags : c_int ,
156138 _reserved : c_int ,
157139 invoke : unsafe extern "C" fn ( * mut Block < A , R > , ...) -> R ,
@@ -207,7 +189,7 @@ impl<A, R> RcBlock<A, R> {
207189 ///
208190 /// The given pointer must point to a valid `Block`.
209191 pub unsafe fn copy ( ptr : * mut Block < A , R > ) -> Self {
210- let ptr = _Block_copy ( ptr as * const c_void ) as * mut Block < A , R > ;
192+ let ptr = ffi :: _Block_copy ( ptr as * const c_void ) as * mut Block < A , R > ;
211193 RcBlock { ptr }
212194 }
213195}
@@ -229,7 +211,7 @@ impl<A, R> Deref for RcBlock<A, R> {
229211impl < A , R > Drop for RcBlock < A , R > {
230212 fn drop ( & mut self ) {
231213 unsafe {
232- _Block_release ( self . ptr as * const c_void ) ;
214+ ffi :: _Block_release ( self . ptr as * const c_void ) ;
233215 }
234216 }
235217}
@@ -366,7 +348,7 @@ concrete_block_impl!(
366348#[ repr( C ) ]
367349pub struct ConcreteBlock < A , R , F > {
368350 base : BlockBase < A , R > ,
369- descriptor : Box < BlockDescriptor < ConcreteBlock < A , R , F > > > ,
351+ descriptor : * const BlockDescriptor < ConcreteBlock < A , R , F > > ,
370352 closure : F ,
371353}
372354
@@ -391,19 +373,25 @@ where
391373}
392374
393375impl < A , R , F > ConcreteBlock < A , R , F > {
376+ const DESCRIPTOR : BlockDescriptor < Self > = BlockDescriptor {
377+ _reserved : 0 ,
378+ block_size : mem:: size_of :: < Self > ( ) as c_ulong ,
379+ copy_helper : block_context_copy :: < Self > ,
380+ dispose_helper : block_context_dispose :: < Self > ,
381+ } ;
382+
394383 /// Constructs a `ConcreteBlock` with the given invoke function and closure.
395384 /// Unsafe because the caller must ensure the invoke function takes the
396385 /// correct arguments.
397386 unsafe fn with_invoke ( invoke : unsafe extern "C" fn ( * mut Self , ...) -> R , closure : F ) -> Self {
398387 ConcreteBlock {
399388 base : BlockBase {
400- isa : & _NSConcreteStackBlock,
401- // 1 << 25 = BLOCK_HAS_COPY_DISPOSE
402- flags : 1 << 25 ,
389+ isa : & ffi:: _NSConcreteStackBlock,
390+ flags : ffi:: BLOCK_HAS_COPY_DISPOSE ,
403391 _reserved : 0 ,
404392 invoke : mem:: transmute ( invoke) ,
405393 } ,
406- descriptor : Box :: new ( BlockDescriptor :: new ( ) ) ,
394+ descriptor : & Self :: DESCRIPTOR ,
407395 closure,
408396 }
409397 }
@@ -469,17 +457,6 @@ struct BlockDescriptor<B> {
469457 dispose_helper : unsafe extern "C" fn ( & mut B ) ,
470458}
471459
472- impl < B > BlockDescriptor < B > {
473- fn new ( ) -> BlockDescriptor < B > {
474- BlockDescriptor {
475- _reserved : 0 ,
476- block_size : mem:: size_of :: < B > ( ) as c_ulong ,
477- copy_helper : block_context_copy :: < B > ,
478- dispose_helper : block_context_dispose :: < B > ,
479- }
480- }
481- }
482-
483460#[ cfg( test) ]
484461mod tests {
485462 use super :: { ConcreteBlock , RcBlock } ;
0 commit comments