pub trait TokioTask: AsyncRunnable {
// Required method
fn async_start(self) -> JoinHandle<Self::Output>;
}Available on crate feature
tokio only.Expand description
An extension trait that provides a method to spawn a Tokio task for an AsyncRunnable task.
This trait is automatically implemented for any type that implements AsyncRunnable
when the tokio feature is enabled. You do not need to implement this trait manually.
§Examples
use struct_threads::{AsyncRunnable, TokioTask};
struct AsyncMathTask(i32, i32);
impl AsyncRunnable for AsyncMathTask {
type Output = i32;
fn run(self) -> impl std::future::Future<Output = Self::Output> + Send {
async move {
tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
self.0 + self.1
}
}
}
#[tokio::main]
async fn main() {
let task = AsyncMathTask(5, 7);
let handle = task.async_start(); // Provided by the TokioTask trait
assert_eq!(handle.await.unwrap(), 12);
}Required Methods§
Sourcefn async_start(self) -> JoinHandle<Self::Output>
fn async_start(self) -> JoinHandle<Self::Output>
Spawns a new Tokio task to execute the run method.
This acts as a zero-cost abstraction over tokio::task::spawn.
§Returns
Returns a tokio::task::JoinHandle that can be awaited to get the task’s output.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.