|
1 | | -use std::{ |
2 | | - io::{self, Result}, |
3 | | - slice, |
4 | | -}; |
| 1 | +use std::io::{self, Result}; |
| 2 | + |
| 3 | +use zerocopy::IntoBytes; |
5 | 4 |
|
6 | 5 | use crate::ByteOrder; |
7 | 6 |
|
@@ -560,10 +559,7 @@ pub trait ReadBytesExt: io::Read { |
560 | 559 | /// ``` |
561 | 560 | #[inline] |
562 | 561 | 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())?; |
567 | 563 | T::from_slice_u16(dst); |
568 | 564 | Ok(()) |
569 | 565 | } |
@@ -595,10 +591,7 @@ pub trait ReadBytesExt: io::Read { |
595 | 591 | /// ``` |
596 | 592 | #[inline] |
597 | 593 | 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())?; |
602 | 595 | T::from_slice_u32(dst); |
603 | 596 | Ok(()) |
604 | 597 | } |
@@ -633,10 +626,7 @@ pub trait ReadBytesExt: io::Read { |
633 | 626 | /// ``` |
634 | 627 | #[inline] |
635 | 628 | 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())?; |
640 | 630 | T::from_slice_u64(dst); |
641 | 631 | Ok(()) |
642 | 632 | } |
@@ -674,10 +664,7 @@ pub trait ReadBytesExt: io::Read { |
674 | 664 | &mut self, |
675 | 665 | dst: &mut [u128], |
676 | 666 | ) -> 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())?; |
681 | 668 | T::from_slice_u128(dst); |
682 | 669 | Ok(()) |
683 | 670 | } |
@@ -714,8 +701,7 @@ pub trait ReadBytesExt: io::Read { |
714 | 701 | /// ``` |
715 | 702 | #[inline] |
716 | 703 | 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()) |
719 | 705 | } |
720 | 706 |
|
721 | 707 | /// Reads a sequence of signed 16 bit integers from the underlying |
@@ -745,10 +731,7 @@ pub trait ReadBytesExt: io::Read { |
745 | 731 | /// ``` |
746 | 732 | #[inline] |
747 | 733 | 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())?; |
752 | 735 | T::from_slice_i16(dst); |
753 | 736 | Ok(()) |
754 | 737 | } |
@@ -780,10 +763,7 @@ pub trait ReadBytesExt: io::Read { |
780 | 763 | /// ``` |
781 | 764 | #[inline] |
782 | 765 | 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())?; |
787 | 767 | T::from_slice_i32(dst); |
788 | 768 | Ok(()) |
789 | 769 | } |
@@ -818,10 +798,7 @@ pub trait ReadBytesExt: io::Read { |
818 | 798 | /// ``` |
819 | 799 | #[inline] |
820 | 800 | 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())?; |
825 | 802 | T::from_slice_i64(dst); |
826 | 803 | Ok(()) |
827 | 804 | } |
@@ -859,10 +836,7 @@ pub trait ReadBytesExt: io::Read { |
859 | 836 | &mut self, |
860 | 837 | dst: &mut [i128], |
861 | 838 | ) -> 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())?; |
866 | 840 | T::from_slice_i128(dst); |
867 | 841 | Ok(()) |
868 | 842 | } |
@@ -900,10 +874,7 @@ pub trait ReadBytesExt: io::Read { |
900 | 874 | /// ``` |
901 | 875 | #[inline] |
902 | 876 | 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())?; |
907 | 878 | T::from_slice_f32(dst); |
908 | 879 | Ok(()) |
909 | 880 | } |
@@ -985,10 +956,7 @@ pub trait ReadBytesExt: io::Read { |
985 | 956 | /// ``` |
986 | 957 | #[inline] |
987 | 958 | 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())?; |
992 | 960 | T::from_slice_f64(dst); |
993 | 961 | Ok(()) |
994 | 962 | } |
@@ -1577,16 +1545,3 @@ pub trait WriteBytesExt: io::Write { |
1577 | 1545 | /// All types that implement `Write` get methods defined in `WriteBytesExt` |
1578 | 1546 | /// for free. |
1579 | 1547 | 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