xwt_dyn/session/
stream.rs

1#![allow(missing_docs)]
2
3use alloc::boxed::Box;
4use xwt_core::utils::maybe;
5
6use crate::utils::traits::{maybe_send, maybe_send_sync};
7
8#[dyn_safe::dyn_safe(true)]
9pub trait SendStream:
10    crate::stream::Write
11    + crate::stream::WriteAbort
12    + crate::stream::WriteAborted
13    + crate::stream::Finish
14{
15}
16
17impl<X> SendStream for X where
18    X: crate::stream::Write
19        + crate::stream::WriteAbort
20        + crate::stream::WriteAborted
21        + crate::stream::Finish
22{
23}
24
25#[dyn_safe::dyn_safe(true)]
26pub trait RecvStream: crate::stream::Read + crate::stream::ReadAbort {}
27
28impl<X> RecvStream for X where X: crate::stream::Read + crate::stream::ReadAbort {}
29
30pub type StreamsTuple<'a> = (Box<dyn SendStream + 'a>, Box<dyn RecvStream + 'a>);
31
32#[dyn_safe::dyn_safe(true)]
33pub trait OpeningBi: maybe::Send {
34    fn wait_bi(
35        self: Box<Self>,
36    ) -> maybe_send::BoxedFuture<
37        'static,
38        Result<StreamsTuple<'static>, maybe_send_sync::BoxedError<'static>>,
39    >;
40}
41
42impl<X> OpeningBi for X
43where
44    X: xwt_core::session::stream::OpeningBi,
45    X: 'static,
46{
47    fn wait_bi(
48        self: Box<Self>,
49    ) -> maybe_send::BoxedFuture<
50        'static,
51        Result<StreamsTuple<'static>, maybe_send_sync::BoxedError<'static>>,
52    > {
53        Box::pin(async move {
54            <X as xwt_core::session::stream::OpeningBi>::wait_bi(*self)
55                .await
56                .map(|(tx, rx)| (Box::new(tx) as _, Box::new(rx) as _))
57                .map_err(|error| Box::new(error) as _)
58        })
59    }
60}
61
62#[dyn_safe::dyn_safe(true)]
63pub trait OpenBi: maybe::Send + maybe::Sync {
64    fn open_bi(
65        &self,
66    ) -> maybe_send::BoxedFuture<
67        '_,
68        Result<Box<dyn OpeningBi + 'static>, maybe_send_sync::BoxedError<'static>>,
69    >;
70}
71
72impl<X> OpenBi for X
73where
74    X: xwt_core::session::stream::OpenBi,
75    X: maybe::Sync,
76    X: 'static,
77{
78    fn open_bi(
79        &self,
80    ) -> maybe_send::BoxedFuture<
81        '_,
82        Result<Box<dyn OpeningBi + 'static>, maybe_send_sync::BoxedError<'static>>,
83    > {
84        Box::pin(async move {
85            <X as xwt_core::session::stream::OpenBi>::open_bi(self)
86                .await
87                .map(|val| Box::new(val) as _)
88                .map_err(|error| Box::new(error) as _)
89        })
90    }
91}
92
93#[dyn_safe::dyn_safe(true)]
94pub trait AcceptBi: maybe::Send {
95    fn accept_bi(
96        &self,
97    ) -> maybe_send::BoxedFuture<
98        '_,
99        Result<StreamsTuple<'static>, maybe_send_sync::BoxedError<'static>>,
100    >;
101}
102
103impl<X> AcceptBi for X
104where
105    X: xwt_core::session::stream::AcceptBi,
106    X: maybe::Sync,
107    X: 'static,
108{
109    fn accept_bi(
110        &self,
111    ) -> maybe_send::BoxedFuture<
112        '_,
113        Result<StreamsTuple<'static>, maybe_send_sync::BoxedError<'static>>,
114    > {
115        Box::pin(async move {
116            <X as xwt_core::session::stream::AcceptBi>::accept_bi(self)
117                .await
118                .map(|(tx, rx)| (Box::new(tx) as _, Box::new(rx) as _))
119                .map_err(|error| Box::new(error) as _)
120        })
121    }
122}
123
124#[dyn_safe::dyn_safe(true)]
125pub trait OpeningUni: maybe::Send {
126    fn wait_uni(
127        self: Box<Self>,
128    ) -> maybe_send::BoxedFuture<
129        'static,
130        Result<Box<dyn SendStream + 'static>, maybe_send_sync::BoxedError<'static>>,
131    >;
132}
133
134impl<X> OpeningUni for X
135where
136    X: xwt_core::session::stream::OpeningUni,
137    X: 'static,
138{
139    fn wait_uni(
140        self: Box<Self>,
141    ) -> maybe_send::BoxedFuture<
142        'static,
143        Result<Box<dyn SendStream + 'static>, maybe_send_sync::BoxedError<'static>>,
144    > {
145        Box::pin(async move {
146            <X as xwt_core::session::stream::OpeningUni>::wait_uni(*self)
147                .await
148                .map(|val| Box::new(val) as _)
149                .map_err(|error| Box::new(error) as _)
150        })
151    }
152}
153
154#[dyn_safe::dyn_safe(true)]
155pub trait OpenUni: maybe::Send + maybe::Sync {
156    fn open_uni(
157        &self,
158    ) -> maybe_send::BoxedFuture<
159        '_,
160        Result<Box<dyn OpeningUni + 'static>, maybe_send_sync::BoxedError<'static>>,
161    >;
162}
163
164impl<X> OpenUni for X
165where
166    X: xwt_core::session::stream::OpenUni,
167    X: maybe::Sync,
168    X: 'static,
169{
170    fn open_uni(
171        &self,
172    ) -> maybe_send::BoxedFuture<
173        '_,
174        Result<Box<dyn OpeningUni + 'static>, maybe_send_sync::BoxedError<'static>>,
175    > {
176        Box::pin(async move {
177            <X as xwt_core::session::stream::OpenUni>::open_uni(self)
178                .await
179                .map(|val| Box::new(val) as _)
180                .map_err(|error| Box::new(error) as _)
181        })
182    }
183}
184
185#[dyn_safe::dyn_safe(true)]
186pub trait AcceptUni: maybe::Send {
187    fn accept_uni(
188        &self,
189    ) -> maybe_send::BoxedFuture<
190        '_,
191        Result<Box<dyn RecvStream + 'static>, maybe_send_sync::BoxedError<'static>>,
192    >;
193}
194
195impl<X> AcceptUni for X
196where
197    X: xwt_core::session::stream::AcceptUni,
198    X: maybe::Sync,
199    X: 'static,
200{
201    fn accept_uni(
202        &self,
203    ) -> maybe_send::BoxedFuture<
204        '_,
205        Result<Box<dyn RecvStream + 'static>, maybe_send_sync::BoxedError<'static>>,
206    > {
207        Box::pin(async move {
208            <X as xwt_core::session::stream::AcceptUni>::accept_uni(self)
209                .await
210                .map(|val| Box::new(val) as _)
211                .map_err(|error| Box::new(error) as _)
212        })
213    }
214}