@@ -20,6 +20,8 @@ Core encoding and decoding interfaces.
2020#![ cfg_attr( test, feature( test) ) ]
2121#![ allow( rustc:: internal) ]
2222
23+ use std:: convert:: TryInto ;
24+
2325pub use self :: serialize:: { Decodable , Decoder , Encodable , Encoder } ;
2426
2527mod collection_impls;
@@ -31,3 +33,80 @@ pub mod leb128;
3133pub mod opaque;
3234
3335pub mod raw;
36+
37+ // An integer that will always encode to 8 bytes.
38+ pub struct IntEncodedWithFixedSize ( pub u64 ) ;
39+
40+ impl IntEncodedWithFixedSize {
41+ pub const ENCODED_SIZE : usize = 8 ;
42+ }
43+
44+ impl Encodable < opaque:: Encoder > for IntEncodedWithFixedSize {
45+ #[ inline]
46+ fn encode ( & self , e : & mut opaque:: Encoder ) -> opaque:: EncodeResult {
47+ let _start_pos = e. position ( ) ;
48+ e. emit_raw_bytes ( & self . 0 . to_le_bytes ( ) ) ?;
49+ let _end_pos = e. position ( ) ;
50+ debug_assert_eq ! ( ( _end_pos - _start_pos) , IntEncodedWithFixedSize :: ENCODED_SIZE ) ;
51+ Ok ( ( ) )
52+ }
53+ }
54+
55+ impl Encodable < opaque:: FileEncoder > for IntEncodedWithFixedSize {
56+ #[ inline]
57+ fn encode ( & self , e : & mut opaque:: FileEncoder ) -> opaque:: FileEncodeResult {
58+ let _start_pos = e. position ( ) ;
59+ e. emit_raw_bytes ( & self . 0 . to_le_bytes ( ) ) ?;
60+ let _end_pos = e. position ( ) ;
61+ debug_assert_eq ! ( ( _end_pos - _start_pos) , IntEncodedWithFixedSize :: ENCODED_SIZE ) ;
62+ Ok ( ( ) )
63+ }
64+ }
65+
66+ impl < ' a > Decodable < opaque:: Decoder < ' a > > for IntEncodedWithFixedSize {
67+ #[ inline]
68+ fn decode ( decoder : & mut opaque:: Decoder < ' a > ) -> IntEncodedWithFixedSize {
69+ let _start_pos = decoder. position ( ) ;
70+ let bytes = decoder. read_raw_bytes ( IntEncodedWithFixedSize :: ENCODED_SIZE ) ;
71+ let value = u64:: from_le_bytes ( bytes. try_into ( ) . unwrap ( ) ) ;
72+ let _end_pos = decoder. position ( ) ;
73+ debug_assert_eq ! ( ( _end_pos - _start_pos) , IntEncodedWithFixedSize :: ENCODED_SIZE ) ;
74+
75+ IntEncodedWithFixedSize ( value)
76+ }
77+ }
78+
79+ impl Encodable < raw:: Encoder > for IntEncodedWithFixedSize {
80+ #[ inline]
81+ fn encode ( & self , e : & mut raw:: Encoder ) -> raw:: EncodeResult {
82+ let _start_pos = e. position ( ) ;
83+ e. emit_raw_bytes ( & self . 0 . to_le_bytes ( ) ) ?;
84+ let _end_pos = e. position ( ) ;
85+ debug_assert_eq ! ( ( _end_pos - _start_pos) , IntEncodedWithFixedSize :: ENCODED_SIZE ) ;
86+ Ok ( ( ) )
87+ }
88+ }
89+
90+ impl Encodable < raw:: FileEncoder > for IntEncodedWithFixedSize {
91+ #[ inline]
92+ fn encode ( & self , e : & mut raw:: FileEncoder ) -> raw:: FileEncodeResult {
93+ let _start_pos = e. position ( ) ;
94+ e. emit_raw_bytes ( & self . 0 . to_le_bytes ( ) ) ?;
95+ let _end_pos = e. position ( ) ;
96+ debug_assert_eq ! ( ( _end_pos - _start_pos) , IntEncodedWithFixedSize :: ENCODED_SIZE ) ;
97+ Ok ( ( ) )
98+ }
99+ }
100+
101+ impl < ' a > Decodable < raw:: Decoder < ' a > > for IntEncodedWithFixedSize {
102+ #[ inline]
103+ fn decode ( decoder : & mut raw:: Decoder < ' a > ) -> IntEncodedWithFixedSize {
104+ let _start_pos = decoder. position ( ) ;
105+ let bytes = decoder. read_raw_bytes ( IntEncodedWithFixedSize :: ENCODED_SIZE ) ;
106+ let _end_pos = decoder. position ( ) ;
107+ debug_assert_eq ! ( ( _end_pos - _start_pos) , IntEncodedWithFixedSize :: ENCODED_SIZE ) ;
108+
109+ let value = u64:: from_le_bytes ( bytes. try_into ( ) . unwrap ( ) ) ;
110+ IntEncodedWithFixedSize ( value)
111+ }
112+ }
0 commit comments