From 5003b3dc31d422dd568eaa6d8339ecdb0df2c526 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Thu, 4 Mar 2021 19:24:11 +0100 Subject: [PATCH] Move IntEncodedWithFixedSize to rustc_serialize. --- .../src/ty/query/on_disk_cache.rs | 38 +-------------- compiler/rustc_serialize/src/opaque.rs | 48 +++++++++++++++++++ 2 files changed, 49 insertions(+), 37 deletions(-) diff --git a/compiler/rustc_middle/src/ty/query/on_disk_cache.rs b/compiler/rustc_middle/src/ty/query/on_disk_cache.rs index 78193acc74ac..8d2654e2157b 100644 --- a/compiler/rustc_middle/src/ty/query/on_disk_cache.rs +++ b/compiler/rustc_middle/src/ty/query/on_disk_cache.rs @@ -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> for [u8] { } } -// An integer that will always encode to 8 bytes. -struct IntEncodedWithFixedSize(u64); - -impl IntEncodedWithFixedSize { - pub const ENCODED_SIZE: usize = 8; -} - -impl Encodable 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> for IntEncodedWithFixedSize { - fn decode(decoder: &mut opaque::Decoder<'a>) -> Result { - 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>, diff --git a/compiler/rustc_serialize/src/opaque.rs b/compiler/rustc_serialize/src/opaque.rs index 3e37fc87ce6f..8d833dbf88e2 100644 --- a/compiler/rustc_serialize/src/opaque.rs +++ b/compiler/rustc_serialize/src/opaque.rs @@ -718,3 +718,51 @@ impl<'a> serialize::Decodable> for Vec { 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 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 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> for IntEncodedWithFixedSize { + fn decode(decoder: &mut Decoder<'a>) -> Result { + 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)) + } +}