iroh_quinn_proto/
coding.rs

1//! Coding related traits.
2
3use std::net::{Ipv4Addr, Ipv6Addr};
4
5use bytes::{Buf, BufMut};
6use thiserror::Error;
7
8use crate::VarInt;
9
10/// Error indicating that the provided buffer was too small
11#[derive(Error, Debug, Copy, Clone, Eq, PartialEq)]
12#[error("unexpected end of buffer")]
13pub struct UnexpectedEnd;
14
15/// Coding result type
16pub type Result<T> = ::std::result::Result<T, UnexpectedEnd>;
17
18/// Infallible encoding of QUIC primitives.
19pub trait Encodable {
20    /// Append the encoding of `self` to the provided buffer.
21    fn encode<B: BufMut>(&self, buf: &mut B);
22}
23
24/// Decoding of QUIC primitives.
25pub trait Decodable: Sized {
26    /// Decode a `Self` from the provided buffer, if the buffer is large enough
27    fn decode<B: Buf>(buf: &mut B) -> Result<Self>;
28}
29
30impl Decodable for u8 {
31    fn decode<B: Buf>(buf: &mut B) -> Result<Self> {
32        if buf.remaining() < 1 {
33            return Err(UnexpectedEnd);
34        }
35        Ok(buf.get_u8())
36    }
37}
38impl Encodable for u8 {
39    fn encode<B: BufMut>(&self, buf: &mut B) {
40        buf.put_u8(*self);
41    }
42}
43
44impl Decodable for u16 {
45    fn decode<B: Buf>(buf: &mut B) -> Result<Self> {
46        if buf.remaining() < 2 {
47            return Err(UnexpectedEnd);
48        }
49        Ok(buf.get_u16())
50    }
51}
52impl Encodable for u16 {
53    fn encode<B: BufMut>(&self, buf: &mut B) {
54        buf.put_u16(*self);
55    }
56}
57
58impl Decodable for u32 {
59    fn decode<B: Buf>(buf: &mut B) -> Result<Self> {
60        if buf.remaining() < 4 {
61            return Err(UnexpectedEnd);
62        }
63        Ok(buf.get_u32())
64    }
65}
66impl Encodable for u32 {
67    fn encode<B: BufMut>(&self, buf: &mut B) {
68        buf.put_u32(*self);
69    }
70}
71
72impl Decodable for u64 {
73    fn decode<B: Buf>(buf: &mut B) -> Result<Self> {
74        if buf.remaining() < 8 {
75            return Err(UnexpectedEnd);
76        }
77        Ok(buf.get_u64())
78    }
79}
80impl Encodable for u64 {
81    fn encode<B: BufMut>(&self, buf: &mut B) {
82        buf.put_u64(*self);
83    }
84}
85
86impl Decodable for Ipv4Addr {
87    fn decode<B: Buf>(buf: &mut B) -> Result<Self> {
88        if buf.remaining() < 4 {
89            return Err(UnexpectedEnd);
90        }
91        let mut octets = [0; 4];
92        buf.copy_to_slice(&mut octets);
93        Ok(octets.into())
94    }
95}
96impl Encodable for Ipv4Addr {
97    fn encode<B: BufMut>(&self, buf: &mut B) {
98        buf.put_slice(&self.octets());
99    }
100}
101
102impl Decodable for Ipv6Addr {
103    fn decode<B: Buf>(buf: &mut B) -> Result<Self> {
104        if buf.remaining() < 16 {
105            return Err(UnexpectedEnd);
106        }
107        let mut octets = [0; 16];
108        buf.copy_to_slice(&mut octets);
109        Ok(octets.into())
110    }
111}
112impl Encodable for Ipv6Addr {
113    fn encode<B: BufMut>(&self, buf: &mut B) {
114        buf.put_slice(&self.octets());
115    }
116}
117
118pub(crate) trait BufExt {
119    fn get<T: Decodable>(&mut self) -> Result<T>;
120    fn get_var(&mut self) -> Result<u64>;
121}
122
123impl<T: Buf> BufExt for T {
124    fn get<U: Decodable>(&mut self) -> Result<U> {
125        U::decode(self)
126    }
127
128    fn get_var(&mut self) -> Result<u64> {
129        Ok(VarInt::decode(self)?.into_inner())
130    }
131}
132
133pub(crate) trait BufMutExt {
134    fn write<T: Encodable>(&mut self, x: T);
135    fn write_var(&mut self, x: u64);
136}
137
138impl<T: BufMut> BufMutExt for T {
139    fn write<U: Encodable>(&mut self, x: U) {
140        x.encode(self);
141    }
142
143    fn write_var(&mut self, x: u64) {
144        VarInt::from_u64(x).unwrap().encode(self);
145    }
146}