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
|
@ -41,8 +41,8 @@ impl fmt::Display for CrateNum {
|
|||
/// As a local identifier, a `CrateNum` is only meaningful within its context, e.g. within a tcx.
|
||||
/// Therefore, make sure to include the context when encode a `CrateNum`.
|
||||
impl<E: Encoder> Encodable<E> for CrateNum {
|
||||
default fn encode(&self, s: &mut E) -> Result<(), E::Error> {
|
||||
s.emit_u32(self.as_u32())
|
||||
default fn encode(&self, s: &mut E) {
|
||||
s.emit_u32(self.as_u32());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -203,7 +203,7 @@ rustc_index::newtype_index! {
|
|||
}
|
||||
|
||||
impl<E: Encoder> Encodable<E> for DefIndex {
|
||||
default fn encode(&self, _: &mut E) -> Result<(), E::Error> {
|
||||
default fn encode(&self, _: &mut E) {
|
||||
panic!("cannot encode `DefIndex` with `{}`", std::any::type_name::<E>());
|
||||
}
|
||||
}
|
||||
|
@ -306,9 +306,9 @@ impl DefId {
|
|||
}
|
||||
|
||||
impl<E: Encoder> Encodable<E> for DefId {
|
||||
default fn encode(&self, s: &mut E) -> Result<(), E::Error> {
|
||||
self.krate.encode(s)?;
|
||||
self.index.encode(s)
|
||||
default fn encode(&self, s: &mut E) {
|
||||
self.krate.encode(s);
|
||||
self.index.encode(s);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -382,8 +382,8 @@ impl fmt::Debug for LocalDefId {
|
|||
}
|
||||
|
||||
impl<E: Encoder> Encodable<E> for LocalDefId {
|
||||
fn encode(&self, s: &mut E) -> Result<(), E::Error> {
|
||||
self.to_def_id().encode(s)
|
||||
fn encode(&self, s: &mut E) {
|
||||
self.to_def_id().encode(s);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue