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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
use crate::bufext::BufMutExt;
use crate::errors::ProtocolError;
use crate::framing::{Framing, FramingDecoded, FramingEncoded, FramingEncodedFinal};
use crate::thrift_protocol::{MessageType, ProtocolID};
use crate::ttype::TType;
use crate::Result;
const DEFAULT_RECURSION_DEPTH: i32 = 64;
pub type ProtocolEncoded<P> = FramingEncoded<<P as Protocol>::Frame>;
pub type ProtocolEncodedFinal<P> = FramingEncodedFinal<<P as Protocol>::Frame>;
pub type ProtocolDecoded<P> = FramingDecoded<<P as Protocol>::Frame>;
pub trait Protocol: 'static {
type Frame: Framing;
type Sizer: ProtocolWriter<Final = usize>;
type Serializer: ProtocolWriter<Final = <<Self::Frame as Framing>::EncBuf as BufMutExt>::Final>;
type Deserializer: ProtocolReader;
const PROTOCOL_ID: ProtocolID;
fn serializer<SZ, SER>(sz: SZ, ser: SER) -> <Self::Serializer as ProtocolWriter>::Final
where
SZ: FnOnce(&mut Self::Sizer),
SER: FnOnce(&mut Self::Serializer);
fn deserializer(buf: <Self::Frame as Framing>::DecBuf) -> Self::Deserializer;
fn into_buffer(_: Self::Deserializer) -> <Self::Frame as Framing>::DecBuf;
}
fn skip_inner<P: ProtocolReader + ?Sized>(
p: &mut P,
field_type: TType,
max_depth: i32,
) -> Result<()> {
if max_depth <= 0 {
bail_err!(ProtocolError::SkipDepthExceeded)
}
match field_type {
TType::Void => {}
TType::Bool => {
p.read_bool()?;
}
TType::Byte => {
p.read_byte()?;
}
TType::I16 => {
p.read_i16()?;
}
TType::I32 => {
p.read_i32()?;
}
TType::I64 => {
p.read_i64()?;
}
TType::Double => {
p.read_double()?;
}
TType::Float => {
p.read_float()?;
}
TType::Struct => {
p.read_struct_begin(|_| ())?;
loop {
let (_, type_id, _) = p.read_field_begin(|_| ())?;
if type_id == TType::Stop {
break;
}
skip_inner(p, type_id, max_depth - 1)?;
p.read_field_end()?;
}
p.read_struct_end()?;
}
TType::Map => {
let (key_type, value_type, size) = p.read_map_begin()?;
for _ in 0..size {
skip_inner(p, key_type, max_depth - 1)?;
skip_inner(p, value_type, max_depth)?;
}
p.read_map_end()?;
}
TType::Set => {
let (elem_type, size) = p.read_set_begin()?;
for _ in 0..size {
skip_inner(p, elem_type, max_depth - 1)?;
}
p.read_set_end()?;
}
TType::List => {
let (elem_type, size) = p.read_list_begin()?;
for _ in 0..size {
skip_inner(p, elem_type, max_depth - 1)?;
}
p.read_list_end()?;
}
TType::UTF8 => {
p.read_string()?;
}
TType::UTF16 => {
p.read_string()?;
}
TType::String => {
p.read_binary()?;
}
TType::Stream => bail_err!(ProtocolError::StreamUnsupported),
TType::Stop => bail_err!(ProtocolError::UnexpectedStopInSkip),
};
Ok(())
}
pub trait ProtocolWriter {
type Final;
fn write_message_begin(&mut self, name: &str, type_id: MessageType, seqid: u32);
fn write_message_end(&mut self);
fn write_struct_begin(&mut self, name: &str);
fn write_struct_end(&mut self);
fn write_field_begin(&mut self, name: &str, type_id: TType, id: i16);
fn write_field_end(&mut self);
fn write_field_stop(&mut self);
fn write_map_begin(&mut self, key_type: TType, value_type: TType, size: usize);
fn write_map_key_begin(&mut self);
fn write_map_value_begin(&mut self);
fn write_map_end(&mut self);
fn write_list_begin(&mut self, elem_type: TType, size: usize);
fn write_list_value_begin(&mut self);
fn write_list_end(&mut self);
fn write_set_begin(&mut self, elem_type: TType, size: usize);
fn write_set_value_begin(&mut self);
fn write_set_end(&mut self);
fn write_bool(&mut self, value: bool);
fn write_byte(&mut self, value: i8);
fn write_i16(&mut self, value: i16);
fn write_i32(&mut self, value: i32);
fn write_i64(&mut self, value: i64);
fn write_double(&mut self, value: f64);
fn write_float(&mut self, value: f32);
fn write_string(&mut self, value: &str);
fn write_binary(&mut self, value: &[u8]);
fn finish(self) -> Self::Final;
}
pub trait ProtocolReader {
fn read_message_begin<F, T>(&mut self, method: F) -> Result<(T, MessageType, u32)>
where
F: FnOnce(&[u8]) -> T;
fn read_message_end(&mut self) -> Result<()>;
fn read_struct_begin<F, T>(&mut self, strukt: F) -> Result<T>
where
F: FnOnce(&[u8]) -> T;
fn read_struct_end(&mut self) -> Result<()>;
fn read_field_begin<F, T>(&mut self, field: F) -> Result<(T, TType, i16)>
where
F: FnOnce(&[u8]) -> T;
fn read_field_end(&mut self) -> Result<()>;
fn read_map_begin(&mut self) -> Result<(TType, TType, usize)>;
fn read_map_key_begin(&mut self) -> Result<()>;
fn read_map_value_begin(&mut self) -> Result<()>;
fn read_map_end(&mut self) -> Result<()>;
fn read_list_begin(&mut self) -> Result<(TType, usize)>;
fn read_list_value_begin(&mut self) -> Result<()>;
fn read_list_end(&mut self) -> Result<()>;
fn read_set_begin(&mut self) -> Result<(TType, usize)>;
fn read_set_value_begin(&mut self) -> Result<()>;
fn read_set_end(&mut self) -> Result<()>;
fn read_bool(&mut self) -> Result<bool>;
fn read_byte(&mut self) -> Result<i8>;
fn read_i16(&mut self) -> Result<i16>;
fn read_i32(&mut self) -> Result<i32>;
fn read_i64(&mut self) -> Result<i64>;
fn read_double(&mut self) -> Result<f64>;
fn read_float(&mut self) -> Result<f32>;
fn read_string(&mut self) -> Result<String>;
fn read_binary(&mut self) -> Result<Vec<u8>>;
fn skip(&mut self, field_type: TType) -> Result<()> {
skip_inner(self, field_type, DEFAULT_RECURSION_DEPTH)
}
}
#[macro_export]
macro_rules! serialize {
($protocol:ty, $serializer:expr) => {
<$protocol as $crate::Protocol>::serializer($serializer, $serializer)
};
}
pub fn write_message<P, F>(p: &mut P, name: &str, messagetype: MessageType, seqid: u32, body: F)
where
P: ProtocolWriter,
F: FnOnce(&mut P),
{
p.write_message_begin(name, messagetype, seqid);
body(p);
p.write_message_end();
}