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

impl<K, V, A: Allocator + Clone> UnwindSafe for BTreeMap<K, V, A>where
    A: UnwindSafe,
    K: RefUnwindSafe,
    V: RefUnwindSafe,

impl<T: RefUnwindSafe + ?Sized> UnwindSafe for Rc<T>

impl<T: RefUnwindSafe + ?Sized> UnwindSafe for Arc<T>

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

impl<T: ?Sized> UnwindSafe for RwLock<T>

impl<K, V, S> UnwindSafe for HashMap<K, V, S>where
    K: UnwindSafe,
    V: UnwindSafe,
    S: UnwindSafe,

impl<T, F: UnwindSafe> UnwindSafe for LazyLock<T, F>where
    OnceLock<T>: UnwindSafe,

impl UnwindSafe for Once

impl<T: UnwindSafe> UnwindSafe for OnceLock<T>

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<T> UnwindSafe for BinaryHeap<T>where
    T: UnwindSafe,

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

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

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

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

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

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

impl<'a, K, V, A = Global> !UnwindSafe for Entry<'a, K, V, A>

impl<'a, K, V, A = Global> !UnwindSafe for VacantEntry<'a, K, V, A>

impl<'a, K, V, A = Global> !UnwindSafe for OccupiedEntry<'a, K, V, A>

impl<'a, K, V, A = Global> !UnwindSafe for OccupiedError<'a, K, V, A>

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

impl<'a, K, V> !UnwindSafe for IterMut<'a, K, V>

impl<K, V, A> UnwindSafe for IntoIter<K, V, A>where
    A: UnwindSafe,
    K: RefUnwindSafe,
    V: RefUnwindSafe,

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

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

impl<'a, K, V> !UnwindSafe for ValuesMut<'a, K, V>

impl<K, V, A> UnwindSafe for IntoKeys<K, V, A>where
    A: UnwindSafe,
    K: RefUnwindSafe,
    V: RefUnwindSafe,

impl<K, V, A> UnwindSafe for IntoValues<K, V, A>where
    A: UnwindSafe,
    K: RefUnwindSafe,
    V: RefUnwindSafe,

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

impl<'a, K, V> !UnwindSafe for RangeMut<'a, K, V>

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

impl<T, A> UnwindSafe for BTreeSet<T, A>where
    A: UnwindSafe,
    T: RefUnwindSafe,

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

impl<T> !UnwindSafe for Weak<T>

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

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

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

impl<'a, I, A> UnwindSafe for Splice<'a, I, A>where
    A: RefUnwindSafe,
    I: UnwindSafe,
    <I as Iterator>::Item: RefUnwindSafe,

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 Level

impl UnwindSafe for Span

impl UnwindSafe for Group

impl UnwindSafe for Punct

impl UnwindSafe for Ident

impl<T> UnwindSafe for LocalKey<T>

impl<'scope, 'env> !UnwindSafe for Scope<'scope, 'env>

impl<'scope, T> !UnwindSafe for ScopedJoinHandle<'scope, T>

impl<T> !UnwindSafe for JoinHandle<T>

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

impl<'a, K, V> !UnwindSafe for IterMut<'a, K, V>

impl<K, V> UnwindSafe for IntoIter<K, V>where
    K: UnwindSafe + RefUnwindSafe,
    V: UnwindSafe + RefUnwindSafe,

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

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

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

impl<'a, K, V, F> !UnwindSafe for DrainFilter<'a, K, V, F>

impl<'a, K, V> !UnwindSafe for ValuesMut<'a, K, V>

impl<K, V> UnwindSafe for IntoKeys<K, V>where
    K: UnwindSafe + RefUnwindSafe,
    V: UnwindSafe + RefUnwindSafe,

impl<K, V> UnwindSafe for IntoValues<K, V>where
    K: UnwindSafe + RefUnwindSafe,
    V: UnwindSafe + RefUnwindSafe,

impl<'a, K, V, S> !UnwindSafe for RawEntryBuilderMut<'a, K, V, S>

impl<'a, K, V, S> !UnwindSafe for RawEntryMut<'a, K, V, S>

impl<'a, K, V, S> !UnwindSafe for RawOccupiedEntryMut<'a, K, V, S>

impl<'a, K, V, S> !UnwindSafe for RawVacantEntryMut<'a, K, V, S>

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

impl<'a, K, V> !UnwindSafe for Entry<'a, K, V>

impl<'a, K, V> !UnwindSafe for OccupiedEntry<'a, K, V>

impl<'a, K, V> !UnwindSafe for VacantEntry<'a, K, V>

impl<'a, K, V> !UnwindSafe for OccupiedError<'a, K, V>

impl<T, S> UnwindSafe for HashSet<T, S>where
    S: UnwindSafe,
    T: UnwindSafe,

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

impl<K> UnwindSafe for IntoIter<K>where
    K: UnwindSafe + RefUnwindSafe,

impl<'a, K> UnwindSafe for Drain<'a, K>where
    K: UnwindSafe + RefUnwindSafe,

impl<'a, K, F> !UnwindSafe for DrainFilter<'a, K, F>

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

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

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

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

impl UnwindSafe for Vars

impl<'a> UnwindSafe for SplitPaths<'a>

impl UnwindSafe for Args

impl<E> UnwindSafe for Report<E>where
    E: UnwindSafe,

impl UnwindSafe for OsStr

impl UnwindSafe for File

impl<R> UnwindSafe for BufReader<R>where
    R: UnwindSafe,

impl<W> UnwindSafe for BufWriter<W>where
    W: UnwindSafe,

impl<W> UnwindSafe for LineWriter<W>where
    W: UnwindSafe,

impl<W> !UnwindSafe for IntoInnerError<W>

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

impl !UnwindSafe for Error

impl<'data> !UnwindSafe for BorrowedBuf<'data>

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

impl UnwindSafe for Stdin

impl<'a> UnwindSafe for StdinLock<'a>

impl<'a> UnwindSafe for StdoutLock<'a>

impl<'a> UnwindSafe for StderrLock<'a>

impl UnwindSafe for Empty

impl UnwindSafe for Sink

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

impl<'a> UnwindSafe for IoSlice<'a>

impl<T, U> UnwindSafe for Chain<T, U>where
    T: UnwindSafe,
    U: UnwindSafe,

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

impl<R> UnwindSafe for Bytes<R>where
    R: UnwindSafe,

impl<B> UnwindSafe for Split<B>where
    B: UnwindSafe,

impl<B> UnwindSafe for Lines<B>where
    B: UnwindSafe,

impl<'a> UnwindSafe for Incoming<'a>

impl<'a> UnwindSafe for ScmRights<'a>

impl<'a> UnwindSafe for ScmCredentials<'a>

impl<'a> UnwindSafe for AncillaryData<'a>

impl<'a> UnwindSafe for Messages<'a>

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

impl<'a> UnwindSafe for Incoming<'a>

impl UnwindSafe for UCred

impl UnwindSafe for PidFd

impl UnwindSafe for stat

impl<'a> UnwindSafe for EncodeWide<'a>

impl<'handle> UnwindSafe for BorrowedHandle<'handle>

impl<'socket> UnwindSafe for BorrowedSocket<'socket>

impl<'fd> UnwindSafe for BorrowedFd<'fd>

impl<'a> UnwindSafe for Prefix<'a>

impl<'a> UnwindSafe for PrefixComponent<'a>

impl<'a> UnwindSafe for Component<'a>

impl<'a> UnwindSafe for Components<'a>

impl<'a> UnwindSafe for Iter<'a>

impl<'a> UnwindSafe for Ancestors<'a>

impl UnwindSafe for Path

impl<'a> UnwindSafe for Display<'a>

impl<'a> UnwindSafe for CommandEnvs<'a>

impl UnwindSafe for Child

impl<'a> UnwindSafe for CommandArgs<'a>

impl UnwindSafe for Stdio

impl<T> UnwindSafe for Receiver<T>

impl<'a, T> UnwindSafe for Iter<'a, T>

impl<'a, T> UnwindSafe for TryIter<'a, T>

impl<T> UnwindSafe for IntoIter<T>

impl<T> UnwindSafe for Sender<T>

impl<T> UnwindSafe for SyncSender<T>

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

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

impl<'a, T: ?Sized> UnwindSafe for MutexGuard<'a, T>

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

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

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

impl<'a, T: ?Sized> UnwindSafe for RwLockWriteGuard<'a, T>

impl<Ret, T> UnwindSafe for fn (T₁, T₂, …, Tₙ) -> Ret

impl UnwindSafe for ()

impl<T> UnwindSafe for (T₁, T₂, …, Tₙ)where
    T: UnwindSafe,

impl UnwindSafe for f64

impl UnwindSafe for f32

impl !UnwindSafe for TestFn