Trait objc2::rc::AutoreleaseSafe

source ·
pub unsafe trait AutoreleaseSafe { }
Expand description

Marks types that are safe to pass across the closure in an autoreleasepool.

With the "unstable-autoreleasesafe" feature enabled, this is an auto trait that is implemented for all types except AutoreleasePool.

Otherwise it is a dummy trait that is implemented for all types; the safety invariants are checked with debug assertions instead.

You should not normally need to implement this trait yourself.

Safety

Must not be implemented for types that interract with the autorelease pool. So if you reimplement the AutoreleasePool struct or likewise, this should be negatively implemented for that.

This can be accomplished with an PhantomData<AutoreleasePool<'_>> if the "unstable-autoreleasesafe" feature is enabled.

Examples

Most types are AutoreleaseSafe.

use objc2::rc::{AutoreleasePool, AutoreleaseSafe};
fn requires_autoreleasesafe<T: AutoreleaseSafe>() {}
requires_autoreleasesafe::<()>();
requires_autoreleasesafe::<Box<Vec<i32>>>();
requires_autoreleasesafe::<fn(AutoreleasePool<'_>)>();

But AutoreleasePool isn’t (if the "unstable-autoreleasesafe" feature is enabled).

 use objc2::rc::AutoreleasePool;
 requires_autoreleasesafe::<AutoreleasePool<'static>>();

This also means that trait objects aren’t (since they may contain an AutoreleasePool internally):

 requires_autoreleasesafe::<&dyn std::io::Write>();

Implementors§

source§

impl<T: ?Sized> AutoreleaseSafe for T

Available on non-crate feature unstable-autoreleasesafe only.