madsmtm / objc2

Bindings to Apple's frameworks in Rust
https://docs.rs/objc2/
MIT License
280 stars 35 forks source link

Change syntax for blocks, and allow them to carry a lifetime #569

Closed madsmtm closed 3 months ago

madsmtm commented 3 months ago

Part of https://github.com/madsmtm/objc2/issues/168.

Change the syntax to Block<dyn Fn(Args) -> R>, which more clearly describes the required signature, and allows us to introduce lifetimes to correctly mark noescape/NS_NOESCAPE blocks, and make functions taking those safe.

// Before
fn takes_block(block: &Block<(u32,), u8>) { ... }
unsafe fn takes_noescape_block(block: &Block<(u32, u32), ()>) { ... }

// After
fn takes_block(block: &Block<dyn Fn(u32) -> u8>) { ... }
fn takes_noescape_block(block: &Block<dyn Fn(u32, u32) + '_>) { ... }