pub trait SenderAndReceiver<P>where
P: PoolItem,{
// Required method
fn send_and_receive<'a, T>(
&'a self,
requests: impl Iterator<Item = T> + 'a,
) -> Result<Box<dyn Iterator<Item = T::Response> + 'a>, SendError<SenderCouplet<P>>>
where T: RequestWithResponse<P> + IdTargeted + 'a;
// Provided method
fn send_and_receive_one<'a, T>(
&'a self,
request: T,
) -> Result<T::Response, SendError<SenderCouplet<P>>>
where T: RequestWithResponse<P> + IdTargeted + 'a { ... }
}Expand description
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 directly:
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 directly.
If you don’t need the abstraction for testing, you can use ThreadPool directly
to get impl Iterator returns.
Required Methods§
Sourcefn send_and_receive<'a, T>(
&'a self,
requests: impl Iterator<Item = T> + 'a,
) -> Result<Box<dyn Iterator<Item = T::Response> + 'a>, SendError<SenderCouplet<P>>>where
T: RequestWithResponse<P> + IdTargeted + 'a,
fn send_and_receive<'a, T>(
&'a self,
requests: impl Iterator<Item = T> + 'a,
) -> Result<Box<dyn Iterator<Item = T::Response> + 'a>, SendError<SenderCouplet<P>>>where
T: RequestWithResponse<P> + IdTargeted + 'a,
Send multiple requests and receive their responses.
Requests are distributed to the appropriate threads based on their IDs. Responses are returned in the order they complete (not necessarily request order).
§Arguments
requests- Iterator of requests to send. Each request must implementRequestWithResponseandIdTargeted.
§Returns
A boxed iterator of responses. The iterator will yield one response per request.
§Errors
Returns SendError if the thread pool has been shut down.
Provided Methods§
Sourcefn send_and_receive_one<'a, T>(
&'a self,
request: T,
) -> Result<T::Response, SendError<SenderCouplet<P>>>where
T: RequestWithResponse<P> + IdTargeted + 'a,
fn send_and_receive_one<'a, T>(
&'a self,
request: T,
) -> Result<T::Response, SendError<SenderCouplet<P>>>where
T: RequestWithResponse<P> + IdTargeted + 'a,
Convenience method for sending a single request and receiving its response.
This is equivalent to calling send_and_receive with a single-item iterator,
but with cleaner ergonomics for the single-request case.
§Example
use messaging_thread_pool::{SenderAndReceiver, SenderAndReceiverMock, samples::*};
let mock = SenderAndReceiverMock::<Randoms, MeanRequest>::new(
vec![MeanResponse { id: 1, result: 42 }],
);
let response = mock.send_and_receive_one(MeanRequest(1)).expect("mock works");
assert_eq!(response.mean(), 42);Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementors§
impl<P> SenderAndReceiver<P> for SenderAndReceiverRawMock<P>
impl<P> SenderAndReceiver<P> for ThreadPool<P>where
P: PoolItem,
An implementation of the SenderAndReceiver trait for ThreadPool.