Trait sgx_tstd::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<K, V, S, A> UnwindSafe for HashMap<K, V, S, A>where
    A: UnwindSafe,
    K: UnwindSafe,
    S: UnwindSafe,
    V: UnwindSafe,

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: UnwindSafe + RefUnwindSafe,
    V: UnwindSafe + RefUnwindSafe,

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

impl<K, V, A> UnwindSafe for IntoValues<K, V, A>where
    A: UnwindSafe,
    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, A> UnwindSafe for Drain<'a, K, V, A>where
    A: UnwindSafe + RefUnwindSafe,
    K: UnwindSafe + RefUnwindSafe,
    V: UnwindSafe + RefUnwindSafe,

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

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

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

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

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

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

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

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

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

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

impl<'a, 'b, K, Q, V, S, A = Global> !UnwindSafe for EntryRef<'a, 'b, K, Q, V, S, A>

impl<'a, 'b, K, Q, V, S, A = Global> !UnwindSafe for OccupiedEntryRef<'a, 'b, K, Q, V, S, A>

impl<'a, 'b, K, Q, V, S, A = Global> !UnwindSafe for VacantEntryRef<'a, 'b, K, Q, V, S, A>

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

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

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

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

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

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

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

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

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

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

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

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

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

impl UnwindSafe for Nonce

impl<A> UnwindSafe for AesCcm<A>where
    A: UnwindSafe,

impl<A> UnwindSafe for Aad<A>where
    A: UnwindSafe,

impl UnwindSafe for Nonce

impl<A> UnwindSafe for AesGcm<A>where
    A: UnwindSafe,

impl<A> UnwindSafe for Aad<A>where
    A: UnwindSafe,

impl UnwindSafe for Nonce

impl UnwindSafe for HMac

impl UnwindSafe for Sha1

impl UnwindSafe for Sm3

impl UnwindSafe for Nonce

impl<A> UnwindSafe for Sm4Ccm<A>where
    A: UnwindSafe,

impl<A> UnwindSafe for Aad<A>where
    A: UnwindSafe,

impl UnwindSafe for Nonce

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

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

impl<T> UnwindSafe for MapObject<T>where
    T: RefUnwindSafe,

impl UnwindSafe for Zero