-impl<W: Write> Writer<W> {
- pub fn new(writer: W) -> Writer<W> {
- return Writer { writer }
- }
- pub fn into_inner(self) -> W { self.writer }
- pub fn get_ref(&self) -> &W { &self.writer }
- fn write_u64(&mut self, v: u64) -> Result<(), DecodeError> {
- Ok(self.writer.write_all(&be64_to_array(v))?)
- }
- fn write_u32(&mut self, v: u32) -> Result<(), DecodeError> {
- Ok(self.writer.write_all(&be32_to_array(v))?)
- }
- fn write_u16(&mut self, v: u16) -> Result<(), DecodeError> {
- Ok(self.writer.write_all(&be16_to_array(v))?)
- }
- fn write_u8(&mut self, v: u8) -> Result<(), DecodeError> {
- Ok(self.writer.write_all(&[v])?)
- }
- fn write_bool(&mut self, v: bool) -> Result<(), DecodeError> {
- Ok(self.writer.write_all(&[if v {1} else {0}])?)
- }
- pub fn write_all(&mut self, v: &[u8]) -> Result<(), DecodeError> {
- Ok(self.writer.write_all(v)?)
+macro_rules! impl_writeable_primitive {
+ ($val_type:ty, $meth_write:ident, $len: expr, $meth_read:ident) => {
+ impl<W: Writer> Writeable<W> for $val_type {
+ #[inline]
+ fn write(&self, writer: &mut W) -> Result<(), DecodeError> {
+ Ok(writer.write_all(&$meth_write(*self))?)
+ }
+ }
+ impl<R: Read> Readable<R> for $val_type {
+ #[inline]
+ fn read(reader: &mut R) -> Result<$val_type, DecodeError> {
+ let mut buf = [0; $len];
+ reader.read_exact(&mut buf)?;
+ Ok($meth_read(&buf))
+ }
+ }