Struct objc2::rc::AutoreleasePool

source ·
pub struct AutoreleasePool { /* private fields */ }
Expand description

An Objective-C autorelease pool.

The pool is drained when dropped.

This is not Send, since objc_autoreleasePoolPop must be called on the same thread.

And this is not Sync, since you can only autorelease a reference to a pool on the current thread.

Implementations§

source§

impl AutoreleasePool

source

pub unsafe fn ptr_as_ref<'p, T: ?Sized>(&'p self, ptr: *const T) -> &'p 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<'p, T: ?Sized>(&'p self, ptr: *mut T) -> &'p 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 Debug for AutoreleasePool

source§

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

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

impl Drop for AutoreleasePool

source§

fn drop(&mut self)

Drains the autoreleasepool.

The clang documentation says that @autoreleasepool blocks are not drained when exceptions occur because:

Not draining the pool during an unwind is apparently required by the Objective-C exceptions implementation.

However, we would like to do this anyway whenever possible, since the unwind is probably caused by Rust, and forgetting to pop the pool will likely leak memory.

Fortunately, the above statement was true in the past, but since revision 371 of objc4 (ships with MacOS 10.5) the exception is now retained when @throw is encountered.

Hence it is safe to drain the pool when unwinding.

TODO: Verify this claim on 32bit!

source§

impl Pointer for AutoreleasePool

source§

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

Formats the value using the given formatter.

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere 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 Twhere 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 Twhere T: ?Sized,