#[repr(C)]pub struct Block<F: ?Sized> { /* private fields */ }Expand description
An opaque type that holds an Objective-C block.
The generic type F must be a dyn Fn that implements
the BlockFn trait (which means parameter and return types must be
“encodable”), and describes the parameter and return types of the block.
For example, you may have the type Block<dyn Fn(u8, u8) -> i32>, and
that would be a 'static block that takes two u8s, and returns an
i32.
If you want the block to carry a lifetime, use Block<dyn Fn() + 'a>,
just like you’d usually do with dyn Fn.
§Memory layout
This is intended to be an extern type, and as such the memory layout of
this type is not guaranteed. That said, pointers to this type are
always thin, and match that of Objective-C blocks. So the layout of e.g.
&Block<dyn Fn(...) -> ... + '_> is defined, and guaranteed to be
pointer-sized and ABI-compatible with a block pointer.
§Safety invariant
Calling this potentially invokes foreign code, so you must verify, when creating a reference to this, or returning it from an external API, that it doesn’t violate any of Rust’s safety rules.
In particular, blocks are sharable with multiple references (see e.g.
Block::copy), so the caller must ensure that calling it can never
cause a data race. This usually means you’ll have to use some form of
interior mutability, if you need to mutate something from inside a block.
Implementations§
Source§impl<F: ?Sized> Block<F>
impl<F: ?Sized> Block<F>
Sourcepub fn copy(&self) -> RcBlock<F>
pub fn copy(&self) -> RcBlock<F>
Copy the block onto the heap as an RcBlock.
The behaviour of this function depends on whether the block is from a
RcBlock or a StackBlock. In the former case, it will bump the
reference-count (just as-if you’d Clone’d the RcBlock), in the
latter case it will construct a new RcBlock from the StackBlock.
This distinction should not matter, except for micro-optimizations.