From b093db63a396e229b91bea117f353cf810339997 Mon Sep 17 00:00:00 2001 From: Tim Date: Sat, 14 Apr 2018 21:16:35 -0700 Subject: [PATCH] Remove usage of Cursor (#186) * Add intellij files to .gitignore * Remove Cursor usage when deserializing * Don't use methods that copy into another temp buffer --- .gitignore | 2 ++ src/protocol.rs | 16 ++++++++-------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index cd2dea55..15eeb075 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ Cargo.lock .cargo *.swp *.bk +tarpc.iml +.idea diff --git a/src/protocol.rs b/src/protocol.rs index 61930b21..e7e31313 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -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}; @@ -34,7 +34,7 @@ enum CodecState { impl Codec { fn new(max_payload_size: u64) -> Self { Codec { - max_payload_size: max_payload_size, + max_payload_size, state: CodecState::Id, _phantom_data: PhantomData, } @@ -105,9 +105,9 @@ where } Id => { let mut id_buf = buf.split_to(mem::size_of::()); - let id = Cursor::new(&mut id_buf).read_u64::()?; + 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::() => { trace!( @@ -118,7 +118,7 @@ where } Len { id } => { let len_buf = buf.split_to(mem::size_of::()); - let len = Cursor::new(len_buf).read_u64::()?; + let len = BigEndian::read_u64(&*len_buf); trace!( "--> Parsed payload length = {}, remaining buffer length = {}", len, @@ -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!( @@ -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;