Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Encodable and Encoder infallible. #94732

Merged
merged 5 commits into from
Jun 8, 2022

Commits on Jun 7, 2022

  1. Configuration menu
    Copy the full SHA
    92b1ab8 View commit details
    Browse the repository at this point in the history
  2. Don't pass in a vector to Encoder::new.

    It's not necessary.
    nnethercote committed Jun 7, 2022
    Configuration menu
    Copy the full SHA
    582b9cb View commit details
    Browse the repository at this point in the history
  3. 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`.
    nnethercote committed Jun 7, 2022
    Configuration menu
    Copy the full SHA
    1acbe75 View commit details
    Browse the repository at this point in the history
  4. Move finish out of the Encoder trait.

    This simplifies things, but requires making `CacheEncoder` non-generic.
    nnethercote committed Jun 7, 2022
    Configuration menu
    Copy the full SHA
    dc08bc5 View commit details
    Browse the repository at this point in the history
  5. Rename rustc_serialize::opaque::Encoder as MemEncoder.

    This avoids the name clash with `rustc_serialize::Encoder` (a trait),
    and allows lots qualifiers to be removed and imports to be simplified
    (e.g. fewer `as` imports).
    nnethercote committed Jun 7, 2022
    Configuration menu
    Copy the full SHA
    b983e42 View commit details
    Browse the repository at this point in the history