@@ -15,63 +15,75 @@ pub struct UnexpectedEnd;
1515/// Coding result type
1616pub type Result < T > = :: std:: result:: Result < T , UnexpectedEnd > ;
1717
18- /// Infallible encoding and decoding of QUIC primitives
19- pub trait Codec : Sized {
18+ /// Infallible encoding of QUIC primitives.
19+ pub 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.
25+ pub trait Decodable : Sized {
2026 /// Decode a `Self` from the provided buffer, if the buffer is large enough
2127 fn decode < B : Buf > ( buf : & mut B ) -> Result < Self > ;
22- /// Append the encoding of `self` to the provided buffer
23- fn encode < B : BufMut > ( & self , buf : & mut B ) ;
2428}
2529
26- impl Codec for u8 {
30+ impl Decodable for u8 {
2731 fn decode < B : Buf > ( buf : & mut B ) -> Result < Self > {
2832 if buf. remaining ( ) < 1 {
2933 return Err ( UnexpectedEnd ) ;
3034 }
3135 Ok ( buf. get_u8 ( ) )
3236 }
37+ }
38+ impl Encodable for u8 {
3339 fn encode < B : BufMut > ( & self , buf : & mut B ) {
3440 buf. put_u8 ( * self ) ;
3541 }
3642}
3743
38- impl Codec for u16 {
44+ impl Decodable for u16 {
3945 fn decode < B : Buf > ( buf : & mut B ) -> Result < Self > {
4046 if buf. remaining ( ) < 2 {
4147 return Err ( UnexpectedEnd ) ;
4248 }
4349 Ok ( buf. get_u16 ( ) )
4450 }
51+ }
52+ impl Encodable for u16 {
4553 fn encode < B : BufMut > ( & self , buf : & mut B ) {
4654 buf. put_u16 ( * self ) ;
4755 }
4856}
4957
50- impl Codec for u32 {
58+ impl Decodable for u32 {
5159 fn decode < B : Buf > ( buf : & mut B ) -> Result < Self > {
5260 if buf. remaining ( ) < 4 {
5361 return Err ( UnexpectedEnd ) ;
5462 }
5563 Ok ( buf. get_u32 ( ) )
5664 }
65+ }
66+ impl Encodable for u32 {
5767 fn encode < B : BufMut > ( & self , buf : & mut B ) {
5868 buf. put_u32 ( * self ) ;
5969 }
6070}
6171
62- impl Codec for u64 {
72+ impl Decodable for u64 {
6373 fn decode < B : Buf > ( buf : & mut B ) -> Result < Self > {
6474 if buf. remaining ( ) < 8 {
6575 return Err ( UnexpectedEnd ) ;
6676 }
6777 Ok ( buf. get_u64 ( ) )
6878 }
79+ }
80+ impl Encodable for u64 {
6981 fn encode < B : BufMut > ( & self , buf : & mut B ) {
7082 buf. put_u64 ( * self ) ;
7183 }
7284}
7385
74- impl Codec for Ipv4Addr {
86+ impl Decodable for Ipv4Addr {
7587 fn decode < B : Buf > ( buf : & mut B ) -> Result < Self > {
7688 if buf. remaining ( ) < 4 {
7789 return Err ( UnexpectedEnd ) ;
@@ -80,12 +92,14 @@ impl Codec for Ipv4Addr {
8092 buf. copy_to_slice ( & mut octets) ;
8193 Ok ( octets. into ( ) )
8294 }
95+ }
96+ impl Encodable for Ipv4Addr {
8397 fn encode < B : BufMut > ( & self , buf : & mut B ) {
8498 buf. put_slice ( & self . octets ( ) ) ;
8599 }
86100}
87101
88- impl Codec for Ipv6Addr {
102+ impl Decodable for Ipv6Addr {
89103 fn decode < B : Buf > ( buf : & mut B ) -> Result < Self > {
90104 if buf. remaining ( ) < 16 {
91105 return Err ( UnexpectedEnd ) ;
@@ -94,18 +108,20 @@ impl Codec for Ipv6Addr {
94108 buf. copy_to_slice ( & mut octets) ;
95109 Ok ( octets. into ( ) )
96110 }
111+ }
112+ impl Encodable for Ipv6Addr {
97113 fn encode < B : BufMut > ( & self , buf : & mut B ) {
98114 buf. put_slice ( & self . octets ( ) ) ;
99115 }
100116}
101117
102118pub ( crate ) trait BufExt {
103- fn get < T : Codec > ( & mut self ) -> Result < T > ;
119+ fn get < T : Decodable > ( & mut self ) -> Result < T > ;
104120 fn get_var ( & mut self ) -> Result < u64 > ;
105121}
106122
107123impl < T : Buf > BufExt for T {
108- fn get < U : Codec > ( & mut self ) -> Result < U > {
124+ fn get < U : Decodable > ( & mut self ) -> Result < U > {
109125 U :: decode ( self )
110126 }
111127
@@ -115,12 +131,12 @@ impl<T: Buf> BufExt for T {
115131}
116132
117133pub ( crate ) trait BufMutExt {
118- fn write < T : Codec > ( & mut self , x : T ) ;
134+ fn write < T : Encodable > ( & mut self , x : T ) ;
119135 fn write_var ( & mut self , x : u64 ) ;
120136}
121137
122138impl < T : BufMut > BufMutExt for T {
123- fn write < U : Codec > ( & mut self , x : U ) {
139+ fn write < U : Encodable > ( & mut self , x : U ) {
124140 x. encode ( self ) ;
125141 }
126142
0 commit comments