]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_serialize/src/opaque.rs
Modify the buffer position directly when reading leb128 values.
[rust.git] / compiler / rustc_serialize / src / opaque.rs
1 use crate::leb128::{self, max_leb128_len};
2 use crate::serialize::{self, Encoder as _};
3 use std::borrow::Cow;
4 use std::convert::TryInto;
5 use std::fs::File;
6 use std::io::{self, Write};
7 use std::mem::MaybeUninit;
8 use std::path::Path;
9 use std::ptr;
10
11 // -----------------------------------------------------------------------------
12 // Encoder
13 // -----------------------------------------------------------------------------
14
15 pub type EncodeResult = Result<(), !>;
16
17 pub struct Encoder {
18     pub data: Vec<u8>,
19 }
20
21 impl Encoder {
22     pub fn new(data: Vec<u8>) -> Encoder {
23         Encoder { data }
24     }
25
26     pub fn into_inner(self) -> Vec<u8> {
27         self.data
28     }
29
30     #[inline]
31     pub fn position(&self) -> usize {
32         self.data.len()
33     }
34 }
35
36 macro_rules! write_leb128 {
37     ($enc:expr, $value:expr, $int_ty:ty, $fun:ident) => {{
38         const MAX_ENCODED_LEN: usize = max_leb128_len!($int_ty);
39         let old_len = $enc.data.len();
40
41         if MAX_ENCODED_LEN > $enc.data.capacity() - old_len {
42             $enc.data.reserve(MAX_ENCODED_LEN);
43         }
44
45         // SAFETY: The above check and `reserve` ensures that there is enough
46         // room to write the encoded value to the vector's internal buffer.
47         unsafe {
48             let buf = &mut *($enc.data.as_mut_ptr().add(old_len)
49                 as *mut [MaybeUninit<u8>; MAX_ENCODED_LEN]);
50             let encoded = leb128::$fun(buf, $value);
51             $enc.data.set_len(old_len + encoded.len());
52         }
53
54         Ok(())
55     }};
56 }
57
58 /// A byte that [cannot occur in UTF8 sequences][utf8]. Used to mark the end of a string.
59 /// This way we can skip validation and still be relatively sure that deserialization
60 /// did not desynchronize.
61 ///
62 /// [utf8]: https://en.wikipedia.org/w/index.php?title=UTF-8&oldid=1058865525#Codepage_layout
63 const STR_SENTINEL: u8 = 0xC1;
64
65 impl serialize::Encoder for Encoder {
66     type Error = !;
67
68     #[inline]
69     fn emit_unit(&mut self) -> EncodeResult {
70         Ok(())
71     }
72
73     #[inline]
74     fn emit_usize(&mut self, v: usize) -> EncodeResult {
75         write_leb128!(self, v, usize, write_usize_leb128)
76     }
77
78     #[inline]
79     fn emit_u128(&mut self, v: u128) -> EncodeResult {
80         write_leb128!(self, v, u128, write_u128_leb128)
81     }
82
83     #[inline]
84     fn emit_u64(&mut self, v: u64) -> EncodeResult {
85         write_leb128!(self, v, u64, write_u64_leb128)
86     }
87
88     #[inline]
89     fn emit_u32(&mut self, v: u32) -> EncodeResult {
90         write_leb128!(self, v, u32, write_u32_leb128)
91     }
92
93     #[inline]
94     fn emit_u16(&mut self, v: u16) -> EncodeResult {
95         write_leb128!(self, v, u16, write_u16_leb128)
96     }
97
98     #[inline]
99     fn emit_u8(&mut self, v: u8) -> EncodeResult {
100         self.data.push(v);
101         Ok(())
102     }
103
104     #[inline]
105     fn emit_isize(&mut self, v: isize) -> EncodeResult {
106         write_leb128!(self, v, isize, write_isize_leb128)
107     }
108
109     #[inline]
110     fn emit_i128(&mut self, v: i128) -> EncodeResult {
111         write_leb128!(self, v, i128, write_i128_leb128)
112     }
113
114     #[inline]
115     fn emit_i64(&mut self, v: i64) -> EncodeResult {
116         write_leb128!(self, v, i64, write_i64_leb128)
117     }
118
119     #[inline]
120     fn emit_i32(&mut self, v: i32) -> EncodeResult {
121         write_leb128!(self, v, i32, write_i32_leb128)
122     }
123
124     #[inline]
125     fn emit_i16(&mut self, v: i16) -> EncodeResult {
126         write_leb128!(self, v, i16, write_i16_leb128)
127     }
128
129     #[inline]
130     fn emit_i8(&mut self, v: i8) -> EncodeResult {
131         let as_u8: u8 = unsafe { std::mem::transmute(v) };
132         self.emit_u8(as_u8)
133     }
134
135     #[inline]
136     fn emit_bool(&mut self, v: bool) -> EncodeResult {
137         self.emit_u8(if v { 1 } else { 0 })
138     }
139
140     #[inline]
141     fn emit_f64(&mut self, v: f64) -> EncodeResult {
142         let as_u64: u64 = v.to_bits();
143         self.emit_u64(as_u64)
144     }
145
146     #[inline]
147     fn emit_f32(&mut self, v: f32) -> EncodeResult {
148         let as_u32: u32 = v.to_bits();
149         self.emit_u32(as_u32)
150     }
151
152     #[inline]
153     fn emit_char(&mut self, v: char) -> EncodeResult {
154         self.emit_u32(v as u32)
155     }
156
157     #[inline]
158     fn emit_str(&mut self, v: &str) -> EncodeResult {
159         self.emit_usize(v.len())?;
160         self.emit_raw_bytes(v.as_bytes())?;
161         self.emit_u8(STR_SENTINEL)
162     }
163
164     #[inline]
165     fn emit_raw_bytes(&mut self, s: &[u8]) -> EncodeResult {
166         self.data.extend_from_slice(s);
167         Ok(())
168     }
169 }
170
171 pub type FileEncodeResult = Result<(), io::Error>;
172
173 // `FileEncoder` encodes data to file via fixed-size buffer.
174 //
175 // When encoding large amounts of data to a file, using `FileEncoder` may be
176 // preferred over using `Encoder` to encode to a `Vec`, and then writing the
177 // `Vec` to file, as the latter uses as much memory as there is encoded data,
178 // while the former uses the fixed amount of memory allocated to the buffer.
179 // `FileEncoder` also has the advantage of not needing to reallocate as data
180 // is appended to it, but the disadvantage of requiring more error handling,
181 // which has some runtime overhead.
182 pub struct FileEncoder {
183     // The input buffer. For adequate performance, we need more control over
184     // buffering than `BufWriter` offers. If `BufWriter` ever offers a raw
185     // buffer access API, we can use it, and remove `buf` and `buffered`.
186     buf: Box<[MaybeUninit<u8>]>,
187     buffered: usize,
188     flushed: usize,
189     file: File,
190 }
191
192 impl FileEncoder {
193     pub fn new<P: AsRef<Path>>(path: P) -> io::Result<Self> {
194         const DEFAULT_BUF_SIZE: usize = 8192;
195         FileEncoder::with_capacity(path, DEFAULT_BUF_SIZE)
196     }
197
198     pub fn with_capacity<P: AsRef<Path>>(path: P, capacity: usize) -> io::Result<Self> {
199         // Require capacity at least as large as the largest LEB128 encoding
200         // here, so that we don't have to check or handle this on every write.
201         assert!(capacity >= max_leb128_len());
202
203         // Require capacity small enough such that some capacity checks can be
204         // done using guaranteed non-overflowing add rather than sub, which
205         // shaves an instruction off those code paths (on x86 at least).
206         assert!(capacity <= usize::MAX - max_leb128_len());
207
208         let file = File::create(path)?;
209
210         Ok(FileEncoder { buf: Box::new_uninit_slice(capacity), buffered: 0, flushed: 0, file })
211     }
212
213     #[inline]
214     pub fn position(&self) -> usize {
215         // Tracking position this way instead of having a `self.position` field
216         // means that we don't have to update the position on every write call.
217         self.flushed + self.buffered
218     }
219
220     pub fn flush(&mut self) -> FileEncodeResult {
221         // This is basically a copy of `BufWriter::flush`. If `BufWriter` ever
222         // offers a raw buffer access API, we can use it, and remove this.
223
224         /// Helper struct to ensure the buffer is updated after all the writes
225         /// are complete. It tracks the number of written bytes and drains them
226         /// all from the front of the buffer when dropped.
227         struct BufGuard<'a> {
228             buffer: &'a mut [u8],
229             encoder_buffered: &'a mut usize,
230             encoder_flushed: &'a mut usize,
231             flushed: usize,
232         }
233
234         impl<'a> BufGuard<'a> {
235             fn new(
236                 buffer: &'a mut [u8],
237                 encoder_buffered: &'a mut usize,
238                 encoder_flushed: &'a mut usize,
239             ) -> Self {
240                 assert_eq!(buffer.len(), *encoder_buffered);
241                 Self { buffer, encoder_buffered, encoder_flushed, flushed: 0 }
242             }
243
244             /// The unwritten part of the buffer
245             fn remaining(&self) -> &[u8] {
246                 &self.buffer[self.flushed..]
247             }
248
249             /// Flag some bytes as removed from the front of the buffer
250             fn consume(&mut self, amt: usize) {
251                 self.flushed += amt;
252             }
253
254             /// true if all of the bytes have been written
255             fn done(&self) -> bool {
256                 self.flushed >= *self.encoder_buffered
257             }
258         }
259
260         impl Drop for BufGuard<'_> {
261             fn drop(&mut self) {
262                 if self.flushed > 0 {
263                     if self.done() {
264                         *self.encoder_flushed += *self.encoder_buffered;
265                         *self.encoder_buffered = 0;
266                     } else {
267                         self.buffer.copy_within(self.flushed.., 0);
268                         *self.encoder_flushed += self.flushed;
269                         *self.encoder_buffered -= self.flushed;
270                     }
271                 }
272             }
273         }
274
275         let mut guard = BufGuard::new(
276             unsafe { MaybeUninit::slice_assume_init_mut(&mut self.buf[..self.buffered]) },
277             &mut self.buffered,
278             &mut self.flushed,
279         );
280
281         while !guard.done() {
282             match self.file.write(guard.remaining()) {
283                 Ok(0) => {
284                     return Err(io::Error::new(
285                         io::ErrorKind::WriteZero,
286                         "failed to write the buffered data",
287                     ));
288                 }
289                 Ok(n) => guard.consume(n),
290                 Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {}
291                 Err(e) => return Err(e),
292             }
293         }
294
295         Ok(())
296     }
297
298     #[inline]
299     fn capacity(&self) -> usize {
300         self.buf.len()
301     }
302
303     #[inline]
304     fn write_one(&mut self, value: u8) -> FileEncodeResult {
305         // We ensure this during `FileEncoder` construction.
306         debug_assert!(self.capacity() >= 1);
307
308         let mut buffered = self.buffered;
309
310         if std::intrinsics::unlikely(buffered >= self.capacity()) {
311             self.flush()?;
312             buffered = 0;
313         }
314
315         // SAFETY: The above check and `flush` ensures that there is enough
316         // room to write the input to the buffer.
317         unsafe {
318             *MaybeUninit::slice_as_mut_ptr(&mut self.buf).add(buffered) = value;
319         }
320
321         self.buffered = buffered + 1;
322
323         Ok(())
324     }
325
326     #[inline]
327     fn write_all(&mut self, buf: &[u8]) -> FileEncodeResult {
328         let capacity = self.capacity();
329         let buf_len = buf.len();
330
331         if std::intrinsics::likely(buf_len <= capacity) {
332             let mut buffered = self.buffered;
333
334             if std::intrinsics::unlikely(buf_len > capacity - buffered) {
335                 self.flush()?;
336                 buffered = 0;
337             }
338
339             // SAFETY: The above check and `flush` ensures that there is enough
340             // room to write the input to the buffer.
341             unsafe {
342                 let src = buf.as_ptr();
343                 let dst = MaybeUninit::slice_as_mut_ptr(&mut self.buf).add(buffered);
344                 ptr::copy_nonoverlapping(src, dst, buf_len);
345             }
346
347             self.buffered = buffered + buf_len;
348
349             Ok(())
350         } else {
351             self.write_all_unbuffered(buf)
352         }
353     }
354
355     fn write_all_unbuffered(&mut self, mut buf: &[u8]) -> FileEncodeResult {
356         if self.buffered > 0 {
357             self.flush()?;
358         }
359
360         // This is basically a copy of `Write::write_all` but also updates our
361         // `self.flushed`. It's necessary because `Write::write_all` does not
362         // return the number of bytes written when an error is encountered, and
363         // without that, we cannot accurately update `self.flushed` on error.
364         while !buf.is_empty() {
365             match self.file.write(buf) {
366                 Ok(0) => {
367                     return Err(io::Error::new(
368                         io::ErrorKind::WriteZero,
369                         "failed to write whole buffer",
370                     ));
371                 }
372                 Ok(n) => {
373                     buf = &buf[n..];
374                     self.flushed += n;
375                 }
376                 Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {}
377                 Err(e) => return Err(e),
378             }
379         }
380
381         Ok(())
382     }
383 }
384
385 impl Drop for FileEncoder {
386     fn drop(&mut self) {
387         let _result = self.flush();
388     }
389 }
390
391 macro_rules! file_encoder_write_leb128 {
392     ($enc:expr, $value:expr, $int_ty:ty, $fun:ident) => {{
393         const MAX_ENCODED_LEN: usize = max_leb128_len!($int_ty);
394
395         // We ensure this during `FileEncoder` construction.
396         debug_assert!($enc.capacity() >= MAX_ENCODED_LEN);
397
398         let mut buffered = $enc.buffered;
399
400         // This can't overflow. See assertion in `FileEncoder::with_capacity`.
401         if std::intrinsics::unlikely(buffered + MAX_ENCODED_LEN > $enc.capacity()) {
402             $enc.flush()?;
403             buffered = 0;
404         }
405
406         // SAFETY: The above check and flush ensures that there is enough
407         // room to write the encoded value to the buffer.
408         let buf = unsafe {
409             &mut *($enc.buf.as_mut_ptr().add(buffered) as *mut [MaybeUninit<u8>; MAX_ENCODED_LEN])
410         };
411
412         let encoded = leb128::$fun(buf, $value);
413         $enc.buffered = buffered + encoded.len();
414
415         Ok(())
416     }};
417 }
418
419 impl serialize::Encoder for FileEncoder {
420     type Error = io::Error;
421
422     #[inline]
423     fn emit_unit(&mut self) -> FileEncodeResult {
424         Ok(())
425     }
426
427     #[inline]
428     fn emit_usize(&mut self, v: usize) -> FileEncodeResult {
429         file_encoder_write_leb128!(self, v, usize, write_usize_leb128)
430     }
431
432     #[inline]
433     fn emit_u128(&mut self, v: u128) -> FileEncodeResult {
434         file_encoder_write_leb128!(self, v, u128, write_u128_leb128)
435     }
436
437     #[inline]
438     fn emit_u64(&mut self, v: u64) -> FileEncodeResult {
439         file_encoder_write_leb128!(self, v, u64, write_u64_leb128)
440     }
441
442     #[inline]
443     fn emit_u32(&mut self, v: u32) -> FileEncodeResult {
444         file_encoder_write_leb128!(self, v, u32, write_u32_leb128)
445     }
446
447     #[inline]
448     fn emit_u16(&mut self, v: u16) -> FileEncodeResult {
449         file_encoder_write_leb128!(self, v, u16, write_u16_leb128)
450     }
451
452     #[inline]
453     fn emit_u8(&mut self, v: u8) -> FileEncodeResult {
454         self.write_one(v)
455     }
456
457     #[inline]
458     fn emit_isize(&mut self, v: isize) -> FileEncodeResult {
459         file_encoder_write_leb128!(self, v, isize, write_isize_leb128)
460     }
461
462     #[inline]
463     fn emit_i128(&mut self, v: i128) -> FileEncodeResult {
464         file_encoder_write_leb128!(self, v, i128, write_i128_leb128)
465     }
466
467     #[inline]
468     fn emit_i64(&mut self, v: i64) -> FileEncodeResult {
469         file_encoder_write_leb128!(self, v, i64, write_i64_leb128)
470     }
471
472     #[inline]
473     fn emit_i32(&mut self, v: i32) -> FileEncodeResult {
474         file_encoder_write_leb128!(self, v, i32, write_i32_leb128)
475     }
476
477     #[inline]
478     fn emit_i16(&mut self, v: i16) -> FileEncodeResult {
479         file_encoder_write_leb128!(self, v, i16, write_i16_leb128)
480     }
481
482     #[inline]
483     fn emit_i8(&mut self, v: i8) -> FileEncodeResult {
484         let as_u8: u8 = unsafe { std::mem::transmute(v) };
485         self.emit_u8(as_u8)
486     }
487
488     #[inline]
489     fn emit_bool(&mut self, v: bool) -> FileEncodeResult {
490         self.emit_u8(if v { 1 } else { 0 })
491     }
492
493     #[inline]
494     fn emit_f64(&mut self, v: f64) -> FileEncodeResult {
495         let as_u64: u64 = v.to_bits();
496         self.emit_u64(as_u64)
497     }
498
499     #[inline]
500     fn emit_f32(&mut self, v: f32) -> FileEncodeResult {
501         let as_u32: u32 = v.to_bits();
502         self.emit_u32(as_u32)
503     }
504
505     #[inline]
506     fn emit_char(&mut self, v: char) -> FileEncodeResult {
507         self.emit_u32(v as u32)
508     }
509
510     #[inline]
511     fn emit_str(&mut self, v: &str) -> FileEncodeResult {
512         self.emit_usize(v.len())?;
513         self.emit_raw_bytes(v.as_bytes())?;
514         self.emit_u8(STR_SENTINEL)
515     }
516
517     #[inline]
518     fn emit_raw_bytes(&mut self, s: &[u8]) -> FileEncodeResult {
519         self.write_all(s)
520     }
521 }
522
523 // -----------------------------------------------------------------------------
524 // Decoder
525 // -----------------------------------------------------------------------------
526
527 pub struct Decoder<'a> {
528     pub data: &'a [u8],
529     position: usize,
530 }
531
532 impl<'a> Decoder<'a> {
533     #[inline]
534     pub fn new(data: &'a [u8], position: usize) -> Decoder<'a> {
535         Decoder { data, position }
536     }
537
538     #[inline]
539     pub fn position(&self) -> usize {
540         self.position
541     }
542
543     #[inline]
544     pub fn set_position(&mut self, pos: usize) {
545         self.position = pos
546     }
547
548     #[inline]
549     pub fn advance(&mut self, bytes: usize) {
550         self.position += bytes;
551     }
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 }
560
561 macro_rules! read_leb128 {
562     ($dec:expr, $fun:ident) => {{ Ok(leb128::$fun($dec.data, &mut $dec.position)) }};
563 }
564
565 impl<'a> serialize::Decoder for Decoder<'a> {
566     type Error = String;
567
568     #[inline]
569     fn read_nil(&mut self) -> Result<(), Self::Error> {
570         Ok(())
571     }
572
573     #[inline]
574     fn read_u128(&mut self) -> Result<u128, Self::Error> {
575         read_leb128!(self, read_u128_leb128)
576     }
577
578     #[inline]
579     fn read_u64(&mut self) -> Result<u64, Self::Error> {
580         read_leb128!(self, read_u64_leb128)
581     }
582
583     #[inline]
584     fn read_u32(&mut self) -> Result<u32, Self::Error> {
585         read_leb128!(self, read_u32_leb128)
586     }
587
588     #[inline]
589     fn read_u16(&mut self) -> Result<u16, Self::Error> {
590         read_leb128!(self, read_u16_leb128)
591     }
592
593     #[inline]
594     fn read_u8(&mut self) -> Result<u8, Self::Error> {
595         let value = self.data[self.position];
596         self.position += 1;
597         Ok(value)
598     }
599
600     #[inline]
601     fn read_usize(&mut self) -> Result<usize, Self::Error> {
602         read_leb128!(self, read_usize_leb128)
603     }
604
605     #[inline]
606     fn read_i128(&mut self) -> Result<i128, Self::Error> {
607         read_leb128!(self, read_i128_leb128)
608     }
609
610     #[inline]
611     fn read_i64(&mut self) -> Result<i64, Self::Error> {
612         read_leb128!(self, read_i64_leb128)
613     }
614
615     #[inline]
616     fn read_i32(&mut self) -> Result<i32, Self::Error> {
617         read_leb128!(self, read_i32_leb128)
618     }
619
620     #[inline]
621     fn read_i16(&mut self) -> Result<i16, Self::Error> {
622         read_leb128!(self, read_i16_leb128)
623     }
624
625     #[inline]
626     fn read_i8(&mut self) -> Result<i8, Self::Error> {
627         let as_u8 = self.data[self.position];
628         self.position += 1;
629         unsafe { Ok(::std::mem::transmute(as_u8)) }
630     }
631
632     #[inline]
633     fn read_isize(&mut self) -> Result<isize, Self::Error> {
634         read_leb128!(self, read_isize_leb128)
635     }
636
637     #[inline]
638     fn read_bool(&mut self) -> Result<bool, Self::Error> {
639         let value = self.read_u8()?;
640         Ok(value != 0)
641     }
642
643     #[inline]
644     fn read_f64(&mut self) -> Result<f64, Self::Error> {
645         let bits = self.read_u64()?;
646         Ok(f64::from_bits(bits))
647     }
648
649     #[inline]
650     fn read_f32(&mut self) -> Result<f32, Self::Error> {
651         let bits = self.read_u32()?;
652         Ok(f32::from_bits(bits))
653     }
654
655     #[inline]
656     fn read_char(&mut self) -> Result<char, Self::Error> {
657         let bits = self.read_u32()?;
658         Ok(std::char::from_u32(bits).unwrap())
659     }
660
661     #[inline]
662     fn read_str(&mut self) -> Result<Cow<'_, str>, Self::Error> {
663         let len = self.read_usize()?;
664         let sentinel = self.data[self.position + len];
665         assert!(sentinel == STR_SENTINEL);
666         let s = unsafe {
667             std::str::from_utf8_unchecked(&self.data[self.position..self.position + len])
668         };
669         self.position += len + 1;
670         Ok(Cow::Borrowed(s))
671     }
672
673     #[inline]
674     fn error(&mut self, err: &str) -> Self::Error {
675         err.to_string()
676     }
677
678     #[inline]
679     fn read_raw_bytes_into(&mut self, s: &mut [u8]) -> Result<(), String> {
680         let start = self.position;
681         self.position += s.len();
682         s.copy_from_slice(&self.data[start..self.position]);
683         Ok(())
684     }
685 }
686
687 // Specializations for contiguous byte sequences follow. The default implementations for slices
688 // encode and decode each element individually. This isn't necessary for `u8` slices when using
689 // opaque encoders and decoders, because each `u8` is unchanged by encoding and decoding.
690 // Therefore, we can use more efficient implementations that process the entire sequence at once.
691
692 // Specialize encoding byte slices. This specialization also applies to encoding `Vec<u8>`s, etc.,
693 // since the default implementations call `encode` on their slices internally.
694 impl serialize::Encodable<Encoder> for [u8] {
695     fn encode(&self, e: &mut Encoder) -> EncodeResult {
696         serialize::Encoder::emit_usize(e, self.len())?;
697         e.emit_raw_bytes(self)
698     }
699 }
700
701 impl serialize::Encodable<FileEncoder> for [u8] {
702     fn encode(&self, e: &mut FileEncoder) -> FileEncodeResult {
703         serialize::Encoder::emit_usize(e, self.len())?;
704         e.emit_raw_bytes(self)
705     }
706 }
707
708 // Specialize decoding `Vec<u8>`. This specialization also applies to decoding `Box<[u8]>`s, etc.,
709 // since the default implementations call `decode` to produce a `Vec<u8>` internally.
710 impl<'a> serialize::Decodable<Decoder<'a>> for Vec<u8> {
711     fn decode(d: &mut Decoder<'a>) -> Result<Self, String> {
712         let len = serialize::Decoder::read_usize(d)?;
713         Ok(d.read_raw_bytes(len).to_owned())
714     }
715 }
716
717 // An integer that will always encode to 8 bytes.
718 pub struct IntEncodedWithFixedSize(pub u64);
719
720 impl IntEncodedWithFixedSize {
721     pub const ENCODED_SIZE: usize = 8;
722 }
723
724 impl serialize::Encodable<Encoder> for IntEncodedWithFixedSize {
725     #[inline]
726     fn encode(&self, e: &mut Encoder) -> EncodeResult {
727         let _start_pos = e.position();
728         e.emit_raw_bytes(&self.0.to_le_bytes())?;
729         let _end_pos = e.position();
730         debug_assert_eq!((_end_pos - _start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
731         Ok(())
732     }
733 }
734
735 impl serialize::Encodable<FileEncoder> for IntEncodedWithFixedSize {
736     #[inline]
737     fn encode(&self, e: &mut FileEncoder) -> FileEncodeResult {
738         let _start_pos = e.position();
739         e.emit_raw_bytes(&self.0.to_le_bytes())?;
740         let _end_pos = e.position();
741         debug_assert_eq!((_end_pos - _start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
742         Ok(())
743     }
744 }
745
746 impl<'a> serialize::Decodable<Decoder<'a>> for IntEncodedWithFixedSize {
747     #[inline]
748     fn decode(decoder: &mut Decoder<'a>) -> Result<IntEncodedWithFixedSize, String> {
749         let _start_pos = decoder.position();
750         let bytes = decoder.read_raw_bytes(IntEncodedWithFixedSize::ENCODED_SIZE);
751         let _end_pos = decoder.position();
752         debug_assert_eq!((_end_pos - _start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
753
754         let value = u64::from_le_bytes(bytes.try_into().unwrap());
755         Ok(IntEncodedWithFixedSize(value))
756     }
757 }