Struct std::panic::AssertUnwindSafe 1.9.0[−][src]
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
may 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; })); // ...Run
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; }) }; // ...Run
Trait Implementations
impl<T> UnwindSafe for AssertUnwindSafe<T>
[src]
impl<T> UnwindSafe for AssertUnwindSafe<T>
impl<T> RefUnwindSafe for AssertUnwindSafe<T>
[src]
impl<T> RefUnwindSafe for AssertUnwindSafe<T>
impl<T> Deref for AssertUnwindSafe<T>
[src]
impl<T> Deref for AssertUnwindSafe<T>
type Target = T
The resulting type after dereferencing.
ⓘImportant traits for &'a mut Ifn deref(&self) -> &T
[src]
fn deref(&self) -> &T
Dereferences the value.
impl<T> DerefMut for AssertUnwindSafe<T>
[src]
impl<T> DerefMut for AssertUnwindSafe<T>
ⓘImportant traits for &'a mut Ifn deref_mut(&mut self) -> &mut T
[src]
fn deref_mut(&mut self) -> &mut T
Mutably dereferences the value.
impl<R, F: FnOnce() -> R> FnOnce<()> for AssertUnwindSafe<F>
[src]
impl<R, F: FnOnce() -> R> FnOnce<()> for AssertUnwindSafe<F>
type Output = R
The returned type after the call operator is used.
extern "rust-call" fn call_once(self, _args: ()) -> R
[src]
extern "rust-call" fn call_once(self, _args: ()) -> R
Performs the call operation.
impl<T: Debug> Debug for AssertUnwindSafe<T>
1.16.0[src]
impl<T: Debug> Debug for AssertUnwindSafe<T>
fn fmt(&self, f: &mut Formatter) -> Result
[src]
fn fmt(&self, f: &mut Formatter) -> Result
Formats the value using the given formatter. Read more
impl<'a, F: Future> Future for AssertUnwindSafe<F>
[src]
impl<'a, F: Future> Future for AssertUnwindSafe<F>
type Output = F::Output
🔬 This is a nightly-only experimental API. (futures_api
#50547)
futures in libcore are unstable
The result of the Future
.
fn poll(self: PinMut<Self>, cx: &mut Context) -> Poll<Self::Output>
[src]
fn poll(self: PinMut<Self>, cx: &mut Context) -> Poll<Self::Output>
🔬 This is a nightly-only experimental API. (futures_api
#50547)
futures in libcore are unstable
Attempt to resolve the future to a final value, registering the current task for wakeup if the value is not yet available. Read more
Auto Trait Implementations
impl<T> Send for AssertUnwindSafe<T> where
T: Send,
impl<T> Send for AssertUnwindSafe<T> where
T: Send,
impl<T> Sync for AssertUnwindSafe<T> where
T: Sync,
impl<T> Sync for AssertUnwindSafe<T> where
T: Sync,