struct-threads
A simple Rust library that provides a clean, object-oriented way to structure thread logic by using structs implementing a Runnable trait.
Motivation
While std::thread::spawn is powerful, complex thread logic often requires passing multiple variables, channels, or shared state into a closure. This can make code difficult to read and maintain.
struct-threads solves this by encapsulating thread state and logic within a struct. This approach:
- Improves readability by separating state initialization from execution.
- Simplifies testing by allowing you to instantiate thread state without immediately spawning it.
- Provides a clear contract for the thread's return value via associated types.
Installation
Install using cargo:
Basic Usage
Define your task state in a struct, implement Runnable, and call .start().
use ;
Advanced Usage: Channels
This pattern truly shines when your thread needs to communicate with the main thread or hold more complex state, like channels.
use mpsc;
use ;
Contributing
Contributions are welcome! To get started:
- Clone the repository:
git clone https://github.com/AsfhtgkDavid/struct-threads.git - Run tests to ensure everything works locally:
cargo test - Submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.