acceptor_std/mpsc/
mpsc_sender.rs

1use std::sync::mpsc::{SendError, Sender};
2
3use accepts::{Accepts, AsyncAccepts};
4
5/// Sends values through an `mpsc::Sender` and forwards the `Result` downstream.
6#[derive(Debug, Clone)]
7pub struct MpscSender<Value, NextAccepts> {
8    sender: Sender<Value>,
9    next_acceptor: NextAccepts,
10}
11
12impl<Value, NextAccepts> MpscSender<Value, NextAccepts> {
13    pub fn new(sender: Sender<Value>, next_acceptor: NextAccepts) -> Self {
14        Self {
15            sender,
16            next_acceptor,
17        }
18    }
19}
20
21impl<Value, NextAccepts> Accepts<Value> for MpscSender<Value, NextAccepts>
22where
23    NextAccepts: Accepts<Result<(), SendError<Value>>>,
24{
25    fn accept(&self, value: Value) {
26        let result = self.sender.send(value);
27        self.next_acceptor.accept(result);
28    }
29}
30
31impl<Value, NextAccepts> AsyncAccepts<Value> for MpscSender<Value, NextAccepts>
32where
33    NextAccepts: AsyncAccepts<Result<(), SendError<Value>>>,
34{
35    fn accept_async<'a>(&'a self, value: Value) -> impl core::future::Future<Output = ()> + 'a
36    where
37        Value: 'a,
38    {
39        async move {
40            let result = self.sender.send(value);
41            self.next_acceptor.accept_async(result).await;
42        }
43    }
44}