diff options
Diffstat (limited to 'src/primitive/unsignedint.rs')
| -rw-r--r-- | src/primitive/unsignedint.rs | 32 |
1 files changed, 16 insertions, 16 deletions
diff --git a/src/primitive/unsignedint.rs b/src/primitive/unsignedint.rs index 90ec696..b5d76cd 100644 --- a/src/primitive/unsignedint.rs +++ b/src/primitive/unsignedint.rs @@ -5,77 +5,77 @@ use std::io::Cursor; use std::result::Result; use std::vec::Vec; -use failure::Error; - use crate::error::ProtocolError; use crate::{deserialize::*, serialize::*}; impl Serialize for bool { - fn serialize(&self) -> Result<Vec<u8>, Error> { + fn serialize(&self) -> Result<Vec<u8>, ProtocolError> { Ok({ let i = *self as i8; Vec::from(i.to_be_bytes()) }) } } + impl Deserialize for bool { - fn parse(b: &[u8]) -> Result<(usize, Self), Error> { + fn parse(b: &[u8]) -> Result<(usize, Self), ProtocolError> { if b[0] == 0 { - return Ok((1, false)); + Ok((1, false)) } else if b[0] == 1 { - return Ok((1, true)); + Ok((1, true)) } else { - bail!(ProtocolError::BoolOutOfRange); - }; + Err(ProtocolError::BoolOutOfRange) + } } } + impl Serialize for u64 { - fn serialize(&self) -> Result<Vec<u8>, Error> { + fn serialize(&self) -> Result<Vec<u8>, ProtocolError> { Ok(Vec::from(self.to_be_bytes())) } } impl Deserialize for u64 { - fn parse(b: &[u8]) -> Result<(usize, Self), Error> { + fn parse(b: &[u8]) -> Result<(usize, Self), ProtocolError> { let mut rdr = Cursor::new(&b[0..8]); return Ok((8, rdr.read_u64::<BigEndian>()?)); } } impl Serialize for u32 { - fn serialize(&self) -> Result<Vec<u8>, Error> { + fn serialize(&self) -> Result<Vec<u8>, ProtocolError> { Ok(Vec::from(self.to_be_bytes())) } } impl Deserialize for u32 { - fn parse(b: &[u8]) -> Result<(usize, Self), Error> { + fn parse(b: &[u8]) -> Result<(usize, Self), ProtocolError> { let mut rdr = Cursor::new(&b[0..4]); return Ok((4, rdr.read_u32::<BigEndian>()?)); } } impl Serialize for u16 { - fn serialize(&self) -> Result<Vec<u8>, Error> { + fn serialize(&self) -> Result<Vec<u8>, ProtocolError> { Ok(Vec::from(self.to_be_bytes())) } } impl Deserialize for u16 { - fn parse(b: &[u8]) -> Result<(usize, Self), Error> { + fn parse(b: &[u8]) -> Result<(usize, Self), ProtocolError> { let mut rdr = Cursor::new(&b[0..2]); return Ok((2, rdr.read_u16::<BigEndian>()?)); } } impl Serialize for u8 { - fn serialize(&self) -> Result<Vec<u8>, Error> { + fn serialize(&self) -> Result<Vec<u8>, ProtocolError> { Ok(Vec::from(self.to_be_bytes())) } } impl Deserialize for u8 { - fn parse(b: &[u8]) -> Result<(usize, Self), Error> { + fn parse(b: &[u8]) -> Result<(usize, Self), ProtocolError> { return Ok((1, b[0])); } } |
