diff --git a/src/buf/buf.rs b/src/buf/buf.rs index 4871e68df268522de9c5a2a67fa911d85f89dd3a..57b8ac7e219983f3fc0903631a517a4bec9c3581 100644 --- a/src/buf/buf.rs +++ b/src/buf/buf.rs @@ -1,5 +1,5 @@ use super::{IntoBuf, Take, Reader, Iter, FromBuf, Chain}; -use byteorder::ByteOrder; +use byteorder::{BigEndian, ByteOrder, LittleEndian}; use iovec::IoVec; use std::{cmp, io, ptr}; @@ -271,236 +271,468 @@ pub trait Buf { buf[0] as i8 } - /// Gets an unsigned 16 bit integer from `self` in the specified byte order. + /// Gets an unsigned 16 bit integer from `self` in big-endian byte order. /// /// The current position is advanced by 2. /// /// # Examples /// /// ``` - /// use bytes::{Buf, BigEndian}; + /// use bytes::Buf; /// use std::io::Cursor; /// /// let mut buf = Cursor::new(b"\x08\x09 hello"); - /// assert_eq!(0x0809, buf.get_u16::<BigEndian>()); + /// assert_eq!(0x0809, buf.get_u16()); /// ``` /// /// # Panics /// /// This function panics if there is not enough remaining data in `self`. - fn get_u16<T: ByteOrder>(&mut self) -> u16 { + fn get_u16(&mut self) -> u16 { let mut buf = [0; 2]; self.copy_to_slice(&mut buf); - T::read_u16(&buf) + BigEndian::read_u16(&buf) } - /// Gets a signed 16 bit integer from `self` in the specified byte order. + /// Gets an unsigned 16 bit integer from `self` in little-endian byte order. /// /// The current position is advanced by 2. /// /// # Examples /// /// ``` - /// use bytes::{Buf, BigEndian}; + /// use bytes::Buf; + /// use std::io::Cursor; + /// + /// let mut buf = Cursor::new(b"\x09\x08 hello"); + /// assert_eq!(0x0809, buf.get_u16_le()); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_u16_le(&mut self) -> u16 { + let mut buf = [0; 2]; + self.copy_to_slice(&mut buf); + LittleEndian::read_u16(&buf) + } + + /// Gets a signed 16 bit integer from `self` in big-endian byte order. + /// + /// The current position is advanced by 2. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; /// use std::io::Cursor; /// /// let mut buf = Cursor::new(b"\x08\x09 hello"); - /// assert_eq!(0x0809, buf.get_i16::<BigEndian>()); + /// assert_eq!(0x0809, buf.get_i16()); /// ``` /// /// # Panics /// /// This function panics if there is not enough remaining data in `self`. - fn get_i16<T: ByteOrder>(&mut self) -> i16 { + fn get_i16(&mut self) -> i16 { let mut buf = [0; 2]; self.copy_to_slice(&mut buf); - T::read_i16(&buf) + BigEndian::read_i16(&buf) } - /// Gets an unsigned 32 bit integer from `self` in the specified byte order. + /// Gets a signed 16 bit integer from `self` in little-endian byte order. + /// + /// The current position is advanced by 2. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// use std::io::Cursor; + /// + /// let mut buf = Cursor::new(b"\x09\x08 hello"); + /// assert_eq!(0x0809, buf.get_i16_le()); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_i16_le(&mut self) -> i16 { + let mut buf = [0; 2]; + self.copy_to_slice(&mut buf); + LittleEndian::read_i16(&buf) + } + + /// Gets an unsigned 32 bit integer from `self` in the big-endian byte order. /// /// The current position is advanced by 4. /// /// # Examples /// /// ``` - /// use bytes::{Buf, BigEndian}; + /// use bytes::Buf; /// use std::io::Cursor; /// /// let mut buf = Cursor::new(b"\x08\x09\xA0\xA1 hello"); - /// assert_eq!(0x0809A0A1, buf.get_u32::<BigEndian>()); + /// assert_eq!(0x0809A0A1, buf.get_u32()); /// ``` /// /// # Panics /// /// This function panics if there is not enough remaining data in `self`. - fn get_u32<T: ByteOrder>(&mut self) -> u32 { + fn get_u32(&mut self) -> u32 { let mut buf = [0; 4]; self.copy_to_slice(&mut buf); - T::read_u32(&buf) + BigEndian::read_u32(&buf) } - /// Gets a signed 32 bit integer from `self` in the specified byte order. + /// Gets an unsigned 32 bit integer from `self` in the little-endian byte order. /// /// The current position is advanced by 4. /// /// # Examples /// /// ``` - /// use bytes::{Buf, BigEndian}; + /// use bytes::Buf; + /// use std::io::Cursor; + /// + /// let mut buf = Cursor::new(b"\xA1\xA0\x09\x08 hello"); + /// assert_eq!(0x0809A0A1, buf.get_u32_le()); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_u32_le(&mut self) -> u32 { + let mut buf = [0; 4]; + self.copy_to_slice(&mut buf); + LittleEndian::read_u32(&buf) + } + + /// Gets a signed 32 bit integer from `self` in big-endian byte order. + /// + /// The current position is advanced by 4. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; /// use std::io::Cursor; /// /// let mut buf = Cursor::new(b"\x08\x09\xA0\xA1 hello"); - /// assert_eq!(0x0809A0A1, buf.get_i32::<BigEndian>()); + /// assert_eq!(0x0809A0A1, buf.get_i32()); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_i32(&mut self) -> i32 { + let mut buf = [0; 4]; + self.copy_to_slice(&mut buf); + BigEndian::read_i32(&buf) + } + + /// Gets a signed 32 bit integer from `self` in little-endian byte order. + /// + /// The current position is advanced by 4. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// use std::io::Cursor; + /// + /// let mut buf = Cursor::new(b"\xA1\xA0\x09\x08 hello"); + /// assert_eq!(0x0809A0A1, buf.get_i32_le()); /// ``` /// /// # Panics /// /// This function panics if there is not enough remaining data in `self`. - fn get_i32<T: ByteOrder>(&mut self) -> i32 { + fn get_i32_le(&mut self) -> i32 { let mut buf = [0; 4]; self.copy_to_slice(&mut buf); - T::read_i32(&buf) + LittleEndian::read_i32(&buf) } - /// Gets an unsigned 64 bit integer from `self` in the specified byte order. + /// Gets an unsigned 64 bit integer from `self` in big-endian byte order. /// /// The current position is advanced by 8. /// /// # Examples /// /// ``` - /// use bytes::{Buf, BigEndian}; + /// use bytes::Buf; /// use std::io::Cursor; /// /// let mut buf = Cursor::new(b"\x01\x02\x03\x04\x05\x06\x07\x08 hello"); - /// assert_eq!(0x0102030405060708, buf.get_u64::<BigEndian>()); + /// assert_eq!(0x0102030405060708, buf.get_u64()); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_u64(&mut self) -> u64 { + let mut buf = [0; 8]; + self.copy_to_slice(&mut buf); + BigEndian::read_u64(&buf) + } + + /// Gets an unsigned 64 bit integer from `self` in little-endian byte order. + /// + /// The current position is advanced by 8. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// use std::io::Cursor; + /// + /// let mut buf = Cursor::new(b"\x08\x07\x06\x05\x04\x03\x02\x01 hello"); + /// assert_eq!(0x0102030405060708, buf.get_u64_le()); /// ``` /// /// # Panics /// /// This function panics if there is not enough remaining data in `self`. - fn get_u64<T: ByteOrder>(&mut self) -> u64 { + fn get_u64_le(&mut self) -> u64 { let mut buf = [0; 8]; self.copy_to_slice(&mut buf); - T::read_u64(&buf) + LittleEndian::read_u64(&buf) } - /// Gets a signed 64 bit integer from `self` in the specified byte order. + /// Gets a signed 64 bit integer from `self` in big-endian byte order. /// /// The current position is advanced by 8. /// /// # Examples /// /// ``` - /// use bytes::{Buf, BigEndian}; + /// use bytes::Buf; /// use std::io::Cursor; /// /// let mut buf = Cursor::new(b"\x01\x02\x03\x04\x05\x06\x07\x08 hello"); - /// assert_eq!(0x0102030405060708, buf.get_i64::<BigEndian>()); + /// assert_eq!(0x0102030405060708, buf.get_i64()); /// ``` /// /// # Panics /// /// This function panics if there is not enough remaining data in `self`. - fn get_i64<T: ByteOrder>(&mut self) -> i64 { + fn get_i64(&mut self) -> i64 { let mut buf = [0; 8]; self.copy_to_slice(&mut buf); - T::read_i64(&buf) + BigEndian::read_i64(&buf) } - /// Gets an unsigned n-byte integer from `self` in the specified byte order. + /// Gets a signed 64 bit integer from `self` in little-endian byte order. + /// + /// The current position is advanced by 8. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// use std::io::Cursor; + /// + /// let mut buf = Cursor::new(b"\x08\x07\x06\x05\x04\x03\x02\x01 hello"); + /// assert_eq!(0x0102030405060708, buf.get_i64_le()); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_i64_le(&mut self) -> i64 { + let mut buf = [0; 8]; + self.copy_to_slice(&mut buf); + LittleEndian::read_i64(&buf) + } + + /// Gets an unsigned n-byte integer from `self` in big-endian byte order. /// /// The current position is advanced by `nbytes`. /// /// # Examples /// /// ``` - /// use bytes::{Buf, BigEndian}; + /// use bytes::Buf; /// use std::io::Cursor; /// /// let mut buf = Cursor::new(b"\x01\x02\x03 hello"); - /// assert_eq!(0x010203, buf.get_uint::<BigEndian>(3)); + /// assert_eq!(0x010203, buf.get_uint(3)); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_uint(&mut self, nbytes: usize) -> u64 { + let mut buf = [0; 8]; + self.copy_to_slice(&mut buf[..nbytes]); + BigEndian::read_uint(&buf[..nbytes], nbytes) + } + + /// Gets an unsigned n-byte integer from `self` in little-endian byte order. + /// + /// The current position is advanced by `nbytes`. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// use std::io::Cursor; + /// + /// let mut buf = Cursor::new(b"\x03\x02\x01 hello"); + /// assert_eq!(0x010203, buf.get_uint_le(3)); /// ``` /// /// # Panics /// /// This function panics if there is not enough remaining data in `self`. - fn get_uint<T: ByteOrder>(&mut self, nbytes: usize) -> u64 { + fn get_uint_le(&mut self, nbytes: usize) -> u64 { let mut buf = [0; 8]; self.copy_to_slice(&mut buf[..nbytes]); - T::read_uint(&buf[..nbytes], nbytes) + LittleEndian::read_uint(&buf[..nbytes], nbytes) } - /// Gets a signed n-byte integer from `self` in the specified byte order. + /// Gets a signed n-byte integer from `self` in big-endian byte order. /// /// The current position is advanced by `nbytes`. /// /// # Examples /// /// ``` - /// use bytes::{Buf, BigEndian}; + /// use bytes::Buf; /// use std::io::Cursor; /// /// let mut buf = Cursor::new(b"\x01\x02\x03 hello"); - /// assert_eq!(0x010203, buf.get_int::<BigEndian>(3)); + /// assert_eq!(0x010203, buf.get_int(3)); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_int(&mut self, nbytes: usize) -> i64 { + let mut buf = [0; 8]; + self.copy_to_slice(&mut buf[..nbytes]); + BigEndian::read_int(&buf[..nbytes], nbytes) + } + + /// Gets a signed n-byte integer from `self` in little-endian byte order. + /// + /// The current position is advanced by `nbytes`. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// use std::io::Cursor; + /// + /// let mut buf = Cursor::new(b"\x03\x02\x01 hello"); + /// assert_eq!(0x010203, buf.get_int_le(3)); /// ``` /// /// # Panics /// /// This function panics if there is not enough remaining data in `self`. - fn get_int<T: ByteOrder>(&mut self, nbytes: usize) -> i64 { + fn get_int_le(&mut self, nbytes: usize) -> i64 { let mut buf = [0; 8]; self.copy_to_slice(&mut buf[..nbytes]); - T::read_int(&buf[..nbytes], nbytes) + LittleEndian::read_int(&buf[..nbytes], nbytes) } /// Gets an IEEE754 single-precision (4 bytes) floating point number from - /// `self` in the specified byte order. + /// `self` in big-endian byte order. /// /// The current position is advanced by 4. /// /// # Examples /// /// ``` - /// use bytes::{Buf, BigEndian}; + /// use bytes::Buf; /// use std::io::Cursor; /// /// let mut buf = Cursor::new(b"\x3F\x99\x99\x9A hello"); - /// assert_eq!(1.2f32, buf.get_f32::<BigEndian>()); + /// assert_eq!(1.2f32, buf.get_f32()); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_f32(&mut self) -> f32 { + let mut buf = [0; 4]; + self.copy_to_slice(&mut buf); + BigEndian::read_f32(&buf) + } + + /// Gets an IEEE754 single-precision (4 bytes) floating point number from + /// `self` in little-endian byte order. + /// + /// The current position is advanced by 4. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// use std::io::Cursor; + /// + /// let mut buf = Cursor::new(b"\x9A\x99\x99\x3F hello"); + /// assert_eq!(1.2f32, buf.get_f32_le()); /// ``` /// /// # Panics /// /// This function panics if there is not enough remaining data in `self`. - fn get_f32<T: ByteOrder>(&mut self) -> f32 { + fn get_f32_le(&mut self) -> f32 { let mut buf = [0; 4]; self.copy_to_slice(&mut buf); - T::read_f32(&buf) + LittleEndian::read_f32(&buf) } /// Gets an IEEE754 double-precision (8 bytes) floating point number from - /// `self` in the specified byte order. + /// `self` in big-endian byte order. /// /// The current position is advanced by 8. /// /// # Examples /// /// ``` - /// use bytes::{Buf, BigEndian}; + /// use bytes::Buf; /// use std::io::Cursor; /// /// let mut buf = Cursor::new(b"\x3F\xF3\x33\x33\x33\x33\x33\x33 hello"); - /// assert_eq!(1.2f64, buf.get_f64::<BigEndian>()); + /// assert_eq!(1.2f64, buf.get_f64()); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining data in `self`. + fn get_f64(&mut self) -> f64 { + let mut buf = [0; 8]; + self.copy_to_slice(&mut buf); + BigEndian::read_f64(&buf) + } + + /// Gets an IEEE754 double-precision (8 bytes) floating point number from + /// `self` in little-endian byte order. + /// + /// The current position is advanced by 8. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// use std::io::Cursor; + /// + /// let mut buf = Cursor::new(b"\x33\x33\x33\x33\x33\x33\xF3\x3F hello"); + /// assert_eq!(1.2f64, buf.get_f64_le()); /// ``` /// /// # Panics /// /// This function panics if there is not enough remaining data in `self`. - fn get_f64<T: ByteOrder>(&mut self) -> f64 { + fn get_f64_le(&mut self) -> f64 { let mut buf = [0; 8]; self.copy_to_slice(&mut buf); - T::read_f64(&buf) + LittleEndian::read_f64(&buf) } /// Transforms a `Buf` into a concrete buffer. @@ -749,3 +981,7 @@ impl Buf for Option<[u8; 1]> { } } } + +// The existance of this function makes the compiler catch if the Buf +// trait is "object-safe" or not. +fn _assert_trait_object(_b: &Buf) {} diff --git a/src/buf/buf_mut.rs b/src/buf/buf_mut.rs index 72eb5cb62a9045b4b26110209a9db34f81bc80cc..414c5195b713d184fb79fd7405ebffaa6d98fb2a 100644 --- a/src/buf/buf_mut.rs +++ b/src/buf/buf_mut.rs @@ -1,5 +1,5 @@ use super::{IntoBuf, Writer}; -use byteorder::ByteOrder; +use byteorder::{LittleEndian, ByteOrder, BigEndian}; use iovec::IoVecMut; use std::{cmp, io, ptr, usize}; @@ -338,17 +338,17 @@ pub trait BufMut { self.put_slice(&src) } - /// Writes an unsigned 16 bit integer to `self` in the specified byte order. + /// Writes an unsigned 16 bit integer to `self` in big-endian byte order. /// /// The current position is advanced by 2. /// /// # Examples /// /// ``` - /// use bytes::{BufMut, BigEndian}; + /// use bytes::BufMut; /// /// let mut buf = vec![]; - /// buf.put_u16::<BigEndian>(0x0809); + /// buf.put_u16(0x0809); /// assert_eq!(buf, b"\x08\x09"); /// ``` /// @@ -356,23 +356,47 @@ pub trait BufMut { /// /// This function panics if there is not enough remaining capacity in /// `self`. - fn put_u16<T: ByteOrder>(&mut self, n: u16) { + fn put_u16(&mut self, n: u16) { + let mut buf = [0; 2]; + BigEndian::write_u16(&mut buf, n); + self.put_slice(&buf) + } + + /// Writes an unsigned 16 bit integer to `self` in little-endian byte order. + /// + /// The current position is advanced by 2. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_u16_le(0x0809); + /// assert_eq!(buf, b"\x09\x08"); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_u16_le(&mut self, n: u16) { let mut buf = [0; 2]; - T::write_u16(&mut buf, n); + LittleEndian::write_u16(&mut buf, n); self.put_slice(&buf) } - /// Writes a signed 16 bit integer to `self` in the specified byte order. + /// Writes a signed 16 bit integer to `self` in big-endian byte order. /// /// The current position is advanced by 2. /// /// # Examples /// /// ``` - /// use bytes::{BufMut, BigEndian}; + /// use bytes::BufMut; /// /// let mut buf = vec![]; - /// buf.put_i16::<BigEndian>(0x0809); + /// buf.put_i16(0x0809); /// assert_eq!(buf, b"\x08\x09"); /// ``` /// @@ -380,23 +404,47 @@ pub trait BufMut { /// /// This function panics if there is not enough remaining capacity in /// `self`. - fn put_i16<T: ByteOrder>(&mut self, n: i16) { + fn put_i16(&mut self, n: i16) { let mut buf = [0; 2]; - T::write_i16(&mut buf, n); + BigEndian::write_i16(&mut buf, n); self.put_slice(&buf) } - /// Writes an unsigned 32 bit integer to `self` in the specified byte order. + /// Writes a signed 16 bit integer to `self` in little-endian byte order. + /// + /// The current position is advanced by 2. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_i16_le(0x0809); + /// assert_eq!(buf, b"\x09\x08"); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_i16_le(&mut self, n: i16) { + let mut buf = [0; 2]; + LittleEndian::write_i16(&mut buf, n); + self.put_slice(&buf) + } + + /// Writes an unsigned 32 bit integer to `self` in big-endian byte order. /// /// The current position is advanced by 4. /// /// # Examples /// /// ``` - /// use bytes::{BufMut, BigEndian}; + /// use bytes::BufMut; /// /// let mut buf = vec![]; - /// buf.put_u32::<BigEndian>(0x0809A0A1); + /// buf.put_u32(0x0809A0A1); /// assert_eq!(buf, b"\x08\x09\xA0\xA1"); /// ``` /// @@ -404,23 +452,47 @@ pub trait BufMut { /// /// This function panics if there is not enough remaining capacity in /// `self`. - fn put_u32<T: ByteOrder>(&mut self, n: u32) { + fn put_u32(&mut self, n: u32) { + let mut buf = [0; 4]; + BigEndian::write_u32(&mut buf, n); + self.put_slice(&buf) + } + + /// Writes an unsigned 32 bit integer to `self` in little-endian byte order. + /// + /// The current position is advanced by 4. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_u32_le(0x0809A0A1); + /// assert_eq!(buf, b"\xA1\xA0\x09\x08"); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_u32_le(&mut self, n: u32) { let mut buf = [0; 4]; - T::write_u32(&mut buf, n); + LittleEndian::write_u32(&mut buf, n); self.put_slice(&buf) } - /// Writes a signed 32 bit integer to `self` in the specified byte order. + /// Writes a signed 32 bit integer to `self` in big-endian byte order. /// /// The current position is advanced by 4. /// /// # Examples /// /// ``` - /// use bytes::{BufMut, BigEndian}; + /// use bytes::BufMut; /// /// let mut buf = vec![]; - /// buf.put_i32::<BigEndian>(0x0809A0A1); + /// buf.put_i32(0x0809A0A1); /// assert_eq!(buf, b"\x08\x09\xA0\xA1"); /// ``` /// @@ -428,23 +500,47 @@ pub trait BufMut { /// /// This function panics if there is not enough remaining capacity in /// `self`. - fn put_i32<T: ByteOrder>(&mut self, n: i32) { + fn put_i32(&mut self, n: i32) { let mut buf = [0; 4]; - T::write_i32(&mut buf, n); + BigEndian::write_i32(&mut buf, n); self.put_slice(&buf) } - /// Writes an unsigned 64 bit integer to `self` in the specified byte order. + /// Writes a signed 32 bit integer to `self` in little-endian byte order. + /// + /// The current position is advanced by 4. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_i32_le(0x0809A0A1); + /// assert_eq!(buf, b"\xA1\xA0\x09\x08"); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_i32_le(&mut self, n: i32) { + let mut buf = [0; 4]; + LittleEndian::write_i32(&mut buf, n); + self.put_slice(&buf) + } + + /// Writes an unsigned 64 bit integer to `self` in the big-endian byte order. /// /// The current position is advanced by 8. /// /// # Examples /// /// ``` - /// use bytes::{BufMut, BigEndian}; + /// use bytes::BufMut; /// /// let mut buf = vec![]; - /// buf.put_u64::<BigEndian>(0x0102030405060708); + /// buf.put_u64(0x0102030405060708); /// assert_eq!(buf, b"\x01\x02\x03\x04\x05\x06\x07\x08"); /// ``` /// @@ -452,23 +548,47 @@ pub trait BufMut { /// /// This function panics if there is not enough remaining capacity in /// `self`. - fn put_u64<T: ByteOrder>(&mut self, n: u64) { + fn put_u64(&mut self, n: u64) { let mut buf = [0; 8]; - T::write_u64(&mut buf, n); + BigEndian::write_u64(&mut buf, n); self.put_slice(&buf) } - /// Writes a signed 64 bit integer to `self` in the specified byte order. + /// Writes an unsigned 64 bit integer to `self` in little-endian byte order. /// /// The current position is advanced by 8. /// /// # Examples /// /// ``` - /// use bytes::{BufMut, BigEndian}; + /// use bytes::BufMut; /// /// let mut buf = vec![]; - /// buf.put_i64::<BigEndian>(0x0102030405060708); + /// buf.put_u64_le(0x0102030405060708); + /// assert_eq!(buf, b"\x08\x07\x06\x05\x04\x03\x02\x01"); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_u64_le(&mut self, n: u64) { + let mut buf = [0; 8]; + LittleEndian::write_u64(&mut buf, n); + self.put_slice(&buf) + } + + /// Writes a signed 64 bit integer to `self` in the big-endian byte order. + /// + /// The current position is advanced by 8. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_i64(0x0102030405060708); /// assert_eq!(buf, b"\x01\x02\x03\x04\x05\x06\x07\x08"); /// ``` /// @@ -476,23 +596,47 @@ pub trait BufMut { /// /// This function panics if there is not enough remaining capacity in /// `self`. - fn put_i64<T: ByteOrder>(&mut self, n: i64) { + fn put_i64(&mut self, n: i64) { let mut buf = [0; 8]; - T::write_i64(&mut buf, n); + BigEndian::write_i64(&mut buf, n); self.put_slice(&buf) } - /// Writes an unsigned n-byte integer to `self` in the specified byte order. + /// Writes a signed 64 bit integer to `self` in little-endian byte order. + /// + /// The current position is advanced by 8. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_i64_le(0x0102030405060708); + /// assert_eq!(buf, b"\x08\x07\x06\x05\x04\x03\x02\x01"); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_i64_le(&mut self, n: i64) { + let mut buf = [0; 8]; + LittleEndian::write_i64(&mut buf, n); + self.put_slice(&buf) + } + + /// Writes an unsigned n-byte integer to `self` in big-endian byte order. /// /// The current position is advanced by `nbytes`. /// /// # Examples /// /// ``` - /// use bytes::{BufMut, BigEndian}; + /// use bytes::BufMut; /// /// let mut buf = vec![]; - /// buf.put_uint::<BigEndian>(0x010203, 3); + /// buf.put_uint(0x010203, 3); /// assert_eq!(buf, b"\x01\x02\x03"); /// ``` /// @@ -500,23 +644,47 @@ pub trait BufMut { /// /// This function panics if there is not enough remaining capacity in /// `self`. - fn put_uint<T: ByteOrder>(&mut self, n: u64, nbytes: usize) { + fn put_uint(&mut self, n: u64, nbytes: usize) { let mut buf = [0; 8]; - T::write_uint(&mut buf, n, nbytes); + BigEndian::write_uint(&mut buf, n, nbytes); self.put_slice(&buf[0..nbytes]) } - /// Writes a signed n-byte integer to `self` in the specified byte order. + /// Writes an unsigned n-byte integer to `self` in the little-endian byte order. /// /// The current position is advanced by `nbytes`. /// /// # Examples /// /// ``` - /// use bytes::{BufMut, BigEndian}; + /// use bytes::BufMut; /// /// let mut buf = vec![]; - /// buf.put_int::<BigEndian>(0x010203, 3); + /// buf.put_uint_le(0x010203, 3); + /// assert_eq!(buf, b"\x03\x02\x01"); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_uint_le(&mut self, n: u64, nbytes: usize) { + let mut buf = [0; 8]; + LittleEndian::write_uint(&mut buf, n, nbytes); + self.put_slice(&buf[0..nbytes]) + } + + /// Writes a signed n-byte integer to `self` in big-endian byte order. + /// + /// The current position is advanced by `nbytes`. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_int(0x010203, 3); /// assert_eq!(buf, b"\x01\x02\x03"); /// ``` /// @@ -524,24 +692,48 @@ pub trait BufMut { /// /// This function panics if there is not enough remaining capacity in /// `self`. - fn put_int<T: ByteOrder>(&mut self, n: i64, nbytes: usize) { + fn put_int(&mut self, n: i64, nbytes: usize) { let mut buf = [0; 8]; - T::write_int(&mut buf, n, nbytes); + BigEndian::write_int(&mut buf, n, nbytes); + self.put_slice(&buf[0..nbytes]) + } + + /// Writes a signed n-byte integer to `self` in little-endian byte order. + /// + /// The current position is advanced by `nbytes`. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_int_le(0x010203, 3); + /// assert_eq!(buf, b"\x03\x02\x01"); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_int_le(&mut self, n: i64, nbytes: usize) { + let mut buf = [0; 8]; + LittleEndian::write_int(&mut buf, n, nbytes); self.put_slice(&buf[0..nbytes]) } /// Writes an IEEE754 single-precision (4 bytes) floating point number to - /// `self` in the specified byte order. + /// `self` in big-endian byte order. /// /// The current position is advanced by 4. /// /// # Examples /// /// ``` - /// use bytes::{BufMut, BigEndian}; + /// use bytes::BufMut; /// /// let mut buf = vec![]; - /// buf.put_f32::<BigEndian>(1.2f32); + /// buf.put_f32(1.2f32); /// assert_eq!(buf, b"\x3F\x99\x99\x9A"); /// ``` /// @@ -549,24 +741,49 @@ pub trait BufMut { /// /// This function panics if there is not enough remaining capacity in /// `self`. - fn put_f32<T: ByteOrder>(&mut self, n: f32) { + fn put_f32(&mut self, n: f32) { + let mut buf = [0; 4]; + BigEndian::write_f32(&mut buf, n); + self.put_slice(&buf) + } + + /// Writes an IEEE754 single-precision (4 bytes) floating point number to + /// `self` in little-endian byte order. + /// + /// The current position is advanced by 4. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_f32_le(1.2f32); + /// assert_eq!(buf, b"\x9A\x99\x99\x3F"); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_f32_le(&mut self, n: f32) { let mut buf = [0; 4]; - T::write_f32(&mut buf, n); + LittleEndian::write_f32(&mut buf, n); self.put_slice(&buf) } /// Writes an IEEE754 double-precision (8 bytes) floating point number to - /// `self` in the specified byte order. + /// `self` in big-endian byte order. /// /// The current position is advanced by 8. /// /// # Examples /// /// ``` - /// use bytes::{BufMut, BigEndian}; + /// use bytes::BufMut; /// /// let mut buf = vec![]; - /// buf.put_f64::<BigEndian>(1.2f64); + /// buf.put_f64(1.2f64); /// assert_eq!(buf, b"\x3F\xF3\x33\x33\x33\x33\x33\x33"); /// ``` /// @@ -574,9 +791,34 @@ pub trait BufMut { /// /// This function panics if there is not enough remaining capacity in /// `self`. - fn put_f64<T: ByteOrder>(&mut self, n: f64) { + fn put_f64(&mut self, n: f64) { let mut buf = [0; 8]; - T::write_f64(&mut buf, n); + BigEndian::write_f64(&mut buf, n); + self.put_slice(&buf) + } + + /// Writes an IEEE754 double-precision (8 bytes) floating point number to + /// `self` in little-endian byte order. + /// + /// The current position is advanced by 8. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut buf = vec![]; + /// buf.put_f64_le(1.2f64); + /// assert_eq!(buf, b"\x33\x33\x33\x33\x33\x33\xF3\x3F"); + /// ``` + /// + /// # Panics + /// + /// This function panics if there is not enough remaining capacity in + /// `self`. + fn put_f64_le(&mut self, n: f64) { + let mut buf = [0; 8]; + LittleEndian::write_f64(&mut buf, n); self.put_slice(&buf) } @@ -734,3 +976,7 @@ impl BufMut for Vec<u8> { &mut slice::from_raw_parts_mut(ptr, cap)[len..] } } + +// The existance of this function makes the compiler catch if the BufMut +// trait is "object-safe" or not. +fn _assert_trait_object(_b: &BufMut) {} diff --git a/src/buf/into_buf.rs b/src/buf/into_buf.rs index 1071908a2efcf5b0b612aded15c785942d873b77..96419daa84d0e4e41f2b204d0f2b8b1af15bcb3c 100644 --- a/src/buf/into_buf.rs +++ b/src/buf/into_buf.rs @@ -11,12 +11,12 @@ use std::io; /// # Examples /// /// ``` -/// use bytes::{Buf, IntoBuf, BigEndian}; +/// use bytes::{Buf, IntoBuf}; /// /// let bytes = b"\x00\x01hello world"; /// let mut buf = bytes.into_buf(); /// -/// assert_eq!(1, buf.get_u16::<BigEndian>()); +/// assert_eq!(1, buf.get_u16()); /// /// let mut rest = [0; 11]; /// buf.copy_to_slice(&mut rest); @@ -32,12 +32,12 @@ pub trait IntoBuf { /// # Examples /// /// ``` - /// use bytes::{Buf, IntoBuf, BigEndian}; + /// use bytes::{Buf, IntoBuf}; /// /// let bytes = b"\x00\x01hello world"; /// let mut buf = bytes.into_buf(); /// - /// assert_eq!(1, buf.get_u16::<BigEndian>()); + /// assert_eq!(1, buf.get_u16()); /// /// let mut rest = [0; 11]; /// buf.copy_to_slice(&mut rest); diff --git a/src/lib.rs b/src/lib.rs index 87dc15a0ec7d6e5a3b983172ef6dc13062e2a844..fe5c4833c03a547df500bd562ba8abfc82ab559b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,11 +23,11 @@ //! example: //! //! ```rust -//! use bytes::{BytesMut, BufMut, BigEndian}; +//! use bytes::{BytesMut, BufMut}; //! //! let mut buf = BytesMut::with_capacity(1024); //! buf.put(&b"hello world"[..]); -//! buf.put_u16::<BigEndian>(1234); +//! buf.put_u16(1234); //! //! let a = buf.take(); //! assert_eq!(a, b"hello world\x04\xD2"[..]); @@ -92,8 +92,6 @@ mod bytes; mod debug; pub use bytes::{Bytes, BytesMut}; -pub use byteorder::{ByteOrder, BigEndian, LittleEndian}; - // Optional Serde support #[cfg(feature = "serde")] #[doc(hidden)] diff --git a/tests/test_buf.rs b/tests/test_buf.rs index 0243e25c385e4b42e32950d9a8304bb9c39edc8e..93817eabbe87c4298e401d3bc57f74017616f69a 100644 --- a/tests/test_buf.rs +++ b/tests/test_buf.rs @@ -33,15 +33,15 @@ fn test_get_u8() { #[test] fn test_get_u16() { let buf = b"\x21\x54zomg"; - assert_eq!(0x2154, Cursor::new(buf).get_u16::<byteorder::BigEndian>()); - assert_eq!(0x5421, Cursor::new(buf).get_u16::<byteorder::LittleEndian>()); + assert_eq!(0x2154, Cursor::new(buf).get_u16()); + assert_eq!(0x5421, Cursor::new(buf).get_u16_le()); } #[test] #[should_panic] fn test_get_u16_buffer_underflow() { let mut buf = Cursor::new(b"\x21"); - buf.get_u16::<byteorder::BigEndian>(); + buf.get_u16(); } #[test] diff --git a/tests/test_buf_mut.rs b/tests/test_buf_mut.rs index 8b5e3dbb851fea0578ca4bdb3561713fd292ade7..0179259d5a59dc055f6ba3a55949711f0706842f 100644 --- a/tests/test_buf_mut.rs +++ b/tests/test_buf_mut.rs @@ -41,11 +41,11 @@ fn test_put_u8() { #[test] fn test_put_u16() { let mut buf = Vec::with_capacity(8); - buf.put_u16::<byteorder::BigEndian>(8532); + buf.put_u16(8532); assert_eq!(b"\x21\x54", &buf[..]); buf.clear(); - buf.put_u16::<byteorder::LittleEndian>(8532); + buf.put_u16_le(8532); assert_eq!(b"\x54\x21", &buf[..]); }