From 862cf9733139293875e6dc1c1dbd4812dadfc563 Mon Sep 17 00:00:00 2001 From: Thinkofname Date: Fri, 8 Apr 2016 18:46:07 +0100 Subject: [PATCH] Clean up the protocol implementation to use generics instead of trait objects --- protocol/src/item.rs | 6 +++--- protocol/src/nbt/mod.rs | 15 +++++++-------- protocol/src/types/metadata.rs | 8 +++----- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/protocol/src/item.rs b/protocol/src/item.rs index eeeeee3..45e4017 100644 --- a/protocol/src/item.rs +++ b/protocol/src/item.rs @@ -13,7 +13,7 @@ // limitations under the License. use nbt; -use protocol::Serializable; +use protocol::{self, Serializable}; use std::io; use byteorder::{BigEndian, WriteBytesExt, ReadBytesExt}; @@ -38,7 +38,7 @@ impl Default for Stack { } impl Serializable for Option { - fn read_from(buf: &mut io::Read) -> Result, io::Error> { + fn read_from(buf: &mut R) -> Result, protocol::Error> { let id = try!(buf.read_i16::()); if id == -1 { return Ok(None); @@ -50,7 +50,7 @@ impl Serializable for Option { tag: try!(Serializable::read_from(buf)), })) } - fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> { + fn write_to(&self, buf: &mut W) -> Result<(), protocol::Error> { match *self { Some(ref val) => { try!(buf.write_i16::(val.id as i16)); diff --git a/protocol/src/nbt/mod.rs b/protocol/src/nbt/mod.rs index 4ea53bf..5883c9e 100644 --- a/protocol/src/nbt/mod.rs +++ b/protocol/src/nbt/mod.rs @@ -172,7 +172,7 @@ impl Tag { } } - fn read_type(id: u8, buf: &mut io::Read) -> Result { + fn read_type(id: u8, buf: &mut R) -> Result { match id { 0 => unreachable!(), 1 => Ok(Tag::Byte(try!(buf.read_i8()))), @@ -217,18 +217,17 @@ impl Tag { } data })), - _ => Err(io::Error::new(io::ErrorKind::InvalidData, - protocol::Error::Err("invalid tag".to_owned()))), + _ => Err(protocol::Error::Err("invalid tag".to_owned())), } } } impl Serializable for Tag { - fn read_from(buf: &mut io::Read) -> Result { + fn read_from(buf: &mut R) -> Result { Tag::read_type(10, buf) } - fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> { + fn write_to(&self, buf: &mut W) -> Result<(), protocol::Error> { match *self { Tag::End => {} Tag::Byte(val) => try!(buf.write_i8(val)), @@ -273,13 +272,13 @@ impl Serializable for Tag { } } -pub fn write_string(buf: &mut io::Write, s: &str) -> io::Result<()> { +pub fn write_string(buf: &mut W, s: &str) -> Result<(), protocol::Error> { let data = s.as_bytes(); try!((data.len() as i16).write_to(buf)); - buf.write_all(data) + buf.write_all(data).map_err(|v| v.into()) } -pub fn read_string(buf: &mut io::Read) -> io::Result { +pub fn read_string(buf: &mut R) -> Result { let len: i16 = try!(buf.read_i16::()); let mut ret = String::new(); try!(buf.take(len as u64).read_to_string(&mut ret)); diff --git a/protocol/src/types/metadata.rs b/protocol/src/types/metadata.rs index eab44a6..e5c8040 100644 --- a/protocol/src/types/metadata.rs +++ b/protocol/src/types/metadata.rs @@ -62,7 +62,7 @@ impl Metadata { impl Serializable for Metadata { - fn read_from(buf: &mut io::Read) -> Result { + fn read_from(buf: &mut R) -> Result { let mut m = Metadata::new(); loop { let index = try!(u8::read_from(buf)) as i32; @@ -99,15 +99,13 @@ impl Serializable for Metadata { } } 12 => m.put_raw(index, try!(protocol::VarInt::read_from(buf)).0 as u16), - _ => return Err(io::Error::new(io::ErrorKind::InvalidInput, - protocol::Error::Err("unknown metadata type" - .to_owned()))), + _ => return Err(protocol::Error::Err("unknown metadata type".to_owned())), } } Ok(m) } - fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> { + fn write_to(&self, buf: &mut W) -> Result<(), protocol::Error> { for (k, v) in &self.map { try!((*k as u8).write_to(buf)); match *v {