1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//! # 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.
//!
//! ```rust
//! 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:
//!
//! ```rust
//! 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:
//!
//! ```rust
//! 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 `RandomsBatch` contains 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:
//!
//! ```rust
//! use messaging_thread_pool::samples::*;
//! ```
//!
//! See the integration tests in `tests/` for more complete examples of each pattern.
// re-export
pub use *;
pub use *;
pub use *;
pub use *;