Skip to main content

AssertUnwindSafe

Struct AssertUnwindSafe 

1.9.0 ยท Source
pub struct AssertUnwindSafe<T>(pub T);
Expand description

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;
    })
};
// ...

Tuple Fieldsยง

ยง0: T

Trait Implementationsยง

Sourceยง

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

Sourceยง

type Item = <S as AsyncIterator>::Item

๐Ÿ”ฌThis is a nightly-only experimental API. (async_iterator #79024)
The type of items yielded by the async iterator.
Sourceยง

fn poll_next( self: Pin<&mut AssertUnwindSafe<S>>, cx: &mut Context<'_>, ) -> Poll<Option<<S as AsyncIterator>::Item>>

๐Ÿ”ฌThis is a nightly-only experimental API. (async_iterator #79024)
Attempts to pull out the next value of this async iterator, registering the current task for wakeup if the value is not yet available, and returning None if the async iterator is exhausted. Read more
Sourceยง

fn size_hint(&self) -> (usize, Option<usize>)

๐Ÿ”ฌThis is a nightly-only experimental API. (async_iterator #79024)
Returns the bounds on the remaining length of the async iterator. Read more
1.16.0 ยท Sourceยง

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

Sourceยง

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

Formats the value using the given formatter. Read more
1.62.0 ยท Sourceยง

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

Sourceยง

fn default() -> AssertUnwindSafe<T> โ“˜

Returns the โ€œdefault valueโ€ for a type. Read more
1.9.0 (const: unstable) ยท Sourceยง

impl<T> Deref for AssertUnwindSafe<T>

Sourceยง

type Target = T

The resulting type after dereferencing.
Sourceยง

fn deref(&self) -> &T

Dereferences the value.
1.9.0 (const: unstable) ยท Sourceยง

impl<T> DerefMut for AssertUnwindSafe<T>

Sourceยง

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

Mutably dereferences the value.
1.9.0 ยท Sourceยง

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

Sourceยง

type Output = R

The returned type after the call operator is used.
Sourceยง

extern "rust-call" fn call_once(self, _args: ()) -> R

๐Ÿ”ฌThis is a nightly-only experimental API. (fn_traits #29625)
Performs the call operation.
1.96.0 ยท Sourceยง

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

If a valueโ€™s type is already UnwindSafe, wrapping it in AssertUnwindSafe is never incorrect.

Sourceยง

fn from(value: T) -> AssertUnwindSafe<T> โ“˜

Converts to this type from the input type.
1.36.0 ยท Sourceยง

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

Sourceยง

type Output = <F as Future>::Output

The type of value produced on completion.
Sourceยง

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

Attempts to resolve the future to a final value, registering the current task for wakeup if the value is not yet available. Read more
1.9.0 ยท Sourceยง

impl<T> RefUnwindSafe for AssertUnwindSafe<T>

1.9.0 ยท Sourceยง

impl<T> UnwindSafe for AssertUnwindSafe<T>

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ยง

Sourceยง

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

Sourceยง

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Sourceยง

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

Sourceยง

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Sourceยง

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

Sourceยง

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

Mutably borrows from an owned value. Read more
Sourceยง

impl<T> From<!> for T

Sourceยง

fn from(t: !) -> T

Converts to this type from the input type.
Sourceยง

impl<T> From<T> for T

Sourceยง

fn from(t: T) -> T

Returns the argument unchanged.

Sourceยง

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

Sourceยง

fn into(self) -> U

Calls U::from(self).

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

Sourceยง

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

Sourceยง

type Item = <I as AsyncIterator>::Item

๐Ÿ”ฌThis is a nightly-only experimental API. (async_iterator #79024)
The type of the item yielded by the iterator
Sourceยง

type IntoAsyncIter = I

๐Ÿ”ฌThis is a nightly-only experimental API. (async_iterator #79024)
The type of the resulting iterator
Sourceยง

fn into_async_iter(self) -> <I as IntoAsyncIterator>::IntoAsyncIter

๐Ÿ”ฌThis is a nightly-only experimental API. (async_iterator #79024)
Converts self into an async iterator
Sourceยง

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

Sourceยง

type Output = <F as Future>::Output

The output that the future will produce on completion.
Sourceยง

type IntoFuture = F

Which kind of future are we turning this into?
Sourceยง

fn into_future(self) -> <F as IntoFuture>::IntoFuture

Creates a future from a value. Read more
Sourceยง

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

Sourceยง

type Target = T

๐Ÿ”ฌThis is a nightly-only experimental API. (arbitrary_self_types #44874)
The target type on which the method may be called.
Sourceยง

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

Sourceยง

type Error = Infallible

The type returned in the event of a conversion error.
Sourceยง

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

Performs the conversion.
Sourceยง

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

Sourceยง

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Sourceยง

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

Performs the conversion.