From 046c8645431ae2ffa25aad549f6a0d6e2df44930 Mon Sep 17 00:00:00 2001 From: Carl Lerche <me@carllerche.com> Date: Wed, 31 Aug 2016 12:07:44 -0700 Subject: [PATCH] Integrate with byteorder --- Cargo.toml | 2 +- src/buf/mod.rs | 188 ++++++++++++++++++++++++++++++++++++++++--- src/bytes/mod.rs | 4 +- src/lib.rs | 2 + test/test.rs | 1 + test/test_buf.rs | 41 ++++------ test/test_mut_buf.rs | 45 +++++++++++ 7 files changed, 246 insertions(+), 37 deletions(-) create mode 100644 test/test_mut_buf.rs diff --git a/Cargo.toml b/Cargo.toml index a8b1c6c..b88cc19 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,10 +20,10 @@ exclude = [ [dependencies] log = "0.3.6" +byteorder = "0.5.3" [dev-dependencies] rand = "0.3.5" -byteorder = "0.5.3" [[bench]] diff --git a/src/buf/mod.rs b/src/buf/mod.rs index 985dfcb..587de2c 100644 --- a/src/buf/mod.rs +++ b/src/buf/mod.rs @@ -5,6 +5,7 @@ pub mod ring; pub mod take; use {Bytes}; +use byteorder::ByteOrder; use std::{cmp, fmt, io, ptr, usize}; /// A trait for values that provide sequential read access to bytes. @@ -69,23 +70,100 @@ pub trait Buf { } } - /// Read a single byte from the `Buf` - fn read_byte(&mut self) -> Option<u8> { + /// Reads an unsigned 8 bit integer from the `Buf` without advancing the + /// buffer cursor + fn peek_u8(&self) -> Option<u8> { if self.has_remaining() { - let mut dst = [0]; - self.read_slice(&mut dst); - Some(dst[0]) + Some(self.bytes()[0]) } else { None } } - fn peek_byte(&self) -> Option<u8> { - if self.has_remaining() { - Some(self.bytes()[0]) - } else { - None - } + /// Reads an unsigned 8 bit integer from the `Buf`. + fn read_u8(&mut self) -> u8 { + let mut buf = [0; 1]; + self.read_slice(&mut buf); + buf[0] + } + + /// Reads a signed 8 bit integer from the `Buf`. + fn read_i8(&mut self) -> i8 { + let mut buf = [0; 1]; + self.read_slice(&mut buf); + buf[0] as i8 + } + + /// Reads an unsigned 16 bit integer from the `Buf` + fn read_u16<T: ByteOrder>(&mut self) -> u16 { + let mut buf = [0; 2]; + self.read_slice(&mut buf); + T::read_u16(&buf) + } + + /// Reads a signed 16 bit integer from the `Buf` + fn read_i16<T: ByteOrder>(&mut self) -> i16 { + let mut buf = [0; 2]; + self.read_slice(&mut buf); + T::read_i16(&buf) + } + + /// Reads an unsigned 32 bit integer from the `Buf` + fn read_u32<T: ByteOrder>(&mut self) -> u32 { + let mut buf = [0; 4]; + self.read_slice(&mut buf); + T::read_u32(&buf) + } + + /// Reads a signed 32 bit integer from the `Buf` + fn read_i32<T: ByteOrder>(&mut self) -> i32 { + let mut buf = [0; 4]; + self.read_slice(&mut buf); + T::read_i32(&buf) + } + + /// Reads an unsigned 64 bit integer from the `Buf` + fn read_u64<T: ByteOrder>(&mut self) -> u64 { + let mut buf = [0; 8]; + self.read_slice(&mut buf); + T::read_u64(&buf) + } + + /// Reads a signed 64 bit integer from the `Buf` + fn read_i64<T: ByteOrder>(&mut self) -> i64 { + let mut buf = [0; 8]; + self.read_slice(&mut buf); + T::read_i64(&buf) + } + + /// Reads an unsigned n-bytes integer from the `Buf` + fn read_uint<T: ByteOrder>(&mut self, nbytes: usize) -> u64 { + let mut buf = [0; 8]; + self.read_slice(&mut buf[..nbytes]); + T::read_uint(&buf[..nbytes], nbytes) + } + + /// Reads a signed n-bytes integer from the `Buf` + fn read_int<T: ByteOrder>(&mut self, nbytes: usize) -> i64 { + let mut buf = [0; 8]; + self.read_slice(&mut buf[..nbytes]); + T::read_int(&buf[..nbytes], nbytes) + } + + /// Reads a IEEE754 single-precision (4 bytes) floating point number from + /// the `Buf` + fn read_f32<T: ByteOrder>(&mut self) -> f32 { + let mut buf = [0; 4]; + self.read_slice(&mut buf); + T::read_f32(&buf) + } + + /// Reads a IEEE754 double-precision (8 bytes) floating point number from + /// the `Buf` + fn read_f64<T: ByteOrder>(&mut self) -> f64 { + let mut buf = [0; 8]; + self.read_slice(&mut buf); + T::read_f64(&buf) } } @@ -163,6 +241,94 @@ pub trait MutBuf { fn write_str(&mut self, src: &str) { self.write_slice(src.as_bytes()); } + + /// Writes an unsigned 8 bit integer to the MutBuf. + fn write_u8(&mut self, n: u8) { + self.write_slice(&[n]) + } + + /// Writes a signed 8 bit integer to the MutBuf. + fn write_i8(&mut self, n: i8) { + self.write_slice(&[n as u8]) + } + + /// Writes an unsigned 16 bit integer to the MutBuf. + fn write_u16<T: ByteOrder>(&mut self, n: u16) { + let mut buf = [0; 2]; + T::write_u16(&mut buf, n); + self.write_slice(&buf) + } + + /// Writes a signed 16 bit integer to the MutBuf. + fn write_i16<T: ByteOrder>(&mut self, n: i16) { + let mut buf = [0; 2]; + T::write_i16(&mut buf, n); + self.write_slice(&buf) + } + + /// Writes an unsigned 32 bit integer to the MutBuf. + fn write_u32<T: ByteOrder>(&mut self, n: u32) { + let mut buf = [0; 4]; + T::write_u32(&mut buf, n); + self.write_slice(&buf) + } + + /// Writes a signed 32 bit integer to the MutBuf. + fn write_i32<T: ByteOrder>(&mut self, n: i32) { + let mut buf = [0; 4]; + T::write_i32(&mut buf, n); + self.write_slice(&buf) + } + + /// Writes an unsigned 64 bit integer to the MutBuf. + fn write_u64<T: ByteOrder>(&mut self, n: u64) { + let mut buf = [0; 8]; + T::write_u64(&mut buf, n); + self.write_slice(&buf) + } + + /// Writes a signed 64 bit integer to the MutBuf. + fn write_i64<T: ByteOrder>(&mut self, n: i64) { + let mut buf = [0; 8]; + T::write_i64(&mut buf, n); + self.write_slice(&buf) + } + + /// Writes an unsigned n-bytes integer to the MutBuf. + /// + /// If the given integer is not representable in the given number of bytes, + /// this method panics. If `nbytes > 8`, this method panics. + fn write_uint<T: ByteOrder>(&mut self, n: u64, nbytes: usize) { + let mut buf = [0; 8]; + T::write_uint(&mut buf, n, nbytes); + self.write_slice(&buf[0..nbytes]) + } + + /// Writes a signed n-bytes integer to the MutBuf. + /// + /// If the given integer is not representable in the given number of bytes, + /// this method panics. If `nbytes > 8`, this method panics. + fn write_int<T: ByteOrder>(&mut self, n: i64, nbytes: usize) { + let mut buf = [0; 8]; + T::write_int(&mut buf, n, nbytes); + self.write_slice(&buf[0..nbytes]) + } + + /// Writes a IEEE754 single-precision (4 bytes) floating point number to + /// the MutBuf. + fn write_f32<T: ByteOrder>(&mut self, n: f32) { + let mut buf = [0; 4]; + T::write_f32(&mut buf, n); + self.write_slice(&buf) + } + + /// Writes a IEEE754 double-precision (8 bytes) floating point number to + /// the MutBuf. + fn write_f64<T: ByteOrder>(&mut self, n: f64) { + let mut buf = [0; 8]; + T::write_f64(&mut buf, n); + self.write_slice(&buf) + } } /* diff --git a/src/bytes/mod.rs b/src/bytes/mod.rs index fd6bdcb..47548c6 100644 --- a/src/bytes/mod.rs +++ b/src/bytes/mod.rs @@ -213,7 +213,9 @@ impl fmt::Debug for Bytes { let mut rem = 128; - while let Some(byte) = buf.read_byte() { + while buf.has_remaining() { + let byte = buf.read_u8(); + if rem > 0 { if is_ascii(byte) { try!(write!(fmt, "{}", byte as char)); diff --git a/src/lib.rs b/src/lib.rs index edf0006..e31b763 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,8 @@ #[macro_use] extern crate log; +extern crate byteorder; + mod buf; mod bytes; diff --git a/test/test.rs b/test/test.rs index c2c078e..f4edae2 100644 --- a/test/test.rs +++ b/test/test.rs @@ -9,6 +9,7 @@ mod test_append; mod test_buf; mod test_buf_fill; mod test_byte_buf; +mod test_mut_buf; mod test_ring; // == Bytes diff --git a/test/test_buf.rs b/test/test_buf.rs index 3c0175b..bb5c0f5 100644 --- a/test/test_buf.rs +++ b/test/test_buf.rs @@ -1,10 +1,9 @@ -use std::usize; +use bytes::{Buf}; +use byteorder; use std::io::{Cursor}; #[test] pub fn test_fresh_cursor_vec() { - use bytes::Buf; - let mut buf = Cursor::new(b"hello".to_vec()); assert_eq!(buf.remaining(), 5); @@ -27,27 +26,21 @@ pub fn test_fresh_cursor_vec() { } #[test] -pub fn test_vec_as_mut_buf() { - use bytes::MutBuf; - - let mut buf = Vec::with_capacity(64); - - assert_eq!(buf.remaining(), usize::MAX); - - unsafe { - assert!(buf.mut_bytes().len() >= 64); - } - - buf.copy_from(&b"zomg"[..]); - - assert_eq!(&buf, b"zomg"); - - assert_eq!(buf.remaining(), usize::MAX - 4); - assert_eq!(buf.capacity(), 64); +pub fn test_read_u8() { + let mut buf = Cursor::new(b"\x21zomg"); + assert_eq!(0x21, buf.read_u8()); +} - for _ in 0..16 { - buf.copy_from(&b"zomg"[..]); - } +#[test] +fn test_read_u16() { + let buf = b"\x21\x54zomg"; + assert_eq!(0x2154, Cursor::new(buf).read_u16::<byteorder::BigEndian>()); + assert_eq!(0x5421, Cursor::new(buf).read_u16::<byteorder::LittleEndian>()); +} - assert_eq!(buf.len(), 68); +#[test] +#[should_panic] +fn test_read_u16_buffer_underflow() { + let mut buf = Cursor::new(b"\x21"); + buf.read_u16::<byteorder::BigEndian>(); } diff --git a/test/test_mut_buf.rs b/test/test_mut_buf.rs new file mode 100644 index 0000000..bb00d1b --- /dev/null +++ b/test/test_mut_buf.rs @@ -0,0 +1,45 @@ +use bytes::MutBuf; +use byteorder; +use std::usize; + +#[test] +pub fn test_vec_as_mut_buf() { + let mut buf = Vec::with_capacity(64); + + assert_eq!(buf.remaining(), usize::MAX); + + unsafe { + assert!(buf.mut_bytes().len() >= 64); + } + + buf.copy_from(&b"zomg"[..]); + + assert_eq!(&buf, b"zomg"); + + assert_eq!(buf.remaining(), usize::MAX - 4); + assert_eq!(buf.capacity(), 64); + + for _ in 0..16 { + buf.copy_from(&b"zomg"[..]); + } + + assert_eq!(buf.len(), 68); +} + +#[test] +pub fn test_write_u8() { + let mut buf = Vec::with_capacity(8); + buf.write_u8(33); + assert_eq!(b"\x21", &buf[..]); +} + +#[test] +fn test_write_u16() { + let mut buf = Vec::with_capacity(8); + buf.write_u16::<byteorder::BigEndian>(8532); + assert_eq!(b"\x21\x54", &buf[..]); + + buf.clear(); + buf.write_u16::<byteorder::LittleEndian>(8532); + assert_eq!(b"\x54\x21", &buf[..]); +} -- GitLab