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