diff options
Diffstat (limited to 'compiler/rustc_serialize')
| -rw-r--r-- | compiler/rustc_serialize/src/opaque.rs | 52 | ||||
| -rw-r--r-- | compiler/rustc_serialize/tests/opaque.rs | 10 | 
2 files changed, 32 insertions, 30 deletions
| diff --git a/compiler/rustc_serialize/src/opaque.rs b/compiler/rustc_serialize/src/opaque.rs index b2dbf937eb7..f828c0b7106 100644 --- a/compiler/rustc_serialize/src/opaque.rs +++ b/compiler/rustc_serialize/src/opaque.rs @@ -1,5 +1,5 @@ use crate::leb128::{self, max_leb128_len}; -use crate::serialize::{self, Decoder as _, Encoder as _}; +use crate::serialize::{Decodable, Decoder, Encodable, Encoder}; use std::convert::TryInto; use std::fs::File; use std::io::{self, Write}; @@ -11,13 +11,13 @@ use std::ptr; // Encoder // ----------------------------------------------------------------------------- -pub struct Encoder { +pub struct MemEncoder { pub data: Vec<u8>, } -impl Encoder { - pub fn new() -> Encoder { - Encoder { data: vec![] } +impl MemEncoder { + pub fn new() -> MemEncoder { + MemEncoder { data: vec![] } } #[inline] @@ -53,7 +53,7 @@ macro_rules! write_leb128 { /// [utf8]: https://en.wikipedia.org/w/index.php?title=UTF-8&oldid=1058865525#Codepage_layout const STR_SENTINEL: u8 = 0xC1; -impl serialize::Encoder for Encoder { +impl Encoder for MemEncoder { type Ok = Vec<u8>; type Err = !; @@ -161,7 +161,7 @@ pub type FileEncodeResult = Result<usize, io::Error>; // `FileEncoder` encodes data to file via fixed-size buffer. // // When encoding large amounts of data to a file, using `FileEncoder` may be -// preferred over using `Encoder` to encode to a `Vec`, and then writing the +// preferred over using `MemEncoder` to encode to a `Vec`, and then writing the // `Vec` to file, as the latter uses as much memory as there is encoded data, // while the former uses the fixed amount of memory allocated to the buffer. // `FileEncoder` also has the advantage of not needing to reallocate as data @@ -425,7 +425,7 @@ macro_rules! file_encoder_write_leb128 { }}; } -impl serialize::Encoder for FileEncoder { +impl Encoder for FileEncoder { type Ok = usize; type Err = io::Error; @@ -535,15 +535,15 @@ impl serialize::Encoder for FileEncoder { // Decoder // ----------------------------------------------------------------------------- -pub struct Decoder<'a> { +pub struct MemDecoder<'a> { pub data: &'a [u8], position: usize, } -impl<'a> Decoder<'a> { +impl<'a> MemDecoder<'a> { #[inline] - pub fn new(data: &'a [u8], position: usize) -> Decoder<'a> { - Decoder { data, position } + pub fn new(data: &'a [u8], position: usize) -> MemDecoder<'a> { + MemDecoder { data, position } } #[inline] @@ -566,7 +566,7 @@ macro_rules! read_leb128 { ($dec:expr, $fun:ident) => {{ leb128::$fun($dec.data, &mut $dec.position) }}; } -impl<'a> serialize::Decoder for Decoder<'a> { +impl<'a> Decoder for MemDecoder<'a> { #[inline] fn read_u128(&mut self) -> u128 { read_leb128!(self, read_u128_leb128) @@ -688,25 +688,25 @@ impl<'a> serialize::Decoder for Decoder<'a> { // Specialize encoding byte slices. This specialization also applies to encoding `Vec<u8>`s, etc., // since the default implementations call `encode` on their slices internally. -impl serialize::Encodable<Encoder> for [u8] { - fn encode(&self, e: &mut Encoder) { - serialize::Encoder::emit_usize(e, self.len()); +impl Encodable<MemEncoder> for [u8] { + fn encode(&self, e: &mut MemEncoder) { + Encoder::emit_usize(e, self.len()); e.emit_raw_bytes(self); } } -impl serialize::Encodable<FileEncoder> for [u8] { +impl Encodable<FileEncoder> for [u8] { fn encode(&self, e: &mut FileEncoder) { - serialize::Encoder::emit_usize(e, self.len()); + Encoder::emit_usize(e, self.len()); e.emit_raw_bytes(self); } } // Specialize decoding `Vec<u8>`. This specialization also applies to decoding `Box<[u8]>`s, etc., // since the default implementations call `decode` to produce a `Vec<u8>` internally. -impl<'a> serialize::Decodable<Decoder<'a>> for Vec<u8> { - fn decode(d: &mut Decoder<'a>) -> Self { - let len = serialize::Decoder::read_usize(d); +impl<'a> Decodable<MemDecoder<'a>> for Vec<u8> { + fn decode(d: &mut MemDecoder<'a>) -> Self { + let len = Decoder::read_usize(d); d.read_raw_bytes(len).to_owned() } } @@ -718,9 +718,9 @@ impl IntEncodedWithFixedSize { pub const ENCODED_SIZE: usize = 8; } -impl serialize::Encodable<Encoder> for IntEncodedWithFixedSize { +impl Encodable<MemEncoder> for IntEncodedWithFixedSize { #[inline] - fn encode(&self, e: &mut Encoder) { + fn encode(&self, e: &mut MemEncoder) { let _start_pos = e.position(); e.emit_raw_bytes(&self.0.to_le_bytes()); let _end_pos = e.position(); @@ -728,7 +728,7 @@ impl serialize::Encodable<Encoder> for IntEncodedWithFixedSize { } } -impl serialize::Encodable<FileEncoder> for IntEncodedWithFixedSize { +impl Encodable<FileEncoder> for IntEncodedWithFixedSize { #[inline] fn encode(&self, e: &mut FileEncoder) { let _start_pos = e.position(); @@ -738,9 +738,9 @@ impl serialize::Encodable<FileEncoder> for IntEncodedWithFixedSize { } } -impl<'a> serialize::Decodable<Decoder<'a>> for IntEncodedWithFixedSize { +impl<'a> Decodable<MemDecoder<'a>> for IntEncodedWithFixedSize { #[inline] - fn decode(decoder: &mut Decoder<'a>) -> IntEncodedWithFixedSize { + fn decode(decoder: &mut MemDecoder<'a>) -> IntEncodedWithFixedSize { let _start_pos = decoder.position(); let bytes = decoder.read_raw_bytes(IntEncodedWithFixedSize::ENCODED_SIZE); let value = u64::from_le_bytes(bytes.try_into().unwrap()); diff --git a/compiler/rustc_serialize/tests/opaque.rs b/compiler/rustc_serialize/tests/opaque.rs index 703b7f5e7a5..4eafb6fabde 100644 --- a/compiler/rustc_serialize/tests/opaque.rs +++ b/compiler/rustc_serialize/tests/opaque.rs @@ -1,7 +1,7 @@ #![allow(rustc::internal)] use rustc_macros::{Decodable, Encodable}; -use rustc_serialize::opaque::{Decoder, Encoder}; +use rustc_serialize::opaque::{MemDecoder, MemEncoder}; use rustc_serialize::{Decodable, Encodable, Encoder as EncoderTrait}; use std::fmt::Debug; @@ -28,16 +28,18 @@ struct Struct { q: Option<u32>, } -fn check_round_trip<T: Encodable<Encoder> + for<'a> Decodable<Decoder<'a>> + PartialEq + Debug>( +fn check_round_trip< + T: Encodable<MemEncoder> + for<'a> Decodable<MemDecoder<'a>> + PartialEq + Debug, +>( values: Vec<T>, ) { - let mut encoder = Encoder::new(); + let mut encoder = MemEncoder::new(); for value in &values { Encodable::encode(value, &mut encoder); } let data = encoder.finish().unwrap(); - let mut decoder = Decoder::new(&data[..], 0); + let mut decoder = MemDecoder::new(&data[..], 0); for value in values { let decoded = Decodable::decode(&mut decoder); | 
