#blocks #objective-c #ios #macos-ios #block #sys #macos

sys no-std block-sys

Raw bindings to Apple's C language extension of blocks

9 releases

0.2.1 Jan 7, 2024
0.2.0 Feb 7, 2023
0.1.0-beta.2 Dec 24, 2022
0.1.0-beta.1 Aug 28, 2022
0.0.1 Nov 22, 2021

#135 in macOS and iOS APIs

Download history 27982/week @ 2023-12-17 21418/week @ 2023-12-24 29116/week @ 2023-12-31 40573/week @ 2024-01-07 41096/week @ 2024-01-14 41679/week @ 2024-01-21 38467/week @ 2024-01-28 45026/week @ 2024-02-04 41696/week @ 2024-02-11 37627/week @ 2024-02-18 45940/week @ 2024-02-25 46901/week @ 2024-03-03 45210/week @ 2024-03-10 48184/week @ 2024-03-17 55193/week @ 2024-03-24 44573/week @ 2024-03-31

197,710 downloads per month
Used in 8 crates (via block2)

MIT license

46KB
459 lines

block-sys

Latest version License Documentation CI

This crate is deprecated. Use block2::ffi instead.

Raw Rust bindings to Apple's C language extension of blocks.

This crate is part of the objc2 project, see that for related crates.

Runtime Support

This library is a raw interface to the aptly specified Blocks ABI. However, different runtime implementations exist and act in slightly different ways (and have several different helper functions), the most important aspect being that the libraries are named differently, so the linking must take that into account.

You can choose the desired runtime by using the relevant cargo feature flags, see the following sections (you might have to disable the default apple feature first). Note that if the objc-sys crate is present in the module tree, this should have the same feature flag enabled as that.

Apple's libclosure

  • Feature flag: apple.

This is the most sophisticated runtime, and it has quite a lot more features than the specification mandates. It is used by default.

The minimum required operating system versions are as follows:

  • macOS: 10.6
  • iOS: 3.2
  • tvOS: Unknown
  • watchOS: Unknown

Though in practice Rust itself requires higher versions than this.

LLVM compiler-rt's libBlocksRuntime

  • Feature flag: compiler-rt.

This is a copy of Apple's older (around macOS 10.6) runtime, and is now used in Swift's libdispatch and Swift's Foundation as well.

The runtime and associated headers can be installed on many Linux systems with the libblocksruntime-dev package.

Using this runtime probably won't work together with objc-sys crate.

GNUStep's libobjc2

  • Feature flag: gnustep-1-7, gnustep-1-8, gnustep-1-9, gnustep-2-0 and gnustep-2-1 depending on the version you're using.

GNUStep is a bit odd, because it bundles blocks support into its Objective-C runtime. This means we have to link to libobjc, and this is done by depending on the objc-sys crate. A bit unorthodox, yes, but it works.

Sources:

Microsoft's WinObjC

  • Feature flag: unstable-winobjc.

Unstable: Hasn't been tested on Windows yet!

A fork based on GNUStep's libobjc2 version 1.8.

ObjFW

  • Feature flag: unstable-objfw.

Unstable: Doesn't work yet!

TODO.

C Compiler configuration

To our knowledge, currently only clang supports the Language Specification for Blocks. To assist in compiling C (or Objective-C) sources using these features, this crate's build script expose the DEP_BLOCK_0_2_CC_ARGS environment variable to downstream build scripts.

Example usage in your build.rs (using the cc crate) would be as follows:

fn main() {
    let mut builder = cc::Build::new();
    builder.compiler("clang");
    builder.file("my_script_using_blocks.c");

    for flag in std::env::var("DEP_BLOCK_0_2_CC_ARGS").unwrap().split(' ') {
        builder.flag(flag);
    }

    builder.compile("libmy_script_using_blocks.a");
}

Dependencies