Clean up the protocol implementation to use generics instead of trait objects

This commit is contained in:
Thinkofname 2016-04-08 18:46:07 +01:00
parent d0704b2a67
commit 862cf97331
3 changed files with 13 additions and 16 deletions

View File

@ -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<Stack> {
fn read_from(buf: &mut io::Read) -> Result<Option<Stack>, io::Error> {
fn read_from<R: io::Read>(buf: &mut R) -> Result<Option<Stack>, protocol::Error> {
let id = try!(buf.read_i16::<BigEndian>());
if id == -1 {
return Ok(None);
@ -50,7 +50,7 @@ impl Serializable for Option<Stack> {
tag: try!(Serializable::read_from(buf)),
}))
}
fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> {
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), protocol::Error> {
match *self {
Some(ref val) => {
try!(buf.write_i16::<BigEndian>(val.id as i16));

View File

@ -172,7 +172,7 @@ impl Tag {
}
}
fn read_type(id: u8, buf: &mut io::Read) -> Result<Tag, io::Error> {
fn read_type<R: io::Read>(id: u8, buf: &mut R) -> Result<Tag, protocol::Error> {
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<Tag, io::Error> {
fn read_from<R: io::Read>(buf: &mut R) -> Result<Tag, protocol::Error> {
Tag::read_type(10, buf)
}
fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> {
fn write_to<W: io::Write>(&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<W: io::Write>(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<String> {
pub fn read_string<R: io::Read>(buf: &mut R) -> Result<String, protocol::Error> {
let len: i16 = try!(buf.read_i16::<BigEndian>());
let mut ret = String::new();
try!(buf.take(len as u64).read_to_string(&mut ret));

View File

@ -62,7 +62,7 @@ impl Metadata {
impl Serializable for Metadata {
fn read_from(buf: &mut io::Read) -> Result<Self, io::Error> {
fn read_from<R: io::Read>(buf: &mut R) -> Result<Self, protocol::Error> {
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<W: io::Write>(&self, buf: &mut W) -> Result<(), protocol::Error> {
for (k, v) in &self.map {
try!((*k as u8).write_to(buf));
match *v {