Stable 1.0.0
I am excited to announce the first stable release of struct-threads! 🦀
struct-threads is a lightweight Rust library designed to provide a clean, object-oriented approach to structuring thread logic. By encapsulating thread state within structs that implement the Runnable trait, this library helps you write more readable, maintainable, and testable concurrent code.
✨ Features in this release
- The
RunnableTrait: Define your task logic and its precise return type (Output) clearly and safely. - The
ThreadExtension Trait: Automatically gain a convenient.start()method for anyRunnablestruct. - Zero-Cost Abstraction: Under the hood, it acts as a direct, zero-overhead wrapper around
std::thread::spawn. - Type-Safe Returns: Seamlessly join the thread and retrieve the exact type specified in your task's
Output.
🚀 Quick Start
// Add to your project: cargo add struct-threads
use struct_threads::{Runnable, Thread};
struct MyTask { data: i32 }
impl Runnable for MyTask {
type Output = i32;
fn run(self) -> Self::Output {
self.data * 2
}
}
let handle = MyTask { data: 21 }.start();
assert_eq!(handle.join().unwrap(), 42);Feedback, bug reports, and pull requests are highly appreciated! If you find this library useful, consider giving it a ⭐.