pub struct BackgroundHandle { /* private fields */ }Expand description
A cloneable capability handed to background-task subscribers so they can request cancellation of a single background task without tearing down the whole request.
The handle wraps a per-task CancellationToken (a child of the task context’s
token). Calling stop cancels only that task: the
producer’s background loop observes the cancellation and terminates the task, while
sibling tasks and the enclosing request keep running. Request-level cancellation still
cascades down, because the per-task token is a child of the request token.
The handle also carries a creation timestamp (for observing run time) and an optional,
type-erased application payload attached with with_data.
The payload is shared by Arc across every clone (the subscriber’s copy and the one
kept in BackgroundTaskControls), so a subscriber can keep per-task bookkeeping on the
handle itself instead of maintaining a parallel map. Use interior mutability (e.g.
Mutex) inside the payload when it must change over the task’s lifetime.
Implementations§
Source§impl BackgroundHandle
impl BackgroundHandle
Sourcepub fn new(task_id: impl Into<String>, token: CancellationToken) -> Self
pub fn new(task_id: impl Into<String>, token: CancellationToken) -> Self
Wraps a per-task cancellation token under its task ID. The creation timestamp is stamped now and no payload is attached.
Sourcepub fn with_data<T: Any + Send + Sync>(self, data: T) -> Self
pub fn with_data<T: Any + Send + Sync>(self, data: T) -> Self
Attaches an application-defined payload, wrapping it in an Arc so every clone of
the handle observes the same value.
Attaches an already-shared payload, so the producer can keep its own Arc clone in
addition to the copy carried by the handle.
Sourcepub fn data<T: Any + Send + Sync>(&self) -> Option<Arc<T>>
pub fn data<T: Any + Send + Sync>(&self) -> Option<Arc<T>>
Downcasts the attached payload to T, returning a shared handle to it. Returns None
when no payload is attached or it is of another type.
Sourcepub fn created_at(&self) -> u64
pub fn created_at(&self) -> u64
Wall-clock time in milliseconds (Unix epoch) when this handle, and thus the task, was created.
Sourcepub fn elapsed_ms(&self) -> u64
pub fn elapsed_ms(&self) -> u64
Milliseconds elapsed since the task was created, for observing its run time.
Sourcepub fn stop(&self)
pub fn stop(&self)
Requests the producer stop this background task. Idempotent and safe to call after the task has already ended.
Sourcepub fn is_stopped(&self) -> bool
pub fn is_stopped(&self) -> bool
Returns whether a stop has already been requested for this task.
Trait Implementations§
Source§impl Clone for BackgroundHandle
impl Clone for BackgroundHandle
Source§fn clone(&self) -> BackgroundHandle
fn clone(&self) -> BackgroundHandle
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl !RefUnwindSafe for BackgroundHandle
impl !UnwindSafe for BackgroundHandle
impl Freeze for BackgroundHandle
impl Send for BackgroundHandle
impl Sync for BackgroundHandle
impl Unpin for BackgroundHandle
impl UnsafeUnpin for BackgroundHandle
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSend for T
impl<T> DowncastSend for T
Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
impl<T> Fruit for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more