1
1
use crate :: leb128:: { self , max_leb128_len} ;
2
- use crate :: serialize;
2
+ use crate :: serialize:: { self , Decoder as _ , Encoder as _ } ;
3
3
use std:: borrow:: Cow ;
4
4
use std:: fs:: File ;
5
5
use std:: io:: { self , Write } ;
@@ -30,11 +30,6 @@ impl Encoder {
30
30
pub fn position ( & self ) -> usize {
31
31
self . data . len ( )
32
32
}
33
-
34
- #[ inline]
35
- pub fn emit_raw_bytes ( & mut self , s : & [ u8 ] ) {
36
- self . data . extend_from_slice ( s) ;
37
- }
38
33
}
39
34
40
35
macro_rules! write_leb128 {
@@ -154,7 +149,12 @@ impl serialize::Encoder for Encoder {
154
149
#[ inline]
155
150
fn emit_str ( & mut self , v : & str ) -> EncodeResult {
156
151
self . emit_usize ( v. len ( ) ) ?;
157
- self . emit_raw_bytes ( v. as_bytes ( ) ) ;
152
+ self . emit_raw_bytes ( v. as_bytes ( ) )
153
+ }
154
+
155
+ #[ inline]
156
+ fn emit_raw_bytes ( & mut self , s : & [ u8 ] ) -> EncodeResult {
157
+ self . data . extend_from_slice ( s) ;
158
158
Ok ( ( ) )
159
159
}
160
160
}
@@ -208,11 +208,6 @@ impl FileEncoder {
208
208
self . flushed + self . buffered
209
209
}
210
210
211
- #[ inline]
212
- pub fn emit_raw_bytes ( & mut self , s : & [ u8 ] ) -> FileEncodeResult {
213
- self . write_all ( s)
214
- }
215
-
216
211
pub fn flush ( & mut self ) -> FileEncodeResult {
217
212
// This is basically a copy of `BufWriter::flush`. If `BufWriter` ever
218
213
// offers a raw buffer access API, we can use it, and remove this.
@@ -508,6 +503,11 @@ impl serialize::Encoder for FileEncoder {
508
503
self . emit_usize ( v. len ( ) ) ?;
509
504
self . emit_raw_bytes ( v. as_bytes ( ) )
510
505
}
506
+
507
+ #[ inline]
508
+ fn emit_raw_bytes ( & mut self , s : & [ u8 ] ) -> FileEncodeResult {
509
+ self . write_all ( s)
510
+ }
511
511
}
512
512
513
513
// -----------------------------------------------------------------------------
@@ -539,26 +539,6 @@ impl<'a> Decoder<'a> {
539
539
pub fn advance ( & mut self , bytes : usize ) {
540
540
self . position += bytes;
541
541
}
542
-
543
- #[ inline]
544
- pub fn read_raw_bytes ( & mut self , s : & mut [ MaybeUninit < u8 > ] ) -> Result < ( ) , String > {
545
- let start = self . position ;
546
- let end = start + s. len ( ) ;
547
- assert ! ( end <= self . data. len( ) ) ;
548
-
549
- // SAFETY: Both `src` and `dst` point to at least `s.len()` elements:
550
- // `src` points to at least `s.len()` elements by above assert, and
551
- // `dst` points to `s.len()` elements by derivation from `s`.
552
- unsafe {
553
- let src = self . data . as_ptr ( ) . add ( start) ;
554
- let dst = s. as_mut_ptr ( ) as * mut u8 ;
555
- ptr:: copy_nonoverlapping ( src, dst, s. len ( ) ) ;
556
- }
557
-
558
- self . position = end;
559
-
560
- Ok ( ( ) )
561
- }
562
542
}
563
543
564
544
macro_rules! read_leb128 {
@@ -677,6 +657,26 @@ impl<'a> serialize::Decoder for Decoder<'a> {
677
657
fn error ( & mut self , err : & str ) -> Self :: Error {
678
658
err. to_string ( )
679
659
}
660
+
661
+ #[ inline]
662
+ fn read_raw_bytes ( & mut self , s : & mut [ MaybeUninit < u8 > ] ) -> Result < ( ) , String > {
663
+ 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
+
678
+ Ok ( ( ) )
679
+ }
680
680
}
681
681
682
682
// Specializations for contiguous byte sequences follow. The default implementations for slices
@@ -689,8 +689,7 @@ impl<'a> serialize::Decoder for Decoder<'a> {
689
689
impl serialize:: Encodable < Encoder > for [ u8 ] {
690
690
fn encode ( & self , e : & mut Encoder ) -> EncodeResult {
691
691
serialize:: Encoder :: emit_usize ( e, self . len ( ) ) ?;
692
- e. emit_raw_bytes ( self ) ;
693
- Ok ( ( ) )
692
+ e. emit_raw_bytes ( self )
694
693
}
695
694
}
696
695
@@ -727,32 +726,35 @@ impl IntEncodedWithFixedSize {
727
726
}
728
727
729
728
impl serialize:: Encodable < Encoder > for IntEncodedWithFixedSize {
729
+ #[ inline]
730
730
fn encode ( & self , e : & mut Encoder ) -> EncodeResult {
731
- let start_pos = e. position ( ) ;
732
- e. emit_raw_bytes ( & self . 0 . to_le_bytes ( ) ) ;
733
- let end_pos = e. position ( ) ;
734
- assert_eq ! ( ( end_pos - start_pos ) , IntEncodedWithFixedSize :: ENCODED_SIZE ) ;
731
+ let _start_pos = e. position ( ) ;
732
+ e. emit_raw_bytes ( & self . 0 . to_le_bytes ( ) ) ? ;
733
+ let _end_pos = e. position ( ) ;
734
+ debug_assert_eq ! ( ( _end_pos - _start_pos ) , IntEncodedWithFixedSize :: ENCODED_SIZE ) ;
735
735
Ok ( ( ) )
736
736
}
737
737
}
738
738
739
739
impl serialize:: Encodable < FileEncoder > for IntEncodedWithFixedSize {
740
+ #[ inline]
740
741
fn encode ( & self , e : & mut FileEncoder ) -> FileEncodeResult {
741
- let start_pos = e. position ( ) ;
742
+ let _start_pos = e. position ( ) ;
742
743
e. emit_raw_bytes ( & self . 0 . to_le_bytes ( ) ) ?;
743
- let end_pos = e. position ( ) ;
744
- assert_eq ! ( ( end_pos - start_pos ) , IntEncodedWithFixedSize :: ENCODED_SIZE ) ;
744
+ let _end_pos = e. position ( ) ;
745
+ debug_assert_eq ! ( ( _end_pos - _start_pos ) , IntEncodedWithFixedSize :: ENCODED_SIZE ) ;
745
746
Ok ( ( ) )
746
747
}
747
748
}
748
749
749
750
impl < ' a > serialize:: Decodable < Decoder < ' a > > for IntEncodedWithFixedSize {
751
+ #[ inline]
750
752
fn decode ( decoder : & mut Decoder < ' a > ) -> Result < IntEncodedWithFixedSize , String > {
751
753
let mut bytes = MaybeUninit :: uninit_array ( ) ;
752
- let start_pos = decoder. position ( ) ;
754
+ let _start_pos = decoder. position ( ) ;
753
755
decoder. read_raw_bytes ( & mut bytes) ?;
754
- let end_pos = decoder. position ( ) ;
755
- assert_eq ! ( ( end_pos - start_pos ) , IntEncodedWithFixedSize :: ENCODED_SIZE ) ;
756
+ let _end_pos = decoder. position ( ) ;
757
+ debug_assert_eq ! ( ( _end_pos - _start_pos ) , IntEncodedWithFixedSize :: ENCODED_SIZE ) ;
756
758
757
759
let value = u64:: from_le_bytes ( unsafe { MaybeUninit :: array_assume_init ( bytes) } ) ;
758
760
Ok ( IntEncodedWithFixedSize ( value) )
0 commit comments