aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorfpgaminer <fpgaminer@bitcoin-mining.com>2018-07-30 13:33:42 -0700
committerAndrew Gallant <jamslam@gmail.com>2018-07-30 16:33:42 -0400
commit5bdd2715f3ba81e3d534e0227483525600778f1d (patch)
treed0cd43d4227e10609a40fdde4bb65899d46d63b9
parent03bffa3ded48370a6949c72126bb9611eb4aa44a (diff)
downloadplatform_external_rust_crates_byteorder-5bdd2715f3ba81e3d534e0227483525600778f1d.tar.gz
platform_external_rust_crates_byteorder-5bdd2715f3ba81e3d534e0227483525600778f1d.tar.bz2
platform_external_rust_crates_byteorder-5bdd2715f3ba81e3d534e0227483525600778f1d.zip
byteorder: add {u,i}48 methods
PR #128
-rw-r--r--src/io.rs80
-rw-r--r--src/lib.rs90
2 files changed, 170 insertions, 0 deletions
diff --git a/src/io.rs b/src/io.rs
index 9a66e79..546907e 100644
--- a/src/io.rs
+++ b/src/io.rs
@@ -244,6 +244,58 @@ pub trait ReadBytesExt: io::Read {
Ok(T::read_i32(&buf))
}
+ /// Reads an unsigned 48 bit integer from the underlying reader.
+ ///
+ /// # Errors
+ ///
+ /// This method returns the same errors as [`Read::read_exact`].
+ ///
+ /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
+ ///
+ /// # Examples
+ ///
+ /// Read unsigned 48 bit big-endian integers from a `Read`:
+ ///
+ /// ```rust
+ /// use std::io::Cursor;
+ /// use byteorder::{BigEndian, ReadBytesExt};
+ ///
+ /// let mut rdr = Cursor::new(vec![0xb6, 0x71, 0x6b, 0xdc, 0x2b, 0x31]);
+ /// assert_eq!(200598257150769, rdr.read_u48::<BigEndian>().unwrap());
+ /// ```
+ #[inline]
+ fn read_u48<T: ByteOrder>(&mut self) -> Result<u64> {
+ let mut buf = [0; 6];
+ try!(self.read_exact(&mut buf));
+ Ok(T::read_u48(&buf))
+ }
+
+ /// Reads a signed 48 bit integer from the underlying reader.
+ ///
+ /// # Errors
+ ///
+ /// This method returns the same errors as [`Read::read_exact`].
+ ///
+ /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
+ ///
+ /// # Examples
+ ///
+ /// Read signed 48 bit big-endian integers from a `Read`:
+ ///
+ /// ```rust
+ /// use std::io::Cursor;
+ /// use byteorder::{BigEndian, ReadBytesExt};
+ ///
+ /// let mut rdr = Cursor::new(vec![0x9d, 0x71, 0xab, 0xe7, 0x97, 0x8f]);
+ /// assert_eq!(-108363435763825, rdr.read_i48::<BigEndian>().unwrap());
+ /// ```
+ #[inline]
+ fn read_i48<T: ByteOrder>(&mut self) -> Result<i64> {
+ let mut buf = [0; 6];
+ try!(self.read_exact(&mut buf));
+ Ok(T::read_i48(&buf))
+ }
+
/// Reads an unsigned 64 bit integer from the underlying reader.
///
/// # Errors
@@ -1108,6 +1160,34 @@ pub trait WriteBytesExt: io::Write {
self.write_all(&buf)
}
+ /// Writes an unsigned 48 bit integer to the underlying writer.
+ ///
+ /// # Errors
+ ///
+ /// This method returns the same errors as [`Write::write_all`].
+ ///
+ /// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all
+ #[inline]
+ fn write_u48<T: ByteOrder>(&mut self, n: u64) -> Result<()> {
+ let mut buf = [0; 6];
+ T::write_u48(&mut buf, n);
+ self.write_all(&buf)
+ }
+
+ /// Writes a signed 48 bit integer to the underlying writer.
+ ///
+ /// # Errors
+ ///
+ /// This method returns the same errors as [`Write::write_all`].
+ ///
+ /// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all
+ #[inline]
+ fn write_i48<T: ByteOrder>(&mut self, n: i64) -> Result<()> {
+ let mut buf = [0; 6];
+ T::write_i48(&mut buf, n);
+ self.write_all(&buf)
+ }
+
/// Writes an unsigned 64 bit integer to the underlying writer.
///
/// # Errors
diff --git a/src/lib.rs b/src/lib.rs
index a4a7add..712ce64 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -258,6 +258,27 @@ pub trait ByteOrder
/// ```
fn read_u32(buf: &[u8]) -> u32;
+ /// Reads an unsigned 48 bit integer from `buf`, stored in u64.
+ ///
+ /// # Panics
+ ///
+ /// Panics when `buf.len() < 6`.
+ ///
+ /// # Examples
+ ///
+ /// Write and read 48 bit `u64` numbers in little endian order:
+ ///
+ /// ```rust
+ /// use byteorder::{ByteOrder, LittleEndian};
+ ///
+ /// let mut buf = [0; 6];
+ /// LittleEndian::write_u48(&mut buf, 1_000_000_000_000);
+ /// assert_eq!(1_000_000_000_000, LittleEndian::read_u48(&buf));
+ /// ```
+ fn read_u48(buf: &[u8]) -> u64 {
+ Self::read_uint(buf, 6) as u64
+ }
+
/// Reads an unsigned 64 bit integer from `buf`.
///
/// # Panics
@@ -397,6 +418,27 @@ pub trait ByteOrder
/// ```
fn write_u32(buf: &mut [u8], n: u32);
+ /// Writes an unsigned 48 bit integer `n` to `buf`, stored in u64.
+ ///
+ /// # Panics
+ ///
+ /// Panics when `buf.len() < 6`.
+ ///
+ /// # Examples
+ ///
+ /// Write and read 48 bit `u64` numbers in little endian order:
+ ///
+ /// ```rust
+ /// use byteorder::{ByteOrder, LittleEndian};
+ ///
+ /// let mut buf = [0; 6];
+ /// LittleEndian::write_u48(&mut buf, 1_000_000_000_000);
+ /// assert_eq!(1_000_000_000_000, LittleEndian::read_u48(&buf));
+ /// ```
+ fn write_u48(buf: &mut [u8], n: u64) {
+ Self::write_uint(buf, n as u64, 6)
+ }
+
/// Writes an unsigned 64 bit integer `n` to `buf`.
///
/// # Panics
@@ -543,6 +585,28 @@ pub trait ByteOrder
Self::read_u32(buf) as i32
}
+ /// Reads a signed 48 bit integer from `buf`, stored in i64.
+ ///
+ /// # Panics
+ ///
+ /// Panics when `buf.len() < 6`.
+ ///
+ /// # Examples
+ ///
+ /// Write and read 48 bit `i64` numbers in little endian order:
+ ///
+ /// ```rust
+ /// use byteorder::{ByteOrder, LittleEndian};
+ ///
+ /// let mut buf = [0; 6];
+ /// LittleEndian::write_i48(&mut buf, -1_000_000_000_000);
+ /// assert_eq!(-1_000_000_000_000, LittleEndian::read_i48(&buf));
+ /// ```
+ #[inline]
+ fn read_i48(buf: &[u8]) -> i64 {
+ Self::read_int(buf, 6) as i64
+ }
+
/// Reads a signed 64 bit integer from `buf`.
///
/// # Panics
@@ -747,6 +811,28 @@ pub trait ByteOrder
Self::write_u32(buf, n as u32)
}
+ /// Writes a signed 48 bit integer `n` to `buf`, stored in i64.
+ ///
+ /// # Panics
+ ///
+ /// Panics when `buf.len() < 6`.
+ ///
+ /// # Examples
+ ///
+ /// Write and read 48 bit `i64` numbers in little endian order:
+ ///
+ /// ```rust
+ /// use byteorder::{ByteOrder, LittleEndian};
+ ///
+ /// let mut buf = [0; 6];
+ /// LittleEndian::write_i48(&mut buf, -1_000_000_000_000);
+ /// assert_eq!(-1_000_000_000_000, LittleEndian::read_i48(&buf));
+ /// ```
+ #[inline]
+ fn write_i48(buf: &mut [u8], n: i64) {
+ Self::write_int(buf, n as i64, 6)
+ }
+
/// Writes a signed 64 bit integer `n` to `buf`.
///
/// # Panics
@@ -2226,6 +2312,8 @@ mod test {
pub const U24_MAX: u32 = 16_777_215;
pub const I24_MAX: i32 = 8_388_607;
+ pub const U48_MAX: u64 = 281_474_976_710_655;
+ pub const I48_MAX: i64 = 140_737_488_355_327;
pub const U64_MAX: u64 = ::core::u64::MAX;
pub const I64_MAX: u64 = ::core::i64::MAX as u64;
@@ -2370,6 +2458,8 @@ mod test {
qc_byte_order!(prop_i24, i32, ::test::I24_MAX as u64, read_i24, write_i24);
qc_byte_order!(prop_u32, u32, ::core::u32::MAX as u64, read_u32, write_u32);
qc_byte_order!(prop_i32, i32, ::core::i32::MAX as u64, read_i32, write_i32);
+ qc_byte_order!(prop_u48, u64, ::test::U48_MAX as u64, read_u48, write_u48);
+ qc_byte_order!(prop_i48, i64, ::test::I48_MAX as u64, read_i48, write_i48);
qc_byte_order!(prop_u64, u64, ::core::u64::MAX as u64, read_u64, write_u64);
qc_byte_order!(prop_i64, i64, ::core::i64::MAX as u64, read_i64, write_i64);
qc_byte_order!(prop_f32, f32, ::core::u64::MAX as u64, read_f32, write_f32);