xwt_core/
stream_utils.rs

1//! Utilitites for the WebTransport streams.
2
3use crate::stream;
4
5/// A shortcut for the error type for a given [`stream::Read`] type.
6pub type ReadErrorFor<T> = <T as stream::Read>::Error;
7
8/// A shortcut for the error type for a given [`stream::Write`] type.
9pub type WriteErrorFor<T> = <T as stream::Write>::Error;
10
11/// A shortcut for the error type for a given [`stream::ReadChunk] type.
12pub type ReadChunkErrorFor<T, Data> = <T as stream::ReadChunk<Data>>::Error;
13
14/// A shortcut for the error type for a given [`stream::WriteChunk] type.
15pub type WriteChunkErrorFor<T, Data> = <T as stream::WriteChunk<Data>>::Error;
16
17/// A shortcut for the error type for a given [`stream::ReadAbort`] type.
18pub type ReadAbortErrorFor<T> = <T as stream::ReadAbort>::Error;
19
20/// A shortcut for the error type for a given [`stream::WriteAbort`] type.
21pub type WriteAbortErrorFor<T> = <T as stream::WriteAbort>::Error;
22
23/// A shortcut for the error type for a given [`stream::WriteAborted`] type.
24pub type WriteAbortedErrorFor<T> = <T as stream::WriteAborted>::Error;
25
26/// A shortcut for the error type for a given [`stream::Finish`] type.
27pub type FinishErrorFor<T> = <T as stream::Finish>::Error;
28
29/// Extensions to the [`stream::ErrorAsErrorCode`] providing some convenience methods.
30pub trait ErrorAsErrorCodeExt: stream::ErrorAsErrorCode {
31    /// Check of the error code matches the given value.
32    fn is_error_code(&self, expected_code: stream::ErrorCode) -> bool;
33
34    /// Checks that the error code exists and is zero.
35    fn is_closed(&self) -> bool;
36}
37
38// Check that this trait is dyn-safe.
39impl dyn ErrorAsErrorCodeExt {}
40
41impl<T> ErrorAsErrorCodeExt for T
42where
43    T: ?Sized + stream::ErrorAsErrorCode,
44{
45    fn is_error_code(&self, expected_error_code: stream::ErrorCode) -> bool {
46        let Some(error_code) = self.as_error_code() else {
47            return false;
48        };
49
50        error_code == expected_error_code
51    }
52
53    fn is_closed(&self) -> bool {
54        self.is_error_code(0)
55    }
56}