Skip to main content

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