Skip to content

Commit 45d2ae4

Browse files
committed
Move raw bytes handling to Encoder/Decoder.
1 parent 61365c0 commit 45d2ae4

File tree

6 files changed

+59
-38
lines changed

6 files changed

+59
-38
lines changed

compiler/rustc_data_structures/src/fingerprint.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::stable_hasher;
22
use rustc_serialize::{
33
opaque::{self, EncodeResult, FileEncodeResult},
4-
Decodable, Encodable,
4+
Decodable, Decoder, Encodable, Encoder,
55
};
66
use std::hash::{Hash, Hasher};
77
use std::mem::{self, MaybeUninit};
@@ -158,7 +158,7 @@ impl<E: rustc_serialize::Encoder> FingerprintEncoder for E {
158158
impl FingerprintEncoder for opaque::Encoder {
159159
fn encode_fingerprint(&mut self, f: &Fingerprint) -> EncodeResult {
160160
let bytes: [u8; 16] = unsafe { mem::transmute([f.0.to_le(), f.1.to_le()]) };
161-
self.emit_raw_bytes(&bytes);
161+
self.emit_raw_bytes(&bytes)?;
162162
Ok(())
163163
}
164164
}

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/encoder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2073,10 +2073,10 @@ pub(super) fn encode_metadata(tcx: TyCtxt<'_>) -> EncodedMetadata {
20732073

20742074
fn encode_metadata_impl(tcx: TyCtxt<'_>) -> EncodedMetadata {
20752075
let mut encoder = opaque::Encoder::new(vec![]);
2076-
encoder.emit_raw_bytes(METADATA_HEADER);
2076+
encoder.emit_raw_bytes(METADATA_HEADER).unwrap();
20772077

20782078
// Will be filled with the root position after encoding everything.
2079-
encoder.emit_raw_bytes(&[0, 0, 0, 0]);
2079+
encoder.emit_raw_bytes(&[0, 0, 0, 0]).unwrap();
20802080

20812081
let source_map_files = tcx.sess.source_map().files();
20822082
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_serialize/src/opaque.rs

Lines changed: 34 additions & 33 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,13 @@ 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+
Ok(())
154+
}
155+
156+
#[inline]
157+
fn emit_raw_bytes(&mut self, s: &[u8]) -> EncodeResult {
158+
self.data.extend_from_slice(s);
158159
Ok(())
159160
}
160161
}
@@ -208,11 +209,6 @@ impl FileEncoder {
208209
self.flushed + self.buffered
209210
}
210211

211-
#[inline]
212-
pub fn emit_raw_bytes(&mut self, s: &[u8]) -> FileEncodeResult {
213-
self.write_all(s)
214-
}
215-
216212
pub fn flush(&mut self) -> FileEncodeResult {
217213
// This is basically a copy of `BufWriter::flush`. If `BufWriter` ever
218214
// offers a raw buffer access API, we can use it, and remove this.
@@ -508,6 +504,11 @@ impl serialize::Encoder for FileEncoder {
508504
self.emit_usize(v.len())?;
509505
self.emit_raw_bytes(v.as_bytes())
510506
}
507+
508+
#[inline]
509+
fn emit_raw_bytes(&mut self, s: &[u8]) -> FileEncodeResult {
510+
self.write_all(s)
511+
}
511512
}
512513

513514
// -----------------------------------------------------------------------------
@@ -539,26 +540,6 @@ impl<'a> Decoder<'a> {
539540
pub fn advance(&mut self, bytes: usize) {
540541
self.position += bytes;
541542
}
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-
}
562543
}
563544

564545
macro_rules! read_leb128 {
@@ -677,6 +658,26 @@ impl<'a> serialize::Decoder for Decoder<'a> {
677658
fn error(&mut self, err: &str) -> Self::Error {
678659
err.to_string()
679660
}
661+
662+
#[inline]
663+
fn read_raw_bytes(&mut self, s: &mut [MaybeUninit<u8>]) -> Result<(), String> {
664+
let start = self.position;
665+
let end = start + s.len();
666+
assert!(end <= self.data.len());
667+
668+
// SAFETY: Both `src` and `dst` point to at least `s.len()` elements:
669+
// `src` points to at least `s.len()` elements by above assert, and
670+
// `dst` points to `s.len()` elements by derivation from `s`.
671+
unsafe {
672+
let src = self.data.as_ptr().add(start);
673+
let dst = s.as_mut_ptr() as *mut u8;
674+
ptr::copy_nonoverlapping(src, dst, s.len());
675+
}
676+
677+
self.position = end;
678+
679+
Ok(())
680+
}
680681
}
681682

682683
// Specializations for contiguous byte sequences follow. The default implementations for slices
@@ -689,7 +690,7 @@ impl<'a> serialize::Decoder for Decoder<'a> {
689690
impl serialize::Encodable<Encoder> for [u8] {
690691
fn encode(&self, e: &mut Encoder) -> EncodeResult {
691692
serialize::Encoder::emit_usize(e, self.len())?;
692-
e.emit_raw_bytes(self);
693+
e.emit_raw_bytes(self)?;
693694
Ok(())
694695
}
695696
}

compiler/rustc_serialize/src/serialize.rs

Lines changed: 18 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;
@@ -200,6 +201,14 @@ pub trait Encoder {
200201
{
201202
f(self)
202203
}
204+
205+
#[inline]
206+
fn emit_raw_bytes(&mut self, s: &[u8]) -> Result<(), Self::Error> {
207+
for &c in s.iter() {
208+
self.emit_u8(c)?;
209+
}
210+
Ok(())
211+
}
203212
}
204213

205214
pub trait Decoder {
@@ -377,6 +386,15 @@ pub trait Decoder {
377386

378387
// Failure
379388
fn error(&mut self, err: &str) -> Self::Error;
389+
390+
#[inline]
391+
fn read_raw_bytes(&mut self, s: &mut [MaybeUninit<u8>]) -> Result<(), Self::Error> {
392+
for c in s.iter_mut() {
393+
let h = self.read_u8()?;
394+
unsafe { *c.as_mut_ptr() = h };
395+
}
396+
Ok(())
397+
}
380398
}
381399

382400
/// Trait for types that can be serialized

0 commit comments

Comments
 (0)