Expand description
§Sample Pool Item Implementations
This module contains example PoolItem implementations that demonstrate
different patterns and use cases for the library.
§Samples Overview
| Sample | Complexity | Demonstrates |
|---|---|---|
UserSession | Beginner | Rc<RefCell<T>>, helper structs, basic #[pool_item] usage |
ChatRoom | Beginner | Simple state management, minimal boilerplate |
Randoms | Intermediate | Shutdown hooks, benchmarking patterns |
RandomsBatch | Advanced | Generics, nested thread pools, custom Init types |
§Recommended Learning Path
§1. Start with UserSession
UserSession is the canonical example showing the main advantage of this library:
using Rc and RefCell for shared state without locks.
use messaging_thread_pool::{ThreadPool, samples::*};
let pool = ThreadPool::<UserSession>::new(2);
// Create a session
pool.send_and_receive_once(UserSessionInit(1)).unwrap();
// Log actions
pool.send_and_receive_once(LogActionRequest(1, "Login".to_string())).unwrap();
// Get the log
let log: Vec<String> = pool
.send_and_receive_once(GetLogRequest(1))
.unwrap()
.result;§2. See ChatRoom for Minimal Boilerplate
ChatRoom shows the simplest possible #[pool_item] usage with mutable state:
use messaging_thread_pool::{ThreadPool, samples::*};
let pool = ThreadPool::<ChatRoom>::new(2);
pool.send_and_receive_once(ChatRoomInit(1)).unwrap();
pool.send_and_receive_once(PostRequest(1, "Alice".into(), "Hello!".into())).unwrap();
let history = pool.send_and_receive_once(GetHistoryRequest(1)).unwrap();
assert_eq!(history.result, vec!["Alice: Hello!"]);§3. See Randoms for Shutdown Hooks
Randoms demonstrates the Shutdown parameter for cleanup on pool termination:
use messaging_thread_pool::{ThreadPool, samples::*};
let pool = ThreadPool::<Randoms>::new(2);
pool.send_and_receive_once(RandomsAddRequest(1)).unwrap();
// Shutdown returns responses from each item
let shutdown_responses = pool.shutdown();§4. See RandomsBatch for Advanced Patterns
RandomsBatch shows complex patterns including:
- Generic pool items
- Custom initialization types (
Init = "RandomsBatchAddRequest<P>") - Nested thread pools (a
RandomsBatchcontains references to another pool) - Mocking nested pools for testing
§Using Samples in Tests
These samples are re-exported for use in your tests and benchmarks:
use messaging_thread_pool::samples::*;See the integration tests in tests/ for more complete examples of each pattern.
Structs§
- Chat
Room - A simple chat room that manages a history of messages.
- Chat
Room Init - GetHistory
Request - GetHistory
Response - GetLog
Request - GetLog
Response - History
Tracker - A helper struct that needs access to the session’s data.
- LogAction
Request - LogAction
Response - Mean
Request - Mean
Response - Panic
Request - Panic
Response - Post
Request - Post
Response - Randoms
- A collection of random numbers managed by the thread pool.
- Randoms
AddRequest - Alias for backwards compatibility
- Randoms
Batch - A batch of
Randomsitems, demonstrating nested thread pools. - Randoms
Batch AddRequest - Custom initialization request for
RandomsBatch. - Randoms
Init - Alias for backwards compatibility
- Randoms
Thread Pool - Marker type for using a real
ThreadPool<Randoms>as the inner pool. - SumOf
Sums Request - SumOf
Sums Response - SumRequest
- SumResponse
- User
Session - A user session that tracks actions performed by a user.
- User
Session Init
Enums§
Traits§
- Inner
Thread Pool - Trait for abstracting the inner thread pool type.
Type Aliases§
- Chat
Room_ GetHistory Request_ Request Response - Chat
Room_ Post Request_ Request Response - Randoms
Batch_ SumOf Sums Request_ Request Response - Randoms_
Mean Request_ Request Response - Randoms_
Panic Request_ Request Response - Randoms_
SumRequest_ Request Response - User
Session_ GetLog Request_ Request Response - User
Session_ LogAction Request_ Request Response