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>
impl<'pool> AutoreleasePool<'pool>
sourcepub unsafe fn ptr_as_ref<T: ?Sized>(self, ptr: *const T) -> &'pool T
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.
sourcepub unsafe fn ptr_as_mut<T: ?Sized>(self, ptr: *mut T) -> &'pool mut T
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>
impl<'pool> Clone for AutoreleasePool<'pool>
source§fn clone(&self) -> AutoreleasePool<'pool>
fn clone(&self) -> AutoreleasePool<'pool>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more