Skip to main content

webtrans_proto/
stream.rs

1//! WebTransport stream type identifiers and helpers for encoding/decoding.
2
3use bytes::{Buf, BufMut};
4
5use super::{VarInt, VarIntUnexpectedEnd};
6use crate::grease::is_grease_value;
7
8/// Sent as the leading bytes on a unidirectional stream to indicate its stream type.
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
10pub struct UniStream(pub VarInt);
11
12impl UniStream {
13    /// Decode a unidirectional stream type identifier.
14    pub fn decode<B: Buf>(buf: &mut B) -> Result<Self, VarIntUnexpectedEnd> {
15        Ok(UniStream(VarInt::decode(buf)?))
16    }
17
18    /// Encode this unidirectional stream type identifier.
19    pub fn encode<B: BufMut>(&self, buf: &mut B) {
20        self.0.encode(buf)
21    }
22
23    /// Return `true` when this type uses RFC 9114 GREASE spacing.
24    pub fn is_grease(&self) -> bool {
25        is_grease_value(self.0.into_inner())
26    }
27
28    /// Build a unidirectional stream type from a known `u32` value.
29    pub const fn from_u32(value: u32) -> Self {
30        Self(VarInt::from_u32(value))
31    }
32
33    /// HTTP/3 control stream type.
34    pub const CONTROL: UniStream = UniStream::from_u32(0x00);
35    /// HTTP/3 push stream type.
36    pub const PUSH: UniStream = UniStream::from_u32(0x01);
37    /// HTTP/3 QPACK encoder stream type.
38    pub const QPACK_ENCODER: UniStream = UniStream::from_u32(0x02);
39    /// HTTP/3 QPACK decoder stream type.
40    pub const QPACK_DECODER: UniStream = UniStream::from_u32(0x03);
41    /// WebTransport unidirectional stream type.
42    pub const WEBTRANSPORT: UniStream = UniStream::from_u32(0x54);
43}