Struct std::panic::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;
}));
// ...
RunWrapping 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;
})
};
// ...
RunTuple Fields
0: T
Trait Implementations
sourceimpl<S> AsyncIterator for AssertUnwindSafe<S> where
S: AsyncIterator,
impl<S> AsyncIterator for AssertUnwindSafe<S> where
S: AsyncIterator,
sourcefn poll_next(
self: Pin<&mut AssertUnwindSafe<S>>,
cx: &mut Context<'_>
) -> Poll<Option<<S as AsyncIterator>::Item>>
fn poll_next(
self: Pin<&mut AssertUnwindSafe<S>>,
cx: &mut Context<'_>
) -> Poll<Option<<S as AsyncIterator>::Item>>
Attempt 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
1.16.0 · sourceimpl<T> Debug for AssertUnwindSafe<T> where
T: Debug,
impl<T> Debug for AssertUnwindSafe<T> where
T: Debug,
1.62.0 · sourceimpl<T> Default for AssertUnwindSafe<T> where
T: Default,
impl<T> Default for AssertUnwindSafe<T> where
T: Default,
sourcefn default() -> AssertUnwindSafe<T>ⓘNotable traits for AssertUnwindSafe<F>impl<F> Future for AssertUnwindSafe<F> where
F: Future, type Output = <F as Future>::Output;
fn default() -> AssertUnwindSafe<T>ⓘNotable traits for AssertUnwindSafe<F>impl<F> Future for AssertUnwindSafe<F> where
F: Future, type Output = <F as Future>::Output;
F: Future, type Output = <F as Future>::Output;
Returns the “default value” for a type. Read more
sourceimpl<T> Deref for AssertUnwindSafe<T>
impl<T> Deref for AssertUnwindSafe<T>
sourceimpl<T> DerefMut for AssertUnwindSafe<T>
impl<T> DerefMut for AssertUnwindSafe<T>
sourceimpl<R, F> FnOnce() for AssertUnwindSafe<F> where
F: FnOnce() -> R,
impl<R, F> FnOnce() for AssertUnwindSafe<F> where
F: FnOnce() -> R,
1.36.0 · sourceimpl<F> Future for AssertUnwindSafe<F> where
F: Future,
impl<F> Future for AssertUnwindSafe<F> where
F: Future,
sourcefn poll(
self: Pin<&mut AssertUnwindSafe<F>>,
cx: &mut Context<'_>
) -> Poll<<AssertUnwindSafe<F> as Future>::Output>
fn poll(
self: Pin<&mut AssertUnwindSafe<F>>,
cx: &mut Context<'_>
) -> Poll<<AssertUnwindSafe<F> as Future>::Output>
Attempt to resolve the future to a final value, registering the current task for wakeup if the value is not yet available. Read more
impl<T> RefUnwindSafe for AssertUnwindSafe<T>
impl<T> UnwindSafe for AssertUnwindSafe<T>
Auto Trait Implementations
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,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<F> IntoFuture for F where
F: Future,
impl<F> IntoFuture for F where
F: Future,
type IntoFuture = F
type IntoFuture = F
Which kind of future are we turning this into?
sourcefn into_future(self) -> <F as IntoFuture>::IntoFuture
fn into_future(self) -> <F as IntoFuture>::IntoFuture
Creates a future from a value. Read more