sec_http3/webtransport/
session_id.rs1use std::convert::TryFrom;
2
3use crate::proto::{
4 coding::{Decode, Encode},
5 stream::{InvalidStreamId, StreamId},
6 varint::VarInt,
7};
8
9#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
13pub struct SessionId(u64);
14impl SessionId {
15 pub(crate) fn from_varint(id: VarInt) -> SessionId {
16 Self(id.0)
17 }
18
19 pub(crate) fn into_inner(self) -> u64 {
20 self.0
21 }
22}
23
24impl TryFrom<u64> for SessionId {
25 type Error = InvalidStreamId;
26 fn try_from(v: u64) -> Result<Self, Self::Error> {
27 if v > VarInt::MAX.0 {
28 return Err(InvalidStreamId(v));
29 }
30 Ok(Self(v))
31 }
32}
33
34impl Encode for SessionId {
35 fn encode<B: bytes::BufMut>(&self, buf: &mut B) {
36 VarInt::from_u64(self.0).unwrap().encode(buf);
37 }
38}
39
40impl Decode for SessionId {
41 fn decode<B: bytes::Buf>(buf: &mut B) -> crate::proto::coding::Result<Self> {
42 Ok(Self(VarInt::decode(buf)?.into_inner()))
43 }
44}
45
46impl From<StreamId> for SessionId {
47 fn from(value: StreamId) -> Self {
48 Self(value.index())
49 }
50}