struct_threads/traits.rs
1/// A trait for defining a task that can be executed, typically in a separate thread.
2///
3/// Types implementing `Runnable` must be `Send` and `'static` to ensure they
4/// can be safely transferred across thread boundaries.
5pub trait Runnable: Send + 'static {
6 /// The type of value returned when the task completes.
7 type Output: Send + 'static;
8
9 /// Executes the task logic.
10 fn run(self) -> Self::Output;
11}
12
13/// An extension trait that provides a method to spawn a thread for a `Runnable` task.
14pub trait Thread: Runnable {
15 /// Spawns a new standard library thread to execute the `run` method.
16 ///
17 /// Returns a `JoinHandle` that can be used to wait for the thread to finish
18 /// and extract its `Output`.
19 fn start(self) -> std::thread::JoinHandle<Self::Output>;
20}
21
22impl<T: Runnable> Thread for T {
23 fn start(self) -> std::thread::JoinHandle<Self::Output> {
24 std::thread::spawn(move || self.run())
25 }
26}