s2n_quic/
stream.rs

1// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4#[macro_use]
5mod connection;
6#[macro_use]
7mod splittable;
8#[macro_use]
9mod receive;
10#[macro_use]
11mod send;
12#[macro_use]
13mod bidirectional;
14
15mod local;
16mod peer;
17
18pub use s2n_quic_core::stream::{StreamError as Error, StreamType as Type};
19
20pub use bidirectional::*;
21pub use local::*;
22pub use peer::*;
23pub use receive::*;
24pub use send::*;
25pub use splittable::*;
26
27pub type Result<T, E = Error> = core::result::Result<T, E>;
28
29/// An enum of all the possible types of QUIC streams.
30#[derive(Debug)]
31pub enum Stream {
32    Bidirectional(BidirectionalStream),
33    Receive(ReceiveStream),
34    Send(SendStream),
35}
36
37impl Stream {
38    /// Returns the stream's identifier
39    ///
40    /// This value is unique to a particular connection. The format follows the same as what is
41    /// defined in the
42    /// [QUIC Transport RFC](https://www.rfc-editor.org/rfc/rfc9000.html#name-stream-types-and-identifier).
43    ///
44    /// # Examples
45    ///
46    /// ```rust,no_run
47    /// # async fn test() -> s2n_quic::stream::Result<()> {
48    /// #   let connection: s2n_quic::connection::Connection = todo!();
49    /// #
50    /// use s2n_quic::stream::Stream;
51    ///
52    /// let stream = connection.open_bidirectional_stream().await?;
53    /// let stream: Stream = stream.into();
54    /// println!("New stream's id: {}", stream.id());
55    /// #
56    /// #   Ok(())
57    /// # }
58    /// ```
59    #[inline]
60    pub fn id(&self) -> u64 {
61        match self {
62            Self::Bidirectional(stream) => stream.id(),
63            Self::Receive(stream) => stream.id(),
64            Self::Send(stream) => stream.id(),
65        }
66    }
67
68    impl_connection_api!(|stream| match stream {
69        Stream::Bidirectional(stream) => stream.connection(),
70        Stream::Receive(stream) => stream.connection(),
71        Stream::Send(stream) => stream.connection(),
72    });
73
74    impl_receive_stream_api!(|stream, dispatch| match stream {
75        Stream::Bidirectional(stream) => dispatch!(stream),
76        Stream::Receive(stream) => dispatch!(stream),
77        Stream::Send(_stream) => dispatch!(),
78    });
79
80    impl_send_stream_api!(|stream, dispatch| match stream {
81        Stream::Bidirectional(stream) => dispatch!(stream),
82        Stream::Receive(_stream) => dispatch!(),
83        Stream::Send(stream) => dispatch!(stream),
84    });
85
86    impl_splittable_stream_api!();
87}
88
89impl_receive_stream_trait!(Stream, |stream, dispatch| match stream {
90    Stream::Bidirectional(stream) => dispatch!(stream),
91    Stream::Receive(stream) => dispatch!(stream),
92    Stream::Send(_stream) => dispatch!(),
93});
94impl_send_stream_trait!(Stream, |stream, dispatch| match stream {
95    Stream::Bidirectional(stream) => dispatch!(stream),
96    Stream::Receive(_stream) => dispatch!(),
97    Stream::Send(stream) => dispatch!(stream),
98});
99impl_splittable_stream_trait!(Stream, |stream| match stream {
100    Stream::Bidirectional(stream) => SplittableStream::split(stream),
101    Stream::Receive(stream) => SplittableStream::split(stream),
102    Stream::Send(stream) => SplittableStream::split(stream),
103});
104
105impl From<ReceiveStream> for Stream {
106    #[inline]
107    fn from(stream: ReceiveStream) -> Self {
108        Self::Receive(stream)
109    }
110}
111
112impl From<SendStream> for Stream {
113    #[inline]
114    fn from(stream: SendStream) -> Self {
115        Self::Send(stream)
116    }
117}
118
119impl From<BidirectionalStream> for Stream {
120    #[inline]
121    fn from(stream: BidirectionalStream) -> Self {
122        Self::Bidirectional(stream)
123    }
124}