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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//! # Sender and Receiver Abstractions
//!
//! This module provides traits for abstracting thread pool communication, enabling
//! dependency injection and testing.
//!
//! ## Key Types
//!
//! - [`SenderAndReceiver`] - Main trait for sending requests and receiving responses
//! - [`SenderAndReceiverMock`] - Mock implementation for testing
//! - [`ThreadSafeSenderAndReceiver`] - Thread-safe version for nested thread pools
//!
//! ## Testing with Mocks
//!
//! The [`SenderAndReceiver`] trait allows you to write code that works with both
//! real thread pools and mocks:
//!
//! ```rust
//! use messaging_thread_pool::{SenderAndReceiver, samples::*};
//!
//! // Function that depends on thread pool through trait
//! fn calculate_total_sum<T: SenderAndReceiver<Randoms>>(
//! pool: &T,
//! ids: &[u64],
//! ) -> u128 {
//! pool.send_and_receive(ids.iter().map(|id| SumRequest(*id)))
//! .expect("pool available")
//! .map(|r: SumResponse| r.sum())
//! .sum()
//! }
//! ```
//!
//! In tests, inject a [`SenderAndReceiverMock`]:
//!
//! ```rust
//! use messaging_thread_pool::{SenderAndReceiverMock, samples::*};
//!
//! let mock = SenderAndReceiverMock::<Randoms, SumRequest>::new_with_expected_requests(
//! vec![SumRequest(1), SumRequest(2)],
//! vec![
//! SumResponse { id: 1, result: 100 },
//! SumResponse { id: 2, result: 200 },
//! ],
//! );
//!
//! // The mock verifies requests match expectations and returns predefined responses
//! ```
use iter;
use crate::;
use SendError;
pub use SenderAndReceiverMock;
/// Trait for types that can send requests to pool items and receive responses.
///
/// This trait abstracts the communication mechanism with pool items, allowing:
/// - Code to be written generically over the communication mechanism
/// - Mock implementations for testing without spawning threads
/// - Different implementations for different threading strategies
///
/// # Usage
///
/// Write code that depends on this trait rather than [`ThreadPool`](crate::ThreadPool) directly:
///
/// ```rust
/// use messaging_thread_pool::{SenderAndReceiver, samples::*};
///
/// struct MyService<T: SenderAndReceiver<Randoms>> {
/// pool: T,
/// }
///
/// impl<T: SenderAndReceiver<Randoms>> MyService<T> {
/// fn get_mean(&self, id: u64) -> u128 {
/// self.pool
/// .send_and_receive_one(MeanRequest(id))
/// .expect("pool available")
/// .mean()
/// }
/// }
/// ```
///
/// # Note on Return Types
///
/// The `send_and_receive` method returns `Box<dyn Iterator>` rather than `impl Iterator`
/// due to limitations with trait return types. This has a small performance cost compared
/// to using [`ThreadPool`](crate::ThreadPool) directly.
///
/// If you don't need the abstraction for testing, you can use `ThreadPool` directly
/// to get `impl Iterator` returns.
/// A thread-safe version of [`SenderAndReceiver`].
///
/// This trait is useful when building nested thread pools, where inner thread pools
/// need to be `Send + Sync` to be shared across the outer pool's threads.
///
/// # Example: Nested Thread Pools
///
/// ```rust,ignore
/// use std::sync::Arc;
/// use messaging_thread_pool::{ThreadPool, ThreadSafeSenderAndReceiver};
///
/// struct OuterItem<T: ThreadSafeSenderAndReceiver<InnerItem>> {
/// id: u64,
/// inner_pool: Arc<T>, // Shared across outer pool threads
/// }
/// ```
///
/// See [`samples::RandomsBatch`](crate::samples::RandomsBatch) for a complete example
/// of nested thread pools.