Crateblock [−][src]
A Rust interface for Objective-C blocks.
For more information on the specifics of the block implementation, seeClang's documentation:http://clang.llvm.org/docs/Block-ABI-Apple.html
Invoking blocks
TheBlock
struct is used for invoking blocks from Objective-C. For example,consider this Objective-C function:
int32_t sum(int32_t (^block)(int32_t, int32_t)) { return block(5, 8);}
We could write it in Rust as the following:
unsafefnsum(block:&Block<(i32,i32),i32>)->i32 {block.call((5,8))}
Note the extra parentheses in thecall
method, since the arguments must bepassed as a tuple.
Creating blocks
Creating a block to pass to Objective-C can be done with theConcreteBlock
struct. For example, to create a block that adds twoi32
s, we could write:
letblock=ConcreteBlock::new(|a:i32,b:i32|a+b);letblock=block.copy();assert!(unsafe {block.call((5,8)) }==13);
It is important to copy your block to the heap (with thecopy
method) beforepassing it to Objective-C; this is because ourConcreteBlock
is only meantto be copied once, and we can enforce this in Rust, but if Objective-C codewere to copy it twice we could have a double free.
Structs
Block | An Objective-C block that takes arguments of |
ConcreteBlock | An Objective-C block whose size is known at compile time and may beconstructed on the stack. |
RcBlock | A reference-counted Objective-C block. |
Traits
BlockArguments | Types that may be used as the arguments to an Objective-C block. |
IntoConcreteBlock | Types that may be converted into a |