1
Fork 0

Move IntEncodedWithFixedSize to rustc_serialize.

This commit is contained in:
Camille GILLOT 2021-03-04 19:24:11 +01:00
parent 0ce0fedb67
commit 5003b3dc31
2 changed files with 49 additions and 37 deletions

View file

@ -17,7 +17,7 @@ use rustc_index::vec::{Idx, IndexVec};
use rustc_query_system::dep_graph::DepContext;
use rustc_query_system::query::QueryContext;
use rustc_serialize::{
opaque::{self, FileEncodeResult, FileEncoder},
opaque::{self, FileEncodeResult, FileEncoder, IntEncodedWithFixedSize},
Decodable, Decoder, Encodable, Encoder,
};
use rustc_session::{CrateDisambiguator, Session};
@ -1180,42 +1180,6 @@ impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx, FileEncoder>> for [u8] {
}
}
// An integer that will always encode to 8 bytes.
struct IntEncodedWithFixedSize(u64);
impl IntEncodedWithFixedSize {
pub const ENCODED_SIZE: usize = 8;
}
impl<E: OpaqueEncoder> Encodable<E> for IntEncodedWithFixedSize {
fn encode(&self, e: &mut E) -> Result<(), E::Error> {
let start_pos = e.position();
for i in 0..IntEncodedWithFixedSize::ENCODED_SIZE {
((self.0 >> (i * 8)) as u8).encode(e)?;
}
let end_pos = e.position();
assert_eq!((end_pos - start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
Ok(())
}
}
impl<'a> Decodable<opaque::Decoder<'a>> for IntEncodedWithFixedSize {
fn decode(decoder: &mut opaque::Decoder<'a>) -> Result<IntEncodedWithFixedSize, String> {
let mut value: u64 = 0;
let start_pos = decoder.position();
for i in 0..IntEncodedWithFixedSize::ENCODED_SIZE {
let byte: u8 = Decodable::decode(decoder)?;
value |= (byte as u64) << (i * 8);
}
let end_pos = decoder.position();
assert_eq!((end_pos - start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
Ok(IntEncodedWithFixedSize(value))
}
}
pub fn encode_query_results<'a, 'tcx, CTX, Q>(
tcx: CTX,
encoder: &mut CacheEncoder<'a, 'tcx, FileEncoder>,

View file

@ -718,3 +718,51 @@ impl<'a> serialize::Decodable<Decoder<'a>> for Vec<u8> {
Ok(v)
}
}
// An integer that will always encode to 8 bytes.
pub struct IntEncodedWithFixedSize(pub u64);
impl IntEncodedWithFixedSize {
pub const ENCODED_SIZE: usize = 8;
}
impl serialize::Encodable<Encoder> for IntEncodedWithFixedSize {
fn encode(&self, e: &mut Encoder) -> EncodeResult {
let start_pos = e.position();
for i in 0..IntEncodedWithFixedSize::ENCODED_SIZE {
((self.0 >> (i * 8)) as u8).encode(e)?;
}
let end_pos = e.position();
assert_eq!((end_pos - start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
Ok(())
}
}
impl serialize::Encodable<FileEncoder> for IntEncodedWithFixedSize {
fn encode(&self, e: &mut FileEncoder) -> FileEncodeResult {
let start_pos = e.position();
for i in 0..IntEncodedWithFixedSize::ENCODED_SIZE {
((self.0 >> (i * 8)) as u8).encode(e)?;
}
let end_pos = e.position();
assert_eq!((end_pos - start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
Ok(())
}
}
impl<'a> serialize::Decodable<Decoder<'a>> for IntEncodedWithFixedSize {
fn decode(decoder: &mut Decoder<'a>) -> Result<IntEncodedWithFixedSize, String> {
let mut value: u64 = 0;
let start_pos = decoder.position();
for i in 0..IntEncodedWithFixedSize::ENCODED_SIZE {
let byte: u8 = serialize::Decodable::decode(decoder)?;
value |= (byte as u64) << (i * 8);
}
let end_pos = decoder.position();
assert_eq!((end_pos - start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
Ok(IntEncodedWithFixedSize(value))
}
}