meslin/dynamic/
wrappers.rs

1use crate::*;
2use core::future::Future;
3use std::marker::PhantomData;
4
5/// A wrapper around a sender, which provides a default `with`-value.
6pub struct WithValueSender<T: IsSender> {
7    sender: T,
8    with: T::With,
9}
10
11impl<T> Clone for WithValueSender<T>
12where
13    T: Clone + IsSender,
14    T::With: Clone,
15{
16    fn clone(&self) -> Self {
17        Self {
18            sender: self.sender.clone(),
19            with: self.with.clone(),
20        }
21    }
22}
23
24impl<T: IsSender> WithValueSender<T> {
25    pub fn new(sender: T, with: T::With) -> Self {
26        Self { sender, with }
27    }
28
29    pub fn into_inner(self) -> (T, T::With) {
30        (self.sender, self.with)
31    }
32
33    pub fn inner_ref(&self) -> (&T, &T::With) {
34        (&self.sender, &self.with)
35    }
36
37    pub fn inner_mut(&mut self) -> (&mut T, &mut T::With) {
38        (&mut self.sender, &mut self.with)
39    }
40}
41
42impl<T> IsSender for WithValueSender<T>
43where
44    T: IsSender,
45{
46    type With = ();
47
48    fn is_closed(&self) -> bool {
49        self.sender.is_closed()
50    }
51
52    fn capacity(&self) -> Option<usize> {
53        self.sender.capacity()
54    }
55
56    fn len(&self) -> usize {
57        self.sender.len()
58    }
59
60    fn receiver_count(&self) -> usize {
61        self.sender.receiver_count()
62    }
63
64    fn sender_count(&self) -> usize {
65        self.sender.sender_count()
66    }
67}
68
69impl<T> SendsProtocol for WithValueSender<T>
70where
71    T: SendsProtocol,
72    T::With: Clone,
73{
74    type Protocol = T::Protocol;
75
76    fn send_protocol_with(
77        this: &Self,
78        protocol: Self::Protocol,
79        with: (),
80    ) -> impl Future<Output = Result<(), SendError<(Self::Protocol, Self::With)>>> + Send {
81        let fut = T::send_protocol_with(&this.sender, protocol, this.with.clone());
82        async move {
83            match fut.await {
84                Ok(()) => Ok(()),
85                Err(e) => Err(e.map(|(protocol, _)| (protocol, with))),
86            }
87        }
88    }
89
90    fn try_send_protocol_with(
91        this: &Self,
92        protocol: Self::Protocol,
93        with: (),
94    ) -> Result<(), TrySendError<(Self::Protocol, Self::With)>> {
95        match T::try_send_protocol_with(&this.sender, protocol, this.with.clone()) {
96            Ok(()) => Ok(()),
97            Err(e) => Err(e.map(|(protocol, _)| (protocol, with))),
98        }
99    }
100}
101
102/// A wrapper around a sender, which provides a mapping between the `with`-value of the sender and
103/// a custom `with`-value.
104pub struct MappedWithSender<T: IsSender, W> {
105    sender: T,
106    f1: fn(W) -> T::With,
107    f2: fn(T::With) -> W,
108    _marker: PhantomData<fn() -> W>,
109}
110
111impl<T: IsSender + Clone, W> Clone for MappedWithSender<T, W> {
112    fn clone(&self) -> Self {
113        Self {
114            sender: self.sender.clone(),
115            f1: self.f1,
116            f2: self.f2,
117            _marker: PhantomData,
118        }
119    }
120}
121
122impl<T: IsSender, W> MappedWithSender<T, W> {
123    pub fn new(sender: T, f1: fn(W) -> T::With, f2: fn(T::With) -> W) -> Self {
124        Self {
125            sender,
126            f1,
127            f2,
128            _marker: PhantomData,
129        }
130    }
131
132    pub fn into_inner(self) -> (T, fn(W) -> T::With, fn(T::With) -> W) {
133        (self.sender, self.f1, self.f2)
134    }
135
136    pub fn inner_ref(&self) -> (&T, &fn(W) -> T::With, &fn(T::With) -> W) {
137        (&self.sender, &self.f1, &self.f2)
138    }
139
140    pub fn inner_mut(&mut self) -> (&mut T, &mut fn(W) -> T::With, &mut fn(T::With) -> W) {
141        (&mut self.sender, &mut self.f1, &mut self.f2)
142    }
143}
144
145impl<T: IsSender, W> IsSender for MappedWithSender<T, W> {
146    type With = W;
147
148    fn is_closed(&self) -> bool {
149        self.sender.is_closed()
150    }
151
152    fn capacity(&self) -> Option<usize> {
153        self.sender.capacity()
154    }
155
156    fn len(&self) -> usize {
157        self.sender.len()
158    }
159
160    fn receiver_count(&self) -> usize {
161        self.sender.receiver_count()
162    }
163
164    fn sender_count(&self) -> usize {
165        self.sender.sender_count()
166    }
167}
168
169impl<T, W> SendsProtocol for MappedWithSender<T, W>
170where
171    T: SendsProtocol + Send + Sync,
172{
173    type Protocol = T::Protocol;
174
175    fn send_protocol_with(
176        this: &Self,
177        protocol: Self::Protocol,
178        with: W,
179    ) -> impl Future<Output = Result<(), SendError<(Self::Protocol, Self::With)>>> + Send {
180        let fut = T::send_protocol_with(&this.sender, protocol, (this.f1)(with));
181        async {
182            match fut.await {
183                Ok(()) => Ok(()),
184                Err(e) => Err(e.map(|(protocol, with)| (protocol, (this.f2)(with)))),
185            }
186        }
187    }
188
189    fn try_send_protocol_with(
190        this: &Self,
191        protocol: Self::Protocol,
192        with: W,
193    ) -> Result<(), TrySendError<(Self::Protocol, Self::With)>> {
194        match T::try_send_protocol_with(&this.sender, protocol, (this.f1)(with)) {
195            Ok(()) => Ok(()),
196            Err(e) => Err(e.map(|(protocol, with)| (protocol, (this.f2)(with)))),
197        }
198    }
199}