Skip to content

Commit

Permalink
remove unnecessary unsafe
Browse files Browse the repository at this point in the history
See also #105

Fixes #103
  • Loading branch information
BurntSushi committed Nov 29, 2017
1 parent fde8463 commit 50a028f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 61 deletions.
16 changes: 8 additions & 8 deletions src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ pub trait ReadBytesExt: io::Read {
#[inline]
fn read_u16_into<T: ByteOrder>(&mut self, dst: &mut [u16]) -> Result<()> {
{
let mut buf = unsafe { slice_to_u8_mut(dst) };
let buf = unsafe { slice_to_u8_mut(dst) };
try!(self.read_exact(buf));
}
T::from_slice_u16(dst);
Expand Down Expand Up @@ -544,7 +544,7 @@ pub trait ReadBytesExt: io::Read {
#[inline]
fn read_u32_into<T: ByteOrder>(&mut self, dst: &mut [u32]) -> Result<()> {
{
let mut buf = unsafe { slice_to_u8_mut(dst) };
let buf = unsafe { slice_to_u8_mut(dst) };
try!(self.read_exact(buf));
}
T::from_slice_u32(dst);
Expand Down Expand Up @@ -582,7 +582,7 @@ pub trait ReadBytesExt: io::Read {
#[inline]
fn read_u64_into<T: ByteOrder>(&mut self, dst: &mut [u64]) -> Result<()> {
{
let mut buf = unsafe { slice_to_u8_mut(dst) };
let buf = unsafe { slice_to_u8_mut(dst) };
try!(self.read_exact(buf));
}
T::from_slice_u64(dst);
Expand Down Expand Up @@ -659,7 +659,7 @@ pub trait ReadBytesExt: io::Read {
#[inline]
fn read_i16_into<T: ByteOrder>(&mut self, dst: &mut [i16]) -> Result<()> {
{
let mut buf = unsafe { slice_to_u8_mut(dst) };
let buf = unsafe { slice_to_u8_mut(dst) };
try!(self.read_exact(buf));
}
T::from_slice_i16(dst);
Expand Down Expand Up @@ -694,7 +694,7 @@ pub trait ReadBytesExt: io::Read {
#[inline]
fn read_i32_into<T: ByteOrder>(&mut self, dst: &mut [i32]) -> Result<()> {
{
let mut buf = unsafe { slice_to_u8_mut(dst) };
let buf = unsafe { slice_to_u8_mut(dst) };
try!(self.read_exact(buf));
}
T::from_slice_i32(dst);
Expand Down Expand Up @@ -732,7 +732,7 @@ pub trait ReadBytesExt: io::Read {
#[inline]
fn read_i64_into<T: ByteOrder>(&mut self, dst: &mut [i64]) -> Result<()> {
{
let mut buf = unsafe { slice_to_u8_mut(dst) };
let buf = unsafe { slice_to_u8_mut(dst) };
try!(self.read_exact(buf));
}
T::from_slice_i64(dst);
Expand Down Expand Up @@ -826,7 +826,7 @@ pub trait ReadBytesExt: io::Read {
dst: &mut [f32],
) -> Result<()> {
{
let mut buf = slice_to_u8_mut(dst);
let buf = slice_to_u8_mut(dst);
try!(self.read_exact(buf));
}
T::from_slice_f32(dst);
Expand Down Expand Up @@ -878,7 +878,7 @@ pub trait ReadBytesExt: io::Read {
dst: &mut [f64],
) -> Result<()> {
{
let mut buf = slice_to_u8_mut(dst);
let buf = slice_to_u8_mut(dst);
try!(self.read_exact(buf));
}
T::from_slice_f64(dst);
Expand Down
66 changes: 13 additions & 53 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ assert_eq!(wtr, vec![5, 2, 0, 3]);
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(feature = "i128", feature(i128_type))]
#![cfg_attr(all(feature = "i128", test), feature(i128))]
#![doc(html_root_url = "https://docs.rs/byteorder/1.0.0")]
#![doc(html_root_url = "https://docs.rs/byteorder/1.1.0")]

#[cfg(feature = "std")]
extern crate core;
Expand Down Expand Up @@ -611,9 +611,6 @@ pub trait ByteOrder

/// Reads a IEEE754 single-precision (4 bytes) floating point number.
///
/// The return value is always defined; signaling NaN's may be turned into
/// quiet NaN's.
///
/// # Panics
///
/// Panics when `buf.len() < 4`.
Expand All @@ -632,14 +629,11 @@ pub trait ByteOrder
/// ```
#[inline]
fn read_f32(buf: &[u8]) -> f32 {
safe_u32_bits_to_f32(Self::read_u32(buf))
unsafe { transmute(Self::read_u32(buf)) }
}

/// Reads a IEEE754 double-precision (8 bytes) floating point number.
///
/// The return value is always defined; signaling NaN's may be turned into
/// quiet NaN's.
///
/// # Panics
///
/// Panics when `buf.len() < 8`.
Expand All @@ -658,7 +652,7 @@ pub trait ByteOrder
/// ```
#[inline]
fn read_f64(buf: &[u8]) -> f64 {
safe_u64_bits_to_f64(Self::read_u64(buf))
unsafe { transmute(Self::read_u64(buf)) }
}

/// Writes a signed 16 bit integer `n` to `buf`.
Expand Down Expand Up @@ -1066,12 +1060,6 @@ pub trait ByteOrder
/// Reads IEEE754 single-precision (4 bytes) floating point numbers from
/// `src` into `dst`.
///
/// Note that this does not perform any checks on the floating point
/// conversion. In particular, if the `src` data encodes an undefined
/// floating point value for your environment, then the result may be
/// undefined behavior. For example, this function may produce signaling
/// NaN floating point values.
///
/// # Panics
///
/// Panics when `src.len() != 4*dst.len()`.
Expand All @@ -1088,25 +1076,17 @@ pub trait ByteOrder
/// LittleEndian::write_f32_into(&numbers_given, &mut bytes);
///
/// let mut numbers_got = [0.0; 4];
/// unsafe {
/// LittleEndian::read_f32_into_unchecked(&bytes, &mut numbers_got);
/// }
/// LittleEndian::read_f32_into_unchecked(&bytes, &mut numbers_got);
/// assert_eq!(numbers_given, numbers_got);
/// ```
#[inline]
unsafe fn read_f32_into_unchecked(src: &[u8], dst: &mut [f32]) {
Self::read_u32_into(src, transmute(dst));
fn read_f32_into_unchecked(src: &[u8], dst: &mut [f32]) {
Self::read_u32_into(src, unsafe { transmute(dst) });
}

/// Reads IEEE754 single-precision (4 bytes) floating point numbers from
/// `src` into `dst`.
///
/// Note that this does not perform any checks on the floating point
/// conversion. In particular, if the `src` data encodes an undefined
/// floating point value for your environment, then the result may be
/// undefined behavior. For example, this function may produce signaling
/// NaN floating point values.
///
/// # Panics
///
/// Panics when `src.len() != 8*dst.len()`.
Expand All @@ -1123,14 +1103,12 @@ pub trait ByteOrder
/// LittleEndian::write_f64_into(&numbers_given, &mut bytes);
///
/// let mut numbers_got = [0.0; 4];
/// unsafe {
/// LittleEndian::read_f64_into_unchecked(&bytes, &mut numbers_got);
/// }
/// LittleEndian::read_f64_into_unchecked(&bytes, &mut numbers_got);
/// assert_eq!(numbers_given, numbers_got);
/// ```
#[inline]
unsafe fn read_f64_into_unchecked(src: &[u8], dst: &mut [f64]) {
Self::read_u64_into(src, transmute(dst));
fn read_f64_into_unchecked(src: &[u8], dst: &mut [f64]) {
Self::read_u64_into(src, unsafe { transmute(dst) });
}

/// Writes unsigned 16 bit integers from `src` into `dst`.
Expand Down Expand Up @@ -1590,21 +1568,13 @@ pub trait ByteOrder
///
/// 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]);
}

Expand Down Expand Up @@ -1964,7 +1934,7 @@ impl ByteOrder for BigEndian {
if cfg!(target_endian = "little") {
for n in numbers {
let int: u32 = unsafe { transmute(*n) };
*n = safe_u32_bits_to_f32(int.to_be());
*n = unsafe { transmute(int.to_be()) };
}
}
}
Expand All @@ -1974,7 +1944,7 @@ impl ByteOrder for BigEndian {
if cfg!(target_endian = "little") {
for n in numbers {
let int: u64 = unsafe { transmute(*n) };
*n = safe_u64_bits_to_f64(int.to_be());
*n = unsafe { transmute(int.to_be()) };
}
}
}
Expand Down Expand Up @@ -2167,7 +2137,7 @@ impl ByteOrder for LittleEndian {
if cfg!(target_endian = "big") {
for n in numbers {
let int: u32 = unsafe { transmute(*n) };
*n = safe_u32_bits_to_f32(int.to_le());
*n = unsafe { transmute(int.to_le()) };
}
}
}
Expand All @@ -2177,22 +2147,12 @@ impl ByteOrder for LittleEndian {
if cfg!(target_endian = "big") {
for n in numbers {
let int: u64 = unsafe { transmute(*n) };
*n = safe_u64_bits_to_f64(int.to_le());
*n = unsafe { transmute(int.to_le()) };
}
}
}
}

#[inline]
fn safe_u32_bits_to_f32(u: u32) -> f32 {
unsafe { transmute(u) }
}

#[inline]
fn safe_u64_bits_to_f64(u: u64) -> f64 {
unsafe { transmute(u) }
}

#[cfg(test)]
mod test {
extern crate quickcheck;
Expand Down

0 comments on commit 50a028f

Please sign in to comment.