Skip to content

Commit 5fef727

Browse files
committed
Move IntEncodedWithFixedSize.
1 parent f03955f commit 5fef727

File tree

5 files changed

+84
-48
lines changed

5 files changed

+84
-48
lines changed

compiler/rustc_query_impl/src/on_disk_cache.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ use rustc_middle::ty::{self, Ty, TyCtxt};
1515
use rustc_query_system::dep_graph::DepContext;
1616
use rustc_query_system::query::{QueryCache, QueryContext, QuerySideEffects};
1717
use rustc_serialize::{
18-
opaque::{self, FileEncodeResult, FileEncoder, IntEncodedWithFixedSize},
19-
Decodable, Decoder, Encodable, Encoder,
18+
opaque::{self, FileEncodeResult, FileEncoder},
19+
Decodable, Decoder, Encodable, Encoder, IntEncodedWithFixedSize,
2020
};
2121
use rustc_session::Session;
2222
use rustc_span::hygiene::{

compiler/rustc_query_system/src/dep_graph/serialized.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ use rustc_data_structures::fx::FxHashMap;
1919
use rustc_data_structures::profiling::SelfProfilerRef;
2020
use rustc_data_structures::sync::Lock;
2121
use rustc_index::vec::{Idx, IndexVec};
22-
use rustc_serialize::opaque::{self, FileEncodeResult, FileEncoder, IntEncodedWithFixedSize};
23-
use rustc_serialize::{Decodable, Decoder, Encodable};
22+
use rustc_serialize::opaque::{self, FileEncodeResult, FileEncoder};
23+
use rustc_serialize::{Decodable, Decoder, Encodable, IntEncodedWithFixedSize};
2424
use smallvec::SmallVec;
2525
use std::convert::TryInto;
2626

compiler/rustc_serialize/src/lib.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ Core encoding and decoding interfaces.
2020
#![cfg_attr(test, feature(test))]
2121
#![allow(rustc::internal)]
2222

23+
use std::convert::TryInto;
24+
2325
pub use self::serialize::{Decodable, Decoder, Encodable, Encoder};
2426

2527
mod collection_impls;
@@ -31,3 +33,80 @@ pub mod leb128;
3133
pub mod opaque;
3234

3335
pub mod raw;
36+
37+
// An integer that will always encode to 8 bytes.
38+
pub struct IntEncodedWithFixedSize(pub u64);
39+
40+
impl IntEncodedWithFixedSize {
41+
pub const ENCODED_SIZE: usize = 8;
42+
}
43+
44+
impl Encodable<opaque::Encoder> for IntEncodedWithFixedSize {
45+
#[inline]
46+
fn encode(&self, e: &mut opaque::Encoder) -> opaque::EncodeResult {
47+
let _start_pos = e.position();
48+
e.emit_raw_bytes(&self.0.to_le_bytes())?;
49+
let _end_pos = e.position();
50+
debug_assert_eq!((_end_pos - _start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
51+
Ok(())
52+
}
53+
}
54+
55+
impl Encodable<opaque::FileEncoder> for IntEncodedWithFixedSize {
56+
#[inline]
57+
fn encode(&self, e: &mut opaque::FileEncoder) -> opaque::FileEncodeResult {
58+
let _start_pos = e.position();
59+
e.emit_raw_bytes(&self.0.to_le_bytes())?;
60+
let _end_pos = e.position();
61+
debug_assert_eq!((_end_pos - _start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
62+
Ok(())
63+
}
64+
}
65+
66+
impl<'a> Decodable<opaque::Decoder<'a>> for IntEncodedWithFixedSize {
67+
#[inline]
68+
fn decode(decoder: &mut opaque::Decoder<'a>) -> IntEncodedWithFixedSize {
69+
let _start_pos = decoder.position();
70+
let bytes = decoder.read_raw_bytes(IntEncodedWithFixedSize::ENCODED_SIZE);
71+
let value = u64::from_le_bytes(bytes.try_into().unwrap());
72+
let _end_pos = decoder.position();
73+
debug_assert_eq!((_end_pos - _start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
74+
75+
IntEncodedWithFixedSize(value)
76+
}
77+
}
78+
79+
impl Encodable<raw::Encoder> for IntEncodedWithFixedSize {
80+
#[inline]
81+
fn encode(&self, e: &mut raw::Encoder) -> raw::EncodeResult {
82+
let _start_pos = e.position();
83+
e.emit_raw_bytes(&self.0.to_le_bytes())?;
84+
let _end_pos = e.position();
85+
debug_assert_eq!((_end_pos - _start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
86+
Ok(())
87+
}
88+
}
89+
90+
impl Encodable<raw::FileEncoder> for IntEncodedWithFixedSize {
91+
#[inline]
92+
fn encode(&self, e: &mut raw::FileEncoder) -> raw::FileEncodeResult {
93+
let _start_pos = e.position();
94+
e.emit_raw_bytes(&self.0.to_le_bytes())?;
95+
let _end_pos = e.position();
96+
debug_assert_eq!((_end_pos - _start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
97+
Ok(())
98+
}
99+
}
100+
101+
impl<'a> Decodable<raw::Decoder<'a>> for IntEncodedWithFixedSize {
102+
#[inline]
103+
fn decode(decoder: &mut raw::Decoder<'a>) -> IntEncodedWithFixedSize {
104+
let _start_pos = decoder.position();
105+
let bytes = decoder.read_raw_bytes(IntEncodedWithFixedSize::ENCODED_SIZE);
106+
let _end_pos = decoder.position();
107+
debug_assert_eq!((_end_pos - _start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
108+
109+
let value = u64::from_le_bytes(bytes.try_into().unwrap());
110+
IntEncodedWithFixedSize(value)
111+
}
112+
}

compiler/rustc_serialize/src/opaque.rs

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::leb128::{self, max_leb128_len};
22
use crate::serialize::{self, Decoder as _, Encoder as _};
3-
use std::convert::TryInto;
43
use std::fs::File;
54
use std::io::{self, Write};
65
use std::mem::MaybeUninit;
@@ -698,45 +697,3 @@ impl<'a> serialize::Decodable<Decoder<'a>> for Vec<u8> {
698697
d.read_raw_bytes(len).to_owned()
699698
}
700699
}
701-
702-
// An integer that will always encode to 8 bytes.
703-
pub struct IntEncodedWithFixedSize(pub u64);
704-
705-
impl IntEncodedWithFixedSize {
706-
pub const ENCODED_SIZE: usize = 8;
707-
}
708-
709-
impl serialize::Encodable<Encoder> for IntEncodedWithFixedSize {
710-
#[inline]
711-
fn encode(&self, e: &mut Encoder) -> EncodeResult {
712-
let _start_pos = e.position();
713-
e.emit_raw_bytes(&self.0.to_le_bytes())?;
714-
let _end_pos = e.position();
715-
debug_assert_eq!((_end_pos - _start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
716-
Ok(())
717-
}
718-
}
719-
720-
impl serialize::Encodable<FileEncoder> for IntEncodedWithFixedSize {
721-
#[inline]
722-
fn encode(&self, e: &mut FileEncoder) -> FileEncodeResult {
723-
let _start_pos = e.position();
724-
e.emit_raw_bytes(&self.0.to_le_bytes())?;
725-
let _end_pos = e.position();
726-
debug_assert_eq!((_end_pos - _start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
727-
Ok(())
728-
}
729-
}
730-
731-
impl<'a> serialize::Decodable<Decoder<'a>> for IntEncodedWithFixedSize {
732-
#[inline]
733-
fn decode(decoder: &mut Decoder<'a>) -> IntEncodedWithFixedSize {
734-
let _start_pos = decoder.position();
735-
let bytes = decoder.read_raw_bytes(IntEncodedWithFixedSize::ENCODED_SIZE);
736-
let value = u64::from_le_bytes(bytes.try_into().unwrap());
737-
let _end_pos = decoder.position();
738-
debug_assert_eq!((_end_pos - _start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
739-
740-
IntEncodedWithFixedSize(value)
741-
}
742-
}

compiler/rustc_serialize/src/raw.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ impl<'a> Decoder<'a> {
506506
}
507507

508508
#[inline]
509-
fn read_raw_bytes(&mut self, bytes: usize) -> &'a [u8] {
509+
pub fn read_raw_bytes(&mut self, bytes: usize) -> &'a [u8] {
510510
let start = self.position;
511511
self.position += bytes;
512512
&self.data[start..self.position]

0 commit comments

Comments
 (0)