Skip to main content

AsyncRunnable

Trait AsyncRunnable 

Source
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§

Source

type Output: Send + 'static

The type of value returned when the async task completes.

Required Methods§

Source

fn run(self) -> impl Future<Output = Self::Output> + Send

Executes the async task logic.

This method consumes the task (self) and returns a future that must be awaited to get the result.

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.

Implementors§