|
1 | 1 | use crate::leb128::{self, max_leb128_len}; |
2 | | -use crate::serialize::{self, Encoder as _}; |
3 | | -use std::borrow::Cow; |
| 2 | +use crate::serialize::{self, Decoder as _, Encoder as _}; |
4 | 3 | use std::convert::TryInto; |
5 | 4 | use std::fs::File; |
6 | 5 | use std::io::{self, Write}; |
@@ -549,25 +548,13 @@ impl<'a> Decoder<'a> { |
549 | 548 | pub fn advance(&mut self, bytes: usize) { |
550 | 549 | self.position += bytes; |
551 | 550 | } |
552 | | - |
553 | | - #[inline] |
554 | | - pub fn read_raw_bytes(&mut self, bytes: usize) -> &'a [u8] { |
555 | | - let start = self.position; |
556 | | - self.position += bytes; |
557 | | - &self.data[start..self.position] |
558 | | - } |
559 | 551 | } |
560 | 552 |
|
561 | 553 | macro_rules! read_leb128 { |
562 | 554 | ($dec:expr, $fun:ident) => {{ leb128::$fun($dec.data, &mut $dec.position) }}; |
563 | 555 | } |
564 | 556 |
|
565 | 557 | impl<'a> serialize::Decoder for Decoder<'a> { |
566 | | - #[inline] |
567 | | - fn read_unit(&mut self) -> () { |
568 | | - () |
569 | | - } |
570 | | - |
571 | 558 | #[inline] |
572 | 559 | fn read_u128(&mut self) -> u128 { |
573 | 560 | read_leb128!(self, read_u128_leb128) |
@@ -663,22 +650,22 @@ impl<'a> serialize::Decoder for Decoder<'a> { |
663 | 650 | } |
664 | 651 |
|
665 | 652 | #[inline] |
666 | | - fn read_str(&mut self) -> Cow<'_, str> { |
| 653 | + fn read_str(&mut self) -> &'a str { |
667 | 654 | let len = self.read_usize(); |
668 | 655 | let sentinel = self.data[self.position + len]; |
669 | 656 | assert!(sentinel == STR_SENTINEL); |
670 | 657 | let s = unsafe { |
671 | 658 | std::str::from_utf8_unchecked(&self.data[self.position..self.position + len]) |
672 | 659 | }; |
673 | 660 | self.position += len + 1; |
674 | | - Cow::Borrowed(s) |
| 661 | + s |
675 | 662 | } |
676 | 663 |
|
677 | 664 | #[inline] |
678 | | - fn read_raw_bytes_into(&mut self, s: &mut [u8]) { |
| 665 | + fn read_raw_bytes(&mut self, bytes: usize) -> &'a [u8] { |
679 | 666 | let start = self.position; |
680 | | - self.position += s.len(); |
681 | | - s.copy_from_slice(&self.data[start..self.position]); |
| 667 | + self.position += bytes; |
| 668 | + &self.data[start..self.position] |
682 | 669 | } |
683 | 670 | } |
684 | 671 |
|
@@ -746,10 +733,10 @@ impl<'a> serialize::Decodable<Decoder<'a>> for IntEncodedWithFixedSize { |
746 | 733 | fn decode(decoder: &mut Decoder<'a>) -> IntEncodedWithFixedSize { |
747 | 734 | let _start_pos = decoder.position(); |
748 | 735 | let bytes = decoder.read_raw_bytes(IntEncodedWithFixedSize::ENCODED_SIZE); |
| 736 | + let value = u64::from_le_bytes(bytes.try_into().unwrap()); |
749 | 737 | let _end_pos = decoder.position(); |
750 | 738 | debug_assert_eq!((_end_pos - _start_pos), IntEncodedWithFixedSize::ENCODED_SIZE); |
751 | 739 |
|
752 | | - let value = u64::from_le_bytes(bytes.try_into().unwrap()); |
753 | 740 | IntEncodedWithFixedSize(value) |
754 | 741 | } |
755 | 742 | } |
0 commit comments