quic_reverse_transport/traits.rs
1// Copyright 2024-2026 Farlight Networks, LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Transport trait definitions.
16//!
17//! These traits abstract over QUIC connection and stream types,
18//! enabling quic-reverse to work with different QUIC implementations.
19
20use std::future::Future;
21use tokio::io::{AsyncRead, AsyncWrite};
22
23/// Abstraction over a QUIC connection.
24///
25/// This trait provides the minimal interface needed by quic-reverse
26/// to open and accept bidirectional streams.
27pub trait Connection: Send + Sync + 'static {
28 /// The send stream type produced by this connection.
29 type SendStream: SendStream;
30 /// The receive stream type produced by this connection.
31 type RecvStream: RecvStream;
32 /// Error type for opening streams.
33 type OpenError: std::error::Error + Send + Sync + 'static;
34 /// Error type for accepting streams.
35 type AcceptError: std::error::Error + Send + Sync + 'static;
36
37 /// Opens a new bidirectional stream.
38 ///
39 /// Returns a future that resolves to a send/receive stream pair.
40 fn open_bi(
41 &self,
42 ) -> impl Future<Output = Result<(Self::SendStream, Self::RecvStream), Self::OpenError>> + Send;
43
44 /// Accepts an incoming bidirectional stream.
45 ///
46 /// Returns a future that resolves to a send/receive stream pair,
47 /// or `None` if the connection is closing.
48 #[allow(clippy::type_complexity)]
49 fn accept_bi(
50 &self,
51 ) -> impl Future<Output = Result<Option<(Self::SendStream, Self::RecvStream)>, Self::AcceptError>>
52 + Send;
53
54 /// Closes the connection with an error code and reason.
55 fn close(&self, code: u32, reason: &[u8]);
56
57 /// Returns true if the connection is still open.
58 fn is_open(&self) -> bool;
59}
60
61/// Abstraction over a QUIC send stream.
62///
63/// Implementations must support async writing and graceful/abrupt shutdown.
64pub trait SendStream: AsyncWrite + Send + Unpin + 'static {
65 /// Error type for finishing the stream.
66 type FinishError: std::error::Error + Send + Sync + 'static;
67
68 /// Gracefully finishes the stream, signaling no more data will be sent.
69 fn finish(&mut self) -> impl Future<Output = Result<(), Self::FinishError>> + Send;
70
71 /// Abruptly resets the stream with an error code.
72 fn reset(&mut self, code: u32);
73}
74
75/// Abstraction over a QUIC receive stream.
76///
77/// Implementations must support async reading and early termination.
78pub trait RecvStream: AsyncRead + Send + Unpin + 'static {
79 /// Stops reading from the stream, signaling to the peer that
80 /// no more data will be read.
81 fn stop(&mut self, code: u32);
82}