diff --git a/Cargo.toml b/Cargo.toml index a8b1c6c313bac9ce426d7509526eec861df473fe..b88cc193e8ad57a2cc57c5a9bbe4f2d49c2887aa 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 985dfcbcb17d66aa38775dcd748454cb05f65768..587de2c7ee66132bc7e1ad9587e23b310ed287f8 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 fd6bdcbf42665fffe6d177b2c73629f22a4e053f..47548c67a22d74654b421f648a0c8fcf20ee133a 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 edf000669d8238b2bf5f4eb0a51638f53fd97427..e31b763290344a78f74bacd0a7670a9487df7671 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 c2c078e0111d078bc5755fbcc74548dec49e12e0..f4edae29990698128afd6dfbe4b18ac454a281ca 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 3c0175bea0c69cd50e73a54b618f0c00f48feccf..bb5c0f5fb3fe3bf9005eec0ace98450d2c2d4ca1 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 0000000000000000000000000000000000000000..bb00d1b8039fdb1ec705fe037d2e6b73776993ca --- /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[..]); +}