|
1 | 1 | use crate::leb128::{self, max_leb128_len}; |
2 | | -use crate::serialize::{self, Decoder as _, Encoder as _}; |
| 2 | +use crate::serialize::{self, Encoder as _}; |
3 | 3 | use std::borrow::Cow; |
| 4 | +use std::convert::TryInto; |
4 | 5 | use std::fs::File; |
5 | 6 | use std::io::{self, Write}; |
6 | 7 | use std::mem::MaybeUninit; |
@@ -539,6 +540,13 @@ impl<'a> Decoder<'a> { |
539 | 540 | pub fn advance(&mut self, bytes: usize) { |
540 | 541 | self.position += bytes; |
541 | 542 | } |
| 543 | + |
| 544 | + #[inline] |
| 545 | + pub fn read_raw_bytes(&mut self, bytes: usize) -> &'a [u8] { |
| 546 | + let start = self.position; |
| 547 | + self.position += bytes; |
| 548 | + &self.data[start..self.position] |
| 549 | + } |
542 | 550 | } |
543 | 551 |
|
544 | 552 | macro_rules! read_leb128 { |
@@ -659,22 +667,10 @@ impl<'a> serialize::Decoder for Decoder<'a> { |
659 | 667 | } |
660 | 668 |
|
661 | 669 | #[inline] |
662 | | - fn read_raw_bytes(&mut self, s: &mut [MaybeUninit<u8>]) -> Result<(), String> { |
| 670 | + fn read_raw_bytes_into(&mut self, s: &mut [u8]) -> Result<(), String> { |
663 | 671 | let start = self.position; |
664 | | - let end = start + s.len(); |
665 | | - assert!(end <= self.data.len()); |
666 | | - |
667 | | - // SAFETY: Both `src` and `dst` point to at least `s.len()` elements: |
668 | | - // `src` points to at least `s.len()` elements by above assert, and |
669 | | - // `dst` points to `s.len()` elements by derivation from `s`. |
670 | | - unsafe { |
671 | | - let src = self.data.as_ptr().add(start); |
672 | | - let dst = s.as_mut_ptr() as *mut u8; |
673 | | - ptr::copy_nonoverlapping(src, dst, s.len()); |
674 | | - } |
675 | | - |
676 | | - self.position = end; |
677 | | - |
| 672 | + self.position += s.len(); |
| 673 | + s.copy_from_slice(&self.data[start..self.position]); |
678 | 674 | Ok(()) |
679 | 675 | } |
680 | 676 | } |
@@ -705,16 +701,7 @@ impl serialize::Encodable<FileEncoder> for [u8] { |
705 | 701 | impl<'a> serialize::Decodable<Decoder<'a>> for Vec<u8> { |
706 | 702 | fn decode(d: &mut Decoder<'a>) -> Result<Self, String> { |
707 | 703 | let len = serialize::Decoder::read_usize(d)?; |
708 | | - |
709 | | - let mut v = Vec::with_capacity(len); |
710 | | - let buf = &mut v.spare_capacity_mut()[..len]; |
711 | | - d.read_raw_bytes(buf)?; |
712 | | - |
713 | | - unsafe { |
714 | | - v.set_len(len); |
715 | | - } |
716 | | - |
717 | | - Ok(v) |
| 704 | + Ok(d.read_raw_bytes(len).to_owned()) |
718 | 705 | } |
719 | 706 | } |
720 | 707 |
|
@@ -750,13 +737,12 @@ impl serialize::Encodable<FileEncoder> for IntEncodedWithFixedSize { |
750 | 737 | impl<'a> serialize::Decodable<Decoder<'a>> for IntEncodedWithFixedSize { |
751 | 738 | #[inline] |
752 | 739 | fn decode(decoder: &mut Decoder<'a>) -> Result<IntEncodedWithFixedSize, String> { |
753 | | - let mut bytes = MaybeUninit::uninit_array(); |
754 | 740 | let _start_pos = decoder.position(); |
755 | | - decoder.read_raw_bytes(&mut bytes)?; |
| 741 | + let bytes = decoder.read_raw_bytes(IntEncodedWithFixedSize::ENCODED_SIZE); |
756 | 742 | let _end_pos = decoder.position(); |
757 | 743 | debug_assert_eq!((_end_pos - _start_pos), IntEncodedWithFixedSize::ENCODED_SIZE); |
758 | 744 |
|
759 | | - let value = u64::from_le_bytes(unsafe { MaybeUninit::array_assume_init(bytes) }); |
| 745 | + let value = u64::from_le_bytes(bytes.try_into().unwrap()); |
760 | 746 | Ok(IntEncodedWithFixedSize(value)) |
761 | 747 | } |
762 | 748 | } |
0 commit comments