pub struct Catch<'a, T>(/* private fields */);Expand description
A Catch<T> is a Deref/DerefMut wrapper around a T and a F: FnOnce(T)
which calls that function on T in place of drop.
This allows you to provide an owned static value with no indirection or locking to somewhere that needs one, and recover the value automatically by a means of your choosing afterwards.
Implementations§
Source§impl<'a, T: Send + 'a> Catch<'a, T>
impl<'a, T: Send + 'a> Catch<'a, T>
Sourcepub fn new(this: T, catch: Box<dyn FnOnce(T) + Send + 'a>) -> Self
pub fn new(this: T, catch: Box<dyn FnOnce(T) + Send + 'a>) -> Self
Create a new Catch, which will call the function catch on this when it is dropped.
Sourcepub fn disarm(Catch: Self) -> (T, Box<dyn FnOnce(T) + 'a>)
pub fn disarm(Catch: Self) -> (T, Box<dyn FnOnce(T) + 'a>)
Disarm a Catch by unwrapping it into its original T and catch: impl FnOnce(T). If
you drop the T now, it will be gone forever – but if you wrap the two together again
using Catch::new it will again be caught on drop.
You might use this function if you need owned access to the T itself, but beware: if
you’re expecting that the catch function is always called, you need to manually ensure
that the Catch is re-assembled or the function called on every code path you care about.
Sourcepub fn sync(this: T) -> (Self, impl Fn() -> Option<T>)
pub fn sync(this: T) -> (Self, impl Fn() -> Option<T>)
Convenience method for the common case when you want to use Catch in a synchronous
context, using an internal mutex to hold the caught item.
The returned closure will yield None until the Catch is dropped, and then will yield
Some(T) exactly once, before yielding None again forever.
Sourcepub fn future(this: T) -> (Self, impl Future<Output = Option<T>> + Send + 'a)
pub fn future(this: T) -> (Self, impl Future<Output = Option<T>> + Send + 'a)
Convenience method for the common case when you want to use Catch in an asynchronous
context, using an internal tokio::sync::oneshot to hold the caught item.
The returned future will pend until this is dropped, whereupon it will immediately yield
Some(T). If the Catch is disarmed and the catch function dropped, the future will
yield None.
Trait Implementations§
Source§impl<'a, T: Ord> Ord for Catch<'a, T>
impl<'a, T: Ord> Ord for Catch<'a, T>
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<'a, T: PartialOrd> PartialOrd for Catch<'a, T>
impl<'a, T: PartialOrd> PartialOrd for Catch<'a, T>
impl<'a, T: Eq> Eq for Catch<'a, T>
impl<'a, T> StructuralPartialEq for Catch<'a, T>
Auto Trait Implementations§
impl<'a, T> Freeze for Catch<'a, T>where
T: Freeze,
impl<'a, T> !RefUnwindSafe for Catch<'a, T>
impl<'a, T> Send for Catch<'a, T>where
T: Send,
impl<'a, T> !Sync for Catch<'a, T>
impl<'a, T> Unpin for Catch<'a, T>
impl<'a, T> UnsafeUnpin for Catch<'a, T>where
T: UnsafeUnpin,
impl<'a, T> !UnwindSafe for Catch<'a, T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn map<U, F>(self, f: F) -> Map<Self, F>
fn map<U, F>(self, f: F) -> Map<Self, F>
Source§fn map_into<U>(self) -> MapInto<Self, U>
fn map_into<U>(self) -> MapInto<Self, U>
Source§fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>
fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>
f. Read moreSource§fn left_future<B>(self) -> Either<Self, B>
fn left_future<B>(self) -> Either<Self, B>
Source§fn right_future<A>(self) -> Either<A, Self>
fn right_future<A>(self) -> Either<A, Self>
Source§fn into_stream(self) -> IntoStream<Self>where
Self: Sized,
fn into_stream(self) -> IntoStream<Self>where
Self: Sized,
Source§fn flatten(self) -> Flatten<Self>
fn flatten(self) -> Flatten<Self>
Source§fn flatten_stream(self) -> FlattenStream<Self>
fn flatten_stream(self) -> FlattenStream<Self>
Source§fn fuse(self) -> Fuse<Self>where
Self: Sized,
fn fuse(self) -> Fuse<Self>where
Self: Sized,
poll will never again be called once it has
completed. This method can be used to turn any Future into a
FusedFuture. Read moreSource§fn inspect<F>(self, f: F) -> Inspect<Self, F>
fn inspect<F>(self, f: F) -> Inspect<Self, F>
Source§fn catch_unwind(self) -> CatchUnwind<Self>where
Self: Sized + UnwindSafe,
fn catch_unwind(self) -> CatchUnwind<Self>where
Self: Sized + UnwindSafe,
Source§fn remote_handle(self) -> (Remote<Self>, RemoteHandle<Self::Output>)where
Self: Sized,
fn remote_handle(self) -> (Remote<Self>, RemoteHandle<Self::Output>)where
Self: Sized,
() on completion and sends
its output to another future on a separate task. Read moreSource§fn boxed<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>
fn boxed<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>
Source§fn boxed_local<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + 'a>>where
Self: Sized + 'a,
fn boxed_local<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + 'a>>where
Self: Sized + 'a,
Source§fn unit_error(self) -> UnitError<Self>where
Self: Sized,
fn unit_error(self) -> UnitError<Self>where
Self: Sized,
Future<Output = T> into a
TryFuture<Ok = T, Error = ()>.Source§fn never_error(self) -> NeverError<Self>where
Self: Sized,
fn never_error(self) -> NeverError<Self>where
Self: Sized,
Future<Output = T> into a
TryFuture<Ok = T, Error = Never>.