[go: up one dir, main page]

Block

Struct Block 

Source
#[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>

Source

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.

Source

pub fn call(&self, args: F::Args) -> F::Output
where F: BlockFn,

Call the block.

The arguments must be passed as a tuple. The return is the output of the block.

Trait Implementations§

Source§

impl<F: ?Sized> Debug for Block<F>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<F: ?Sized + BlockFn> RefEncode for Block<F>

Source§

const ENCODING_REF: Encoding = Encoding::Block

The Objective-C type-encoding for a reference of this type. Read more

Auto Trait Implementations§

§

impl<F> Freeze for Block<F>
where F: ?Sized,

§

impl<F> !RefUnwindSafe for Block<F>

§

impl<F> !Send for Block<F>

§

impl<F> !Sync for Block<F>

§

impl<F> Unpin for Block<F>
where F: Unpin + ?Sized,

§

impl<F> !UnwindSafe for Block<F>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> AutoreleaseSafe for T
where T: ?Sized,