1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use bytes::{Buf, BufMut, Bytes, BytesMut};
use crate::{encode, decode::{self, Error}};
use std::{io, marker::PhantomData, usize};
#[derive(Default)]
pub struct Uvi<T>(PhantomData<T>);
macro_rules! encoder_decoder_impls {
($typ:ident, $arr:ident) => {
impl Uvi<$typ> {
fn serialise(&mut self, item: $typ, dst: &mut BytesMut) {
let mut buf = encode::$arr();
dst.extend_from_slice(encode::$typ(item, &mut buf))
}
fn deserialise(&mut self, src: &mut BytesMut) -> Result<Option<$typ>, io::Error> {
let (number, consumed) =
match decode::$typ(src.as_ref()) {
Ok((n, rem)) => (n, src.len() - rem.len()),
Err(Error::Insufficient) => return Ok(None),
Err(e) => return Err(io::Error::new(io::ErrorKind::Other, e))
};
src.advance(consumed);
Ok(Some(number))
}
}
#[cfg(feature = "codec")]
impl tokio_util::codec::Encoder<$typ> for Uvi<$typ> {
type Error = io::Error;
fn encode(&mut self, item: $typ, dst: &mut BytesMut) -> Result<(), Self::Error> {
self.serialise(item, dst);
Ok(())
}
}
#[cfg(feature = "codec")]
impl tokio_util::codec::Decoder for Uvi<$typ> {
type Item = $typ;
type Error = io::Error;
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
self.deserialise(src)
}
}
#[cfg(feature = "futures-codec")]
impl futures_codec::Encoder for Uvi<$typ> {
type Item = $typ;
type Error = io::Error;
fn encode(&mut self, item: Self::Item, dst: &mut BytesMut) -> Result<(), Self::Error> {
self.serialise(item, dst);
Ok(())
}
}
#[cfg(feature = "futures-codec")]
impl futures_codec::Decoder for Uvi<$typ> {
type Item = $typ;
type Error = io::Error;
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
self.deserialise(src)
}
}
}
}
encoder_decoder_impls!(u8, u8_buffer);
encoder_decoder_impls!(u16, u16_buffer);
encoder_decoder_impls!(u32, u32_buffer);
encoder_decoder_impls!(u64, u64_buffer);
encoder_decoder_impls!(u128, u128_buffer);
encoder_decoder_impls!(usize, usize_buffer);
pub struct UviBytes<T = Bytes> {
varint_codec: Uvi<usize>,
len: Option<usize>,
max: usize,
_ty: PhantomData<T>
}
impl<T> Default for UviBytes<T> {
fn default() -> Self {
Self {
varint_codec: Default::default(),
len: None,
max: 128 * 1024 * 1024,
_ty: PhantomData
}
}
}
impl<T> UviBytes<T> {
pub fn set_max_len(&mut self, val: usize) {
self.max = val
}
pub fn max_len(&self) -> usize {
self.max
}
fn deserialise(&mut self, src: &mut BytesMut) -> Result<Option<BytesMut>, io::Error> {
if self.len.is_none() {
self.len = self.varint_codec.deserialise(src)?
}
if let Some(n) = self.len.take() {
if n > self.max {
return Err(io::Error::new(io::ErrorKind::PermissionDenied, "len > max"))
}
if n <= src.len() {
return Ok(Some(src.split_to(n)))
}
let add = n - src.len();
src.reserve(add);
self.len = Some(n)
}
Ok(None)
}
}
impl<T: Buf> UviBytes<T> {
fn serialise(&mut self, item: T, dst: &mut BytesMut) -> Result<(), io::Error> {
if item.remaining() > self.max {
return Err(io::Error::new(io::ErrorKind::PermissionDenied, "len > max when encoding"));
}
self.varint_codec.serialise(item.remaining(), dst);
dst.reserve(item.remaining());
dst.put(item);
Ok(())
}
}
#[cfg(feature = "codec")]
impl<T: Buf> tokio_util::codec::Encoder<T> for UviBytes<T> {
type Error = io::Error;
fn encode(&mut self, item: T, dst: &mut BytesMut) -> Result<(), Self::Error> {
self.serialise(item, dst)
}
}
#[cfg(feature = "codec")]
impl<T> tokio_util::codec::Decoder for UviBytes<T> {
type Item = BytesMut;
type Error = io::Error;
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
self.deserialise(src)
}
}
#[cfg(feature = "futures-codec")]
impl<T: Buf> futures_codec::Encoder for UviBytes<T> {
type Item = T;
type Error = io::Error;
fn encode(&mut self, item: Self::Item, dst: &mut BytesMut) -> Result<(), Self::Error> {
self.serialise(item, dst)
}
}
#[cfg(feature = "futures-codec")]
impl<T> futures_codec::Decoder for UviBytes<T> {
type Item = BytesMut;
type Error = io::Error;
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
self.deserialise(src)
}
}