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::ReadAborted`] type.
21pub type ReadAbortedErrorFor<T> = <T as stream::ReadAborted>::Error;
22
23/// A shortcut for the error type for a given [`stream::WriteAbort`] type.
24pub type WriteAbortErrorFor<T> = <T as stream::WriteAbort>::Error;
25
26/// A shortcut for the error type for a given [`stream::WriteAborted`] type.
27pub type WriteAbortedErrorFor<T> = <T as stream::WriteAborted>::Error;
28
29/// A shortcut for the error type for a given [`stream::Finish`] type.
30pub type FinishErrorFor<T> = <T as stream::Finish>::Error;
31
32/// A shortcut for the error type for a given [`stream::Finished`] type.
33pub type FinishedErrorFor<T> = <T as stream::Finished>::Error;
34
35/// Extensions to the [`stream::ErrorAsErrorCode`] providing some convenience methods.
36pub trait ErrorAsErrorCodeExt: stream::ErrorAsErrorCode {
37    /// Check of the error code matches the given value.
38    fn is_error_code(&self, expected_code: stream::ErrorCode) -> bool;
39
40    /// Checks that the error code exists and is zero.
41    fn is_closed(&self) -> bool;
42}
43
44// Check that this trait is dyn-safe.
45impl dyn ErrorAsErrorCodeExt {}
46
47impl<T> ErrorAsErrorCodeExt for T
48where
49    T: ?Sized + stream::ErrorAsErrorCode,
50{
51    fn is_error_code(&self, expected_error_code: stream::ErrorCode) -> bool {
52        let Some(error_code) = self.as_error_code() else {
53            return false;
54        };
55
56        error_code == expected_error_code
57    }
58
59    fn is_closed(&self) -> bool {
60        self.is_error_code(0)
61    }
62}