pub struct ThreadFuture<T, F, C>where
C: CancellationToken + Send + 'static,{ /* private fields */ }Expand description
Create a future that wraps a thread. The thread is lazily created. The thread is not (and cannot be) terminated when this structure is dropped. The thread must behave nicely and check the cancellation token given to see if it should terminate.
Cancellation safety: If the thread is still running when this future is dropped, the thread will not be forcefully terminated, but the cancellation token will be set so that the thread can check it and terminate early if it wants to. If you want to allow the thread to continue running without setting the cancellation token, you can call Self::detach_on_drop or Self::detach_on_drop_ref to prevent the cancellation token from being set on drop.
Implementations§
Source§impl<T, F> ThreadFuture<T, F, SimpleCancellationToken>
impl<T, F> ThreadFuture<T, F, SimpleCancellationToken>
Sourcepub fn new(work: F) -> Self
pub fn new(work: F) -> Self
Create a new future-tracked thread using the work function given.
The thread will be lazily spawned on the first poll of this future.
The default SimpleCancellationToken will be provided to the thread work function. Check this token to see if the thread should exit.
See Self::new_eager for eagerly spawning the thread with the default SimpleCancellationToken. See Self::new_with_cancellation for providing a custom cancellation token.
Sourcepub fn new_eager(work: F) -> Self
pub fn new_eager(work: F) -> Self
Create a new future-tracked thread using the work function given.
The thread will be eagerly spawned during the call to this function.
See Self::new for lazily spawning the thread with the default SimpleCancellationToken. See Self::new_eager_with_cancellation for providing a custom cancellation token.
Source§impl<T, F, C> ThreadFuture<T, F, C>
impl<T, F, C> ThreadFuture<T, F, C>
Sourcepub fn new_with_cancellation(work: F, cancellation_token: C) -> Self
pub fn new_with_cancellation(work: F, cancellation_token: C) -> Self
Create a new future-tracked thread using the work function given.
The thread will be lazily spawned on the first poll of this future.
Provide a custom cancellation token that implements CancellationToken to share with the thread. The thread can check this token to see if it should exit.
See Self::new_eager for eagerly spawning the thread with the default SimpleCancellationToken. See Self::new_eager_with_cancellation for providing a custom cancellation token.
Sourcepub fn new_eager_with_cancellation(work: F, cancellation_token: C) -> Self
pub fn new_eager_with_cancellation(work: F, cancellation_token: C) -> Self
Create a new future-tracked thread using the work function given.
The thread will be eagerly spawned during the call to this function.
See Self::new for lazily spawning the thread with the default SimpleCancellationToken. See Self::new_with_cancellation for providing a custom cancellation token.
Sourcepub fn detach_on_drop(self) -> Self
pub fn detach_on_drop(self) -> Self
When called, will instruct the wrapper to not to activate the cancellation token when dropped.
Sourcepub fn detach_on_drop_ref(&mut self)
pub fn detach_on_drop_ref(&mut self)
Same as Self::detach_on_drop, but can be called on a mutable reference to the future instead of consuming it.
Sourcepub fn is_cancel_on_drop(&self) -> bool
pub fn is_cancel_on_drop(&self) -> bool
Check if the cancellation token will be activated when this future is dropped.
Sourcepub fn cancellation_token(&self) -> &C
pub fn cancellation_token(&self) -> &C
Get a reference to the cancellation token internally stored.
Trait Implementations§
Source§impl<T, F, C> Drop for ThreadFuture<T, F, C>where
C: CancellationToken + Send + 'static,
impl<T, F, C> Drop for ThreadFuture<T, F, C>where
C: CancellationToken + Send + 'static,
Source§impl<T, F, C> FusedFuture for ThreadFuture<T, F, C>
We know when the future is terminated when the thread
has completed, since we will never poll again after that.
impl<T, F, C> FusedFuture for ThreadFuture<T, F, C>
We know when the future is terminated when the thread has completed, since we will never poll again after that.
The future is not terminated while the thread is still running. This means, even if you activate the cancellation token but the thread has not exited yet, the future is still not terminated.
Source§fn is_terminated(&self) -> bool
fn is_terminated(&self) -> bool
true if the underlying future should no longer be polled.Source§impl<T, F, C> Future for ThreadFuture<T, F, C>
impl<T, F, C> Future for ThreadFuture<T, F, C>
impl<'pin, T, F, C> Unpin for ThreadFuture<T, F, C>
Auto Trait Implementations§
impl<T, F, C> Freeze for ThreadFuture<T, F, C>
impl<T, F, C> !RefUnwindSafe for ThreadFuture<T, F, C>
impl<T, F, C> Send for ThreadFuture<T, F, C>where
F: Send,
impl<T, F, C> Sync for ThreadFuture<T, F, C>
impl<T, F, C> UnsafeUnpin for ThreadFuture<T, F, C>where
C: UnsafeUnpin,
F: UnsafeUnpin,
impl<T, F, C> !UnwindSafe for ThreadFuture<T, F, C>
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 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>.