Skip to main content

xwt_dyn/
stream.rs

1//! Operations on the WebTransport streams.
2
3use core::num::NonZeroUsize;
4
5use alloc::boxed::Box;
6use xwt_core::utils::maybe;
7
8use crate::utils::traits::{maybe_send, maybe_send_sync};
9
10pub use xwt_core::stream::ErrorCode;
11
12#[dyn_safe::dyn_safe(true)]
13pub trait Error: maybe_send_sync::Error + xwt_core::stream::ErrorAsErrorCode {}
14
15impl<X> Error for X where X: maybe_send_sync::Error + xwt_core::stream::ErrorAsErrorCode {}
16
17pub type BoxedError<'a> = Box<dyn Error + 'a>;
18
19#[dyn_safe::dyn_safe(true)]
20pub trait Read: maybe::Send {
21    fn read<'a>(
22        &'a mut self,
23        buf: &'a mut [u8],
24    ) -> maybe_send::BoxedFuture<'a, Result<NonZeroUsize, BoxedError<'static>>>;
25}
26
27impl<X> Read for X
28where
29    X: xwt_core::stream::Read,
30{
31    fn read<'a>(
32        &'a mut self,
33        buf: &'a mut [u8],
34    ) -> maybe_send::BoxedFuture<'a, Result<NonZeroUsize, BoxedError<'static>>> {
35        Box::pin(async move {
36            <X as xwt_core::stream::Read>::read(self, buf)
37                .await
38                .map_err(|error| Box::new(error) as _)
39        })
40    }
41}
42
43#[dyn_safe::dyn_safe(true)]
44pub trait Write: maybe::Send {
45    fn write<'a>(
46        &'a mut self,
47        buf: &'a [u8],
48    ) -> maybe_send::BoxedFuture<'a, Result<NonZeroUsize, BoxedError<'static>>>;
49}
50
51impl<X> Write for X
52where
53    X: xwt_core::stream::Write,
54{
55    fn write<'a>(
56        &'a mut self,
57        buf: &'a [u8],
58    ) -> maybe_send::BoxedFuture<'a, Result<NonZeroUsize, BoxedError<'static>>> {
59        Box::pin(async move {
60            <X as xwt_core::stream::Write>::write(self, buf)
61                .await
62                .map_err(|error| Box::new(error) as _)
63        })
64    }
65}
66
67#[dyn_safe::dyn_safe(true)]
68pub trait ReadAbort: maybe::Send {
69    fn abort(
70        self: Box<Self>,
71        error_code: xwt_core::stream::ErrorCode,
72    ) -> maybe_send::BoxedFuture<'static, Result<(), maybe_send_sync::BoxedError<'static>>>;
73}
74
75impl<X> ReadAbort for X
76where
77    X: xwt_core::stream::ReadAbort,
78    X: 'static,
79{
80    fn abort(
81        self: Box<Self>,
82        error_code: xwt_core::stream::ErrorCode,
83    ) -> maybe_send::BoxedFuture<'static, Result<(), maybe_send_sync::BoxedError<'static>>> {
84        Box::pin(async move {
85            <X as xwt_core::stream::ReadAbort>::abort(*self, error_code)
86                .await
87                .map_err(|error| Box::new(error) as _)
88        })
89    }
90}
91
92#[dyn_safe::dyn_safe(true)]
93pub trait ReadAborted: maybe::Send {
94    fn aborted(
95        self: Box<Self>,
96    ) -> maybe_send::BoxedFuture<
97        'static,
98        Result<xwt_core::stream::ErrorCode, maybe_send_sync::BoxedError<'static>>,
99    >;
100}
101
102impl<X> ReadAborted for X
103where
104    X: xwt_core::stream::ReadAborted,
105    X: 'static,
106{
107    fn aborted(
108        self: Box<Self>,
109    ) -> maybe_send::BoxedFuture<
110        'static,
111        Result<xwt_core::stream::ErrorCode, maybe_send_sync::BoxedError<'static>>,
112    > {
113        Box::pin(async move {
114            <X as xwt_core::stream::ReadAborted>::aborted(*self)
115                .await
116                .map_err(|error| Box::new(error) as _)
117        })
118    }
119}
120
121#[dyn_safe::dyn_safe(true)]
122pub trait WriteAbort: maybe::Send {
123    fn abort(
124        self: Box<Self>,
125        error_code: xwt_core::stream::ErrorCode,
126    ) -> maybe_send::BoxedFuture<'static, Result<(), maybe_send_sync::BoxedError<'static>>>;
127}
128
129impl<X> WriteAbort for X
130where
131    X: xwt_core::stream::WriteAbort,
132    X: 'static,
133{
134    fn abort(
135        self: Box<Self>,
136        error_code: xwt_core::stream::ErrorCode,
137    ) -> maybe_send::BoxedFuture<'static, Result<(), maybe_send_sync::BoxedError<'static>>> {
138        Box::pin(async move {
139            <X as xwt_core::stream::WriteAbort>::abort(*self, error_code)
140                .await
141                .map_err(|error| Box::new(error) as _)
142        })
143    }
144}
145
146#[dyn_safe::dyn_safe(true)]
147pub trait WriteAborted: maybe::Send {
148    fn aborted(
149        self: Box<Self>,
150    ) -> maybe_send::BoxedFuture<
151        'static,
152        Result<xwt_core::stream::ErrorCode, maybe_send_sync::BoxedError<'static>>,
153    >;
154}
155
156impl<X> WriteAborted for X
157where
158    X: xwt_core::stream::WriteAborted,
159    X: 'static,
160{
161    fn aborted(
162        self: Box<Self>,
163    ) -> maybe_send::BoxedFuture<
164        'static,
165        Result<xwt_core::stream::ErrorCode, maybe_send_sync::BoxedError<'static>>,
166    > {
167        Box::pin(async move {
168            <X as xwt_core::stream::WriteAborted>::aborted(*self)
169                .await
170                .map_err(|error| Box::new(error) as _)
171        })
172    }
173}
174
175#[dyn_safe::dyn_safe(true)]
176pub trait Finish: maybe::Send {
177    fn finish(
178        self: Box<Self>,
179    ) -> maybe_send::BoxedFuture<'static, Result<(), maybe_send_sync::BoxedError<'static>>>;
180}
181
182impl<X> Finish for X
183where
184    X: xwt_core::stream::Finish,
185    X: 'static,
186{
187    fn finish(
188        self: Box<Self>,
189    ) -> maybe_send::BoxedFuture<'static, Result<(), maybe_send_sync::BoxedError<'static>>> {
190        Box::pin(async move {
191            <X as xwt_core::stream::Finish>::finish(*self)
192                .await
193                .map_err(|error| Box::new(error) as _)
194        })
195    }
196}
197
198#[dyn_safe::dyn_safe(true)]
199pub trait Finished: maybe::Send {
200    fn finished(
201        self: Box<Self>,
202    ) -> maybe_send::BoxedFuture<'static, Result<(), BoxedError<'static>>>;
203}
204
205impl<X> Finished for X
206where
207    X: xwt_core::stream::Finished,
208    X: 'static,
209{
210    fn finished(
211        self: Box<Self>,
212    ) -> maybe_send::BoxedFuture<'static, Result<(), BoxedError<'static>>> {
213        Box::pin(async move {
214            <X as xwt_core::stream::Finished>::finished(*self)
215                .await
216                .map_err(|error| Box::new(error) as _)
217        })
218    }
219}
220
221#[dyn_safe::dyn_safe(true)]
222pub trait ReadChunkU8: maybe::Send {
223    fn read_chunk(
224        &mut self,
225        max_length: usize,
226        ordered: bool,
227    ) -> maybe_send::BoxedFuture<
228        '_,
229        Result<
230            Option<xwt_core::stream::Chunk<alloc::vec::Vec<u8>>>,
231            maybe_send_sync::BoxedError<'static>,
232        >,
233    >;
234}
235
236#[cfg(feature = "alloc")]
237impl<X> ReadChunkU8 for X
238where
239    X: xwt_core::stream::ReadChunk<xwt_core::stream::chunk::U8>,
240    X: 'static,
241{
242    fn read_chunk(
243        &mut self,
244        max_length: usize,
245        ordered: bool,
246    ) -> maybe_send::BoxedFuture<
247        '_,
248        Result<
249            Option<xwt_core::stream::Chunk<alloc::vec::Vec<u8>>>,
250            maybe_send_sync::BoxedError<'static>,
251        >,
252    > {
253        Box::pin(async move {
254            <X as xwt_core::stream::ReadChunk<xwt_core::stream::chunk::U8>>::read_chunk(
255                self, max_length, ordered,
256            )
257            .await
258            .map_err(|error| Box::new(error) as _)
259        })
260    }
261}
262
263#[dyn_safe::dyn_safe(true)]
264pub trait WriteChunkU8: maybe::Send {
265    fn write_chunk<'a>(
266        &'a mut self,
267        buf: &'a [u8],
268    ) -> maybe_send::BoxedFuture<'a, Result<(), maybe_send_sync::BoxedError<'static>>>;
269}
270
271#[cfg(feature = "alloc")]
272impl<X> WriteChunkU8 for X
273where
274    X: xwt_core::stream::WriteChunk<xwt_core::stream::chunk::U8>,
275    X: 'static,
276{
277    fn write_chunk<'a>(
278        &'a mut self,
279        buf: &'a [u8],
280    ) -> maybe_send::BoxedFuture<'a, Result<(), maybe_send_sync::BoxedError<'static>>> {
281        Box::pin(async move {
282            <X as xwt_core::stream::WriteChunk<xwt_core::stream::chunk::U8>>::write_chunk(self, buf)
283                .await
284                .map_err(|error| Box::new(error) as _)
285        })
286    }
287}