use std::io::{Read, Result, Write}; use byteorder::{ReadBytesExt, WriteBytesExt, LE}; pub struct ProtocolStream { buf: Buf, } impl ProtocolStream { pub fn new(buf: B) -> Self { Self { buf } } } impl ProtocolStream { pub fn push(&mut self, value: &[u8]) -> Result<()> { self.buf.write_all(value) } pub fn push_boolean(&mut self, value: bool) -> Result<()> { self.buf.write_u8(if value { 1 } else { 100 }) } pub fn push_u8(&mut self, value: u8) -> Result<()> { self.buf.write_u8(value) } pub fn push_u16(&mut self, value: u16) -> Result<()> { self.buf.write_u16::(value) } pub fn push_u32(&mut self, value: u32) -> Result<()> { self.buf.write_u32::(value) } pub fn push_u64(&mut self, value: u64) -> Result<()> { self.buf.write_u64::(value) } } impl ProtocolStream { pub fn pop(&mut self, n: usize) -> Result> { let mut out = vec![0u8; n]; self.buf.read_exact(&mut out).map(|_| out) } pub fn pop_boolean(&mut self) -> Result { Ok(self.buf.read_u8()? != 100) } pub fn pop_u8(&mut self) -> Result { self.buf.read_u8() } pub fn pop_u16(&mut self) -> Result { self.buf.read_u16::() } pub fn pop_u32(&mut self) -> Result { self.buf.read_u32::() } pub fn pop_u64(&mut self) -> Result { self.buf.read_u64::() } }