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 WriteAbort: maybe::Send {
94    fn abort(
95        self: Box<Self>,
96        error_code: xwt_core::stream::ErrorCode,
97    ) -> maybe_send::BoxedFuture<'static, Result<(), maybe_send_sync::BoxedError<'static>>>;
98}
99
100impl<X> WriteAbort for X
101where
102    X: xwt_core::stream::WriteAbort,
103    X: 'static,
104{
105    fn abort(
106        self: Box<Self>,
107        error_code: xwt_core::stream::ErrorCode,
108    ) -> maybe_send::BoxedFuture<'static, Result<(), maybe_send_sync::BoxedError<'static>>> {
109        Box::pin(async move {
110            <X as xwt_core::stream::WriteAbort>::abort(*self, error_code)
111                .await
112                .map_err(|error| Box::new(error) as _)
113        })
114    }
115}
116
117#[dyn_safe::dyn_safe(true)]
118pub trait WriteAborted: maybe::Send {
119    fn aborted(
120        self: Box<Self>,
121    ) -> maybe_send::BoxedFuture<
122        'static,
123        Result<xwt_core::stream::ErrorCode, maybe_send_sync::BoxedError<'static>>,
124    >;
125}
126
127impl<X> WriteAborted for X
128where
129    X: xwt_core::stream::WriteAborted,
130    X: 'static,
131{
132    fn aborted(
133        self: Box<Self>,
134    ) -> maybe_send::BoxedFuture<
135        'static,
136        Result<xwt_core::stream::ErrorCode, maybe_send_sync::BoxedError<'static>>,
137    > {
138        Box::pin(async move {
139            <X as xwt_core::stream::WriteAborted>::aborted(*self)
140                .await
141                .map_err(|error| Box::new(error) as _)
142        })
143    }
144}
145
146#[dyn_safe::dyn_safe(true)]
147pub trait Finish: maybe::Send {
148    fn finish(
149        self: Box<Self>,
150    ) -> maybe_send::BoxedFuture<'static, Result<(), maybe_send_sync::BoxedError<'static>>>;
151}
152
153impl<X> Finish for X
154where
155    X: xwt_core::stream::Finish,
156    X: 'static,
157{
158    fn finish(
159        self: Box<Self>,
160    ) -> maybe_send::BoxedFuture<'static, Result<(), maybe_send_sync::BoxedError<'static>>> {
161        Box::pin(async move {
162            <X as xwt_core::stream::Finish>::finish(*self)
163                .await
164                .map_err(|error| Box::new(error) as _)
165        })
166    }
167}
168
169#[dyn_safe::dyn_safe(true)]
170pub trait ReadChunkU8: maybe::Send {
171    fn read_chunk(
172        &mut self,
173        max_length: usize,
174        ordered: bool,
175    ) -> maybe_send::BoxedFuture<
176        '_,
177        Result<
178            Option<xwt_core::stream::Chunk<alloc::vec::Vec<u8>>>,
179            maybe_send_sync::BoxedError<'static>,
180        >,
181    >;
182}
183
184#[cfg(feature = "alloc")]
185impl<X> ReadChunkU8 for X
186where
187    X: xwt_core::stream::ReadChunk<xwt_core::stream::chunk::U8>,
188    X: 'static,
189{
190    fn read_chunk(
191        &mut self,
192        max_length: usize,
193        ordered: bool,
194    ) -> maybe_send::BoxedFuture<
195        '_,
196        Result<
197            Option<xwt_core::stream::Chunk<alloc::vec::Vec<u8>>>,
198            maybe_send_sync::BoxedError<'static>,
199        >,
200    > {
201        Box::pin(async move {
202            <X as xwt_core::stream::ReadChunk<xwt_core::stream::chunk::U8>>::read_chunk(
203                self, max_length, ordered,
204            )
205            .await
206            .map_err(|error| Box::new(error) as _)
207        })
208    }
209}
210
211#[dyn_safe::dyn_safe(true)]
212pub trait WriteChunkU8: maybe::Send {
213    fn write_chunk<'a>(
214        &'a mut self,
215        buf: &'a [u8],
216    ) -> maybe_send::BoxedFuture<'a, Result<(), maybe_send_sync::BoxedError<'static>>>;
217}
218
219#[cfg(feature = "alloc")]
220impl<X> WriteChunkU8 for X
221where
222    X: xwt_core::stream::WriteChunk<xwt_core::stream::chunk::U8>,
223    X: 'static,
224{
225    fn write_chunk<'a>(
226        &'a mut self,
227        buf: &'a [u8],
228    ) -> maybe_send::BoxedFuture<'a, Result<(), maybe_send_sync::BoxedError<'static>>> {
229        Box::pin(async move {
230            <X as xwt_core::stream::WriteChunk<xwt_core::stream::chunk::U8>>::write_chunk(self, buf)
231                .await
232                .map_err(|error| Box::new(error) as _)
233        })
234    }
235}