diff options
author | Andrew Gallant <jamslam@gmail.com> | 2017-07-09 13:10:55 -0400 |
---|---|---|
committer | Andrew Gallant <jamslam@gmail.com> | 2017-07-09 14:26:40 -0400 |
commit | ef7f25767cb199a5738b2bc530d9a2a9a49abd7f (patch) | |
tree | 1a7992136c59a66ac884e9ea52ef2841f6e50f96 /src | |
parent | 3477a8c7b7dc4c74fe3edbb53777966dfcf354a5 (diff) | |
download | byteorder-ef7f25767cb199a5738b2bc530d9a2a9a49abd7f.tar.gz |
slices: add slice methods for ReadBytesExt
As a consequence, this also introduces methods for doing endian
conversion on slices in-place.
Diffstat (limited to 'src')
-rw-r--r-- | src/io.rs | 440 | ||||
-rw-r--r-- | src/lib.rs | 338 |
2 files changed, 771 insertions, 7 deletions
@@ -1,4 +1,6 @@ use std::io::{self, Result}; +use std::slice; +// use std::mem::transmute; use ByteOrder; @@ -431,12 +433,16 @@ pub trait ReadBytesExt: io::Read { /// Read a big-endian single-precision floating point number from a `Read`: /// /// ```rust + /// use std::f32; /// use std::io::Cursor; + /// /// use byteorder::{BigEndian, ReadBytesExt}; - /// use std::f32::consts; /// - /// let mut rdr = Cursor::new(vec![0x40, 0x49, 0x0f, 0xdb]); - /// assert_eq!(consts::PI, rdr.read_f32::<BigEndian>().unwrap()); + /// let mut rdr = Cursor::new(vec![ + /// 0x40, 0x49, 0x0f, 0xdb, + /// ]); + /// assert_eq!(f32::consts::PI, rdr.read_f32::<BigEndian>().unwrap()); + /// ``` #[inline] fn read_f32<T: ByteOrder>(&mut self) -> Result<f32> { let mut buf = [0; 4]; @@ -446,7 +452,6 @@ pub trait ReadBytesExt: io::Read { /// Reads a IEEE754 double-precision (8 bytes) floating point number from /// the underlying reader. - #[inline] /// /// # Errors /// @@ -459,17 +464,426 @@ pub trait ReadBytesExt: io::Read { /// Read a big-endian double-precision floating point number from a `Read`: /// /// ```rust + /// use std::f64; /// use std::io::Cursor; + /// /// use byteorder::{BigEndian, ReadBytesExt}; - /// use std::f64::consts; /// - /// let mut rdr = Cursor::new(vec![0x40, 0x09, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18]); - /// assert_eq!(consts::PI, rdr.read_f64::<BigEndian>().unwrap()); + /// let mut rdr = Cursor::new(vec![ + /// 0x40, 0x09, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18, + /// ]); + /// assert_eq!(f64::consts::PI, rdr.read_f64::<BigEndian>().unwrap()); + /// ``` + #[inline] fn read_f64<T: ByteOrder>(&mut self) -> Result<f64> { let mut buf = [0; 8]; try!(self.read_exact(&mut buf)); Ok(T::read_f64(&buf)) } + + /// Reads a sequence of unsigned 16 bit integers from the underlying + /// reader. + /// + /// The given buffer is either filled completely or an error is returned. + /// If an error is returned, the contents of `dst` are unspecified. + /// + /// # 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 a sequence of unsigned 16 bit big-endian integers from a `Read`: + /// + /// ```rust + /// use std::io::Cursor; + /// use byteorder::{BigEndian, ReadBytesExt}; + /// + /// let mut rdr = Cursor::new(vec![2, 5, 3, 0]); + /// let mut dst = [0; 2]; + /// rdr.read_u16_into::<BigEndian>(&mut dst).unwrap(); + /// assert_eq!([517, 768], dst); + /// ``` + #[inline] + fn read_u16_into<T: ByteOrder>(&mut self, dst: &mut [u16]) -> Result<()> { + { + let mut buf = unsafe { slice_to_u8_mut(dst) }; + try!(self.read_exact(buf)); + } + T::from_slice_u16(dst); + Ok(()) + } + + /// Reads a sequence of unsigned 32 bit integers from the underlying + /// reader. + /// + /// The given buffer is either filled completely or an error is returned. + /// If an error is returned, the contents of `dst` are unspecified. + /// + /// # 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 a sequence of unsigned 32 bit big-endian integers from a `Read`: + /// + /// ```rust + /// use std::io::Cursor; + /// use byteorder::{BigEndian, ReadBytesExt}; + /// + /// let mut rdr = Cursor::new(vec![0, 0, 2, 5, 0, 0, 3, 0]); + /// let mut dst = [0; 2]; + /// rdr.read_u32_into::<BigEndian>(&mut dst).unwrap(); + /// assert_eq!([517, 768], dst); + /// ``` + #[inline] + fn read_u32_into<T: ByteOrder>(&mut self, dst: &mut [u32]) -> Result<()> { + { + let mut buf = unsafe { slice_to_u8_mut(dst) }; + try!(self.read_exact(buf)); + } + T::from_slice_u32(dst); + Ok(()) + } + + /// Reads a sequence of unsigned 64 bit integers from the underlying + /// reader. + /// + /// The given buffer is either filled completely or an error is returned. + /// If an error is returned, the contents of `dst` are unspecified. + /// + /// # 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 a sequence of unsigned 64 bit big-endian integers from a `Read`: + /// + /// ```rust + /// use std::io::Cursor; + /// use byteorder::{BigEndian, ReadBytesExt}; + /// + /// let mut rdr = Cursor::new(vec![ + /// 0, 0, 0, 0, 0, 0, 2, 5, + /// 0, 0, 0, 0, 0, 0, 3, 0, + /// ]); + /// let mut dst = [0; 2]; + /// rdr.read_u64_into::<BigEndian>(&mut dst).unwrap(); + /// assert_eq!([517, 768], dst); + /// ``` + #[inline] + fn read_u64_into<T: ByteOrder>(&mut self, dst: &mut [u64]) -> Result<()> { + { + let mut buf = unsafe { slice_to_u8_mut(dst) }; + try!(self.read_exact(buf)); + } + T::from_slice_u64(dst); + Ok(()) + } + + /// Reads a sequence of unsigned 128 bit integers from the underlying + /// reader. + /// + /// The given buffer is either filled completely or an error is returned. + /// If an error is returned, the contents of `dst` are unspecified. + /// + /// # 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 a sequence of unsigned 128 bit big-endian integers from a `Read`: + /// + /// ```rust + /// use std::io::Cursor; + /// use byteorder::{BigEndian, ReadBytesExt}; + /// + /// let mut rdr = Cursor::new(vec![ + /// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 5, + /// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, + /// ]); + /// let mut dst = [0; 2]; + /// rdr.read_u128_into::<BigEndian>(&mut dst).unwrap(); + /// assert_eq!([517, 768], dst); + /// ``` + #[cfg(feature = "i128")] + #[inline] + fn read_u128_into<T: ByteOrder>( + &mut self, + dst: &mut [u128], + ) -> Result<()> { + { + let mut buf = unsafe { slice_to_u8_mut(dst) }; + try!(self.read_exact(buf)); + } + T::from_slice_u128(dst); + Ok(()) + } + + /// Reads a sequence of signed 16 bit integers from the underlying + /// reader. + /// + /// The given buffer is either filled completely or an error is returned. + /// If an error is returned, the contents of `dst` are unspecified. + /// + /// # 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 a sequence of signed 16 bit big-endian integers from a `Read`: + /// + /// ```rust + /// use std::io::Cursor; + /// use byteorder::{BigEndian, ReadBytesExt}; + /// + /// let mut rdr = Cursor::new(vec![2, 5, 3, 0]); + /// let mut dst = [0; 2]; + /// rdr.read_i16_into::<BigEndian>(&mut dst).unwrap(); + /// assert_eq!([517, 768], dst); + /// ``` + #[inline] + fn read_i16_into<T: ByteOrder>(&mut self, dst: &mut [i16]) -> Result<()> { + { + let mut buf = unsafe { slice_to_u8_mut(dst) }; + try!(self.read_exact(buf)); + } + T::from_slice_i16(dst); + Ok(()) + } + + /// Reads a sequence of signed 32 bit integers from the underlying + /// reader. + /// + /// The given buffer is either filled completely or an error is returned. + /// If an error is returned, the contents of `dst` are unspecified. + /// + /// # 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 a sequence of signed 32 bit big-endian integers from a `Read`: + /// + /// ```rust + /// use std::io::Cursor; + /// use byteorder::{BigEndian, ReadBytesExt}; + /// + /// let mut rdr = Cursor::new(vec![0, 0, 2, 5, 0, 0, 3, 0]); + /// let mut dst = [0; 2]; + /// rdr.read_i32_into::<BigEndian>(&mut dst).unwrap(); + /// assert_eq!([517, 768], dst); + /// ``` + #[inline] + fn read_i32_into<T: ByteOrder>(&mut self, dst: &mut [i32]) -> Result<()> { + { + let mut buf = unsafe { slice_to_u8_mut(dst) }; + try!(self.read_exact(buf)); + } + T::from_slice_i32(dst); + Ok(()) + } + + /// Reads a sequence of signed 64 bit integers from the underlying + /// reader. + /// + /// The given buffer is either filled completely or an error is returned. + /// If an error is returned, the contents of `dst` are unspecified. + /// + /// # 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 a sequence of signed 64 bit big-endian integers from a `Read`: + /// + /// ```rust + /// use std::io::Cursor; + /// use byteorder::{BigEndian, ReadBytesExt}; + /// + /// let mut rdr = Cursor::new(vec![ + /// 0, 0, 0, 0, 0, 0, 2, 5, + /// 0, 0, 0, 0, 0, 0, 3, 0, + /// ]); + /// let mut dst = [0; 2]; + /// rdr.read_i64_into::<BigEndian>(&mut dst).unwrap(); + /// assert_eq!([517, 768], dst); + /// ``` + #[inline] + fn read_i64_into<T: ByteOrder>(&mut self, dst: &mut [i64]) -> Result<()> { + { + let mut buf = unsafe { slice_to_u8_mut(dst) }; + try!(self.read_exact(buf)); + } + T::from_slice_i64(dst); + Ok(()) + } + + /// Reads a sequence of signed 128 bit integers from the underlying + /// reader. + /// + /// The given buffer is either filled completely or an error is returned. + /// If an error is returned, the contents of `dst` are unspecified. + /// + /// # 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 a sequence of signed 128 bit big-endian integers from a `Read`: + /// + /// ```rust + /// use std::io::Cursor; + /// use byteorder::{BigEndian, ReadBytesExt}; + /// + /// let mut rdr = Cursor::new(vec![ + /// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 5, + /// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, + /// ]); + /// let mut dst = [0; 2]; + /// rdr.read_i128_into::<BigEndian>(&mut dst).unwrap(); + /// assert_eq!([517, 768], dst); + /// ``` + #[cfg(feature = "i128")] + #[inline] + fn read_i128_into<T: ByteOrder>( + &mut self, + dst: &mut [i128], + ) -> Result<()> { + { + let mut buf = unsafe { slice_to_u8_mut(dst) }; + try!(self.read_exact(buf)); + } + T::from_slice_i128(dst); + Ok(()) + } + + /// Reads a sequence of IEEE754 single-precision (4 bytes) floating + /// point numbers from the underlying reader. + /// + /// The given buffer is either filled completely or an error is returned. + /// If an error is returned, the contents of `dst` are unspecified. + /// + /// # Safety + /// + /// This method is unsafe because there are no guarantees made about the + /// floating point values. In particular, this method does not check for + /// signaling NaNs, which may result in undefined behavior. + /// + /// # 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 a sequence of big-endian single-precision floating point number + /// from a `Read`: + /// + /// ```rust + /// use std::f32; + /// use std::io::Cursor; + /// + /// use byteorder::{BigEndian, ReadBytesExt}; + /// + /// let mut rdr = Cursor::new(vec![ + /// 0x40, 0x49, 0x0f, 0xdb, + /// 0x3f, 0x80, 0x00, 0x00, + /// ]); + /// let mut dst = [0.0; 2]; + /// unsafe { + /// rdr.read_f32_into_unchecked::<BigEndian>(&mut dst).unwrap(); + /// } + /// assert_eq!([f32::consts::PI, 1.0], dst); + /// ``` + #[inline] + unsafe fn read_f32_into_unchecked<T: ByteOrder>( + &mut self, + dst: &mut [f32], + ) -> Result<()> { + { + let mut buf = slice_to_u8_mut(dst); + try!(self.read_exact(buf)); + } + T::from_slice_f32(dst); + Ok(()) + } + + /// Reads a sequence of IEEE754 double-precision (8 bytes) floating + /// point numbers from the underlying reader. + /// + /// The given buffer is either filled completely or an error is returned. + /// If an error is returned, the contents of `dst` are unspecified. + /// + /// # Safety + /// + /// This method is unsafe because there are no guarantees made about the + /// floating point values. In particular, this method does not check for + /// signaling NaNs, which may result in undefined behavior. + /// + /// # 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 a sequence of big-endian single-precision floating point number + /// from a `Read`: + /// + /// ```rust + /// use std::f64; + /// use std::io::Cursor; + /// + /// use byteorder::{BigEndian, ReadBytesExt}; + /// + /// let mut rdr = Cursor::new(vec![ + /// 0x40, 0x09, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18, + /// 0x3f, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + /// ]); + /// let mut dst = [0.0; 2]; + /// unsafe { + /// rdr.read_f64_into_unchecked::<BigEndian>(&mut dst).unwrap(); + /// } + /// assert_eq!([f64::consts::PI, 1.0], dst); + /// ``` + #[inline] + unsafe fn read_f64_into_unchecked<T: ByteOrder>( + &mut self, + dst: &mut [f64], + ) -> Result<()> { + { + let mut buf = slice_to_u8_mut(dst); + try!(self.read_exact(buf)); + } + T::from_slice_f64(dst); + Ok(()) + } } /// All types that implement `Read` get methods defined in `ReadBytesExt` @@ -761,3 +1175,15 @@ pub trait WriteBytesExt: io::Write { /// All types that implement `Write` get methods defined in `WriteBytesExt` /// for free. impl<W: io::Write + ?Sized> WriteBytesExt for W {} + +/// Convert a slice of T (where T is plain old data) to its mutable binary +/// representation. +/// +/// This function is wildly unsafe because it permits arbitrary modification of +/// the binary representation of any `Copy` type. Use with care. +unsafe fn slice_to_u8_mut<T: Copy>(slice: &mut [T]) -> &mut [u8] { + use std::mem::size_of; + + let len = size_of::<T>() * slice.len(); + slice::from_raw_parts_mut(slice.as_mut_ptr() as *mut u8, len) +} @@ -1382,6 +1382,230 @@ pub trait ByteOrder fn write_f64_into(src: &[f64], dst: &mut [u8]) { Self::write_u64_into(unsafe { transmute(src) }, dst); } + + /// Converts the given slice of unsigned 16 bit integers to a particular + /// endianness. + /// + /// If the endianness matches the endianness of the host platform, then + /// this is a no-op. + /// + /// # Examples + /// + /// Convert the host platform's endianness to big-endian: + /// + /// ```rust + /// use byteorder::{ByteOrder, BigEndian}; + /// + /// let mut numbers = [5, 65000]; + /// BigEndian::from_slice_u16(&mut numbers); + /// if cfg!(target_endian = "little") { + /// assert_eq!(numbers, [5u16.swap_bytes(), 65000u16.swap_bytes()]); + /// } else { + /// assert_eq!(numbers, [5, 65000]); + /// } + /// ``` + fn from_slice_u16(numbers: &mut [u16]); + + /// Converts the given slice of unsigned 32 bit integers to a particular + /// endianness. + /// + /// If the endianness matches the endianness of the host platform, then + /// this is a no-op. + /// + /// # Examples + /// + /// Convert the host platform's endianness to big-endian: + /// + /// ```rust + /// use byteorder::{ByteOrder, BigEndian}; + /// + /// let mut numbers = [5, 65000]; + /// BigEndian::from_slice_u32(&mut numbers); + /// if cfg!(target_endian = "little") { + /// assert_eq!(numbers, [5u32.swap_bytes(), 65000u32.swap_bytes()]); + /// } else { + /// assert_eq!(numbers, [5, 65000]); + /// } + /// ``` + fn from_slice_u32(numbers: &mut [u32]); + + /// Converts the given slice of unsigned 64 bit integers to a particular + /// endianness. + /// + /// If the endianness matches the endianness of the host platform, then + /// this is a no-op. + /// + /// # Examples + /// + /// Convert the host platform's endianness to big-endian: + /// + /// ```rust + /// use byteorder::{ByteOrder, BigEndian}; + /// + /// let mut numbers = [5, 65000]; + /// BigEndian::from_slice_u64(&mut numbers); + /// if cfg!(target_endian = "little") { + /// assert_eq!(numbers, [5u64.swap_bytes(), 65000u64.swap_bytes()]); + /// } else { + /// assert_eq!(numbers, [5, 65000]); + /// } + /// ``` + fn from_slice_u64(numbers: &mut [u64]); + + /// Converts the given slice of unsigned 128 bit integers to a particular + /// endianness. + /// + /// If the endianness matches the endianness of the host platform, then + /// this is a no-op. + /// + /// # Examples + /// + /// Convert the host platform's endianness to big-endian: + /// + /// ```rust + /// #![feature(i128_type)] + /// + /// use byteorder::{ByteOrder, BigEndian}; + /// + /// let mut numbers = [5, 65000]; + /// BigEndian::from_slice_u128(&mut numbers); + /// if cfg!(target_endian = "little") { + /// assert_eq!(numbers, [5u128.swap_bytes(), 65000u128.swap_bytes()]); + /// } else { + /// assert_eq!(numbers, [5, 65000]); + /// } + /// ``` + #[cfg(feature = "i128")] + fn from_slice_u128(numbers: &mut [u128]); + + /// Converts the given slice of signed 16 bit integers to a particular + /// endianness. + /// + /// If the endianness matches the endianness of the host platform, then + /// this is a no-op. + /// + /// # Examples + /// + /// Convert the host platform's endianness to big-endian: + /// + /// ```rust + /// use byteorder::{ByteOrder, BigEndian}; + /// + /// let mut numbers = [5, 65000]; + /// BigEndian::from_slice_i16(&mut numbers); + /// if cfg!(target_endian = "little") { + /// assert_eq!(numbers, [5i16.swap_bytes(), 65000i16.swap_bytes()]); + /// } else { + /// assert_eq!(numbers, [5, 65000]); + /// } + /// ``` + #[inline] + fn from_slice_i16(numbers: &mut [i16]) { + Self::from_slice_u16(unsafe { transmute(numbers) }); + } + + /// Converts the given slice of signed 32 bit integers to a particular + /// endianness. + /// + /// If the endianness matches the endianness of the host platform, then + /// this is a no-op. + /// + /// # Examples + /// + /// Convert the host platform's endianness to big-endian: + /// + /// ```rust + /// use byteorder::{ByteOrder, BigEndian}; + /// + /// let mut numbers = [5, 65000]; + /// BigEndian::from_slice_i32(&mut numbers); + /// if cfg!(target_endian = "little") { + /// assert_eq!(numbers, [5i32.swap_bytes(), 65000i32.swap_bytes()]); + /// } else { + /// assert_eq!(numbers, [5, 65000]); + /// } + /// ``` + #[inline] + fn from_slice_i32(numbers: &mut [i32]) { + Self::from_slice_u32(unsafe { transmute(numbers) }); + } + + /// Converts the given slice of signed 64 bit integers to a particular + /// endianness. + /// + /// If the endianness matches the endianness of the host platform, then + /// this is a no-op. + /// + /// # Examples + /// + /// Convert the host platform's endianness to big-endian: + /// + /// ```rust + /// use byteorder::{ByteOrder, BigEndian}; + /// + /// let mut numbers = [5, 65000]; + /// BigEndian::from_slice_i64(&mut numbers); + /// if cfg!(target_endian = "little") { + /// assert_eq!(numbers, [5i64.swap_bytes(), 65000i64.swap_bytes()]); + /// } else { + /// assert_eq!(numbers, [5, 65000]); + /// } + /// ``` + #[inline] + fn from_slice_i64(numbers: &mut [i64]) { + Self::from_slice_u64(unsafe { transmute(numbers) }); + } + + /// Converts the given slice of signed 128 bit integers to a particular + /// endianness. + /// + /// If the endianness matches the endianness of the host platform, then + /// this is a no-op. + /// + /// # Examples + /// + /// Convert the host platform's endianness to big-endian: + /// + /// ```rust + /// #![feature(i128_type)] + /// + /// use byteorder::{ByteOrder, BigEndian}; + /// + /// let mut numbers = [5, 65000]; + /// BigEndian::from_slice_i128(&mut numbers); + /// if cfg!(target_endian = "little") { + /// assert_eq!(numbers, [5i128.swap_bytes(), 65000i128.swap_bytes()]); + /// } else { + /// assert_eq!(numbers, [5, 65000]); + /// } + /// ``` + #[cfg(feature = "i128")] + #[inline] + fn from_slice_i128(numbers: &mut [i128]) { + Self::from_slice_u128(unsafe { transmute(numbers) }); + } + + /// Converts the given slice of IEEE754 single-precision (4 bytes) floating + /// point numbers to a particular endianness. + /// + /// If the endianness matches the endianness of the host platform, then + /// this is a no-op. + /// + /// Note that the results of this operation are guaranteed to be defined. + /// In particular, this method may replace signaling NaN values with + /// quiet NaN values. + fn from_slice_f32(numbers: &mut [f32]); + + /// Converts the given slice of IEEE754 double-precision (8 bytes) floating + /// point numbers to a particular endianness. + /// + /// If the endianness matches the endianness of the host platform, then + /// this is a no-op. + /// + /// Note that the results of this operation are guaranteed to be defined. + /// In particular, this method may replace signaling NaN values with + /// quiet NaN values. + fn from_slice_f64(numbers: &mut [f64]); } /// Defines big-endian serialization. @@ -1697,6 +1921,63 @@ impl ByteOrder for BigEndian { write_slice!(src, dst, u128, 16, Self::write_u128); } } + + #[inline] + fn from_slice_u16(numbers: &mut [u16]) { + if cfg!(target_endian = "little") { + for n in numbers { + *n = n.to_be(); + } + } + } + + #[inline] + fn from_slice_u32(numbers: &mut [u32]) { + if cfg!(target_endian = "little") { + for n in numbers { + *n = n.to_be(); + } + } + } + + #[inline] + fn from_slice_u64(numbers: &mut [u64]) { + if cfg!(target_endian = "little") { + for n in numbers { + *n = n.to_be(); + } + } + } + + #[cfg(feature = "i128")] + #[inline] + fn from_slice_u128(numbers: &mut [u128]) { + if cfg!(target_endian = "little") { + for n in numbers { + *n = n.to_be(); + } + } + } + + #[inline] + fn from_slice_f32(numbers: &mut [f32]) { + if cfg!(target_endian = "little") { + for n in numbers { + let int: u32 = unsafe { transmute(*n) }; + *n = safe_u32_bits_to_f32(int.to_be()); + } + } + } + + #[inline] + fn from_slice_f64(numbers: &mut [f64]) { + if cfg!(target_endian = "little") { + for n in numbers { + let int: u64 = unsafe { transmute(*n) }; + *n = safe_u64_bits_to_f64(int.to_be()); + } + } + } } impl ByteOrder for LittleEndian { @@ -1843,6 +2124,63 @@ impl ByteOrder for LittleEndian { write_slice!(src, dst, u128, 16, Self::write_u128); } } + + #[inline] + fn from_slice_u16(numbers: &mut [u16]) { + if cfg!(target_endian = "big") { + for n in numbers { + *n = n.to_le(); + } + } + } + + #[inline] + fn from_slice_u32(numbers: &mut [u32]) { + if cfg!(target_endian = "big") { + for n in numbers { + *n = n.to_le(); + } + } + } + + #[inline] + fn from_slice_u64(numbers: &mut [u64]) { + if cfg!(target_endian = "big") { + for n in numbers { + *n = n.to_le(); + } + } + } + + #[cfg(feature = "i128")] + #[inline] + fn from_slice_u128(numbers: &mut [u128]) { + if cfg!(target_endian = "big") { + for n in numbers { + *n = n.to_le(); + } + } + } + + #[inline] + fn from_slice_f32(numbers: &mut [f32]) { + if cfg!(target_endian = "big") { + for n in numbers { + let int: u32 = unsafe { transmute(*n) }; + *n = safe_u32_bits_to_f32(int.to_le()); + } + } + } + + #[inline] + fn from_slice_f64(numbers: &mut [f64]) { + if cfg!(target_endian = "big") { + for n in numbers { + let int: u64 = unsafe { transmute(*n) }; + *n = safe_u64_bits_to_f64(int.to_le()); + } + } + } } #[inline] |