Skip to main content

Runnable

Trait Runnable 

Source
pub trait Runnable: Send + 'static {
    type Output: Send + 'static;

    // Required method
    fn run(self) -> Self::Output;
}
Expand description

A trait for defining a task that can be executed, typically in a separate thread.

Types implementing Runnable must be Send and 'static to ensure they can be safely transferred across thread boundaries. By encapsulating state within a struct that implements this trait, you can easily manage complex thread initialization.

§Examples

use struct_threads::Runnable;

struct GreetingTask {
    name: String,
}

impl Runnable for GreetingTask {
    type Output = String;

    fn run(self) -> Self::Output {
        format!("Hello, {}!", self.name)
    }
}

Required Associated Types§

Source

type Output: Send + 'static

The type of value returned when the task completes.

Required Methods§

Source

fn run(self) -> Self::Output

Executes the task logic.

This method consumes the task (self), meaning the state cannot be reused after the thread has finished executing.

Implementors§