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
impl FluxionTask
Sourcepub fn spawn<F, Fut>(f: F) -> Self
pub fn spawn<F, Fut>(f: F) -> Self
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 aCancellationTokenand 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);Sourcepub fn cancel(&self)
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();Sourcepub fn is_cancelled(&self) -> bool
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());