-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from madsmtm/objc_block
Move `block` crate into this repo
- Loading branch information
Showing
21 changed files
with
790 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
[workspace] | ||
members = [ | ||
"objc", | ||
"objc_block", | ||
"objc_encode", | ||
"objc_exception", | ||
"objc_foundation", | ||
"objc_foundation_derive", | ||
"objc_id", | ||
"objc_test_utils", | ||
] | ||
exclude = ["objc/tests-ios"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
[package] | ||
name = "block" | ||
version = "0.1.6" | ||
authors = ["Steven Sheldon", "Mads Marquart <mads@marquart.dk>"] | ||
edition = "2018" | ||
|
||
description = "Interface for Apple's C language extension of blocks." | ||
keywords = ["objective-c", "macos", "ios", "blocks"] | ||
categories = [ | ||
"api-bindings", | ||
"development-tools::ffi", | ||
"os::macos-apis", | ||
] | ||
readme = "README.md" | ||
repository = "https://github.com/madsmtm/objc" | ||
documentation = "https://docs.rs/block/" | ||
license = "MIT" | ||
|
||
exclude = [ | ||
"tests-ios/**", | ||
] | ||
|
||
[dependencies] | ||
objc-encode = { path = "../objc_encode", version = "1.1.0" } | ||
|
||
[dev-dependencies] | ||
objc_test_utils = { path = "../objc_test_utils", version = "0.0" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# `block` | ||
|
||
[](https://crates.io/crates/block) | ||
[](../LICENSE.txt) | ||
[](https://docs.rs/block/) | ||
[](https://github.com/madsmtm/objc/actions) | ||
|
||
Rust interface for Apple's C language extension of blocks. | ||
|
||
For more information on the specifics of the block implementation, see | ||
Clang's documentation: http://clang.llvm.org/docs/Block-ABI-Apple.html | ||
|
||
## Invoking blocks | ||
|
||
The `Block` struct is used for invoking blocks from Objective-C. For example, | ||
consider this Objective-C function: | ||
|
||
``` objc | ||
int32_t sum(int32_t (^block)(int32_t, int32_t)) { | ||
return block(5, 8); | ||
} | ||
``` | ||
We could write it in Rust as the following: | ||
``` rust | ||
unsafe fn sum(block: &Block<(i32, i32), i32>) -> i32 { | ||
block.call((5, 8)) | ||
} | ||
``` | ||
|
||
Note the extra parentheses in the `call` method, since the arguments must be | ||
passed as a tuple. | ||
|
||
## Creating blocks | ||
|
||
Creating a block to pass to Objective-C can be done with the `ConcreteBlock` | ||
struct. For example, to create a block that adds two `i32`s, we could write: | ||
|
||
``` rust | ||
let block = ConcreteBlock::new(|a: i32, b: i32| a + b); | ||
let block = block.copy(); | ||
assert!(unsafe { block.call((5, 8)) } == 13); | ||
``` | ||
|
||
It is important to copy your block to the heap (with the `copy` method) before | ||
passing it to Objective-C; this is because our `ConcreteBlock` is only meant | ||
to be copied once, and we can enforce this in Rust, but if Objective-C code | ||
were to copy it twice we could have a double free. |
Oops, something went wrong.