4
4
// This file may not be copied, modified, or distributed except according to those terms.
5
5
6
6
use bincode;
7
- use byteorder:: { BigEndian , ReadBytesExt } ;
7
+ use byteorder:: { BigEndian , ByteOrder } ;
8
8
use bytes:: BytesMut ;
9
9
use bytes:: buf:: BufMut ;
10
10
use serde;
11
- use std:: io:: { self , Cursor } ;
11
+ use std:: io;
12
12
use std:: marker:: PhantomData ;
13
13
use std:: mem;
14
14
use tokio_io:: { AsyncRead , AsyncWrite } ;
@@ -34,7 +34,7 @@ enum CodecState {
34
34
impl < Encode , Decode > Codec < Encode , Decode > {
35
35
fn new ( max_payload_size : u64 ) -> Self {
36
36
Codec {
37
- max_payload_size : max_payload_size ,
37
+ max_payload_size,
38
38
state : CodecState :: Id ,
39
39
_phantom_data : PhantomData ,
40
40
}
@@ -105,9 +105,9 @@ where
105
105
}
106
106
Id => {
107
107
let mut id_buf = buf. split_to ( mem:: size_of :: < u64 > ( ) ) ;
108
- let id = Cursor :: new ( & mut id_buf) . read_u64 :: < BigEndian > ( ) ? ;
108
+ let id = BigEndian :: read_u64 ( & * id_buf) ;
109
109
trace ! ( "--> Parsed id = {} from {:?}" , id, id_buf) ;
110
- self . state = Len { id : id } ;
110
+ self . state = Len { id } ;
111
111
}
112
112
Len { .. } if buf. len ( ) < mem:: size_of :: < u64 > ( ) => {
113
113
trace ! (
@@ -118,7 +118,7 @@ where
118
118
}
119
119
Len { id } => {
120
120
let len_buf = buf. split_to ( mem:: size_of :: < u64 > ( ) ) ;
121
- let len = Cursor :: new ( len_buf) . read_u64 :: < BigEndian > ( ) ? ;
121
+ let len = BigEndian :: read_u64 ( & * len_buf) ;
122
122
trace ! (
123
123
"--> Parsed payload length = {}, remaining buffer length = {}" ,
124
124
len,
@@ -127,7 +127,7 @@ where
127
127
if len > self . max_payload_size {
128
128
return Err ( too_big ( len, self . max_payload_size ) ) ;
129
129
}
130
- self . state = Payload { id : id , len : len } ;
130
+ self . state = Payload { id, len } ;
131
131
}
132
132
Payload { len, .. } if buf. len ( ) < len as usize => {
133
133
trace ! (
@@ -139,7 +139,7 @@ where
139
139
}
140
140
Payload { id, len } => {
141
141
let payload = buf. split_to ( len as usize ) ;
142
- let result = bincode:: deserialize_from ( & mut Cursor :: new ( payload) ) ;
142
+ let result = bincode:: deserialize ( & payload) ;
143
143
// Reset the state machine because, either way, we're done processing this
144
144
// message.
145
145
self . state = Id ;
0 commit comments