Skip to content

Commit

Permalink
Remove usage of Cursor (#186)
Browse files Browse the repository at this point in the history
* Add intellij files to .gitignore

* Remove Cursor usage when deserializing

* Don't use methods that copy into another temp buffer
  • Loading branch information
tikue authored Apr 15, 2018
1 parent 8c3e3df commit b093db6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ Cargo.lock
.cargo
*.swp
*.bk
tarpc.iml
.idea
16 changes: 8 additions & 8 deletions src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
// This file may not be copied, modified, or distributed except according to those terms.

use bincode;
use byteorder::{BigEndian, ReadBytesExt};
use byteorder::{BigEndian, ByteOrder};
use bytes::BytesMut;
use bytes::buf::BufMut;
use serde;
use std::io::{self, Cursor};
use std::io;
use std::marker::PhantomData;
use std::mem;
use tokio_io::{AsyncRead, AsyncWrite};
Expand All @@ -34,7 +34,7 @@ enum CodecState {
impl<Encode, Decode> Codec<Encode, Decode> {
fn new(max_payload_size: u64) -> Self {
Codec {
max_payload_size: max_payload_size,
max_payload_size,
state: CodecState::Id,
_phantom_data: PhantomData,
}
Expand Down Expand Up @@ -105,9 +105,9 @@ where
}
Id => {
let mut id_buf = buf.split_to(mem::size_of::<u64>());
let id = Cursor::new(&mut id_buf).read_u64::<BigEndian>()?;
let id = BigEndian::read_u64(&*id_buf);
trace!("--> Parsed id = {} from {:?}", id, id_buf);
self.state = Len { id: id };
self.state = Len { id };
}
Len { .. } if buf.len() < mem::size_of::<u64>() => {
trace!(
Expand All @@ -118,7 +118,7 @@ where
}
Len { id } => {
let len_buf = buf.split_to(mem::size_of::<u64>());
let len = Cursor::new(len_buf).read_u64::<BigEndian>()?;
let len = BigEndian::read_u64(&*len_buf);
trace!(
"--> Parsed payload length = {}, remaining buffer length = {}",
len,
Expand All @@ -127,7 +127,7 @@ where
if len > self.max_payload_size {
return Err(too_big(len, self.max_payload_size));
}
self.state = Payload { id: id, len: len };
self.state = Payload { id, len };
}
Payload { len, .. } if buf.len() < len as usize => {
trace!(
Expand All @@ -139,7 +139,7 @@ where
}
Payload { id, len } => {
let payload = buf.split_to(len as usize);
let result = bincode::deserialize_from(&mut Cursor::new(payload));
let result = bincode::deserialize(&payload);
// Reset the state machine because, either way, we're done processing this
// message.
self.state = Id;
Expand Down

0 comments on commit b093db6

Please sign in to comment.