struct_threads/lib.rs
1//! A simple library providing a clean, object-oriented way to structure thread logic.
2//!
3//! `struct-threads` allows you to define your task state in a `struct`, implement the
4//! [`Runnable`] trait, and seamlessly spawn it using the [`Thread`] extension trait.
5//!
6//! # Example
7//!
8//! ```rust
9//! use struct_threads::{Runnable, Thread};
10//!
11//! struct MyTask(i32);
12//!
13//! impl Runnable for MyTask {
14//! type Output = i32;
15//!
16//! fn run(self) -> Self::Output {
17//! self.0 * 2
18//! }
19//! }
20//!
21//! let handle = MyTask(21).start();
22//! assert_eq!(handle.join().unwrap(), 42);
23//! ```
24
25pub mod traits;
26
27pub use traits::{Runnable, Thread};
28
29#[cfg(test)]
30mod tests {
31 use super::*;
32
33 struct TestTask(i32);
34 impl Runnable for TestTask {
35 type Output = i32;
36
37 fn run(self) -> Self::Output {
38 self.0 * 2
39 }
40 }
41
42 #[test]
43 fn it_works() {
44 let task = TestTask(10);
45 let handle = task.start();
46 assert_eq!(handle.join().unwrap(), 20);
47 }
48}