diff options
author | fpgaminer <fpgaminer@bitcoin-mining.com> | 2018-07-30 13:33:42 -0700 |
---|---|---|
committer | Andrew Gallant <jamslam@gmail.com> | 2018-07-30 16:33:42 -0400 |
commit | 5bdd2715f3ba81e3d534e0227483525600778f1d (patch) | |
tree | d0cd43d4227e10609a40fdde4bb65899d46d63b9 /src/io.rs | |
parent | 03bffa3ded48370a6949c72126bb9611eb4aa44a (diff) | |
download | byteorder-5bdd2715f3ba81e3d534e0227483525600778f1d.tar.gz |
byteorder: add {u,i}48 methods
PR #128
Diffstat (limited to 'src/io.rs')
-rw-r--r-- | src/io.rs | 80 |
1 files changed, 80 insertions, 0 deletions
@@ -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 |