Struct 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;

unsafe 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 that of the pool.
    //
    // Note that this only helps ensuring soundness, our function is still
    // unsafe because the pool cannot be guaranteed to be the innermost
    // pool.
    unsafe { pool.ptr_as_ref(description) }
}

autoreleasepool(|pool| {
    // SAFETY: The given pool is the innermost pool.
    let obj = unsafe { 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.

Note that this is helpful, but not sufficient, for ensuring that the lifetime of the reference does not exceed the lifetime of the autorelease pool. When calling this, you must also ensure that the pool is actually the current innermost pool.

§Panics

If the pool is not the innermost pool, this function may panic when the "std" Cargo feature and debug assertions are enabled.

§Safety

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

The pool must be the innermost pool for the lifetime to be correct.

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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,

Source§

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

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,