Skip to content

Commit 5003b3d

Browse files
committed
Move IntEncodedWithFixedSize to rustc_serialize.
1 parent 0ce0fed commit 5003b3d

File tree

2 files changed

+49
-37
lines changed

2 files changed

+49
-37
lines changed

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

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_index::vec::{Idx, IndexVec};
1717
use rustc_query_system::dep_graph::DepContext;
1818
use rustc_query_system::query::QueryContext;
1919
use rustc_serialize::{
20-
opaque::{self, FileEncodeResult, FileEncoder},
20+
opaque::{self, FileEncodeResult, FileEncoder, IntEncodedWithFixedSize},
2121
Decodable, Decoder, Encodable, Encoder,
2222
};
2323
use rustc_session::{CrateDisambiguator, Session};
@@ -1180,42 +1180,6 @@ impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx, FileEncoder>> for [u8] {
11801180
}
11811181
}
11821182

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-
12191183
pub fn encode_query_results<'a, 'tcx, CTX, Q>(
12201184
tcx: CTX,
12211185
encoder: &mut CacheEncoder<'a, 'tcx, FileEncoder>,

compiler/rustc_serialize/src/opaque.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,3 +718,51 @@ impl<'a> serialize::Decodable<Decoder<'a>> for Vec<u8> {
718718
Ok(v)
719719
}
720720
}
721+
722+
// An integer that will always encode to 8 bytes.
723+
pub struct IntEncodedWithFixedSize(pub u64);
724+
725+
impl IntEncodedWithFixedSize {
726+
pub const ENCODED_SIZE: usize = 8;
727+
}
728+
729+
impl serialize::Encodable<Encoder> for IntEncodedWithFixedSize {
730+
fn encode(&self, e: &mut Encoder) -> EncodeResult {
731+
let start_pos = e.position();
732+
for i in 0..IntEncodedWithFixedSize::ENCODED_SIZE {
733+
((self.0 >> (i * 8)) as u8).encode(e)?;
734+
}
735+
let end_pos = e.position();
736+
assert_eq!((end_pos - start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
737+
Ok(())
738+
}
739+
}
740+
741+
impl serialize::Encodable<FileEncoder> for IntEncodedWithFixedSize {
742+
fn encode(&self, e: &mut FileEncoder) -> FileEncodeResult {
743+
let start_pos = e.position();
744+
for i in 0..IntEncodedWithFixedSize::ENCODED_SIZE {
745+
((self.0 >> (i * 8)) as u8).encode(e)?;
746+
}
747+
let end_pos = e.position();
748+
assert_eq!((end_pos - start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
749+
Ok(())
750+
}
751+
}
752+
753+
impl<'a> serialize::Decodable<Decoder<'a>> for IntEncodedWithFixedSize {
754+
fn decode(decoder: &mut Decoder<'a>) -> Result<IntEncodedWithFixedSize, String> {
755+
let mut value: u64 = 0;
756+
let start_pos = decoder.position();
757+
758+
for i in 0..IntEncodedWithFixedSize::ENCODED_SIZE {
759+
let byte: u8 = serialize::Decodable::decode(decoder)?;
760+
value |= (byte as u64) << (i * 8);
761+
}
762+
763+
let end_pos = decoder.position();
764+
assert_eq!((end_pos - start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
765+
766+
Ok(IntEncodedWithFixedSize(value))
767+
}
768+
}

0 commit comments

Comments
 (0)