@@ -729,9 +729,7 @@ impl IntEncodedWithFixedSize {
729
729
impl serialize:: Encodable < Encoder > for IntEncodedWithFixedSize {
730
730
fn encode ( & self , e : & mut Encoder ) -> EncodeResult {
731
731
let start_pos = e. position ( ) ;
732
- for i in 0 ..IntEncodedWithFixedSize :: ENCODED_SIZE {
733
- ( ( self . 0 >> ( i * 8 ) ) as u8 ) . encode ( e) ?;
734
- }
732
+ e. emit_raw_bytes ( & self . 0 . to_le_bytes ( ) ) ;
735
733
let end_pos = e. position ( ) ;
736
734
assert_eq ! ( ( end_pos - start_pos) , IntEncodedWithFixedSize :: ENCODED_SIZE ) ;
737
735
Ok ( ( ) )
@@ -741,9 +739,7 @@ impl serialize::Encodable<Encoder> for IntEncodedWithFixedSize {
741
739
impl serialize:: Encodable < FileEncoder > for IntEncodedWithFixedSize {
742
740
fn encode ( & self , e : & mut FileEncoder ) -> FileEncodeResult {
743
741
let start_pos = e. position ( ) ;
744
- for i in 0 ..IntEncodedWithFixedSize :: ENCODED_SIZE {
745
- ( ( self . 0 >> ( i * 8 ) ) as u8 ) . encode ( e) ?;
746
- }
742
+ e. emit_raw_bytes ( & self . 0 . to_le_bytes ( ) ) ?;
747
743
let end_pos = e. position ( ) ;
748
744
assert_eq ! ( ( end_pos - start_pos) , IntEncodedWithFixedSize :: ENCODED_SIZE ) ;
749
745
Ok ( ( ) )
@@ -752,17 +748,13 @@ impl serialize::Encodable<FileEncoder> for IntEncodedWithFixedSize {
752
748
753
749
impl < ' a > serialize:: Decodable < Decoder < ' a > > for IntEncodedWithFixedSize {
754
750
fn decode ( decoder : & mut Decoder < ' a > ) -> Result < IntEncodedWithFixedSize , String > {
755
- let mut value : u64 = 0 ;
751
+ let mut bytes = MaybeUninit :: uninit_array ( ) ;
756
752
let start_pos = decoder. position ( ) ;
757
-
758
- for i in 0 ..IntEncodedWithFixedSize :: ENCODED_SIZE {
759
- let byte: u8 = serialize:: Decodable :: decode ( decoder) ?;
760
- value |= ( byte as u64 ) << ( i * 8 ) ;
761
- }
762
-
753
+ decoder. read_raw_bytes ( & mut bytes) ?;
763
754
let end_pos = decoder. position ( ) ;
764
755
assert_eq ! ( ( end_pos - start_pos) , IntEncodedWithFixedSize :: ENCODED_SIZE ) ;
765
756
757
+ let value = u64:: from_le_bytes ( unsafe { MaybeUninit :: array_assume_init ( bytes) } ) ;
766
758
Ok ( IntEncodedWithFixedSize ( value) )
767
759
}
768
760
}
0 commit comments