Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d04c3aa

Browse files
committedMar 22, 2021
Auto merge of #83273 - cjgillot:endecode, r=michaelwoerister
Simplify encoder and decoder Extracted from #83036 and #82780.
2 parents 7f82ddb + 11b3409 commit d04c3aa

File tree

11 files changed

+125
-157
lines changed

11 files changed

+125
-157
lines changed
 

‎compiler/rustc_data_structures/src/fingerprint.rs

Lines changed: 10 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
use crate::stable_hasher;
2-
use rustc_serialize::{
3-
opaque::{self, EncodeResult, FileEncodeResult},
4-
Decodable, Encodable,
5-
};
2+
use rustc_serialize::{Decodable, Encodable};
63
use std::hash::{Hash, Hasher};
74
use std::mem::{self, MaybeUninit};
85

@@ -63,16 +60,6 @@ impl Fingerprint {
6360
pub fn to_hex(&self) -> String {
6461
format!("{:x}{:x}", self.0, self.1)
6562
}
66-
67-
pub fn decode_opaque(decoder: &mut opaque::Decoder<'_>) -> Result<Fingerprint, String> {
68-
let mut bytes: [MaybeUninit<u8>; 16] = MaybeUninit::uninit_array();
69-
70-
decoder.read_raw_bytes(&mut bytes)?;
71-
72-
let [l, r]: [u64; 2] = unsafe { mem::transmute(bytes) };
73-
74-
Ok(Fingerprint(u64::from_le(l), u64::from_le(r)))
75-
}
7663
}
7764

7865
impl std::fmt::Display for Fingerprint {
@@ -130,55 +117,22 @@ impl stable_hasher::StableHasherResult for Fingerprint {
130117
impl_stable_hash_via_hash!(Fingerprint);
131118

132119
impl<E: rustc_serialize::Encoder> Encodable<E> for Fingerprint {
120+
#[inline]
133121
fn encode(&self, s: &mut E) -> Result<(), E::Error> {
134-
s.encode_fingerprint(self)
122+
let bytes: [u8; 16] = unsafe { mem::transmute([self.0.to_le(), self.1.to_le()]) };
123+
s.emit_raw_bytes(&bytes)?;
124+
Ok(())
135125
}
136126
}
137127

138128
impl<D: rustc_serialize::Decoder> Decodable<D> for Fingerprint {
129+
#[inline]
139130
fn decode(d: &mut D) -> Result<Self, D::Error> {
140-
d.decode_fingerprint()
141-
}
142-
}
143-
144-
pub trait FingerprintEncoder: rustc_serialize::Encoder {
145-
fn encode_fingerprint(&mut self, f: &Fingerprint) -> Result<(), Self::Error>;
146-
}
147-
148-
pub trait FingerprintDecoder: rustc_serialize::Decoder {
149-
fn decode_fingerprint(&mut self) -> Result<Fingerprint, Self::Error>;
150-
}
151-
152-
impl<E: rustc_serialize::Encoder> FingerprintEncoder for E {
153-
default fn encode_fingerprint(&mut self, _: &Fingerprint) -> Result<(), E::Error> {
154-
panic!("Cannot encode `Fingerprint` with `{}`", std::any::type_name::<E>());
155-
}
156-
}
157-
158-
impl FingerprintEncoder for opaque::Encoder {
159-
fn encode_fingerprint(&mut self, f: &Fingerprint) -> EncodeResult {
160-
let bytes: [u8; 16] = unsafe { mem::transmute([f.0.to_le(), f.1.to_le()]) };
161-
self.emit_raw_bytes(&bytes);
162-
Ok(())
163-
}
164-
}
165-
166-
impl FingerprintEncoder for opaque::FileEncoder {
167-
fn encode_fingerprint(&mut self, f: &Fingerprint) -> FileEncodeResult {
168-
let bytes: [u8; 16] = unsafe { mem::transmute([f.0.to_le(), f.1.to_le()]) };
169-
self.emit_raw_bytes(&bytes)
170-
}
171-
}
172-
173-
impl<D: rustc_serialize::Decoder> FingerprintDecoder for D {
174-
default fn decode_fingerprint(&mut self) -> Result<Fingerprint, D::Error> {
175-
panic!("Cannot decode `Fingerprint` with `{}`", std::any::type_name::<D>());
176-
}
177-
}
131+
let mut bytes: [MaybeUninit<u8>; 16] = MaybeUninit::uninit_array();
132+
d.read_raw_bytes(&mut bytes)?;
178133

179-
impl FingerprintDecoder for opaque::Decoder<'_> {
180-
fn decode_fingerprint(&mut self) -> Result<Fingerprint, String> {
181-
Fingerprint::decode_opaque(self)
134+
let [l, r]: [u64; 2] = unsafe { mem::transmute(bytes) };
135+
Ok(Fingerprint(u64::from_le(l), u64::from_le(r)))
182136
}
183137
}
184138

‎compiler/rustc_incremental/src/persist/file_format.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use std::io::{self, Read};
1515
use std::path::Path;
1616

1717
use rustc_serialize::opaque::{FileEncodeResult, FileEncoder};
18+
use rustc_serialize::Encoder;
1819

1920
/// The first few bytes of files generated by incremental compilation.
2021
const FILE_MAGIC: &[u8] = b"RSIC";

‎compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use crate::rmeta::*;
77
use rustc_ast as ast;
88
use rustc_attr as attr;
99
use rustc_data_structures::captures::Captures;
10-
use rustc_data_structures::fingerprint::{Fingerprint, FingerprintDecoder};
1110
use rustc_data_structures::fx::FxHashMap;
1211
use rustc_data_structures::svh::Svh;
1312
use rustc_data_structures::sync::{Lock, LockGuard, Lrc, OnceCell};
@@ -351,12 +350,6 @@ impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for DefIndex {
351350
}
352351
}
353352

354-
impl<'a, 'tcx> FingerprintDecoder for DecodeContext<'a, 'tcx> {
355-
fn decode_fingerprint(&mut self) -> Result<Fingerprint, String> {
356-
Fingerprint::decode_opaque(&mut self.opaque)
357-
}
358-
}
359-
360353
impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for SyntaxContext {
361354
fn decode(decoder: &mut DecodeContext<'a, 'tcx>) -> Result<SyntaxContext, String> {
362355
let cdata = decoder.cdata();

‎compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::rmeta::table::{FixedSizeEncoding, TableBuilder};
22
use crate::rmeta::*;
33

4-
use rustc_data_structures::fingerprint::{Fingerprint, FingerprintEncoder};
54
use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
65
use rustc_data_structures::stable_hasher::StableHasher;
76
use rustc_data_structures::sync::{join, par_iter, Lrc, ParallelIterator};
@@ -116,6 +115,7 @@ impl<'a, 'tcx> Encoder for EncodeContext<'a, 'tcx> {
116115
emit_f32(f32);
117116
emit_char(char);
118117
emit_str(&str);
118+
emit_raw_bytes(&[u8]);
119119
}
120120
}
121121

@@ -307,12 +307,6 @@ impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for Span {
307307
}
308308
}
309309

310-
impl<'a, 'tcx> FingerprintEncoder for EncodeContext<'a, 'tcx> {
311-
fn encode_fingerprint(&mut self, f: &Fingerprint) -> Result<(), Self::Error> {
312-
self.opaque.encode_fingerprint(f)
313-
}
314-
}
315-
316310
impl<'a, 'tcx> TyEncoder<'tcx> for EncodeContext<'a, 'tcx> {
317311
const CLEAR_CROSS_CRATE: bool = true;
318312

@@ -2064,10 +2058,10 @@ pub(super) fn encode_metadata(tcx: TyCtxt<'_>) -> EncodedMetadata {
20642058

20652059
fn encode_metadata_impl(tcx: TyCtxt<'_>) -> EncodedMetadata {
20662060
let mut encoder = opaque::Encoder::new(vec![]);
2067-
encoder.emit_raw_bytes(METADATA_HEADER);
2061+
encoder.emit_raw_bytes(METADATA_HEADER).unwrap();
20682062

20692063
// Will be filled with the root position after encoding everything.
2070-
encoder.emit_raw_bytes(&[0, 0, 0, 0]);
2064+
encoder.emit_raw_bytes(&[0, 0, 0, 0]).unwrap();
20712065

20722066
let source_map_files = tcx.sess.source_map().files();
20732067
let source_file_cache = (source_map_files[0].clone(), 0);

‎compiler/rustc_metadata/src/rmeta/table.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::rmeta::*;
22

33
use rustc_index::vec::Idx;
44
use rustc_serialize::opaque::Encoder;
5+
use rustc_serialize::Encoder as _;
56
use std::convert::TryInto;
67
use std::marker::PhantomData;
78
use std::num::NonZeroUsize;
@@ -172,7 +173,7 @@ where
172173

173174
pub(crate) fn encode(&self, buf: &mut Encoder) -> Lazy<Table<I, T>> {
174175
let pos = buf.position();
175-
buf.emit_raw_bytes(&self.bytes);
176+
buf.emit_raw_bytes(&self.bytes).unwrap();
176177
Lazy::from_position_and_meta(NonZeroUsize::new(pos as usize).unwrap(), self.bytes.len())
177178
}
178179
}

‎compiler/rustc_middle/src/ty/codec.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,11 @@ macro_rules! implement_ty_decoder {
472472
read_str -> Cow<'_, str>;
473473
}
474474

475+
#[inline]
476+
fn read_raw_bytes(&mut self, bytes: &mut [std::mem::MaybeUninit<u8>]) -> Result<(), Self::Error> {
477+
self.opaque.read_raw_bytes(bytes)
478+
}
479+
475480
fn error(&mut self, err: &str) -> Self::Error {
476481
self.opaque.error(err)
477482
}

‎compiler/rustc_middle/src/ty/query/on_disk_cache.rs

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use crate::mir::{self, interpret};
44
use crate::ty::codec::{RefDecodable, TyDecoder, TyEncoder};
55
use crate::ty::context::TyCtxt;
66
use crate::ty::{self, Ty};
7-
use rustc_data_structures::fingerprint::{Fingerprint, FingerprintDecoder, FingerprintEncoder};
87
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet};
98
use rustc_data_structures::sync::{HashMapExt, Lock, Lrc, OnceCell};
109
use rustc_data_structures::thin_vec::ThinVec;
@@ -17,7 +16,7 @@ use rustc_index::vec::{Idx, IndexVec};
1716
use rustc_query_system::dep_graph::DepContext;
1817
use rustc_query_system::query::QueryContext;
1918
use rustc_serialize::{
20-
opaque::{self, FileEncodeResult, FileEncoder},
19+
opaque::{self, FileEncodeResult, FileEncoder, IntEncodedWithFixedSize},
2120
Decodable, Decoder, Encodable, Encoder,
2221
};
2322
use rustc_session::{CrateDisambiguator, Session};
@@ -913,12 +912,6 @@ impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for DefId {
913912
}
914913
}
915914

916-
impl<'a, 'tcx> FingerprintDecoder for CacheDecoder<'a, 'tcx> {
917-
fn decode_fingerprint(&mut self) -> Result<Fingerprint, Self::Error> {
918-
Fingerprint::decode_opaque(&mut self.opaque)
919-
}
920-
}
921-
922915
impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx FxHashSet<LocalDefId> {
923916
fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Result<Self, String> {
924917
RefDecodable::decode(d)
@@ -1011,12 +1004,6 @@ where
10111004
}
10121005
}
10131006

1014-
impl<'a, 'tcx, E: OpaqueEncoder> FingerprintEncoder for CacheEncoder<'a, 'tcx, E> {
1015-
fn encode_fingerprint(&mut self, f: &Fingerprint) -> Result<(), E::Error> {
1016-
self.encoder.encode_fingerprint(f)
1017-
}
1018-
}
1019-
10201007
impl<'a, 'tcx, E> Encodable<CacheEncoder<'a, 'tcx, E>> for SyntaxContext
10211008
where
10221009
E: 'a + OpaqueEncoder,
@@ -1167,6 +1154,7 @@ where
11671154
emit_f32(f32);
11681155
emit_char(char);
11691156
emit_str(&str);
1157+
emit_raw_bytes(&[u8]);
11701158
}
11711159
}
11721160

@@ -1180,42 +1168,6 @@ impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx, FileEncoder>> for [u8] {
11801168
}
11811169
}
11821170

1183-
// An integer that will always encode to 8 bytes.
1184-
struct IntEncodedWithFixedSize(u64);
1185-
1186-
impl IntEncodedWithFixedSize {
1187-
pub const ENCODED_SIZE: usize = 8;
1188-
}
1189-
1190-
impl<E: OpaqueEncoder> Encodable<E> for IntEncodedWithFixedSize {
1191-
fn encode(&self, e: &mut E) -> Result<(), E::Error> {
1192-
let start_pos = e.position();
1193-
for i in 0..IntEncodedWithFixedSize::ENCODED_SIZE {
1194-
((self.0 >> (i * 8)) as u8).encode(e)?;
1195-
}
1196-
let end_pos = e.position();
1197-
assert_eq!((end_pos - start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
1198-
Ok(())
1199-
}
1200-
}
1201-
1202-
impl<'a> Decodable<opaque::Decoder<'a>> for IntEncodedWithFixedSize {
1203-
fn decode(decoder: &mut opaque::Decoder<'a>) -> Result<IntEncodedWithFixedSize, String> {
1204-
let mut value: u64 = 0;
1205-
let start_pos = decoder.position();
1206-
1207-
for i in 0..IntEncodedWithFixedSize::ENCODED_SIZE {
1208-
let byte: u8 = Decodable::decode(decoder)?;
1209-
value |= (byte as u64) << (i * 8);
1210-
}
1211-
1212-
let end_pos = decoder.position();
1213-
assert_eq!((end_pos - start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
1214-
1215-
Ok(IntEncodedWithFixedSize(value))
1216-
}
1217-
}
1218-
12191171
pub fn encode_query_results<'a, 'tcx, CTX, Q>(
12201172
tcx: CTX,
12211173
encoder: &mut CacheEncoder<'a, 'tcx, FileEncoder>,

‎compiler/rustc_serialize/src/json.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ use std::collections::{BTreeMap, HashMap};
188188
use std::io;
189189
use std::io::prelude::*;
190190
use std::mem::swap;
191+
use std::mem::MaybeUninit;
191192
use std::num::FpCategory as Fp;
192193
use std::ops::Index;
193194
use std::str::FromStr;
@@ -553,6 +554,12 @@ impl<'a> crate::Encoder for Encoder<'a> {
553554
fn emit_str(&mut self, v: &str) -> EncodeResult {
554555
escape_str(self.writer, v)
555556
}
557+
fn emit_raw_bytes(&mut self, s: &[u8]) -> Result<(), Self::Error> {
558+
for &c in s.iter() {
559+
self.emit_u8(c)?;
560+
}
561+
Ok(())
562+
}
556563

557564
fn emit_enum<F>(&mut self, _name: &str, f: F) -> EncodeResult
558565
where
@@ -879,6 +886,12 @@ impl<'a> crate::Encoder for PrettyEncoder<'a> {
879886
fn emit_str(&mut self, v: &str) -> EncodeResult {
880887
escape_str(self.writer, v)
881888
}
889+
fn emit_raw_bytes(&mut self, s: &[u8]) -> Result<(), Self::Error> {
890+
for &c in s.iter() {
891+
self.emit_u8(c)?;
892+
}
893+
Ok(())
894+
}
882895

883896
fn emit_enum<F>(&mut self, _name: &str, f: F) -> EncodeResult
884897
where
@@ -2354,6 +2367,14 @@ impl crate::Decoder for Decoder {
23542367
expect!(self.pop(), String).map(Cow::Owned)
23552368
}
23562369

2370+
fn read_raw_bytes(&mut self, s: &mut [MaybeUninit<u8>]) -> Result<(), Self::Error> {
2371+
for c in s.iter_mut() {
2372+
let h = self.read_u8()?;
2373+
unsafe { *c.as_mut_ptr() = h };
2374+
}
2375+
Ok(())
2376+
}
2377+
23572378
fn read_enum<T, F>(&mut self, _name: &str, f: F) -> DecodeResult<T>
23582379
where
23592380
F: FnOnce(&mut Decoder) -> DecodeResult<T>,

‎compiler/rustc_serialize/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Core encoding and decoding interfaces.
1717
#![feature(vec_spare_capacity)]
1818
#![feature(core_intrinsics)]
1919
#![feature(int_bits_const)]
20+
#![feature(maybe_uninit_array_assume_init)]
21+
#![feature(maybe_uninit_uninit_array)]
2022
#![feature(maybe_uninit_slice)]
2123
#![feature(new_uninit)]
2224
#![cfg_attr(test, feature(test))]

‎compiler/rustc_serialize/src/opaque.rs

Lines changed: 76 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::leb128::{self, max_leb128_len};
2-
use crate::serialize;
2+
use crate::serialize::{self, Decoder as _, Encoder as _};
33
use std::borrow::Cow;
44
use std::fs::File;
55
use std::io::{self, Write};
@@ -30,11 +30,6 @@ impl Encoder {
3030
pub fn position(&self) -> usize {
3131
self.data.len()
3232
}
33-
34-
#[inline]
35-
pub fn emit_raw_bytes(&mut self, s: &[u8]) {
36-
self.data.extend_from_slice(s);
37-
}
3833
}
3934

4035
macro_rules! write_leb128 {
@@ -154,7 +149,12 @@ impl serialize::Encoder for Encoder {
154149
#[inline]
155150
fn emit_str(&mut self, v: &str) -> EncodeResult {
156151
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);
158158
Ok(())
159159
}
160160
}
@@ -208,11 +208,6 @@ impl FileEncoder {
208208
self.flushed + self.buffered
209209
}
210210

211-
#[inline]
212-
pub fn emit_raw_bytes(&mut self, s: &[u8]) -> FileEncodeResult {
213-
self.write_all(s)
214-
}
215-
216211
pub fn flush(&mut self) -> FileEncodeResult {
217212
// This is basically a copy of `BufWriter::flush`. If `BufWriter` ever
218213
// offers a raw buffer access API, we can use it, and remove this.
@@ -508,6 +503,11 @@ impl serialize::Encoder for FileEncoder {
508503
self.emit_usize(v.len())?;
509504
self.emit_raw_bytes(v.as_bytes())
510505
}
506+
507+
#[inline]
508+
fn emit_raw_bytes(&mut self, s: &[u8]) -> FileEncodeResult {
509+
self.write_all(s)
510+
}
511511
}
512512

513513
// -----------------------------------------------------------------------------
@@ -539,26 +539,6 @@ impl<'a> Decoder<'a> {
539539
pub fn advance(&mut self, bytes: usize) {
540540
self.position += bytes;
541541
}
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-
}
562542
}
563543

564544
macro_rules! read_leb128 {
@@ -677,6 +657,26 @@ impl<'a> serialize::Decoder for Decoder<'a> {
677657
fn error(&mut self, err: &str) -> Self::Error {
678658
err.to_string()
679659
}
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+
}
680680
}
681681

682682
// Specializations for contiguous byte sequences follow. The default implementations for slices
@@ -689,8 +689,7 @@ impl<'a> serialize::Decoder for Decoder<'a> {
689689
impl serialize::Encodable<Encoder> for [u8] {
690690
fn encode(&self, e: &mut Encoder) -> EncodeResult {
691691
serialize::Encoder::emit_usize(e, self.len())?;
692-
e.emit_raw_bytes(self);
693-
Ok(())
692+
e.emit_raw_bytes(self)
694693
}
695694
}
696695

@@ -718,3 +717,46 @@ impl<'a> serialize::Decodable<Decoder<'a>> for Vec<u8> {
718717
Ok(v)
719718
}
720719
}
720+
721+
// An integer that will always encode to 8 bytes.
722+
pub struct IntEncodedWithFixedSize(pub u64);
723+
724+
impl IntEncodedWithFixedSize {
725+
pub const ENCODED_SIZE: usize = 8;
726+
}
727+
728+
impl serialize::Encodable<Encoder> for IntEncodedWithFixedSize {
729+
#[inline]
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+
debug_assert_eq!((_end_pos - _start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
735+
Ok(())
736+
}
737+
}
738+
739+
impl serialize::Encodable<FileEncoder> for IntEncodedWithFixedSize {
740+
#[inline]
741+
fn encode(&self, e: &mut FileEncoder) -> FileEncodeResult {
742+
let _start_pos = e.position();
743+
e.emit_raw_bytes(&self.0.to_le_bytes())?;
744+
let _end_pos = e.position();
745+
debug_assert_eq!((_end_pos - _start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
746+
Ok(())
747+
}
748+
}
749+
750+
impl<'a> serialize::Decodable<Decoder<'a>> for IntEncodedWithFixedSize {
751+
#[inline]
752+
fn decode(decoder: &mut Decoder<'a>) -> Result<IntEncodedWithFixedSize, String> {
753+
let mut bytes = MaybeUninit::uninit_array();
754+
let _start_pos = decoder.position();
755+
decoder.read_raw_bytes(&mut bytes)?;
756+
let _end_pos = decoder.position();
757+
debug_assert_eq!((_end_pos - _start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
758+
759+
let value = u64::from_le_bytes(unsafe { MaybeUninit::array_assume_init(bytes) });
760+
Ok(IntEncodedWithFixedSize(value))
761+
}
762+
}

‎compiler/rustc_serialize/src/serialize.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Core encoding and decoding interfaces.
77
use std::borrow::Cow;
88
use std::cell::{Cell, RefCell};
99
use std::marker::PhantomData;
10+
use std::mem::MaybeUninit;
1011
use std::path;
1112
use std::rc::Rc;
1213
use std::sync::Arc;
@@ -33,6 +34,7 @@ pub trait Encoder {
3334
fn emit_f32(&mut self, v: f32) -> Result<(), Self::Error>;
3435
fn emit_char(&mut self, v: char) -> Result<(), Self::Error>;
3536
fn emit_str(&mut self, v: &str) -> Result<(), Self::Error>;
37+
fn emit_raw_bytes(&mut self, s: &[u8]) -> Result<(), Self::Error>;
3638

3739
// Compound types:
3840
#[inline]
@@ -224,6 +226,7 @@ pub trait Decoder {
224226
fn read_f32(&mut self) -> Result<f32, Self::Error>;
225227
fn read_char(&mut self) -> Result<char, Self::Error>;
226228
fn read_str(&mut self) -> Result<Cow<'_, str>, Self::Error>;
229+
fn read_raw_bytes(&mut self, s: &mut [MaybeUninit<u8>]) -> Result<(), Self::Error>;
227230

228231
// Compound types:
229232
#[inline]

0 commit comments

Comments
 (0)
Please sign in to comment.