s2n_quic/stream/
local.rs

1// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4use super::{BidirectionalStream, SendStream};
5
6/// An enum of all the possible types of QUIC streams that may be opened by a local endpoint.
7#[derive(Debug)]
8pub enum LocalStream {
9    Bidirectional(BidirectionalStream),
10    Send(SendStream),
11}
12
13impl LocalStream {
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, LocalStream};
27    ///
28    /// let stream: LocalStream = connection.open_stream(Type::Bidirectional).await?;
29    /// println!("New stream's id: {}", stream.id());
30    /// #
31    /// #   Ok(())
32    /// # }
33    /// ```
34    pub fn id(&self) -> u64 {
35        match self {
36            Self::Bidirectional(stream) => stream.id(),
37            Self::Send(stream) => stream.id(),
38        }
39    }
40
41    impl_connection_api!(|stream| match stream {
42        LocalStream::Bidirectional(stream) => stream.connection(),
43        LocalStream::Send(stream) => stream.connection(),
44    });
45
46    impl_receive_stream_api!(|stream, dispatch| match stream {
47        LocalStream::Bidirectional(stream) => dispatch!(stream),
48        LocalStream::Send(_stream) => dispatch!(),
49    });
50
51    impl_send_stream_api!(|stream, dispatch| match stream {
52        LocalStream::Bidirectional(stream) => dispatch!(stream),
53        LocalStream::Send(stream) => dispatch!(stream),
54    });
55
56    impl_splittable_stream_api!();
57}
58
59impl_receive_stream_trait!(LocalStream, |stream, dispatch| match stream {
60    LocalStream::Bidirectional(stream) => dispatch!(stream),
61    LocalStream::Send(_stream) => dispatch!(),
62});
63impl_send_stream_trait!(LocalStream, |stream, dispatch| match stream {
64    LocalStream::Bidirectional(stream) => dispatch!(stream),
65    LocalStream::Send(stream) => dispatch!(stream),
66});
67impl_splittable_stream_trait!(LocalStream, |stream| match stream {
68    LocalStream::Bidirectional(stream) => super::SplittableStream::split(stream),
69    LocalStream::Send(stream) => super::SplittableStream::split(stream),
70});
71
72impl From<SendStream> for LocalStream {
73    #[inline]
74    fn from(stream: SendStream) -> Self {
75        Self::Send(stream)
76    }
77}
78
79impl From<BidirectionalStream> for LocalStream {
80    #[inline]
81    fn from(stream: BidirectionalStream) -> Self {
82        Self::Bidirectional(stream)
83    }
84}