FluxionTask

Struct FluxionTask 

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

Runtime-agnostic task handle with automatic cancellation on drop.

FluxionTask spawns a background task using the configured runtime (Tokio, smol, async-std, or WASM) and provides cooperative cancellation through a CancellationToken.

The spawned task receives a CancellationToken that it should monitor to enable graceful shutdown. When the FluxionTask is dropped or manually cancelled, the token is signaled, allowing the task to clean up and exit.

§Runtime Support

  • Tokio: tokio::spawn (default)
  • smol: smol::spawn
  • async-std: async_core::task::spawn
  • WASM: wasm_bindgen_futures::spawn_local

Select runtime via feature flags: runtime-tokio, runtime-smol, runtime-async-std, or automatic WASM detection.

§Example

use fluxion_core::FluxionTask;
use futures::FutureExt;

let task = FluxionTask::spawn(|cancel| async move {
    loop {
        if cancel.is_cancelled() {
            println!("Graceful shutdown");
            break;
        }
        // Do work...
    }
});

// Task automatically cancels on drop
drop(task);

Implementations§

Source§

impl FluxionTask

Source

pub fn spawn<F, Fut>(f: F) -> Self
where F: FnOnce(CancellationToken) -> Fut + Send + 'static, Fut: Future<Output = ()> + Send + 'static,

Spawn a background task with cancellation support.

The provided closure receives a CancellationToken that will be triggered when the task is dropped or manually cancelled. The spawned future should monitor this token and exit gracefully when cancellation is requested.

§Arguments
  • f - A closure that receives a CancellationToken and returns a future
§Example
use fluxion_core::FluxionTask;
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::Arc;

let counter = Arc::new(AtomicU32::new(0));
let counter_clone = counter.clone();

let task = FluxionTask::spawn(|cancel| async move {
    while !cancel.is_cancelled() {
        counter_clone.fetch_add(1, Ordering::SeqCst);
    }
    println!("Stopped at count: {}", counter_clone.load(Ordering::SeqCst));
});

// Do other work...

// Task cancels automatically on drop
drop(task);
Source

pub fn cancel(&self)

This signals the task to stop but doesn’t wait for it to complete. The task will stop at its next cancellation checkpoint.

§Example
use fluxion_core::FluxionTask;

let task = FluxionTask::spawn(|cancel| async move {
    loop {
        if cancel.is_cancelled() {
            break;
        }
        // Do work...
    }
});

// Explicitly cancel before drop
task.cancel();
Source

pub fn is_cancelled(&self) -> bool

Check if cancellation has been requested.

Returns true if the task has been cancelled via cancel() or drop.

§Example
use fluxion_core::FluxionTask;

let task = FluxionTask::spawn(|cancel| async move {
    // Task body...
});

assert!(!task.is_cancelled());
task.cancel();
assert!(task.is_cancelled());

Trait Implementations§

Source§

impl Debug for FluxionTask

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for FluxionTask

Source§

fn drop(&mut self)

Executes the destructor for this type. 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> 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, 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.