s2n_quic/stream/
peer.rs

1// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4use super::{BidirectionalStream, ReceiveStream};
5
6/// An enum of all the possible types of QUIC streams that may be opened by a peer.
7#[derive(Debug)]
8pub enum PeerStream {
9    Bidirectional(BidirectionalStream),
10    Receive(ReceiveStream),
11}
12
13impl PeerStream {
14    /// Returns the stream's identifier
15    ///
16    /// This value is unique to a particular connection. The format follows the same as what is
17    /// defined in the
18    /// [QUIC Transport RFC](https://www.rfc-editor.org/rfc/rfc9000.html#name-stream-types-and-identifier).
19    ///
20    /// # Examples
21    ///
22    /// ```rust,no_run
23    /// # async fn test() -> s2n_quic::stream::Result<()> {
24    /// #   let connection: s2n_quic::connection::Connection = todo!();
25    /// #
26    /// use s2n_quic::stream::Type;
27    ///
28    /// while let Some(stream) = connection.accept().await? {
29    ///     println!("New stream's id: {}", stream.id());
30    /// }
31    /// #
32    /// #   Ok(())
33    /// # }
34    /// ```
35    pub fn id(&self) -> u64 {
36        match self {
37            Self::Bidirectional(stream) => stream.id(),
38            Self::Receive(stream) => stream.id(),
39        }
40    }
41
42    impl_connection_api!(|stream| match stream {
43        PeerStream::Bidirectional(stream) => stream.connection(),
44        PeerStream::Receive(stream) => stream.connection(),
45    });
46
47    impl_receive_stream_api!(|stream, dispatch| match stream {
48        PeerStream::Bidirectional(stream) => dispatch!(stream),
49        PeerStream::Receive(stream) => dispatch!(stream),
50    });
51
52    impl_send_stream_api!(|stream, dispatch| match stream {
53        PeerStream::Bidirectional(stream) => dispatch!(stream),
54        PeerStream::Receive(_stream) => dispatch!(),
55    });
56
57    impl_splittable_stream_api!();
58}
59
60impl_receive_stream_trait!(PeerStream, |stream, dispatch| match stream {
61    PeerStream::Bidirectional(stream) => dispatch!(stream),
62    PeerStream::Receive(stream) => dispatch!(stream),
63});
64impl_send_stream_trait!(PeerStream, |stream, dispatch| match stream {
65    PeerStream::Bidirectional(stream) => dispatch!(stream),
66    PeerStream::Receive(_stream) => dispatch!(),
67});
68impl_splittable_stream_trait!(PeerStream, |stream| match stream {
69    PeerStream::Bidirectional(stream) => super::SplittableStream::split(stream),
70    PeerStream::Receive(stream) => super::SplittableStream::split(stream),
71});
72
73impl From<ReceiveStream> for PeerStream {
74    #[inline]
75    fn from(stream: ReceiveStream) -> Self {
76        Self::Receive(stream)
77    }
78}
79
80impl From<BidirectionalStream> for PeerStream {
81    #[inline]
82    fn from(stream: BidirectionalStream) -> Self {
83        Self::Bidirectional(stream)
84    }
85}