pub trait AsyncRunnable: Send + 'static {
type Output: Send + 'static;
// Required method
fn run(self) -> impl Future<Output = Self::Output> + Send;
}Expand description
A trait for defining an async task that can be executed, typically in a Tokio runtime.
Types implementing AsyncRunnable must be Send and 'static to ensure they
can be safely transferred across async task boundaries. This trait is designed for
async operations and works seamlessly with the Tokio runtime when the tokio feature is enabled.
§Examples
use struct_threads::AsyncRunnable;
struct AsyncGreetingTask {
name: String,
}
impl AsyncRunnable for AsyncGreetingTask {
type Output = String;
fn run(self) -> impl std::future::Future<Output = Self::Output> + Send {
async move {
// Simulate async work
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
format!("Hello, {}!", self.name)
}
}
}Required Associated Types§
Required Methods§
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.