Trait 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 turn 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§

1.9.0 · Source§

impl UnwindSafe for Stderr

1.9.0 · Source§

impl UnwindSafe for StderrLock<'_>

1.9.0 · Source§

impl UnwindSafe for Stdout

1.9.0 · Source§

impl UnwindSafe for StdoutLock<'_>

1.9.0 · Source§

impl UnwindSafe for Condvar

1.59.0 · Source§

impl UnwindSafe for Once

Source§

impl UnwindSafe for CancellationToken

Source§

impl UnwindSafe for Runtime

Source§

impl UnwindSafe for AbortHandle

Source§

impl UnwindSafe for Notify

1.64.0 · Source§

impl<K, V, A> UnwindSafe for BTreeMap<K, V, A>

1.36.0 · Source§

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

1.9.0 · Source§

impl<T> !UnwindSafe for &mut T
where T: ?Sized,

1.9.0 · Source§

impl<T> UnwindSafe for *const T
where T: RefUnwindSafe + ?Sized,

1.9.0 · Source§

impl<T> UnwindSafe for *mut T
where T: RefUnwindSafe + ?Sized,

1.9.0 · Source§

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

1.28.0 · Source§

impl<T> UnwindSafe for NonZero<T>

1.25.0 · Source§

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

Source§

impl<T> UnwindSafe for Receiver<T>

Source§

impl<T> UnwindSafe for Sender<T>

1.70.0 · Source§

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

1.9.0 · Source§

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

1.9.0 · Source§

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

Source§

impl<T> UnwindSafe for ReentrantLock<T>
where T: UnwindSafe + ?Sized,

Source§

impl<T> UnwindSafe for JoinHandle<T>

1.9.0 · Source§

impl<T> UnwindSafe for AssertUnwindSafe<T>

1.9.0 · Source§

impl<T, A> UnwindSafe for Rc<T, A>

1.9.0 · Source§

impl<T, A> UnwindSafe for Arc<T, A>

1.80.0 · Source§

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

Auto implementors§

§

impl !UnwindSafe for rusty_postgres::io::Error

§

impl !UnwindSafe for rusty_postgres::Error

§

impl UnwindSafe for ErrorKind

§

impl UnwindSafe for SeekFrom

§

impl UnwindSafe for BacktraceStyle

§

impl UnwindSafe for Empty

§

impl UnwindSafe for PipeReader

§

impl UnwindSafe for PipeWriter

§

impl UnwindSafe for Repeat

§

impl UnwindSafe for Sink

§

impl UnwindSafe for Stdin

§

impl UnwindSafe for WriterPanicked

§

impl UnwindSafe for AUTO

§

impl UnwindSafe for BIGINT

§

impl UnwindSafe for CUID

§

impl UnwindSafe for UUID

§

impl UnwindSafe for BOOL

§

impl UnwindSafe for CUSTOME

§

impl UnwindSafe for FLOAT

§

impl UnwindSafe for INDEX

§

impl UnwindSafe for JSON

§

impl UnwindSafe for NOTNULL

§

impl UnwindSafe for NOW

§

impl UnwindSafe for NUMBER

§

impl UnwindSafe for PRIMARY

§

impl UnwindSafe for STRING

§

impl UnwindSafe for UNIQUE

§

impl UnwindSafe for Alphanumeric

§

impl UnwindSafe for Client

§

impl UnwindSafe for DirBuilder

§

impl UnwindSafe for File

§

impl UnwindSafe for NaiveDate

§

impl UnwindSafe for NaiveDateTime

§

impl UnwindSafe for NaiveTime

§

impl UnwindSafe for NoTls

§

impl UnwindSafe for Socket

§

impl UnwindSafe for Uuid

§

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

§

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

§

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

§

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

§

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

§

impl<'a> UnwindSafe for Location<'a>

§

impl<'billionaire> UnwindSafe for OneToMany<'billionaire>

§

impl<'billionaire> UnwindSafe for OneToOne<'billionaire>

§

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

§

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

§

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

§

impl<Billionaire> UnwindSafe for DEFAULT<Billionaire>
where Billionaire: UnwindSafe,

§

impl<Billionaire> UnwindSafe for Date<Billionaire>
where Billionaire: UnwindSafe,

§

impl<Billionaire> UnwindSafe for DateTime<Billionaire>
where Billionaire: UnwindSafe,

§

impl<Billionaire> UnwindSafe for ID<Billionaire>
where Billionaire: UnwindSafe,

§

impl<Billionaire> UnwindSafe for Time<Billionaire>
where Billionaire: UnwindSafe,

§

impl<R> UnwindSafe for BufReader<R>
where R: UnwindSafe + ?Sized,

§

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

§

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

§

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

§

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

§

impl<W> !UnwindSafe for IntoInnerError<W>

§

impl<W> UnwindSafe for BufWriter<W>
where W: UnwindSafe + ?Sized,

§

impl<W> UnwindSafe for LineWriter<W>
where W: UnwindSafe + ?Sized,