Clean up the protocol implementation to use generics instead of trait objects
This commit is contained in:
parent
fc82b62a04
commit
bfbd4fe05a
|
@ -13,7 +13,7 @@
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
use nbt;
|
use nbt;
|
||||||
use protocol::Serializable;
|
use protocol::{self, Serializable};
|
||||||
use std::io;
|
use std::io;
|
||||||
use byteorder::{BigEndian, WriteBytesExt, ReadBytesExt};
|
use byteorder::{BigEndian, WriteBytesExt, ReadBytesExt};
|
||||||
|
|
||||||
|
@ -38,7 +38,7 @@ impl Default for Stack {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Serializable for Option<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>());
|
let id = try!(buf.read_i16::<BigEndian>());
|
||||||
if id == -1 {
|
if id == -1 {
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
|
@ -50,7 +50,7 @@ impl Serializable for Option<Stack> {
|
||||||
tag: try!(Serializable::read_from(buf)),
|
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 {
|
match *self {
|
||||||
Some(ref val) => {
|
Some(ref val) => {
|
||||||
try!(buf.write_i16::<BigEndian>(val.id as i16));
|
try!(buf.write_i16::<BigEndian>(val.id as i16));
|
||||||
|
|
|
@ -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 {
|
match id {
|
||||||
0 => unreachable!(),
|
0 => unreachable!(),
|
||||||
1 => Ok(Tag::Byte(try!(buf.read_i8()))),
|
1 => Ok(Tag::Byte(try!(buf.read_i8()))),
|
||||||
|
@ -217,18 +217,17 @@ impl Tag {
|
||||||
}
|
}
|
||||||
data
|
data
|
||||||
})),
|
})),
|
||||||
_ => Err(io::Error::new(io::ErrorKind::InvalidData,
|
_ => Err(protocol::Error::Err("invalid tag".to_owned())),
|
||||||
protocol::Error::Err("invalid tag".to_owned()))),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Serializable for Tag {
|
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)
|
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 {
|
match *self {
|
||||||
Tag::End => {}
|
Tag::End => {}
|
||||||
Tag::Byte(val) => try!(buf.write_i8(val)),
|
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();
|
let data = s.as_bytes();
|
||||||
try!((data.len() as i16).write_to(buf));
|
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 len: i16 = try!(buf.read_i16::<BigEndian>());
|
||||||
let mut ret = String::new();
|
let mut ret = String::new();
|
||||||
try!(buf.take(len as u64).read_to_string(&mut ret));
|
try!(buf.take(len as u64).read_to_string(&mut ret));
|
||||||
|
|
|
@ -93,7 +93,7 @@ macro_rules! state_packets {
|
||||||
|
|
||||||
fn packet_id(&self) -> i32 { internal_ids::$name }
|
fn packet_id(&self) -> i32 { internal_ids::$name }
|
||||||
|
|
||||||
fn write(self, buf: &mut io::Write) -> Result<(), io::Error> {
|
fn write<W: io::Write>(self, buf: &mut W) -> Result<(), Error> {
|
||||||
$(
|
$(
|
||||||
if true $(&& ($cond(&self)))* {
|
if true $(&& ($cond(&self)))* {
|
||||||
try!(self.$field.write_to(buf));
|
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
|
/// Returns the packet for the given state, direction and id after parsing the fields
|
||||||
/// from the buffer.
|
/// from the buffer.
|
||||||
pub fn packet_by_id(state: State, dir: Direction, id: i32, mut buf: &mut io::Read) -> Result<Option<Packet>, io::Error> {
|
pub fn packet_by_id<R: io::Read>(state: State, dir: Direction, id: i32, mut buf: &mut R) -> Result<Option<Packet>, Error> {
|
||||||
match state {
|
match state {
|
||||||
$(
|
$(
|
||||||
State::$stateName => {
|
State::$stateName => {
|
||||||
|
@ -146,24 +146,24 @@ macro_rules! state_packets {
|
||||||
pub mod packet;
|
pub mod packet;
|
||||||
|
|
||||||
pub trait Serializable: Sized {
|
pub trait Serializable: Sized {
|
||||||
fn read_from(buf: &mut io::Read) -> Result<Self, io::Error>;
|
fn read_from<R: io::Read>(buf: &mut R) -> Result<Self, Error>;
|
||||||
fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error>;
|
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Serializable for Vec<u8> {
|
impl Serializable for Vec<u8> {
|
||||||
fn read_from(buf: &mut io::Read) -> Result<Vec<u8>, io::Error> {
|
fn read_from<R: io::Read>(buf: &mut R) -> Result<Vec<u8>, Error> {
|
||||||
let mut v = Vec::new();
|
let mut v = Vec::new();
|
||||||
try!(buf.read_to_end(&mut v));
|
try!(buf.read_to_end(&mut v));
|
||||||
Ok(v)
|
Ok(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> {
|
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
|
||||||
buf.write_all(&self[..])
|
buf.write_all(&self[..]).map_err(|v| v.into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Serializable for Option<nbt::NamedTag>{
|
impl Serializable for Option<nbt::NamedTag>{
|
||||||
fn read_from(buf: &mut io::Read) -> Result<Option<nbt::NamedTag>, io::Error> {
|
fn read_from<R: io::Read>(buf: &mut R) -> Result<Option<nbt::NamedTag>, Error> {
|
||||||
let ty = try!(buf.read_u8());
|
let ty = try!(buf.read_u8());
|
||||||
if ty == 0 {
|
if ty == 0 {
|
||||||
Result::Ok(None)
|
Result::Ok(None)
|
||||||
|
@ -173,7 +173,7 @@ impl Serializable for Option<nbt::NamedTag>{
|
||||||
Result::Ok(Some(nbt::NamedTag(name, tag)))
|
Result::Ok(Some(nbt::NamedTag(name, tag)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> {
|
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
|
||||||
match *self {
|
match *self {
|
||||||
Some(ref val) => {
|
Some(ref val) => {
|
||||||
try!(buf.write_u8(10));
|
try!(buf.write_u8(10));
|
||||||
|
@ -187,10 +187,10 @@ impl Serializable for Option<nbt::NamedTag>{
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <T> Serializable for Option<T> where T : Serializable {
|
impl <T> Serializable for Option<T> where T : Serializable {
|
||||||
fn read_from(buf: &mut io::Read) -> Result<Option<T>, io::Error> {
|
fn read_from<R: io::Read>(buf: &mut R) -> Result<Option<T>, Error> {
|
||||||
Result::Ok(Some(try!(T::read_from(buf))))
|
Result::Ok(Some(try!(T::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<(), Error> {
|
||||||
if self.is_some() {
|
if self.is_some() {
|
||||||
try!(self.as_ref().unwrap().write_to(buf));
|
try!(self.as_ref().unwrap().write_to(buf));
|
||||||
}
|
}
|
||||||
|
@ -199,13 +199,13 @@ impl <T> Serializable for Option<T> where T : Serializable {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Serializable for String {
|
impl Serializable for String {
|
||||||
fn read_from(buf: &mut io::Read) -> Result<String, io::Error> {
|
fn read_from<R: io::Read>(buf: &mut R) -> Result<String, Error> {
|
||||||
let len = try!(VarInt::read_from(buf)).0;
|
let len = try!(VarInt::read_from(buf)).0;
|
||||||
let mut ret = String::new();
|
let mut ret = String::new();
|
||||||
try!(buf.take(len as u64).read_to_string(&mut ret));
|
try!(buf.take(len as u64).read_to_string(&mut ret));
|
||||||
Result::Ok(ret)
|
Result::Ok(ret)
|
||||||
}
|
}
|
||||||
fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> {
|
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
|
||||||
let bytes = self.as_bytes();
|
let bytes = self.as_bytes();
|
||||||
try!(VarInt(bytes.len() as i32).write_to(buf));
|
try!(VarInt(bytes.len() as i32).write_to(buf));
|
||||||
try!(buf.write_all(bytes));
|
try!(buf.write_all(bytes));
|
||||||
|
@ -214,14 +214,14 @@ impl Serializable for String {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Serializable for format::Component {
|
impl Serializable for format::Component {
|
||||||
fn read_from(buf: &mut io::Read) -> Result<Self, io::Error> {
|
fn read_from<R: io::Read>(buf: &mut R) -> Result<Self, Error> {
|
||||||
let len = try!(VarInt::read_from(buf)).0;
|
let len = try!(VarInt::read_from(buf)).0;
|
||||||
let mut ret = String::new();
|
let mut ret = String::new();
|
||||||
try!(buf.take(len as u64).read_to_string(&mut ret));
|
try!(buf.take(len as u64).read_to_string(&mut ret));
|
||||||
let val: serde_json::Value = serde_json::from_str(&ret[..]).unwrap();
|
let val: serde_json::Value = serde_json::from_str(&ret[..]).unwrap();
|
||||||
Result::Ok(Self::from_value(&val))
|
Result::Ok(Self::from_value(&val))
|
||||||
}
|
}
|
||||||
fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> {
|
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
|
||||||
let val = serde_json::to_string(&self.to_value()).unwrap();
|
let val = serde_json::to_string(&self.to_value()).unwrap();
|
||||||
let bytes = val.as_bytes();
|
let bytes = val.as_bytes();
|
||||||
try!(VarInt(bytes.len() as i32).write_to(buf));
|
try!(VarInt(bytes.len() as i32).write_to(buf));
|
||||||
|
@ -231,19 +231,19 @@ impl Serializable for format::Component {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Serializable for () {
|
impl Serializable for () {
|
||||||
fn read_from(_: &mut io::Read) -> Result<(), io::Error> {
|
fn read_from<R: io::Read>(_: &mut R) -> Result<(), Error> {
|
||||||
Result::Ok(())
|
Result::Ok(())
|
||||||
}
|
}
|
||||||
fn write_to(&self, _: &mut io::Write) -> Result<(), io::Error> {
|
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
|
||||||
Result::Ok(())
|
Result::Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Serializable for bool {
|
impl Serializable for bool {
|
||||||
fn read_from(buf: &mut io::Read) -> Result<bool, io::Error> {
|
fn read_from<R: io::Read>(buf: &mut R) -> Result<bool, Error> {
|
||||||
Result::Ok(try!(buf.read_u8()) != 0)
|
Result::Ok(try!(buf.read_u8()) != 0)
|
||||||
}
|
}
|
||||||
fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> {
|
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
|
||||||
try!(buf.write_u8(if *self {
|
try!(buf.write_u8(if *self {
|
||||||
1
|
1
|
||||||
} else {
|
} else {
|
||||||
|
@ -254,90 +254,90 @@ impl Serializable for bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Serializable for i8 {
|
impl Serializable for i8 {
|
||||||
fn read_from(buf: &mut io::Read) -> Result<i8, io::Error> {
|
fn read_from<R: io::Read>(buf: &mut R) -> Result<i8, Error> {
|
||||||
Result::Ok(try!(buf.read_i8()))
|
Result::Ok(try!(buf.read_i8()))
|
||||||
}
|
}
|
||||||
fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> {
|
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
|
||||||
try!(buf.write_i8(*self));
|
try!(buf.write_i8(*self));
|
||||||
Result::Ok(())
|
Result::Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Serializable for i16 {
|
impl Serializable for i16 {
|
||||||
fn read_from(buf: &mut io::Read) -> Result<i16, io::Error> {
|
fn read_from<R: io::Read>(buf: &mut R) -> Result<i16, Error> {
|
||||||
Result::Ok(try!(buf.read_i16::<BigEndian>()))
|
Result::Ok(try!(buf.read_i16::<BigEndian>()))
|
||||||
}
|
}
|
||||||
fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> {
|
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
|
||||||
try!(buf.write_i16::<BigEndian>(*self));
|
try!(buf.write_i16::<BigEndian>(*self));
|
||||||
Result::Ok(())
|
Result::Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Serializable for i32 {
|
impl Serializable for i32 {
|
||||||
fn read_from(buf: &mut io::Read) -> Result<i32, io::Error> {
|
fn read_from<R: io::Read>(buf: &mut R) -> Result<i32, Error> {
|
||||||
Result::Ok(try!(buf.read_i32::<BigEndian>()))
|
Result::Ok(try!(buf.read_i32::<BigEndian>()))
|
||||||
}
|
}
|
||||||
fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> {
|
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
|
||||||
try!(buf.write_i32::<BigEndian>(*self));
|
try!(buf.write_i32::<BigEndian>(*self));
|
||||||
Result::Ok(())
|
Result::Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Serializable for i64 {
|
impl Serializable for i64 {
|
||||||
fn read_from(buf: &mut io::Read) -> Result<i64, io::Error> {
|
fn read_from<R: io::Read>(buf: &mut R) -> Result<i64, Error> {
|
||||||
Result::Ok(try!(buf.read_i64::<BigEndian>()))
|
Result::Ok(try!(buf.read_i64::<BigEndian>()))
|
||||||
}
|
}
|
||||||
fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> {
|
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
|
||||||
try!(buf.write_i64::<BigEndian>(*self));
|
try!(buf.write_i64::<BigEndian>(*self));
|
||||||
Result::Ok(())
|
Result::Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Serializable for u8 {
|
impl Serializable for u8 {
|
||||||
fn read_from(buf: &mut io::Read) -> Result<u8, io::Error> {
|
fn read_from<R: io::Read>(buf: &mut R) -> Result<u8, Error> {
|
||||||
Result::Ok(try!(buf.read_u8()))
|
Result::Ok(try!(buf.read_u8()))
|
||||||
}
|
}
|
||||||
fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> {
|
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
|
||||||
try!(buf.write_u8(*self));
|
try!(buf.write_u8(*self));
|
||||||
Result::Ok(())
|
Result::Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Serializable for u16 {
|
impl Serializable for u16 {
|
||||||
fn read_from(buf: &mut io::Read) -> Result<u16, io::Error> {
|
fn read_from<R: io::Read>(buf: &mut R) -> Result<u16, Error> {
|
||||||
Result::Ok(try!(buf.read_u16::<BigEndian>()))
|
Result::Ok(try!(buf.read_u16::<BigEndian>()))
|
||||||
}
|
}
|
||||||
fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> {
|
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
|
||||||
try!(buf.write_u16::<BigEndian>(*self));
|
try!(buf.write_u16::<BigEndian>(*self));
|
||||||
Result::Ok(())
|
Result::Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Serializable for u64 {
|
impl Serializable for u64 {
|
||||||
fn read_from(buf: &mut io::Read) -> Result<u64, io::Error> {
|
fn read_from<R: io::Read>(buf: &mut R) -> Result<u64, Error> {
|
||||||
Result::Ok(try!(buf.read_u64::<BigEndian>()))
|
Result::Ok(try!(buf.read_u64::<BigEndian>()))
|
||||||
}
|
}
|
||||||
fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> {
|
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
|
||||||
try!(buf.write_u64::<BigEndian>(*self));
|
try!(buf.write_u64::<BigEndian>(*self));
|
||||||
Result::Ok(())
|
Result::Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Serializable for f32 {
|
impl Serializable for f32 {
|
||||||
fn read_from(buf: &mut io::Read) -> Result<f32, io::Error> {
|
fn read_from<R: io::Read>(buf: &mut R) -> Result<f32, Error> {
|
||||||
Result::Ok(try!(buf.read_f32::<BigEndian>()))
|
Result::Ok(try!(buf.read_f32::<BigEndian>()))
|
||||||
}
|
}
|
||||||
fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> {
|
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
|
||||||
try!(buf.write_f32::<BigEndian>(*self));
|
try!(buf.write_f32::<BigEndian>(*self));
|
||||||
Result::Ok(())
|
Result::Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Serializable for f64 {
|
impl Serializable for f64 {
|
||||||
fn read_from(buf: &mut io::Read) -> Result<f64, io::Error> {
|
fn read_from<R: io::Read>(buf: &mut R) -> Result<f64, Error> {
|
||||||
Result::Ok(try!(buf.read_f64::<BigEndian>()))
|
Result::Ok(try!(buf.read_f64::<BigEndian>()))
|
||||||
}
|
}
|
||||||
fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> {
|
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
|
||||||
try!(buf.write_f64::<BigEndian>(*self));
|
try!(buf.write_f64::<BigEndian>(*self));
|
||||||
Result::Ok(())
|
Result::Ok(())
|
||||||
}
|
}
|
||||||
|
@ -375,11 +375,11 @@ impl Default for UUID {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Serializable for UUID {
|
impl Serializable for UUID {
|
||||||
fn read_from(buf: &mut io::Read) -> Result<UUID, io::Error> {
|
fn read_from<R: io::Read>(buf: &mut R) -> Result<UUID, Error> {
|
||||||
Result::Ok(UUID(try!(buf.read_u64::<BigEndian>()),
|
Result::Ok(UUID(try!(buf.read_u64::<BigEndian>()),
|
||||||
try!(buf.read_u64::<BigEndian>())))
|
try!(buf.read_u64::<BigEndian>())))
|
||||||
}
|
}
|
||||||
fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> {
|
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
|
||||||
try!(buf.write_u64::<BigEndian>(self.0));
|
try!(buf.write_u64::<BigEndian>(self.0));
|
||||||
try!(buf.write_u64::<BigEndian>(self.1));
|
try!(buf.write_u64::<BigEndian>(self.1));
|
||||||
Result::Ok(())
|
Result::Ok(())
|
||||||
|
@ -407,7 +407,7 @@ impl <L: Lengthable, V: Default> LenPrefixed<L, V> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <L: Lengthable, V: Serializable> Serializable for LenPrefixed<L, V> {
|
impl <L: Lengthable, V: Serializable> Serializable for LenPrefixed<L, V> {
|
||||||
fn read_from(buf: &mut io::Read) -> Result<LenPrefixed<L, V>, io::Error> {
|
fn read_from<R: io::Read>(buf: &mut R) -> Result<LenPrefixed<L, V>, Error> {
|
||||||
let len_data: L = try!(Serializable::read_from(buf));
|
let len_data: L = try!(Serializable::read_from(buf));
|
||||||
let len: usize = len_data.into();
|
let len: usize = len_data.into();
|
||||||
let mut data: Vec<V> = Vec::with_capacity(len);
|
let mut data: Vec<V> = Vec::with_capacity(len);
|
||||||
|
@ -420,7 +420,7 @@ impl <L: Lengthable, V: Serializable> Serializable for LenPrefixed<L, V> {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> {
|
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
|
||||||
let len_data: L = L::from(self.data.len());
|
let len_data: L = L::from(self.data.len());
|
||||||
try!(len_data.write_to(buf));
|
try!(len_data.write_to(buf));
|
||||||
let data = &self.data;
|
let data = &self.data;
|
||||||
|
@ -463,7 +463,7 @@ impl <L: Lengthable> LenPrefixedBytes<L> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <L: Lengthable> Serializable for LenPrefixedBytes<L> {
|
impl <L: Lengthable> Serializable for LenPrefixedBytes<L> {
|
||||||
fn read_from(buf: &mut io::Read) -> Result<LenPrefixedBytes<L>, io::Error> {
|
fn read_from<R: io::Read>(buf: &mut R) -> Result<LenPrefixedBytes<L>, Error> {
|
||||||
let len_data: L = try!(Serializable::read_from(buf));
|
let len_data: L = try!(Serializable::read_from(buf));
|
||||||
let len: usize = len_data.into();
|
let len: usize = len_data.into();
|
||||||
let mut data: Vec<u8> = Vec::with_capacity(len);
|
let mut data: Vec<u8> = Vec::with_capacity(len);
|
||||||
|
@ -474,7 +474,7 @@ impl <L: Lengthable> Serializable for LenPrefixedBytes<L> {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> {
|
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
|
||||||
let len_data: L = L::from(self.data.len());
|
let len_data: L = L::from(self.data.len());
|
||||||
try!(len_data.write_to(buf));
|
try!(len_data.write_to(buf));
|
||||||
try!(buf.write_all(&self.data[..]));
|
try!(buf.write_all(&self.data[..]));
|
||||||
|
@ -535,7 +535,7 @@ impl Lengthable for VarInt {
|
||||||
|
|
||||||
impl Serializable for VarInt {
|
impl Serializable for VarInt {
|
||||||
/// Decodes a `VarInt` from the Reader
|
/// Decodes a `VarInt` from the Reader
|
||||||
fn read_from(buf: &mut io::Read) -> Result<VarInt, io::Error> {
|
fn read_from<R: io::Read>(buf: &mut R) -> Result<VarInt, Error> {
|
||||||
const PART : u32 = 0x7F;
|
const PART : u32 = 0x7F;
|
||||||
let mut size = 0;
|
let mut size = 0;
|
||||||
let mut val = 0u32;
|
let mut val = 0u32;
|
||||||
|
@ -544,8 +544,7 @@ impl Serializable for VarInt {
|
||||||
val |= (b & PART) << (size * 7);
|
val |= (b & PART) << (size * 7);
|
||||||
size += 1;
|
size += 1;
|
||||||
if size > 5 {
|
if size > 5 {
|
||||||
return Result::Err(io::Error::new(io::ErrorKind::InvalidInput,
|
return Result::Err(Error::Err("VarInt too big".to_owned()));
|
||||||
Error::Err("VarInt too big".to_owned())))
|
|
||||||
}
|
}
|
||||||
if (b & 0x80) == 0 {
|
if (b & 0x80) == 0 {
|
||||||
break
|
break
|
||||||
|
@ -556,7 +555,7 @@ impl Serializable for VarInt {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Encodes a `VarInt` into the Writer
|
/// Encodes a `VarInt` into the Writer
|
||||||
fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> {
|
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
|
||||||
const PART : u32 = 0x7F;
|
const PART : u32 = 0x7F;
|
||||||
let mut val = self.0 as u32;
|
let mut val = self.0 as u32;
|
||||||
loop {
|
loop {
|
||||||
|
@ -599,7 +598,7 @@ impl Lengthable for VarLong {
|
||||||
|
|
||||||
impl Serializable for VarLong {
|
impl Serializable for VarLong {
|
||||||
/// Decodes a `VarLong` from the Reader
|
/// Decodes a `VarLong` from the Reader
|
||||||
fn read_from(buf: &mut io::Read) -> Result<VarLong, io::Error> {
|
fn read_from<R: io::Read>(buf: &mut R) -> Result<VarLong, Error> {
|
||||||
const PART : u64 = 0x7F;
|
const PART : u64 = 0x7F;
|
||||||
let mut size = 0;
|
let mut size = 0;
|
||||||
let mut val = 0u64;
|
let mut val = 0u64;
|
||||||
|
@ -608,8 +607,7 @@ impl Serializable for VarLong {
|
||||||
val |= (b & PART) << (size * 7);
|
val |= (b & PART) << (size * 7);
|
||||||
size += 1;
|
size += 1;
|
||||||
if size > 10 {
|
if size > 10 {
|
||||||
return Result::Err(io::Error::new(io::ErrorKind::InvalidInput,
|
return Result::Err(Error::Err("VarLong too big".to_owned()));
|
||||||
Error::Err("VarLong too big".to_owned())))
|
|
||||||
}
|
}
|
||||||
if (b & 0x80) == 0 {
|
if (b & 0x80) == 0 {
|
||||||
break
|
break
|
||||||
|
@ -620,7 +618,7 @@ impl Serializable for VarLong {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Encodes a `VarLong` into the Writer
|
/// Encodes a `VarLong` into the Writer
|
||||||
fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> {
|
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
|
||||||
const PART : u64 = 0x7F;
|
const PART : u64 = 0x7F;
|
||||||
let mut val = self.0 as u64;
|
let mut val = self.0 as u64;
|
||||||
loop {
|
loop {
|
||||||
|
@ -647,7 +645,7 @@ impl fmt::Debug for VarLong {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Serializable for Position {
|
impl Serializable for Position {
|
||||||
fn read_from(buf: &mut io::Read) -> Result<Position, io::Error> {
|
fn read_from<R: io::Read>(buf: &mut R) -> Result<Position, Error> {
|
||||||
let pos = try!(buf.read_u64::<BigEndian>());
|
let pos = try!(buf.read_u64::<BigEndian>());
|
||||||
Ok(Position::new(
|
Ok(Position::new(
|
||||||
((pos as i64) >> 38) as i32,
|
((pos as i64) >> 38) as i32,
|
||||||
|
@ -655,7 +653,7 @@ impl Serializable for Position {
|
||||||
((pos as i64) << 38 >> 38) as i32
|
((pos as i64) << 38 >> 38) as i32
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> {
|
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
|
||||||
let pos = (((self.x as u64) & 0x3FFFFFF) << 38)
|
let pos = (((self.x as u64) & 0x3FFFFFF) << 38)
|
||||||
| (((self.y as u64) & 0xFFF) << 26)
|
| (((self.y as u64) & 0xFFF) << 26)
|
||||||
| ((self.z as u64) & 0x3FFFFFF);
|
| ((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 packet_id(&self) -> i32;
|
||||||
|
|
||||||
fn write(self, buf: &mut io::Write) -> Result<(), io::Error>;
|
fn write<W: io::Write>(self, buf: &mut W) -> Result<(), Error>;
|
||||||
}
|
}
|
||||||
|
|
|
@ -957,14 +957,14 @@ pub struct Statistic {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Serializable for Statistic {
|
impl Serializable for Statistic {
|
||||||
fn read_from(buf: &mut io::Read) -> Result<Self, io::Error> {
|
fn read_from<R: io::Read>(buf: &mut R) -> Result<Self, Error> {
|
||||||
Ok(Statistic {
|
Ok(Statistic {
|
||||||
name: try!(Serializable::read_from(buf)),
|
name: try!(Serializable::read_from(buf)),
|
||||||
value: 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<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
|
||||||
try!(self.name.write_to(buf));
|
try!(self.name.write_to(buf));
|
||||||
self.value.write_to(buf)
|
self.value.write_to(buf)
|
||||||
}
|
}
|
||||||
|
@ -978,7 +978,7 @@ pub struct BlockChangeRecord {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Serializable for BlockChangeRecord {
|
impl Serializable for BlockChangeRecord {
|
||||||
fn read_from(buf: &mut io::Read) -> Result<Self, io::Error> {
|
fn read_from<R: io::Read>(buf: &mut R) -> Result<Self, Error> {
|
||||||
Ok(BlockChangeRecord {
|
Ok(BlockChangeRecord {
|
||||||
xz: try!(Serializable::read_from(buf)),
|
xz: try!(Serializable::read_from(buf)),
|
||||||
y: 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<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
|
||||||
try!(self.xz.write_to(buf));
|
try!(self.xz.write_to(buf));
|
||||||
try!(self.y.write_to(buf));
|
try!(self.y.write_to(buf));
|
||||||
self.block_id.write_to(buf)
|
self.block_id.write_to(buf)
|
||||||
|
@ -1001,7 +1001,7 @@ pub struct ExplosionRecord {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Serializable for ExplosionRecord {
|
impl Serializable for ExplosionRecord {
|
||||||
fn read_from(buf: &mut io::Read) -> Result<Self, io::Error> {
|
fn read_from<R: io::Read>(buf: &mut R) -> Result<Self, Error> {
|
||||||
Ok(ExplosionRecord {
|
Ok(ExplosionRecord {
|
||||||
x: try!(Serializable::read_from(buf)),
|
x: try!(Serializable::read_from(buf)),
|
||||||
y: 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<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
|
||||||
try!(self.x.write_to(buf));
|
try!(self.x.write_to(buf));
|
||||||
try!(self.y.write_to(buf));
|
try!(self.y.write_to(buf));
|
||||||
self.z.write_to(buf)
|
self.z.write_to(buf)
|
||||||
|
@ -1024,7 +1024,7 @@ pub struct MapIcon {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Serializable for MapIcon {
|
impl Serializable for MapIcon {
|
||||||
fn read_from(buf: &mut io::Read) -> Result<Self, io::Error> {
|
fn read_from<R: io::Read>(buf: &mut R) -> Result<Self, Error> {
|
||||||
Ok(MapIcon {
|
Ok(MapIcon {
|
||||||
direction_type: try!(Serializable::read_from(buf)),
|
direction_type: try!(Serializable::read_from(buf)),
|
||||||
x: 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<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
|
||||||
try!(self.direction_type.write_to(buf));
|
try!(self.direction_type.write_to(buf));
|
||||||
try!(self.x.write_to(buf));
|
try!(self.x.write_to(buf));
|
||||||
self.z.write_to(buf)
|
self.z.write_to(buf)
|
||||||
|
@ -1057,7 +1057,7 @@ pub struct EntityProperty {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Serializable for EntityProperty {
|
impl Serializable for EntityProperty {
|
||||||
fn read_from(buf: &mut io::Read) -> Result<Self, io::Error> {
|
fn read_from<R: io::Read>(buf: &mut R) -> Result<Self, Error> {
|
||||||
Ok(EntityProperty {
|
Ok(EntityProperty {
|
||||||
key: try!(Serializable::read_from(buf)),
|
key: try!(Serializable::read_from(buf)),
|
||||||
value: 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<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
|
||||||
try!(self.key.write_to(buf));
|
try!(self.key.write_to(buf));
|
||||||
try!(self.value.write_to(buf));
|
try!(self.value.write_to(buf));
|
||||||
self.modifiers.write_to(buf)
|
self.modifiers.write_to(buf)
|
||||||
|
@ -1080,7 +1080,7 @@ pub struct PropertyModifier {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Serializable for PropertyModifier {
|
impl Serializable for PropertyModifier {
|
||||||
fn read_from(buf: &mut io::Read) -> Result<Self, io::Error> {
|
fn read_from<R: io::Read>(buf: &mut R) -> Result<Self, Error> {
|
||||||
Ok(PropertyModifier {
|
Ok(PropertyModifier {
|
||||||
uuid: try!(Serializable::read_from(buf)),
|
uuid: try!(Serializable::read_from(buf)),
|
||||||
amount: 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<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
|
||||||
try!(self.uuid.write_to(buf));
|
try!(self.uuid.write_to(buf));
|
||||||
try!(self.amount.write_to(buf));
|
try!(self.amount.write_to(buf));
|
||||||
self.operation.write_to(buf)
|
self.operation.write_to(buf)
|
||||||
|
@ -1102,7 +1102,7 @@ pub struct PlayerInfoData {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Serializable for PlayerInfoData {
|
impl Serializable for PlayerInfoData {
|
||||||
fn read_from(buf: &mut io::Read) -> Result<Self, io::Error> {
|
fn read_from<R: io::Read>(buf: &mut R) -> Result<Self, Error> {
|
||||||
let mut m = PlayerInfoData {
|
let mut m = PlayerInfoData {
|
||||||
action: try!(Serializable::read_from(buf)),
|
action: try!(Serializable::read_from(buf)),
|
||||||
players: Vec::new(),
|
players: Vec::new(),
|
||||||
|
@ -1175,7 +1175,7 @@ impl Serializable for PlayerInfoData {
|
||||||
Ok(m)
|
Ok(m)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_to(&self, _: &mut io::Write) -> Result<(), io::Error> {
|
fn write_to<W: io::Write>(&self, _: &mut W) -> Result<(), Error> {
|
||||||
unimplemented!() // I'm lazy
|
unimplemented!() // I'm lazy
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -62,7 +62,7 @@ impl Metadata {
|
||||||
|
|
||||||
impl Serializable for 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();
|
let mut m = Metadata::new();
|
||||||
loop {
|
loop {
|
||||||
let index = try!(u8::read_from(buf)) as i32;
|
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),
|
12 => m.put_raw(index, try!(protocol::VarInt::read_from(buf)).0 as u16),
|
||||||
_ => return Err(io::Error::new(io::ErrorKind::InvalidInput,
|
_ => return Err(protocol::Error::Err("unknown metadata type".to_owned())),
|
||||||
protocol::Error::Err("unknown metadata type"
|
|
||||||
.to_owned()))),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(m)
|
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 {
|
for (k, v) in &self.map {
|
||||||
try!((*k as u8).write_to(buf));
|
try!((*k as u8).write_to(buf));
|
||||||
match *v {
|
match *v {
|
||||||
|
|
Loading…
Reference in New Issue