Skip to content

Commit d04c3aa

Browse files
committed
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

+10-56
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

+1
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

-7
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

+3-9
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

+2-1
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

+5
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

+2-50
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

+21
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

+2
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))]

0 commit comments

Comments
 (0)