Skip to content

Commit f630b27

Browse files
committed
feat(rt): introduce quic traits
1 parent 974289f commit f630b27

File tree

3 files changed

+90
-1
lines changed

3 files changed

+90
-1
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ nightly = []
101101
level = "warn"
102102
check-cfg = [
103103
'cfg(hyper_unstable_tracing)',
104-
'cfg(hyper_unstable_ffi)'
104+
'cfg(hyper_unstable_ffi)',
105+
'cfg(hyper_unstable_quic)',
105106
]
106107

107108
[package.metadata.docs.rs]

src/rt/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ pub mod bounds;
1717
mod io;
1818
mod timer;
1919

20+
#[cfg(hyper_unstable_quic)]
21+
#[cfg_attr(docsrs, doc(cfg(hyper_unstable_quic)))]
22+
pub mod quic;
23+
2024
pub use self::io::{Read, ReadBuf, ReadBufCursor, Write};
2125
pub use self::timer::{Sleep, Timer};
2226

src/rt/quic.rs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//! Generic QUIC support
2+
3+
use std::pin::Pin;
4+
use std::task::{Context, Poll};
5+
6+
use bytes::Buf;
7+
8+
// TODO: Should this be gated by an `http3` feature?
9+
10+
/// A QUIC connection.
11+
pub trait Connection<B> {
12+
/// Send streams that can be opened by this connection.
13+
type SendStream: SendStream<B>;
14+
/// Receive streams that can be accepted by this connection.
15+
type RecvStream: RecvStream;
16+
/// Bidirectional streams that can be opened or accepted by this connection.
17+
type BidiStream: SendStream<B> + RecvStream;
18+
/// Errors that may occur opening or accepting streams.
19+
type Error;
20+
21+
// Accepting streams
22+
23+
// Q: shorten to bidi?
24+
/// Accept a bidirection stream.
25+
fn poll_accept_bidirectional_stream(self: Pin<&mut Self>, cx: &mut Context<'_>)
26+
-> Poll<Result<Option<Self::BidiStream>, Self::Error>>;
27+
28+
/// Accept a unidirectional receive stream.
29+
fn poll_accept_recv_stream(self: Pin<&mut Self>, cx: &mut Context<'_>)
30+
-> Poll<Result<Option<Self::RecvStream>, Self::Error>>;
31+
32+
// Creating streams
33+
34+
// Q: shorten to bidi?
35+
/// Open a bidirectional stream.
36+
fn poll_open_bidirectional_stream(self: Pin<&mut Self>, cx: &mut Context<'_>)
37+
-> Poll<Result<Self::BidiStream, Self::Error>>;
38+
39+
/// Open a unidirectional send stream.
40+
fn poll_open_send_stream(self: Pin<&mut Self>, cx: &mut Context<'_>)
41+
-> Poll<Result<Self::SendStream, Self::Error>>;
42+
}
43+
44+
/// The send portion of a QUIC stream.
45+
pub trait SendStream<B> {
46+
/// Errors that may happen trying to send data.
47+
type Error; // bounds?
48+
/// Polls that the stream is ready to send more data.
49+
// Q: Should this be Pin<&mut Self>?
50+
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>>;
51+
/// Send data on the stream.
52+
fn send_data(&mut self, data: B) -> Result<(), Self::Error>;
53+
// fn poll_flush?
54+
/// finish?
55+
fn poll_finish(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>>;
56+
/// Close the stream with an error code.
57+
fn reset(&mut self, reset_code: u64);
58+
}
59+
60+
/// The receive portion of a QUIC stream.
61+
pub trait RecvStream {
62+
/// Buffers of data that can be received.
63+
type Buf: Buf;
64+
/// Errors that may be received.
65+
type Error; // bounds?
66+
67+
// Q: should this be Pin?
68+
/// Poll for more data received from the remote on this stream.
69+
fn poll_data(&mut self, cx: &mut Context<'_>)
70+
-> Poll<Result<Option<Self::Buf>, Self::Error>>;
71+
/// Signal to the remote peer to stop sending data.
72+
fn stop_sending(&mut self, error_code: u64);
73+
}
74+
75+
/// An optional trait if a QUIC stream can be split into two sides.
76+
pub trait BidiStream<B>: SendStream<B> + RecvStream {
77+
/// The send side of a stream.
78+
type SendStream: SendStream<B>;
79+
/// The receive side of a stream.
80+
type RecvStream: RecvStream;
81+
82+
/// Split this stream into two sides.
83+
fn split(self) -> (Self::SendStream, Self::RecvStream);
84+
}

0 commit comments

Comments
 (0)