From bfbd4fe05ab8cee6049114968b88cd42ad472147 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 --- src/item.rs | 6 +-- src/nbt/mod.rs | 15 +++--- src/protocol/mod.rs | 108 ++++++++++++++++++++--------------------- src/protocol/packet.rs | 28 +++++------ src/types/metadata.rs | 8 ++- 5 files changed, 80 insertions(+), 85 deletions(-) diff --git a/src/item.rs b/src/item.rs index eeeeee3..45e4017 100644 --- a/src/item.rs +++ b/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/src/nbt/mod.rs b/src/nbt/mod.rs index 4ea53bf..5883c9e 100644 --- a/src/nbt/mod.rs +++ b/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/src/protocol/mod.rs b/src/protocol/mod.rs index 347c8c4..fd86e6d 100644 --- a/src/protocol/mod.rs +++ b/src/protocol/mod.rs @@ -93,7 +93,7 @@ macro_rules! state_packets { fn packet_id(&self) -> i32 { internal_ids::$name } - fn write(self, buf: &mut io::Write) -> Result<(), io::Error> { + fn write(self, buf: &mut W) -> Result<(), Error> { $( if true $(&& ($cond(&self)))* { try!(self.$field.write_to(buf)); @@ -111,7 +111,7 @@ macro_rules! state_packets { /// Returns the packet for the given state, direction and id after parsing the fields /// from the buffer. - pub fn packet_by_id(state: State, dir: Direction, id: i32, mut buf: &mut io::Read) -> Result, io::Error> { + pub fn packet_by_id(state: State, dir: Direction, id: i32, mut buf: &mut R) -> Result, Error> { match state { $( State::$stateName => { @@ -146,24 +146,24 @@ macro_rules! state_packets { pub mod packet; pub trait Serializable: Sized { - fn read_from(buf: &mut io::Read) -> Result; - fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error>; + fn read_from(buf: &mut R) -> Result; + fn write_to(&self, buf: &mut W) -> Result<(), Error>; } impl Serializable for Vec { - fn read_from(buf: &mut io::Read) -> Result, io::Error> { + fn read_from(buf: &mut R) -> Result, Error> { let mut v = Vec::new(); try!(buf.read_to_end(&mut v)); Ok(v) } - fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> { - buf.write_all(&self[..]) + fn write_to(&self, buf: &mut W) -> Result<(), Error> { + buf.write_all(&self[..]).map_err(|v| v.into()) } } impl Serializable for Option{ - fn read_from(buf: &mut io::Read) -> Result, io::Error> { + fn read_from(buf: &mut R) -> Result, Error> { let ty = try!(buf.read_u8()); if ty == 0 { Result::Ok(None) @@ -173,7 +173,7 @@ impl Serializable for Option{ Result::Ok(Some(nbt::NamedTag(name, tag))) } } - fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> { + fn write_to(&self, buf: &mut W) -> Result<(), Error> { match *self { Some(ref val) => { try!(buf.write_u8(10)); @@ -187,10 +187,10 @@ impl Serializable for Option{ } impl Serializable for Option where T : Serializable { - fn read_from(buf: &mut io::Read) -> Result, io::Error> { + fn read_from(buf: &mut R) -> Result, Error> { Result::Ok(Some(try!(T::read_from(buf)))) } - fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> { + fn write_to(&self, buf: &mut W) -> Result<(), Error> { if self.is_some() { try!(self.as_ref().unwrap().write_to(buf)); } @@ -199,13 +199,13 @@ impl Serializable for Option where T : Serializable { } impl Serializable for String { - fn read_from(buf: &mut io::Read) -> Result { + fn read_from(buf: &mut R) -> Result { let len = try!(VarInt::read_from(buf)).0; let mut ret = String::new(); try!(buf.take(len as u64).read_to_string(&mut ret)); Result::Ok(ret) } - fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> { + fn write_to(&self, buf: &mut W) -> Result<(), Error> { let bytes = self.as_bytes(); try!(VarInt(bytes.len() as i32).write_to(buf)); try!(buf.write_all(bytes)); @@ -214,14 +214,14 @@ impl Serializable for String { } impl Serializable for format::Component { - fn read_from(buf: &mut io::Read) -> Result { + fn read_from(buf: &mut R) -> Result { let len = try!(VarInt::read_from(buf)).0; let mut ret = String::new(); try!(buf.take(len as u64).read_to_string(&mut ret)); let val: serde_json::Value = serde_json::from_str(&ret[..]).unwrap(); Result::Ok(Self::from_value(&val)) } - fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> { + fn write_to(&self, buf: &mut W) -> Result<(), Error> { let val = serde_json::to_string(&self.to_value()).unwrap(); let bytes = val.as_bytes(); try!(VarInt(bytes.len() as i32).write_to(buf)); @@ -231,19 +231,19 @@ impl Serializable for format::Component { } impl Serializable for () { - fn read_from(_: &mut io::Read) -> Result<(), io::Error> { + fn read_from(_: &mut R) -> Result<(), Error> { Result::Ok(()) } - fn write_to(&self, _: &mut io::Write) -> Result<(), io::Error> { + fn write_to(&self, buf: &mut W) -> Result<(), Error> { Result::Ok(()) } } impl Serializable for bool { - fn read_from(buf: &mut io::Read) -> Result { + fn read_from(buf: &mut R) -> Result { Result::Ok(try!(buf.read_u8()) != 0) } - fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> { + fn write_to(&self, buf: &mut W) -> Result<(), Error> { try!(buf.write_u8(if *self { 1 } else { @@ -254,90 +254,90 @@ impl Serializable for bool { } impl Serializable for i8 { - fn read_from(buf: &mut io::Read) -> Result { + fn read_from(buf: &mut R) -> Result { Result::Ok(try!(buf.read_i8())) } - fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> { + fn write_to(&self, buf: &mut W) -> Result<(), Error> { try!(buf.write_i8(*self)); Result::Ok(()) } } impl Serializable for i16 { - fn read_from(buf: &mut io::Read) -> Result { + fn read_from(buf: &mut R) -> Result { Result::Ok(try!(buf.read_i16::())) } - fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> { + fn write_to(&self, buf: &mut W) -> Result<(), Error> { try!(buf.write_i16::(*self)); Result::Ok(()) } } impl Serializable for i32 { - fn read_from(buf: &mut io::Read) -> Result { + fn read_from(buf: &mut R) -> Result { Result::Ok(try!(buf.read_i32::())) } - fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> { + fn write_to(&self, buf: &mut W) -> Result<(), Error> { try!(buf.write_i32::(*self)); Result::Ok(()) } } impl Serializable for i64 { - fn read_from(buf: &mut io::Read) -> Result { + fn read_from(buf: &mut R) -> Result { Result::Ok(try!(buf.read_i64::())) } - fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> { + fn write_to(&self, buf: &mut W) -> Result<(), Error> { try!(buf.write_i64::(*self)); Result::Ok(()) } } impl Serializable for u8 { - fn read_from(buf: &mut io::Read) -> Result { + fn read_from(buf: &mut R) -> Result { Result::Ok(try!(buf.read_u8())) } - fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> { + fn write_to(&self, buf: &mut W) -> Result<(), Error> { try!(buf.write_u8(*self)); Result::Ok(()) } } impl Serializable for u16 { - fn read_from(buf: &mut io::Read) -> Result { + fn read_from(buf: &mut R) -> Result { Result::Ok(try!(buf.read_u16::())) } - fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> { + fn write_to(&self, buf: &mut W) -> Result<(), Error> { try!(buf.write_u16::(*self)); Result::Ok(()) } } impl Serializable for u64 { - fn read_from(buf: &mut io::Read) -> Result { + fn read_from(buf: &mut R) -> Result { Result::Ok(try!(buf.read_u64::())) } - fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> { + fn write_to(&self, buf: &mut W) -> Result<(), Error> { try!(buf.write_u64::(*self)); Result::Ok(()) } } impl Serializable for f32 { - fn read_from(buf: &mut io::Read) -> Result { + fn read_from(buf: &mut R) -> Result { Result::Ok(try!(buf.read_f32::())) } - fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> { + fn write_to(&self, buf: &mut W) -> Result<(), Error> { try!(buf.write_f32::(*self)); Result::Ok(()) } } impl Serializable for f64 { - fn read_from(buf: &mut io::Read) -> Result { + fn read_from(buf: &mut R) -> Result { Result::Ok(try!(buf.read_f64::())) } - fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> { + fn write_to(&self, buf: &mut W) -> Result<(), Error> { try!(buf.write_f64::(*self)); Result::Ok(()) } @@ -375,11 +375,11 @@ impl Default for UUID { } impl Serializable for UUID { - fn read_from(buf: &mut io::Read) -> Result { + fn read_from(buf: &mut R) -> Result { Result::Ok(UUID(try!(buf.read_u64::()), try!(buf.read_u64::()))) } - fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> { + fn write_to(&self, buf: &mut W) -> Result<(), Error> { try!(buf.write_u64::(self.0)); try!(buf.write_u64::(self.1)); Result::Ok(()) @@ -407,7 +407,7 @@ impl LenPrefixed { } impl Serializable for LenPrefixed { - fn read_from(buf: &mut io::Read) -> Result, io::Error> { + fn read_from(buf: &mut R) -> Result, Error> { let len_data: L = try!(Serializable::read_from(buf)); let len: usize = len_data.into(); let mut data: Vec = Vec::with_capacity(len); @@ -420,7 +420,7 @@ impl Serializable for LenPrefixed { }) } - fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> { + fn write_to(&self, buf: &mut W) -> Result<(), Error> { let len_data: L = L::from(self.data.len()); try!(len_data.write_to(buf)); let data = &self.data; @@ -463,7 +463,7 @@ impl LenPrefixedBytes { } impl Serializable for LenPrefixedBytes { - fn read_from(buf: &mut io::Read) -> Result, io::Error> { + fn read_from(buf: &mut R) -> Result, Error> { let len_data: L = try!(Serializable::read_from(buf)); let len: usize = len_data.into(); let mut data: Vec = Vec::with_capacity(len); @@ -474,7 +474,7 @@ impl Serializable for LenPrefixedBytes { }) } - fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> { + fn write_to(&self, buf: &mut W) -> Result<(), Error> { let len_data: L = L::from(self.data.len()); try!(len_data.write_to(buf)); try!(buf.write_all(&self.data[..])); @@ -535,7 +535,7 @@ impl Lengthable for VarInt { impl Serializable for VarInt { /// Decodes a `VarInt` from the Reader - fn read_from(buf: &mut io::Read) -> Result { + fn read_from(buf: &mut R) -> Result { const PART : u32 = 0x7F; let mut size = 0; let mut val = 0u32; @@ -544,8 +544,7 @@ impl Serializable for VarInt { val |= (b & PART) << (size * 7); size += 1; if size > 5 { - return Result::Err(io::Error::new(io::ErrorKind::InvalidInput, - Error::Err("VarInt too big".to_owned()))) + return Result::Err(Error::Err("VarInt too big".to_owned())); } if (b & 0x80) == 0 { break @@ -556,7 +555,7 @@ impl Serializable for VarInt { } /// Encodes a `VarInt` into the Writer - fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> { + fn write_to(&self, buf: &mut W) -> Result<(), Error> { const PART : u32 = 0x7F; let mut val = self.0 as u32; loop { @@ -599,7 +598,7 @@ impl Lengthable for VarLong { impl Serializable for VarLong { /// Decodes a `VarLong` from the Reader - fn read_from(buf: &mut io::Read) -> Result { + fn read_from(buf: &mut R) -> Result { const PART : u64 = 0x7F; let mut size = 0; let mut val = 0u64; @@ -608,8 +607,7 @@ impl Serializable for VarLong { val |= (b & PART) << (size * 7); size += 1; if size > 10 { - return Result::Err(io::Error::new(io::ErrorKind::InvalidInput, - Error::Err("VarLong too big".to_owned()))) + return Result::Err(Error::Err("VarLong too big".to_owned())); } if (b & 0x80) == 0 { break @@ -620,7 +618,7 @@ impl Serializable for VarLong { } /// Encodes a `VarLong` into the Writer - fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> { + fn write_to(&self, buf: &mut W) -> Result<(), Error> { const PART : u64 = 0x7F; let mut val = self.0 as u64; loop { @@ -647,7 +645,7 @@ impl fmt::Debug for VarLong { } impl Serializable for Position { - fn read_from(buf: &mut io::Read) -> Result { + fn read_from(buf: &mut R) -> Result { let pos = try!(buf.read_u64::()); Ok(Position::new( ((pos as i64) >> 38) as i32, @@ -655,7 +653,7 @@ impl Serializable for Position { ((pos as i64) << 38 >> 38) as i32 )) } - fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> { + fn write_to(&self, buf: &mut W) -> Result<(), Error> { let pos = (((self.x as u64) & 0x3FFFFFF) << 38) | (((self.y as u64) & 0xFFF) << 26) | ((self.z as u64) & 0x3FFFFFF); @@ -1010,8 +1008,8 @@ impl Clone for Conn { } } -pub trait PacketType: Sized { +pub trait PacketType { fn packet_id(&self) -> i32; - fn write(self, buf: &mut io::Write) -> Result<(), io::Error>; + fn write(self, buf: &mut W) -> Result<(), Error>; } diff --git a/src/protocol/packet.rs b/src/protocol/packet.rs index c931a8d..5dc47c6 100644 --- a/src/protocol/packet.rs +++ b/src/protocol/packet.rs @@ -957,14 +957,14 @@ pub struct Statistic { } impl Serializable for Statistic { - fn read_from(buf: &mut io::Read) -> Result { + fn read_from(buf: &mut R) -> Result { Ok(Statistic { name: try!(Serializable::read_from(buf)), value: try!(Serializable::read_from(buf)), }) } - fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> { + fn write_to(&self, buf: &mut W) -> Result<(), Error> { try!(self.name.write_to(buf)); self.value.write_to(buf) } @@ -978,7 +978,7 @@ pub struct BlockChangeRecord { } impl Serializable for BlockChangeRecord { - fn read_from(buf: &mut io::Read) -> Result { + fn read_from(buf: &mut R) -> Result { Ok(BlockChangeRecord { xz: try!(Serializable::read_from(buf)), y: try!(Serializable::read_from(buf)), @@ -986,7 +986,7 @@ impl Serializable for BlockChangeRecord { }) } - fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> { + fn write_to(&self, buf: &mut W) -> Result<(), Error> { try!(self.xz.write_to(buf)); try!(self.y.write_to(buf)); self.block_id.write_to(buf) @@ -1001,7 +1001,7 @@ pub struct ExplosionRecord { } impl Serializable for ExplosionRecord { - fn read_from(buf: &mut io::Read) -> Result { + fn read_from(buf: &mut R) -> Result { Ok(ExplosionRecord { x: try!(Serializable::read_from(buf)), y: try!(Serializable::read_from(buf)), @@ -1009,7 +1009,7 @@ impl Serializable for ExplosionRecord { }) } - fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> { + fn write_to(&self, buf: &mut W) -> Result<(), Error> { try!(self.x.write_to(buf)); try!(self.y.write_to(buf)); self.z.write_to(buf) @@ -1024,7 +1024,7 @@ pub struct MapIcon { } impl Serializable for MapIcon { - fn read_from(buf: &mut io::Read) -> Result { + fn read_from(buf: &mut R) -> Result { Ok(MapIcon { direction_type: try!(Serializable::read_from(buf)), x: try!(Serializable::read_from(buf)), @@ -1032,7 +1032,7 @@ impl Serializable for MapIcon { }) } - fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> { + fn write_to(&self, buf: &mut W) -> Result<(), Error> { try!(self.direction_type.write_to(buf)); try!(self.x.write_to(buf)); self.z.write_to(buf) @@ -1057,7 +1057,7 @@ pub struct EntityProperty { } impl Serializable for EntityProperty { - fn read_from(buf: &mut io::Read) -> Result { + fn read_from(buf: &mut R) -> Result { Ok(EntityProperty { key: try!(Serializable::read_from(buf)), value: try!(Serializable::read_from(buf)), @@ -1065,7 +1065,7 @@ impl Serializable for EntityProperty { }) } - fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> { + fn write_to(&self, buf: &mut W) -> Result<(), Error> { try!(self.key.write_to(buf)); try!(self.value.write_to(buf)); self.modifiers.write_to(buf) @@ -1080,7 +1080,7 @@ pub struct PropertyModifier { } impl Serializable for PropertyModifier { - fn read_from(buf: &mut io::Read) -> Result { + fn read_from(buf: &mut R) -> Result { Ok(PropertyModifier { uuid: try!(Serializable::read_from(buf)), amount: try!(Serializable::read_from(buf)), @@ -1088,7 +1088,7 @@ impl Serializable for PropertyModifier { }) } - fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> { + fn write_to(&self, buf: &mut W) -> Result<(), Error> { try!(self.uuid.write_to(buf)); try!(self.amount.write_to(buf)); self.operation.write_to(buf) @@ -1102,7 +1102,7 @@ pub struct PlayerInfoData { } impl Serializable for PlayerInfoData { - fn read_from(buf: &mut io::Read) -> Result { + fn read_from(buf: &mut R) -> Result { let mut m = PlayerInfoData { action: try!(Serializable::read_from(buf)), players: Vec::new(), @@ -1175,7 +1175,7 @@ impl Serializable for PlayerInfoData { Ok(m) } - fn write_to(&self, _: &mut io::Write) -> Result<(), io::Error> { + fn write_to(&self, _: &mut W) -> Result<(), Error> { unimplemented!() // I'm lazy } } diff --git a/src/types/metadata.rs b/src/types/metadata.rs index eab44a6..e5c8040 100644 --- a/src/types/metadata.rs +++ b/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 {