Struct block2::RcBlock

source ·
pub struct RcBlock<F: ?Sized> { /* private fields */ }
Expand description

A reference-counted Objective-C block that is stored on the heap.

This is a smart pointer that Derefs to Block.

The generic type F must be a dyn Fn that implements the BlockFn trait, just like described in Block’s documentation.

§Memory-layout

This is guaranteed to have the same size and alignment as a pointer to a block (i.e. same size as *const Block<A, R>).

Additionally, it participates in the null-pointer optimization, that is, Option<RcBlock<A, R>> is guaranteed to have the same size as RcBlock<A, R>.

Implementations§

source§

impl<F: ?Sized> RcBlock<F>

source

pub unsafe fn from_raw(ptr: *mut Block<F>) -> Option<Self>

Construct an RcBlock from the given block pointer by taking ownership.

This will return None if the pointer is NULL.

§Safety

The given pointer must point to a valid block, the parameter and return types must be correct, and the block must have a +1 reference / retain count from somewhere else.

Additionally, the block must be safe to call (or, if it is not, then you must treat every call to the block as unsafe).

source

pub unsafe fn copy(ptr: *mut Block<F>) -> Option<Self>

Construct an RcBlock from the given block pointer.

The block will be copied, and have its reference-count increased by one.

This will return None if the pointer is NULL, or if an allocation failure occurred.

See Block::copy for a safe alternative when you already know the block pointer is valid.

§Safety

The given pointer must point to a valid block, and the parameter and return types must be correct.

Additionally, the block must be safe to call (or, if it is not, then you must treat every call to the block as unsafe).

source§

impl<F: ?Sized> RcBlock<F>

source

pub fn new<'f, A, R, Closure>(closure: Closure) -> Self
where A: EncodeArguments, R: EncodeReturn, Closure: IntoBlock<'f, A, R, Dyn = F>,

Construct a RcBlock with the given closure.

The closure will be coped to the heap on construction.

When the block is called, it will return the value that results from calling the closure.

Methods from Deref<Target = 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 distiction 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> Clone for RcBlock<F>

source§

fn clone(&self) -> Self

Increase the reference-count of the block.

1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

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

source§

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

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

impl<F: ?Sized> Deref for RcBlock<F>

§

type Target = Block<F>

The resulting type after dereferencing.
source§

fn deref(&self) -> &Block<F>

Dereferences the value.
source§

impl<F: ?Sized> Drop for RcBlock<F>

source§

fn drop(&mut self)

Release the block, decreasing the reference-count by 1.

The Drop method of the underlying closure will be called once the reference-count reaches zero.

Auto Trait Implementations§

§

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

§

impl<F> !RefUnwindSafe for RcBlock<F>

§

impl<F> !Send for RcBlock<F>

§

impl<F> !Sync for RcBlock<F>

§

impl<F> Unpin for RcBlock<F>
where F: ?Sized,

§

impl<F> !UnwindSafe for RcBlock<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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

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>,

§

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,