diff options
| author | Nicholas Nethercote <n.nethercote@gmail.com> | 2022-06-10 11:58:29 +1000 |
|---|---|---|
| committer | Nicholas Nethercote <n.nethercote@gmail.com> | 2022-06-10 11:58:29 +1000 |
| commit | 3186e311e526e3dad197dacc91c2d84cde2be846 (patch) | |
| tree | 9a8b86ea039a286af6d64215920531f8b372702e /compiler/rustc_serialize | |
| parent | 7f51a1b97638780a3d22979d886384ad7903cc4e (diff) | |
| download | rust-3186e311e526e3dad197dacc91c2d84cde2be846.tar.gz rust-3186e311e526e3dad197dacc91c2d84cde2be846.zip | |
Revert dc08bc51f2c58a0f5f815a07f9bb3d671153b5a1.
Diffstat (limited to 'compiler/rustc_serialize')
| -rw-r--r-- | compiler/rustc_serialize/src/opaque.rs | 28 | ||||
| -rw-r--r-- | compiler/rustc_serialize/src/serialize.rs | 12 | ||||
| -rw-r--r-- | compiler/rustc_serialize/tests/opaque.rs | 4 |
3 files changed, 28 insertions, 16 deletions
diff --git a/compiler/rustc_serialize/src/opaque.rs b/compiler/rustc_serialize/src/opaque.rs index 88e52397297..b2dbf937eb7 100644 --- a/compiler/rustc_serialize/src/opaque.rs +++ b/compiler/rustc_serialize/src/opaque.rs @@ -24,10 +24,6 @@ impl Encoder { pub fn position(&self) -> usize { self.data.len() } - - pub fn finish(self) -> Vec<u8> { - self.data - } } macro_rules! write_leb128 { @@ -58,6 +54,9 @@ macro_rules! write_leb128 { const STR_SENTINEL: u8 = 0xC1; impl serialize::Encoder for Encoder { + type Ok = Vec<u8>; + type Err = !; + #[inline] fn emit_usize(&mut self, v: usize) { write_leb128!(self, v, usize, write_usize_leb128) @@ -151,6 +150,10 @@ impl serialize::Encoder for Encoder { fn emit_raw_bytes(&mut self, s: &[u8]) { self.data.extend_from_slice(s); } + + fn finish(self) -> Result<Self::Ok, Self::Err> { + Ok(self.data) + } } pub type FileEncodeResult = Result<usize, io::Error>; @@ -386,13 +389,6 @@ impl FileEncoder { } } } - - pub fn finish(mut self) -> Result<usize, io::Error> { - self.flush(); - - let res = std::mem::replace(&mut self.res, Ok(())); - res.map(|()| self.position()) - } } impl Drop for FileEncoder { @@ -430,6 +426,9 @@ macro_rules! file_encoder_write_leb128 { } impl serialize::Encoder for FileEncoder { + type Ok = usize; + type Err = io::Error; + #[inline] fn emit_usize(&mut self, v: usize) { file_encoder_write_leb128!(self, v, usize, write_usize_leb128) @@ -523,6 +522,13 @@ impl serialize::Encoder for FileEncoder { fn emit_raw_bytes(&mut self, s: &[u8]) { self.write_all(s); } + + fn finish(mut self) -> Result<usize, io::Error> { + self.flush(); + + let res = std::mem::replace(&mut self.res, Ok(())); + res.map(|()| self.position()) + } } // ----------------------------------------------------------------------------- diff --git a/compiler/rustc_serialize/src/serialize.rs b/compiler/rustc_serialize/src/serialize.rs index 36585b8d77e..98bb18581f5 100644 --- a/compiler/rustc_serialize/src/serialize.rs +++ b/compiler/rustc_serialize/src/serialize.rs @@ -18,10 +18,13 @@ use std::sync::Arc; /// is pervasive and has non-trivial cost. Instead, impls of this trait must /// implement a delayed error handling strategy. If a failure occurs, they /// should record this internally, and all subsequent encoding operations can -/// be processed or ignored, whichever is appropriate. Then they should provide -/// a `finish` method that finishes up encoding. If the encoder is fallible, -/// `finish` should return a `Result` that indicates success or failure. +/// be processed or ignored, whichever is appropriate. Then when `finish()` is +/// called, an error result should be returned to indicate the failure. If no +/// failures occurred, then `finish()` should return a success result. pub trait Encoder { + type Ok; + type Err; + // Primitive types: fn emit_usize(&mut self, v: usize); fn emit_u128(&mut self, v: u128); @@ -61,6 +64,9 @@ pub trait Encoder { fn emit_fieldless_enum_variant<const ID: usize>(&mut self) { self.emit_usize(ID) } + + // Consume the encoder, getting the result. + fn finish(self) -> Result<Self::Ok, Self::Err>; } // Note: all the methods in this trait are infallible, which may be surprising. diff --git a/compiler/rustc_serialize/tests/opaque.rs b/compiler/rustc_serialize/tests/opaque.rs index 5ed6fc769cc..703b7f5e7a5 100644 --- a/compiler/rustc_serialize/tests/opaque.rs +++ b/compiler/rustc_serialize/tests/opaque.rs @@ -2,7 +2,7 @@ use rustc_macros::{Decodable, Encodable}; use rustc_serialize::opaque::{Decoder, Encoder}; -use rustc_serialize::{Decodable, Encodable}; +use rustc_serialize::{Decodable, Encodable, Encoder as EncoderTrait}; use std::fmt::Debug; #[derive(PartialEq, Clone, Debug, Encodable, Decodable)] @@ -36,7 +36,7 @@ fn check_round_trip<T: Encodable<Encoder> + for<'a> Decodable<Decoder<'a>> + Par Encodable::encode(value, &mut encoder); } - let data = encoder.finish(); + let data = encoder.finish().unwrap(); let mut decoder = Decoder::new(&data[..], 0); for value in values { |
