Trait core::panic::UnwindSafe

1.9.0 · source · []
pub auto trait UnwindSafe { }
Expand description

A marker trait which represents “panic safe” types in Rust.

This trait is implemented by default for many types and behaves similarly in terms of inference of implementation to the Send and Sync traits. The purpose of this trait is to encode what types are safe to cross a catch_unwind boundary with no fear of unwind safety.

What is unwind safety?

In Rust a function can “return” early if it either panics or calls a function which transitively panics. This sort of control flow is not always anticipated, and has the possibility of causing subtle bugs through a combination of two critical components:

  1. A data structure is in a temporarily invalid state when the thread panics.
  2. This broken invariant is then later observed.

Typically in Rust, it is difficult to perform step (2) because catching a panic involves either spawning a thread (which in turns makes it difficult to later witness broken invariants) or using the catch_unwind function in this module. Additionally, even if an invariant is witnessed, it typically isn’t a problem in Rust because there are no uninitialized values (like in C or C++).

It is possible, however, for logical invariants to be broken in Rust, which can end up causing behavioral bugs. Another key aspect of unwind safety in Rust is that, in the absence of unsafe code, a panic cannot lead to memory unsafety.

That was a bit of a whirlwind tour of unwind safety, but for more information about unwind safety and how it applies to Rust, see an associated RFC.

What is UnwindSafe?

Now that we’ve got an idea of what unwind safety is in Rust, it’s also important to understand what this trait represents. As mentioned above, one way to witness broken invariants is through the catch_unwind function in this module as it allows catching a panic and then re-using the environment of the closure.

Simply put, a type T implements UnwindSafe if it cannot easily allow witnessing a broken invariant through the use of catch_unwind (catching a panic). This trait is an auto trait, so it is automatically implemented for many types, and it is also structurally composed (e.g., a struct is unwind safe if all of its components are unwind safe).

Note, however, that this is not an unsafe trait, so there is not a succinct contract that this trait is providing. Instead it is intended as more of a “speed bump” to alert users of catch_unwind that broken invariants may be witnessed and may need to be accounted for.

Who implements UnwindSafe?

Types such as &mut T and &RefCell<T> are examples which are not unwind safe. The general idea is that any mutable state which can be shared across catch_unwind is not unwind safe by default. This is because it is very easy to witness a broken invariant outside of catch_unwind as the data is simply accessed as usual.

Types like &Mutex<T>, however, are unwind safe because they implement poisoning by default. They still allow witnessing a broken invariant, but they already provide their own “speed bumps” to do so.

When should UnwindSafe be used?

It is not intended that most types or functions need to worry about this trait. It is only used as a bound on the catch_unwind function and as mentioned above, the lack of unsafe means it is mostly an advisory. The AssertUnwindSafe wrapper struct can be used to force this trait to be implemented for any closed over variables passed to catch_unwind.

Implementors

Auto implementors

impl<T: ?Sized> UnwindSafe for ThinBox<T>where
    T: UnwindSafe,

impl<T: ?Sized, A> UnwindSafe for Box<T, A>where
    A: UnwindSafe,
    T: UnwindSafe,

impl<'a, B: ?Sized> UnwindSafe for Cow<'a, B>where
    B: RefUnwindSafe,
    <B as ToOwned>::Owned: UnwindSafe,

impl<'a> UnwindSafe for Drain<'a>

impl<'a, T, F, A = Global> !UnwindSafe for DrainFilter<'a, T, F, A>

impl<'a, T, A> UnwindSafe for Drain<'a, T, A>where
    A: RefUnwindSafe,
    T: RefUnwindSafe,

impl<T, A> UnwindSafe for IntoIter<T, A>where
    A: UnwindSafe,
    T: UnwindSafe + RefUnwindSafe,

impl<T, A> UnwindSafe for Vec<T, A>where
    A: UnwindSafe,
    T: UnwindSafe,

impl UnwindSafe for Error

impl UnwindSafe for CStr

impl<T> !UnwindSafe for Arc<T>

impl<'a, T> !UnwindSafe for ArcBorrow<'a, T>

impl<T> !UnwindSafe for UniqueArc<T>

impl<T: ?Sized> UnwindSafe for StaticArc<T>where
    T: UnwindSafe,

impl<'a, L: ?Sized, I> UnwindSafe for Guard<'a, L, I>where
    L: RefUnwindSafe,
    <L as Lock<I>>::GuardContext: UnwindSafe,

impl<T: ?Sized, L: ?Sized> UnwindSafe for LockedBy<T, L>where
    T: UnwindSafe,
    <L as Lock<WriteLock>>::Inner: RefUnwindSafe,

impl<T: ?Sized> UnwindSafe for Mutex<T>where
    T: UnwindSafe,

impl<T: ?Sized> UnwindSafe for NoWaitLock<T>where
    T: UnwindSafe,

impl<'a, T> !UnwindSafe for NoWaitLockGuard<'a, T>

impl UnwindSafe for Guard

impl<F, T> UnwindSafe for Revocable<F, T>where
    <F as LockFactory>::LockedType<Inner<T>>: UnwindSafe,

impl<'a, F, T, I> UnwindSafe for RevocableGuard<'a, F, T, I>where
    <<F as LockFactory>::LockedType<Inner<T>> as Lock<I>>::GuardContext: UnwindSafe,
    <F as LockFactory>::LockedType<Inner<T>>: RefUnwindSafe,

impl<T: ?Sized> UnwindSafe for RwSemaphore<T>where
    T: UnwindSafe,

impl<L: ?Sized> UnwindSafe for SeqLock<L>where
    L: UnwindSafe,

impl<'a, L> !UnwindSafe for SeqLockReadGuard<'a, L>

impl<T> !UnwindSafe for Mutex<T>

impl<T: ?Sized> UnwindSafe for SpinLock<T>where
    T: UnwindSafe,

impl<T: ?Sized> UnwindSafe for RawSpinLock<T>where
    T: UnwindSafe,

impl UnwindSafe for Mode

impl<T, F> UnwindSafe for ScopeGuard<T, F>where
    F: UnwindSafe,
    T: UnwindSafe,

impl<T> UnwindSafe for Opaque<T>where
    T: UnwindSafe,

impl<T> UnwindSafe for Bit<T>where
    T: UnwindSafe,

impl UnwindSafe for True

impl UnwindSafe for False

impl<T> UnwindSafe for ARef<T>where
    T: UnwindSafe + RefUnwindSafe,

impl<L, R> UnwindSafe for Either<L, R>where
    L: UnwindSafe,
    R: UnwindSafe,

impl<const N: usize> UnwindSafe for Registration<N>

impl UnwindSafe for Clk

impl<T, U, V> UnwindSafe for Data<T, U, V>where
    T: UnwindSafe,
    U: UnwindSafe,
    V: UnwindSafe,

impl<T> UnwindSafe for Registration<T>where
    <T as DriverOps>::RegType: UnwindSafe,

impl<T, U, const N: usize> UnwindSafe for IdArray<T, U, N>where
    U: UnwindSafe,
    <T as RawDeviceId>::RawType: UnwindSafe,

impl<'a, T, U> UnwindSafe for IdTable<'a, T, U>where
    U: RefUnwindSafe,
    <T as RawDeviceId>::RawType: RefUnwindSafe,

impl<T> UnwindSafe for Module<T>where
    <T as DriverOps>::RegType: UnwindSafe,

impl UnwindSafe for File

impl<'a> !UnwindSafe for Value<'a>

impl UnwindSafe for Spec

impl<const N: usize, S> !UnwindSafe for SpecArray<N, S>

impl<'a, S> !UnwindSafe for SpecTable<'a, S>

impl<const N: usize> UnwindSafe for ConstantArray<N>

impl<'a> UnwindSafe for ConstantTable<'a>

impl UnwindSafe for Super

impl<'a, T: ?Sized, S> UnwindSafe for NewSuperBlock<'a, T, S>where
    S: UnwindSafe,
    T: RefUnwindSafe,

impl<T: ?Sized> UnwindSafe for SuperBlock<T>where
    T: UnwindSafe,

impl UnwindSafe for INode

impl<T> UnwindSafe for Module<T>where
    T: UnwindSafe,

impl<T> UnwindSafe for RegistrationWithIrqChip<T>where
    T: UnwindSafe,

impl<T> UnwindSafe for Registration<T>where
    T: UnwindSafe,

impl<T> UnwindSafe for Registration<T>where
    T: UnwindSafe,

impl UnwindSafe for Type

impl<H> UnwindSafe for Registration<H>where
    <H as Handler>::Data: UnwindSafe,

impl<H> UnwindSafe for ThreadedRegistration<H>where
    <H as ThreadedHandler>::Data: UnwindSafe,

impl<'a> UnwindSafe for ChainedGuard<'a>

impl<T> !UnwindSafe for AutoStopHandle<T>

impl<'a> !UnwindSafe for Options<'a>

impl<T> UnwindSafe for Registration<T>where
    <T as Operations>::OpenData: UnwindSafe,

impl<T> UnwindSafe for Module<T>

impl UnwindSafe for Area

impl UnwindSafe for Hook

impl UnwindSafe for Hook

impl UnwindSafe for Hook

impl<T> !UnwindSafe for Registration<T>

impl<const ORDER: u32> UnwindSafe for Pages<ORDER>

impl<T> UnwindSafe for NoOperations<T>where
    T: UnwindSafe,

impl<T> UnwindSafe for Revocable<T>where
    T: UnwindSafe,

impl<'a, T> UnwindSafe for RevocableGuard<'a, T>where
    T: RefUnwindSafe,

impl<T> UnwindSafe for AsyncRevocable<T>where
    T: UnwindSafe,

impl<'a, T> !UnwindSafe for AsyncRevocableGuard<'a, T>

impl UnwindSafe for Task

impl<'a> !UnwindSafe for TaskRef<'a>

impl UnwindSafe for Queue

impl UnwindSafe for Work

impl<'a, G> UnwindSafe for Cursor<'a, G>where
    <G as GetLinks>::EntryType: RefUnwindSafe,

impl<T: ?Sized> UnwindSafe for Links<T>where
    T: RefUnwindSafe,

impl<G> UnwindSafe for List<G>where
    <G as GetLinks>::EntryType: RefUnwindSafe,

impl<'a, G> !UnwindSafe for CursorMut<'a, G>

impl<K, V> UnwindSafe for RBTree<K, V>where
    K: UnwindSafe,
    V: UnwindSafe,

impl<'a, K, V> UnwindSafe for RBTreeIterator<'a, K, V>where
    K: RefUnwindSafe,
    V: RefUnwindSafe,

impl<'a, K, V> UnwindSafe for RBTreeIteratorMut<'a, K, V>where
    K: RefUnwindSafe,
    V: RefUnwindSafe,

impl<K, V> UnwindSafe for RBTreeNodeReservation<K, V>where
    K: UnwindSafe,
    V: UnwindSafe,

impl<K, V> UnwindSafe for RBTreeNode<K, V>where
    K: UnwindSafe,
    V: UnwindSafe,

impl<A: ?Sized> UnwindSafe for List<A>where
    <A as Adapter>::EntryType: RefUnwindSafe,

impl<'a, A: ?Sized> UnwindSafe for Iterator<'a, A>where
    <A as Adapter>::EntryType: RefUnwindSafe,

impl<T: ?Sized> UnwindSafe for Links<T>where
    T: RefUnwindSafe,

impl<'a, A: ?Sized> UnwindSafe for Cursor<'a, A>where
    <A as Adapter>::EntryType: RefUnwindSafe,

impl<T> UnwindSafe for Sysctl<T>where
    T: UnwindSafe,

impl<const SIZE: usize> UnwindSafe for IoMem<SIZE>

impl<T> UnwindSafe for Adapter<T>where
    T: UnwindSafe,

impl<'a> UnwindSafe for KParamGuard<'a>

This documentation is an old archive. Please see https://rust.docs.kernel.org instead.