Struct AssertUnwindSafe

pub struct AssertUnwindSafe<T>(pub T);

A simple wrapper around a type to assert that it is unwind safe.

When using catch_unwind it may be the case that some of the closed over variables are not unwind safe. For example if &mut T is captured the compiler will generate a warning indicating that it is not unwind safe. It might not be the case, however, that this is actually a problem due to the specific usage of catch_unwind if unwind safety is specifically taken into account. This wrapper struct is useful for a quick and lightweight annotation that a variable is indeed unwind safe.

Examples

One way to use AssertUnwindSafe is to assert that the entire closure itself is unwind safe, bypassing all checks for all variables:

use std::panic::{self, AssertUnwindSafe};

let mut variable = 4;

// This code will not compile because the closure captures `&mut variable`
// which is not considered unwind safe by default.

// panic::catch_unwind(|| {
//     variable += 3;
// });

// This, however, will compile due to the `AssertUnwindSafe` wrapper
let result = panic::catch_unwind(AssertUnwindSafe(|| {
    variable += 3;
}));
// ...

Wrapping the entire closure amounts to a blanket assertion that all captured variables are unwind safe. This has the downside that if new captures are added in the future, they will also be considered unwind safe. Therefore, you may prefer to just wrap individual captures, as shown below. This is more annotation, but it ensures that if a new capture is added which is not unwind safe, you will get a compilation error at that time, which will allow you to consider whether that new capture in fact represent a bug or not.

use std::panic::{self, AssertUnwindSafe};

let mut variable = 4;
let other_capture = 3;

let result = {
    let mut wrapper = AssertUnwindSafe(&mut variable);
    panic::catch_unwind(move || {
        **wrapper += other_capture;
    })
};
// ...

Fields

0: T

Trait Implementations

impl<F: Future> Future for AssertUnwindSafe<F>

type Output = <F as Future>::Output;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>

impl<R, F: FnOnce() -> R> FnOnce() for AssertUnwindSafe<F>

type Output = R;
extern ""rust-call"" fn call_once(self, _args: ()) -> R

impl<S: AsyncIterator> AsyncIterator for AssertUnwindSafe<S>

type Item = <S as AsyncIterator>::Item;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<S::Item>>
fn size_hint(&self) -> (usize, Option<usize>)

impl<T> Deref for AssertUnwindSafe<T>

type Target = T;
fn deref(&self) -> &T

impl<T> DerefMut for AssertUnwindSafe<T>

fn deref_mut(&mut self) -> &mut T

impl<T> From<T> for AssertUnwindSafe<T> where T: UnwindSafe,

fn from(value: T) -> Self

impl<T> RefUnwindSafe for AssertUnwindSafe<T>

impl<T> UnwindSafe for AssertUnwindSafe<T>

impl<T: Debug> Debug for AssertUnwindSafe<T>

fn fmt(&self, f: &mut Formatter<'_>) -> Result

impl<T: Default> Default for AssertUnwindSafe<T>

fn default() -> Self

Auto Trait Implementations

impl<T> Freeze for AssertUnwindSafe<T> where T: Freeze,

impl<T> Send for AssertUnwindSafe<T> where T: Send,

impl<T> Sync for AssertUnwindSafe<T> where T: Sync,

impl<T> Unpin for AssertUnwindSafe<T> where T: Unpin,

impl<T> UnsafeUnpin for AssertUnwindSafe<T> where T: UnsafeUnpin,

Blanket Implementations

impl<F> IntoFuture for AssertUnwindSafe<T> where F: Future,

type Output = <F as Future>::Output;
type IntoFuture = F;
fn into_future(self) -> <F as IntoFuture>::IntoFuture

impl<I> IntoAsyncIterator for AssertUnwindSafe<T> where I: AsyncIterator,

type Item = <I as AsyncIterator>::Item;
type IntoAsyncIter = I;
fn into_async_iter(self) -> <I as IntoAsyncIterator>::IntoAsyncIter

impl<P, T> Receiver for AssertUnwindSafe<T> where P: Deref<Target = T> + ?Sized, T: ?Sized,

type Target = T;

impl<T> Any for AssertUnwindSafe<T> where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for AssertUnwindSafe<T> where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for AssertUnwindSafe<T> where T: ?Sized,

fn borrow_mut(&mut self) -> &mut T

impl<T> From<T> for AssertUnwindSafe<T>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> From<never> for AssertUnwindSafe<T>

fn from(t: never) -> T

impl<T> SizeHint for AssertUnwindSafe<T> where T: ?Sized,

fn lower_bound(&self) -> usize
fn upper_bound(&self) -> Option<usize>

impl<T> SizedTypeProperties for AssertUnwindSafe<T>

impl<T, U> Into<U> for AssertUnwindSafe<T> where U: From<T>,

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom<U> for AssertUnwindSafe<T> where U: Into<T>,

type Error = Infallible;
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto<U> for AssertUnwindSafe<T> where U: TryFrom<T>,

type Error = <U as TryFrom<T>>::Error;
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>