Skip to content

Commit 7cdf7d3

Browse files
Deserialize
1 parent 9e5c028 commit 7cdf7d3

File tree

4 files changed

+56
-30
lines changed

4 files changed

+56
-30
lines changed

src/binary_protocol.rs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ impl<'a> BinarySerializer<'a> {
2121
}
2222

2323
impl<'a> Serializer for BinarySerializer<'a> {
24-
type Error = Error;
2524

2625
fn serialize_bool(&mut self, val: bool) -> Result<(), Error> {
2726
if val {
@@ -91,9 +90,7 @@ impl<'a> Serializer for BinarySerializer<'a> {
9190
}
9291

9392
impl<'a> ThriftSerializer for BinarySerializer<'a> {
94-
type TError = Error;
95-
96-
fn write_message_begin(&mut self, name: &str, message_type: ThriftMessageType) -> Result<(), Self::TError> {
93+
fn write_message_begin(&mut self, name: &str, message_type: ThriftMessageType) -> Result<(), Error> {
9794
let version = THRIFT_VERSION_1 | message_type as i32;
9895

9996
try!(self.serialize_i32(version));
@@ -103,30 +100,30 @@ impl<'a> ThriftSerializer for BinarySerializer<'a> {
103100
Ok(())
104101
}
105102

106-
fn write_struct_begin(&mut self, name: &str) -> Result<(), Self::TError> {
103+
fn write_struct_begin(&mut self, name: &str) -> Result<(), Error> {
107104
Ok(())
108105
}
109106

110-
fn write_struct_end(&mut self) -> Result<(), Self::TError> {
107+
fn write_struct_end(&mut self) -> Result<(), Error> {
111108
Ok(())
112109
}
113110

114-
fn write_field_begin(&mut self, name: &str, ty: ThriftType, id: i16) -> Result<(), Self::TError> {
111+
fn write_field_begin(&mut self, name: &str, ty: ThriftType, id: i16) -> Result<(), Error> {
115112
try!(self.serialize_i8(ty as i8));
116113
try!(self.serialize_i16(id));
117114
Ok(())
118115
}
119116

120-
fn write_field_end(&mut self) -> Result<(), Self::TError> {
117+
fn write_field_end(&mut self) -> Result<(), Error> {
121118
Ok(())
122119
}
123120

124-
fn write_field_stop(&mut self) -> Result<(), Self::TError> {
121+
fn write_field_stop(&mut self) -> Result<(), Error> {
125122
try!(self.serialize_i8(ThriftType::Stop as i8));
126123
Ok(())
127124
}
128125

129-
fn write_message_end(&mut self) -> Result<(), Self::TError> {
126+
fn write_message_end(&mut self) -> Result<(), Error> {
130127
Ok(())
131128
}
132129
}
@@ -135,9 +132,15 @@ pub struct BinaryDeserializer<R: Read + ReadBytesExt> {
135132
rd: R
136133
}
137134

138-
impl<R: Read + ReadBytesExt> Deserializer for BinaryDeserializer<R> {
139-
type Error = Error;
135+
impl<R: Read + ReadBytesExt> BinaryDeserializer<R> {
136+
pub fn new(rd: R) -> BinaryDeserializer<R> {
137+
BinaryDeserializer {
138+
rd: rd
139+
}
140+
}
141+
}
140142

143+
impl<R: Read + ReadBytesExt> Deserializer for BinaryDeserializer<R> {
141144
fn deserialize_bool(&mut self) -> Result<bool, Error> {
142145
Ok(try!(self.rd.read_i8()) != 0)
143146
}
@@ -199,9 +202,7 @@ impl<R: Read + ReadBytesExt> Deserializer for BinaryDeserializer<R> {
199202
}
200203

201204
impl<R: Read + ReadBytesExt> ThriftDeserializer for BinaryDeserializer<R> {
202-
type TError = Error;
203-
204-
fn read_message_begin(&mut self) -> Result<ThriftMessage, Self::TError> {
205+
fn read_message_begin(&mut self) -> Result<ThriftMessage, Error> {
205206
let size: i32 = try!(self.deserialize_i32());
206207

207208
if size < 0 {
@@ -220,19 +221,19 @@ impl<R: Read + ReadBytesExt> ThriftDeserializer for BinaryDeserializer<R> {
220221
}
221222
}
222223

223-
fn read_message_end(&mut self) -> Result<(), Self::TError> {
224+
fn read_message_end(&mut self) -> Result<(), Error> {
224225
Ok(())
225226
}
226227

227-
fn read_struct_begin(&mut self) -> Result<String, Self::TError> {
228+
fn read_struct_begin(&mut self) -> Result<String, Error> {
228229
Ok("".to_string())
229230
}
230231

231-
fn read_struct_end(&mut self) -> Result<(), Self::TError> {
232+
fn read_struct_end(&mut self) -> Result<(), Error> {
232233
Ok(())
233234
}
234235

235-
fn read_field_begin(&mut self) -> Result<ThriftField, Self::TError> {
236+
fn read_field_begin(&mut self) -> Result<ThriftField, Error> {
236237
let mut field = ThriftField {
237238
name: None,
238239
ty: ThriftType::from(try!(self.deserialize_i8())),
@@ -247,7 +248,7 @@ impl<R: Read + ReadBytesExt> ThriftDeserializer for BinaryDeserializer<R> {
247248
}
248249
}
249250

250-
fn read_field_end(&mut self) -> Result<(), Self::TError> {
251+
fn read_field_end(&mut self) -> Result<(), Error> {
251252
Ok(())
252253
}
253254
}

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ mod ast;
1919
mod generator;
2020
mod event_loop;
2121
mod reactor;
22-
mod protocol;
23-
mod binary_protocol;
22+
pub mod protocol;
23+
pub mod binary_protocol;
2424
mod service;
2525

2626
pub type ThriftResult<T> = Result<T, ThriftCompilerError>;

src/protocol.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::convert;
33
use byteorder;
44
use std::string::FromUtf8Error;
55

6+
#[derive(Debug)]
67
pub enum Error {
78
Byteorder(byteorder::Error),
89
Io(io::Error),
@@ -90,8 +91,6 @@ impl convert::From<i8> for ThriftMessageType {
9091
}
9192

9293
pub trait Serializer {
93-
type Error = ();
94-
9594
fn serialize_bool(&mut self, val: bool) -> Result<(), Error>;
9695
fn serialize_str(&mut self, val: &str) -> Result<(), Error>;
9796
fn serialize_string(&mut self, val: String) -> Result<(), Error>;
@@ -109,8 +108,6 @@ pub trait Serializer {
109108
}
110109

111110
pub trait Deserializer {
112-
type Error = ();
113-
114111
fn deserialize_bool(&mut self) -> Result<bool, Error>;
115112
fn deserialize_usize(&mut self) -> Result<usize, Error>;
116113
fn deserialize_isize(&mut self) -> Result<isize, Error>;
@@ -131,8 +128,6 @@ pub trait Serialize {
131128
}
132129

133130
pub trait ThriftSerializer {
134-
type TError = ();
135-
136131
fn write_message_begin(&mut self, name: &str, message_type: ThriftMessageType) -> Result<(), Error> {
137132
Ok(())
138133
}
@@ -175,8 +170,6 @@ pub struct ThriftField {
175170
}
176171

177172
pub trait ThriftDeserializer {
178-
type TError = ();
179-
180173
fn read_message_begin(&mut self) -> Result<ThriftMessage, Error>;
181174
fn read_message_end(&mut self) -> Result<(), Error>;
182175
fn read_struct_begin(&mut self) -> Result<String, Error>;
@@ -185,6 +178,25 @@ pub trait ThriftDeserializer {
185178
fn read_field_end(&mut self) -> Result<(), Error>;
186179
}
187180

181+
pub trait Deserialize: Sized {
182+
fn deserialize(de: &mut Deserializer) -> Result<Self, Error>;
183+
}
184+
185+
/// ```
186+
/// use std::io::Cursor;
187+
/// use thrust::binary_protocol::BinaryDeserializer;
188+
/// use thrust::protocol::Deserialize;
189+
///
190+
/// let mut de = BinaryDeserializer::new(Cursor::new(vec![100u8]));
191+
/// let val: u8 = Deserialize::deserialize(&mut de).unwrap();
192+
/// assert_eq!(val, 100);
193+
/// ```
194+
impl Deserialize for u8 {
195+
fn deserialize(de: &mut Deserializer) -> Result<Self, Error> {
196+
de.deserialize_u8()
197+
}
198+
}
199+
188200
impl Serialize for bool {
189201
fn serialize<S>(&self, s: &mut S) -> Result<(), Error>
190202
where S: Serializer + ThriftSerializer

src/service.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ impl Serialize for QueryArgs {
2727
}
2828
}
2929

30+
31+
pub struct RpcServer;
32+
33+
impl Service for RpcServer {
34+
fn query(&mut self, val: bool) {
35+
36+
}
37+
}
38+
39+
fn dispatch_query(service: &mut Service, args: QueryArgs) {
40+
service.query(args.val);
41+
}
42+
3043
pub struct RpcClient;
3144

3245
impl Service for RpcClient {

0 commit comments

Comments
 (0)