Skip to content

Commit bf17ec7

Browse files
committed
Replace some unsafe with zerocopy
1 parent 5a82625 commit bf17ec7

File tree

3 files changed

+64
-149
lines changed

3 files changed

+64
-149
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ rust-version = "1.60"
1717
name = "byteorder"
1818
bench = false
1919

20+
[dependencies]
21+
zerocopy = "0.8.26"
22+
2023
[dev-dependencies]
2124
quickcheck = { version = "0.9.2", default-features = false }
2225
rand = "0.7"

src/io.rs

Lines changed: 14 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
use std::{
2-
io::{self, Result},
3-
slice,
4-
};
1+
use std::io::{self, Result};
2+
3+
use zerocopy::IntoBytes;
54

65
use crate::ByteOrder;
76

@@ -560,10 +559,7 @@ pub trait ReadBytesExt: io::Read {
560559
/// ```
561560
#[inline]
562561
fn read_u16_into<T: ByteOrder>(&mut self, dst: &mut [u16]) -> Result<()> {
563-
{
564-
let buf = unsafe { slice_to_u8_mut(dst) };
565-
self.read_exact(buf)?;
566-
}
562+
self.read_exact(dst.as_mut_bytes())?;
567563
T::from_slice_u16(dst);
568564
Ok(())
569565
}
@@ -595,10 +591,7 @@ pub trait ReadBytesExt: io::Read {
595591
/// ```
596592
#[inline]
597593
fn read_u32_into<T: ByteOrder>(&mut self, dst: &mut [u32]) -> Result<()> {
598-
{
599-
let buf = unsafe { slice_to_u8_mut(dst) };
600-
self.read_exact(buf)?;
601-
}
594+
self.read_exact(dst.as_mut_bytes())?;
602595
T::from_slice_u32(dst);
603596
Ok(())
604597
}
@@ -633,10 +626,7 @@ pub trait ReadBytesExt: io::Read {
633626
/// ```
634627
#[inline]
635628
fn read_u64_into<T: ByteOrder>(&mut self, dst: &mut [u64]) -> Result<()> {
636-
{
637-
let buf = unsafe { slice_to_u8_mut(dst) };
638-
self.read_exact(buf)?;
639-
}
629+
self.read_exact(dst.as_mut_bytes())?;
640630
T::from_slice_u64(dst);
641631
Ok(())
642632
}
@@ -674,10 +664,7 @@ pub trait ReadBytesExt: io::Read {
674664
&mut self,
675665
dst: &mut [u128],
676666
) -> Result<()> {
677-
{
678-
let buf = unsafe { slice_to_u8_mut(dst) };
679-
self.read_exact(buf)?;
680-
}
667+
self.read_exact(dst.as_mut_bytes())?;
681668
T::from_slice_u128(dst);
682669
Ok(())
683670
}
@@ -714,8 +701,7 @@ pub trait ReadBytesExt: io::Read {
714701
/// ```
715702
#[inline]
716703
fn read_i8_into(&mut self, dst: &mut [i8]) -> Result<()> {
717-
let buf = unsafe { slice_to_u8_mut(dst) };
718-
self.read_exact(buf)
704+
self.read_exact(dst.as_mut_bytes())
719705
}
720706

721707
/// Reads a sequence of signed 16 bit integers from the underlying
@@ -745,10 +731,7 @@ pub trait ReadBytesExt: io::Read {
745731
/// ```
746732
#[inline]
747733
fn read_i16_into<T: ByteOrder>(&mut self, dst: &mut [i16]) -> Result<()> {
748-
{
749-
let buf = unsafe { slice_to_u8_mut(dst) };
750-
self.read_exact(buf)?;
751-
}
734+
self.read_exact(dst.as_mut_bytes())?;
752735
T::from_slice_i16(dst);
753736
Ok(())
754737
}
@@ -780,10 +763,7 @@ pub trait ReadBytesExt: io::Read {
780763
/// ```
781764
#[inline]
782765
fn read_i32_into<T: ByteOrder>(&mut self, dst: &mut [i32]) -> Result<()> {
783-
{
784-
let buf = unsafe { slice_to_u8_mut(dst) };
785-
self.read_exact(buf)?;
786-
}
766+
self.read_exact(dst.as_mut_bytes())?;
787767
T::from_slice_i32(dst);
788768
Ok(())
789769
}
@@ -818,10 +798,7 @@ pub trait ReadBytesExt: io::Read {
818798
/// ```
819799
#[inline]
820800
fn read_i64_into<T: ByteOrder>(&mut self, dst: &mut [i64]) -> Result<()> {
821-
{
822-
let buf = unsafe { slice_to_u8_mut(dst) };
823-
self.read_exact(buf)?;
824-
}
801+
self.read_exact(dst.as_mut_bytes())?;
825802
T::from_slice_i64(dst);
826803
Ok(())
827804
}
@@ -859,10 +836,7 @@ pub trait ReadBytesExt: io::Read {
859836
&mut self,
860837
dst: &mut [i128],
861838
) -> Result<()> {
862-
{
863-
let buf = unsafe { slice_to_u8_mut(dst) };
864-
self.read_exact(buf)?;
865-
}
839+
self.read_exact(dst.as_mut_bytes())?;
866840
T::from_slice_i128(dst);
867841
Ok(())
868842
}
@@ -900,10 +874,7 @@ pub trait ReadBytesExt: io::Read {
900874
/// ```
901875
#[inline]
902876
fn read_f32_into<T: ByteOrder>(&mut self, dst: &mut [f32]) -> Result<()> {
903-
{
904-
let buf = unsafe { slice_to_u8_mut(dst) };
905-
self.read_exact(buf)?;
906-
}
877+
self.read_exact(dst.as_mut_bytes())?;
907878
T::from_slice_f32(dst);
908879
Ok(())
909880
}
@@ -985,10 +956,7 @@ pub trait ReadBytesExt: io::Read {
985956
/// ```
986957
#[inline]
987958
fn read_f64_into<T: ByteOrder>(&mut self, dst: &mut [f64]) -> Result<()> {
988-
{
989-
let buf = unsafe { slice_to_u8_mut(dst) };
990-
self.read_exact(buf)?;
991-
}
959+
self.read_exact(dst.as_mut_bytes())?;
992960
T::from_slice_f64(dst);
993961
Ok(())
994962
}
@@ -1577,16 +1545,3 @@ pub trait WriteBytesExt: io::Write {
15771545
/// All types that implement `Write` get methods defined in `WriteBytesExt`
15781546
/// for free.
15791547
impl<W: io::Write + ?Sized> WriteBytesExt for W {}
1580-
1581-
/// Convert a slice of T (where T is plain old data) to its mutable binary
1582-
/// representation.
1583-
///
1584-
/// This function is wildly unsafe because it permits arbitrary modification of
1585-
/// the binary representation of any `Copy` type. Use with care. It's intended
1586-
/// to be called only where `T` is a numeric type.
1587-
unsafe fn slice_to_u8_mut<T: Copy>(slice: &mut [T]) -> &mut [u8] {
1588-
use std::mem::size_of;
1589-
1590-
let len = size_of::<T>() * slice.len();
1591-
slice::from_raw_parts_mut(slice.as_mut_ptr() as *mut u8, len)
1592-
}

0 commit comments

Comments
 (0)