TaskHandle

Struct TaskHandle 

Source
pub struct TaskHandle { /* private fields */ }
Expand description

A unified handle for scheduled tasks that serves dual purposes.

TaskHandle implements both Subscription and Future, providing:

  • Cancellation: Call unsubscribe() to stop the task
  • Completion tracking: Await the handle to wait for task completion
  • Status checking: Use is_closed() to check if task is done or cancelled

§Thread Safety

TaskHandle is Clone + Send + Sync, allowing it to be shared across threads and async contexts safely.

§Example

use std::sync::{Arc, Mutex};

use rxrust::{
  scheduler::{LocalScheduler, Scheduler, Task, TaskState},
  subscription::Subscription,
};

let scheduler = LocalScheduler;
let data = Arc::new(Mutex::new(0));
let task = Task::new(data, |_| TaskState::Finished);

let handle = scheduler.schedule(task, None);

// Option 1: Cancel the task
handle.clone().unsubscribe();

// Option 2: Wait for completion
// In async context: handle.await;

// Option 3: Check status
if handle.is_closed() {
  println!("Task finished or was cancelled");
}

Implementations§

Source§

impl TaskHandle

Source

pub fn finished() -> Self

Create a handle for a task that has already completed.

This is useful for:

  • Synchronous operations that complete immediately
  • Error cases where no actual scheduling is needed
  • Testing scenarios

The returned handle will immediately return Poll::Ready(()) when polled and is_closed() will return true.

Trait Implementations§

Source§

impl Clone for TaskHandle

Source§

fn clone(&self) -> TaskHandle

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Future for TaskHandle

Source§

type Output = ()

The type of value produced on completion.
Source§

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>

Attempts to resolve the future to a final value, registering the current task for wakeup if the value is not yet available. Read more
Source§

impl Subscription for TaskHandle

Source§

fn unsubscribe(self)

Cancel the subscription (terminal operation, consumes self)
Source§

fn is_closed(&self) -> bool

Check if the subscription is closed (completed or unsubscribed)
Source§

fn unsubscribe_when_dropped(self) -> SubscriptionGuard<Self>
where Self: Sized,

Activates “RAII” behavior for this subscription. That means unsubscribe() will be called automatically as soon as the returned value goes out of scope. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> BoxedSubscriptionInner for T
where T: Subscription,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoBoxedSubscription<BoxedSubscription> for T
where T: Subscription + 'static,

Source§

fn into_boxed(self) -> BoxedSubscription

Convert this subscription into a boxed subscription.
Source§

impl<T> IntoBoxedSubscription<BoxedSubscriptionSend> for T
where T: Subscription + Send + 'static,

Source§

fn into_boxed(self) -> BoxedSubscriptionSend

Convert this subscription into a boxed subscription.
Source§

impl<F> IntoFuture for F
where F: Future,

Source§

type Output = <F as Future>::Output

The output that the future will produce on completion.
Source§

type IntoFuture = F

Which kind of future are we turning this into?
Source§

fn into_future(self) -> <F as IntoFuture>::IntoFuture

Creates a future from a value. Read more
Source§

impl<F, Sch> Schedulable<Sch> for F
where F: Future<Output = ()>,

Source§

type Future = F

The future type produced by this schedulable item
Source§

fn into_future(self, _scheduler: &Sch) -> <F as Schedulable<Sch>>::Future

Convert this item into a future using the given scheduler’s capabilities
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.