pub struct SpawnedTask { /* private fields */ }Expand description
A single background task with cooperative cancellation.
Created by SpawnedTask::spawn, which hands the body a child CancellationToken to select! on.
Stop it with shutdown (cancel → await up to a grace period → abort)
so a wedged task can never block shutdown indefinitely.
Extra teardown (closing connections, flushing buffers) stays with the caller.
Implementations§
Source§impl SpawnedTask
impl SpawnedTask
Sourcepub fn spawn<F, Fut>(body: F) -> Self
pub fn spawn<F, Fut>(body: F) -> Self
Spawn body with a fresh cancellation token passed to the closure.
The body should select! on token.cancelled() to exit promptly.
Sourcepub fn spawn_with<Fut>(cancel: CancellationToken, body: Fut) -> Self
pub fn spawn_with<Fut>(cancel: CancellationToken, body: Fut) -> Self
Spawn body with the supplied cancellation token (e.g. a shared parent).
Sourcepub const fn from_parts(
cancel: CancellationToken,
handle: JoinHandle<()>,
) -> Self
pub const fn from_parts( cancel: CancellationToken, handle: JoinHandle<()>, ) -> Self
Adopt an already-spawned JoinHandle paired with the token that stops it.
For callers that spawn the task themselves but still want the bounded drain-then-abort shutdown.
Sourcepub fn is_finished(&self) -> bool
pub fn is_finished(&self) -> bool
Return true once the task has finished.
Sourcepub fn cancellation(&self) -> CancellationToken
pub fn cancellation(&self) -> CancellationToken
A clone of the task’s cancellation token, for cooperative shutdown.