Struct objc2::rc::AutoreleasePool

source ·
pub struct AutoreleasePool<'pool> { /* private fields */ }
Expand description

An Objective-C autorelease pool.

Autorelease pools are a way to store objects in a certain thread-local scope, such that they are only released at the end of said scope.

See autoreleasepool and autoreleasepool_leaking for how to create this.

This is not Send nor Sync, since you can only autorelease a reference to a pool on the current thread.

§Example

Use the pool as a bound on a function, and release an object to that pool.

use objc2::rc::{autoreleasepool, AutoreleasePool};
use objc2::runtime::NSObject;
use objc2::msg_send;

fn needs_lifetime_from_pool<'p>(pool: AutoreleasePool<'p>) -> &'p NSObject {
    let obj = NSObject::new();
    // Do action that returns an autoreleased object
    let description: *mut NSObject = unsafe { msg_send![&obj, description] };
    // Bound the lifetime of the reference to the pool
    unsafe { pool.ptr_as_ref(description) }
}

autoreleasepool(|pool| {
    let obj = needs_lifetime_from_pool(pool);
    println!("{obj:?}");
});

Implementations§

source§

impl<'pool> AutoreleasePool<'pool>

source

pub unsafe fn ptr_as_ref<T: ?Sized>(self, ptr: *const T) -> &'pool T

Returns a shared reference to the given autoreleased pointer object.

This is the preferred way to make references from autoreleased objects, since it binds the lifetime of the reference to the pool, and does some extra checks when debug assertions are enabled.

For the mutable counterpart see ptr_as_mut.

§Safety

This is equivalent to &*ptr, and shares the unsafety of that, except the lifetime is bound to the pool instead of being unbounded.

source

pub unsafe fn ptr_as_mut<T: ?Sized>(self, ptr: *mut T) -> &'pool mut T

Returns a unique reference to the given autoreleased pointer object.

This is the preferred way to make mutable references from autoreleased objects, since it binds the lifetime of the reference to the pool, and does some extra checks when debug assertions are enabled.

For the shared counterpart see ptr_as_ref.

§Safety

This is equivalent to &mut *ptr, and shares the unsafety of that, except the lifetime is bound to the pool instead of being unbounded.

Trait Implementations§

source§

impl<'pool> Clone for AutoreleasePool<'pool>

source§

fn clone(&self) -> AutoreleasePool<'pool>

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl<'pool> Debug for AutoreleasePool<'pool>

source§

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

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

impl<'pool> Copy for AutoreleasePool<'pool>

Auto Trait Implementations§

§

impl<'pool> Freeze for AutoreleasePool<'pool>

§

impl<'pool> RefUnwindSafe for AutoreleasePool<'pool>

§

impl<'pool> !Send for AutoreleasePool<'pool>

§

impl<'pool> !Sync for AutoreleasePool<'pool>

§

impl<'pool> Unpin for AutoreleasePool<'pool>

§

impl<'pool> UnwindSafe for AutoreleasePool<'pool>

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,