sunset_async/
async_channel.rs1use core::future::poll_fn;
3
4#[allow(unused_imports)]
5use log::{debug, error, info, log, trace, warn};
6
7use embedded_io_async::{ErrorType, Read, Write};
8
9use crate::*;
10use sunset::{ChanData, ChanNum, Result};
11
12pub(crate) struct ChanIO<'g> {
14 num: ChanNum,
15 dt: ChanData,
16 sunset: &'g dyn async_sunset::ChanCore,
17}
18
19impl<'g> ChanIO<'g> {
20 pub(crate) fn new_normal(
25 num: ChanNum,
26 sunset: &'g dyn async_sunset::ChanCore,
27 ) -> Self {
28 Self { num, dt: ChanData::Normal, sunset }
29 }
30
31 pub(crate) fn clone_stderr(&self) -> Self {
32 let mut c = self.clone();
33 c.dt = ChanData::Stderr;
34 c
35 }
36}
37
38impl core::fmt::Debug for ChanIO<'_> {
39 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
40 f.debug_struct("ChanIO")
41 .field("num", &self.num)
42 .field("dt", &self.dt)
43 .finish_non_exhaustive()
44 }
45}
46
47impl ChanIO<'_> {
48 pub async fn until_closed(&self) -> Result<()> {
49 poll_fn(|cx| self.sunset.poll_until_channel_closed(cx, self.num)).await
50 }
51
52 pub async fn term_window_change(
53 &self,
54 winch: sunset::packets::WinChange,
55 ) -> Result<()> {
56 poll_fn(|cx| self.sunset.poll_term_window_change(cx, self.num, &winch)).await
57 }
58}
59
60impl Drop for ChanIO<'_> {
61 fn drop(&mut self) {
62 self.sunset.dec_chan(self.num)
63 }
64}
65
66impl Clone for ChanIO<'_> {
72 fn clone(&self) -> Self {
73 self.sunset.inc_chan(self.num);
74 Self { num: self.num, dt: self.dt, sunset: self.sunset }
75 }
76}
77
78impl ErrorType for ChanIO<'_> {
79 type Error = sunset::Error;
80}
81
82impl Read for ChanIO<'_> {
83 async fn read(&mut self, buf: &mut [u8]) -> Result<usize, sunset::Error> {
84 poll_fn(|cx| self.sunset.poll_read_channel(cx, self.num, self.dt, buf)).await
85 }
86}
87
88impl Write for ChanIO<'_> {
89 async fn write(&mut self, buf: &[u8]) -> Result<usize, sunset::Error> {
90 poll_fn(|cx| self.sunset.poll_write_channel(cx, self.num, self.dt, buf))
91 .await
92 }
93
94 async fn flush(&mut self) -> Result<()> {
95 Ok(())
97 }
98}
99
100#[derive(Debug)]
118pub struct ChanIn<'g>(ChanIO<'g>);
119
120impl<'g> ChanIn<'g> {
121 pub(crate) fn new(io: ChanIO<'g>) -> Self {
122 io.sunset.inc_read_chan(io.num, io.dt);
123 Self(io)
124 }
125
126 pub fn num(&self) -> ChanNum {
128 self.0.num
129 }
130
131 pub async fn until_closed(&self) -> Result<()> {
133 self.0.until_closed().await
134 }
135}
136
137impl Drop for ChanIn<'_> {
138 fn drop(&mut self) {
139 self.0.sunset.dec_read_chan(self.0.num, self.0.dt)
140 }
141}
142
143impl Clone for ChanIn<'_> {
144 fn clone(&self) -> Self {
145 Self::new(self.0.clone())
146 }
147}
148
149#[derive(Debug, Clone)]
161pub struct ChanOut<'g>(ChanIO<'g>);
162
163impl<'g> ChanOut<'g> {
164 pub(crate) fn new(io: ChanIO<'g>) -> Self {
165 Self(io)
166 }
167
168 pub fn num(&self) -> ChanNum {
170 self.0.num
171 }
172
173 pub async fn until_closed(&self) -> Result<()> {
175 self.0.until_closed().await
176 }
177
178 pub async fn term_window_change(
182 &self,
183 winch: sunset::packets::WinChange,
184 ) -> Result<()> {
185 self.0.term_window_change(winch).await
186 }
187}
188
189#[derive(Debug)]
208pub struct ChanInOut<'g>(ChanIO<'g>);
209
210impl<'g> ChanInOut<'g> {
211 pub(crate) fn new(io: ChanIO<'g>) -> Self {
212 io.sunset.inc_read_chan(io.num, io.dt);
213 Self(io)
214 }
215
216 pub fn num(&self) -> ChanNum {
218 self.0.num
219 }
220
221 pub fn split(&self) -> (ChanIn<'g>, ChanOut<'g>) {
225 (ChanIn::new(self.0.clone()), ChanOut::new(self.0.clone()))
226 }
227
228 pub async fn until_closed(&self) -> Result<()> {
230 self.0.until_closed().await
231 }
232
233 pub async fn term_window_change(
237 &self,
238 winch: sunset::packets::WinChange,
239 ) -> Result<()> {
240 self.0.term_window_change(winch).await
241 }
242}
243
244impl Drop for ChanInOut<'_> {
245 fn drop(&mut self) {
246 self.0.sunset.dec_read_chan(self.0.num, self.0.dt)
247 }
248}
249
250impl Clone for ChanInOut<'_> {
251 fn clone(&self) -> Self {
252 Self::new(self.0.clone())
253 }
254}
255
256impl ErrorType for ChanInOut<'_> {
257 type Error = sunset::Error;
258}
259
260impl ErrorType for ChanIn<'_> {
261 type Error = sunset::Error;
262}
263
264impl ErrorType for ChanOut<'_> {
265 type Error = sunset::Error;
266}
267
268impl Read for ChanInOut<'_> {
269 async fn read(&mut self, buf: &mut [u8]) -> Result<usize, sunset::Error> {
270 self.0.read(buf).await
271 }
272}
273
274impl Write for ChanInOut<'_> {
275 async fn write(&mut self, buf: &[u8]) -> Result<usize, sunset::Error> {
276 self.0.write(buf).await
277 }
278
279 async fn flush(&mut self) -> Result<()> {
280 Ok(())
282 }
283}
284
285impl Read for ChanIn<'_> {
286 async fn read(&mut self, buf: &mut [u8]) -> Result<usize, sunset::Error> {
287 self.0.read(buf).await
288 }
289}
290
291impl Write for ChanOut<'_> {
292 async fn write(&mut self, buf: &[u8]) -> Result<usize, sunset::Error> {
293 self.0.write(buf).await
294 }
295
296 async fn flush(&mut self) -> Result<()> {
297 Ok(())
299 }
300}