Use delayed error handling for Encodable
and Encoder
infallible.
There are two impls of the `Encoder` trait: `opaque::Encoder` and `opaque::FileEncoder`. The former encodes into memory and is infallible, the latter writes to file and is fallible. Currently, standard `Result`/`?`/`unwrap` error handling is used, but this is a bit verbose and has non-trivial cost, which is annoying given how rare failures are (especially in the infallible `opaque::Encoder` case). This commit changes how `Encoder` fallibility is handled. All the `emit_*` methods are now infallible. `opaque::Encoder` requires no great changes for this. `opaque::FileEncoder` now implements a delayed error handling strategy. If a failure occurs, it records this via the `res` field, and all subsequent encoding operations are skipped if `res` indicates an error has occurred. Once encoding is complete, the new `finish` method is called, which returns a `Result`. In other words, there is now a single `Result`-producing method instead of many of them. This has very little effect on how any file errors are reported if `opaque::FileEncoder` has any failures. Much of this commit is boring mechanical changes, removing `Result` return values and `?` or `unwrap` from expressions. The more interesting parts are as follows. - serialize.rs: The `Encoder` trait gains an `Ok` associated type. The `into_inner` method is changed into `finish`, which returns `Result<Vec<u8>, !>`. - opaque.rs: The `FileEncoder` adopts the delayed error handling strategy. Its `Ok` type is a `usize`, returning the number of bytes written, replacing previous uses of `FileEncoder::position`. - Various methods that take an encoder now consume it, rather than being passed a mutable reference, e.g. `serialize_query_result_cache`.
This commit is contained in:
parent
582b9cbc45
commit
1acbe7573d
45 changed files with 611 additions and 682 deletions
|
@ -194,12 +194,10 @@ impl Hash for RealFileName {
|
|||
// This is functionally identical to #[derive(Encodable)], with the exception of
|
||||
// an added assert statement
|
||||
impl<S: Encoder> Encodable<S> for RealFileName {
|
||||
fn encode(&self, encoder: &mut S) -> Result<(), S::Error> {
|
||||
fn encode(&self, encoder: &mut S) {
|
||||
match *self {
|
||||
RealFileName::LocalPath(ref local_path) => encoder.emit_enum_variant(0, |encoder| {
|
||||
Ok({
|
||||
local_path.encode(encoder)?;
|
||||
})
|
||||
local_path.encode(encoder);
|
||||
}),
|
||||
|
||||
RealFileName::Remapped { ref local_path, ref virtual_name } => encoder
|
||||
|
@ -207,9 +205,8 @@ impl<S: Encoder> Encodable<S> for RealFileName {
|
|||
// For privacy and build reproducibility, we must not embed host-dependant path in artifacts
|
||||
// if they have been remapped by --remap-path-prefix
|
||||
assert!(local_path.is_none());
|
||||
local_path.encode(encoder)?;
|
||||
virtual_name.encode(encoder)?;
|
||||
Ok(())
|
||||
local_path.encode(encoder);
|
||||
virtual_name.encode(encoder);
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
@ -946,10 +943,10 @@ impl Default for Span {
|
|||
}
|
||||
|
||||
impl<E: Encoder> Encodable<E> for Span {
|
||||
default fn encode(&self, s: &mut E) -> Result<(), E::Error> {
|
||||
default fn encode(&self, s: &mut E) {
|
||||
let span = self.data();
|
||||
span.lo.encode(s)?;
|
||||
span.hi.encode(s)
|
||||
span.lo.encode(s);
|
||||
span.hi.encode(s);
|
||||
}
|
||||
}
|
||||
impl<D: Decoder> Decodable<D> for Span {
|
||||
|
@ -1297,17 +1294,17 @@ pub struct SourceFile {
|
|||
}
|
||||
|
||||
impl<S: Encoder> Encodable<S> for SourceFile {
|
||||
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
||||
self.name.encode(s)?;
|
||||
self.src_hash.encode(s)?;
|
||||
self.start_pos.encode(s)?;
|
||||
self.end_pos.encode(s)?;
|
||||
fn encode(&self, s: &mut S) {
|
||||
self.name.encode(s);
|
||||
self.src_hash.encode(s);
|
||||
self.start_pos.encode(s);
|
||||
self.end_pos.encode(s);
|
||||
|
||||
// We are always in `Lines` form by the time we reach here.
|
||||
assert!(self.lines.borrow().is_lines());
|
||||
self.lines(|lines| {
|
||||
// Store the length.
|
||||
s.emit_u32(lines.len() as u32)?;
|
||||
s.emit_u32(lines.len() as u32);
|
||||
|
||||
// Compute and store the difference list.
|
||||
if lines.len() != 0 {
|
||||
|
@ -1329,10 +1326,10 @@ impl<S: Encoder> Encodable<S> for SourceFile {
|
|||
};
|
||||
|
||||
// Encode the number of bytes used per diff.
|
||||
s.emit_u8(bytes_per_diff as u8)?;
|
||||
s.emit_u8(bytes_per_diff as u8);
|
||||
|
||||
// Encode the first element.
|
||||
lines[0].encode(s)?;
|
||||
lines[0].encode(s);
|
||||
|
||||
// Encode the difference list.
|
||||
let diff_iter = lines.array_windows().map(|&[fst, snd]| snd - fst);
|
||||
|
@ -1359,16 +1356,15 @@ impl<S: Encoder> Encodable<S> for SourceFile {
|
|||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
s.emit_raw_bytes(&raw_diffs)?;
|
||||
s.emit_raw_bytes(&raw_diffs);
|
||||
}
|
||||
Ok(())
|
||||
})?;
|
||||
});
|
||||
|
||||
self.multibyte_chars.encode(s)?;
|
||||
self.non_narrow_chars.encode(s)?;
|
||||
self.name_hash.encode(s)?;
|
||||
self.normalized_pos.encode(s)?;
|
||||
self.cnum.encode(s)
|
||||
self.multibyte_chars.encode(s);
|
||||
self.non_narrow_chars.encode(s);
|
||||
self.name_hash.encode(s);
|
||||
self.normalized_pos.encode(s);
|
||||
self.cnum.encode(s);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1916,8 +1912,8 @@ impl_pos! {
|
|||
}
|
||||
|
||||
impl<S: rustc_serialize::Encoder> Encodable<S> for BytePos {
|
||||
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
|
||||
s.emit_u32(self.0)
|
||||
fn encode(&self, s: &mut S) {
|
||||
s.emit_u32(self.0);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue