diff options
| author | Nicholas Nethercote <n.nethercote@gmail.com> | 2022-06-07 13:30:45 +1000 |
|---|---|---|
| committer | Nicholas Nethercote <n.nethercote@gmail.com> | 2022-06-08 07:01:26 +1000 |
| commit | 1acbe7573dc32f917f51a784a36b7afc690900e3 (patch) | |
| tree | 1b4e8d414de3d349bd3ae038c28e9dc77e54c345 /compiler/rustc_macros/src/serialize.rs | |
| parent | 582b9cbc45334a73467d6ccaf0a8b9de559c2011 (diff) | |
| download | rust-1acbe7573dc32f917f51a784a36b7afc690900e3.tar.gz rust-1acbe7573dc32f917f51a784a36b7afc690900e3.zip | |
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`.
Diffstat (limited to 'compiler/rustc_macros/src/serialize.rs')
| -rw-r--r-- | compiler/rustc_macros/src/serialize.rs | 22 |
1 files changed, 7 insertions, 15 deletions
diff --git a/compiler/rustc_macros/src/serialize.rs b/compiler/rustc_macros/src/serialize.rs index e99fa6c113b..82e6972d027 100644 --- a/compiler/rustc_macros/src/serialize.rs +++ b/compiler/rustc_macros/src/serialize.rs @@ -146,21 +146,17 @@ fn encodable_body( .map(|binding| { let bind_ident = &binding.binding; let result = quote! { - match ::rustc_serialize::Encodable::<#encoder_ty>::encode( + ::rustc_serialize::Encodable::<#encoder_ty>::encode( #bind_ident, __encoder, - ) { - ::std::result::Result::Ok(()) => (), - ::std::result::Result::Err(__err) - => return ::std::result::Result::Err(__err), - } + ); }; result }) .collect::<TokenStream>() }); quote! { - ::std::result::Result::Ok(match *self { #encode_inner }) + match *self { #encode_inner } } } _ => { @@ -172,14 +168,10 @@ fn encodable_body( .map(|binding| { let bind_ident = &binding.binding; let result = quote! { - match ::rustc_serialize::Encodable::<#encoder_ty>::encode( + ::rustc_serialize::Encodable::<#encoder_ty>::encode( #bind_ident, __encoder, - ) { - ::std::result::Result::Ok(()) => (), - ::std::result::Result::Err(__err) - => return ::std::result::Result::Err(__err), - } + ); }; result }) @@ -190,7 +182,7 @@ fn encodable_body( ::rustc_serialize::Encoder::emit_enum_variant( __encoder, #variant_idx, - |__encoder| { ::std::result::Result::Ok({ #encode_fields }) } + |__encoder| { #encode_fields } ) } } else { @@ -223,7 +215,7 @@ fn encodable_body( fn encode( &self, __encoder: &mut #encoder_ty, - ) -> ::std::result::Result<(), <#encoder_ty as ::rustc_serialize::Encoder>::Error> { + ) { #lints #encode_body } |
