diff options
| author | Camille GILLOT <gillot.camille@gmail.com> | 2021-03-04 19:24:11 +0100 |
|---|---|---|
| committer | Camille GILLOT <gillot.camille@gmail.com> | 2021-03-18 20:09:00 +0100 |
| commit | 5003b3dc31d422dd568eaa6d8339ecdb0df2c526 (patch) | |
| tree | 8c5fb09d0e436e679ca6f38bda388809bd280c80 | |
| parent | 0ce0fedb67fa66d50aa819ef8b12f1d89eb22d7d (diff) | |
| download | rust-5003b3dc31d422dd568eaa6d8339ecdb0df2c526.tar.gz rust-5003b3dc31d422dd568eaa6d8339ecdb0df2c526.zip | |
Move IntEncodedWithFixedSize to rustc_serialize.
| -rw-r--r-- | compiler/rustc_middle/src/ty/query/on_disk_cache.rs | 38 | ||||
| -rw-r--r-- | 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 78193acc74a..8d2654e2157 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<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>, diff --git a/compiler/rustc_serialize/src/opaque.rs b/compiler/rustc_serialize/src/opaque.rs index 3e37fc87ce6..8d833dbf88e 100644 --- a/compiler/rustc_serialize/src/opaque.rs +++ b/compiler/rustc_serialize/src/opaque.rs @@ -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)) + } +} |
