Skip to content

Make error types usable in more contexts #1297

Closed
@joshlf

Description

@joshlf

Per #1288 (comment):

Error types like SizeError do not implement std::error::Error, so the functions like read_from_bytes() that now return a Result can't be used with anyhow::Context, while the previous version that returned Option could. Since Debug and Display are already implemented, I think this should just be a matter of adding impl std::error::Error for SizeError {}, but I didn't actually try this out. It may still not be possible to call .context() due to other constraints (Send + Sync). This is easy enough to work around (assuming the caller doesn't really care about the underlying error), and we didn't use this pattern in many places, so it's not really a big deal for us.

Implementing std::error::Error is implemented in #1298.

Supporting Send + Sync may be harder, since the contained Src type may not be Send or Sync. We may need to provide a way to discard the Src type, and we will need to consider how discoverable this mechanism is for people who are encountering a trait-not-satisfied error. I could see a few options:

  • Provide an Error<Src> -> Error<()> conversion
  • Provide a new type alias type Foo = Error<()> so that type signatures are cleaner
  • Provide an entirely new type which contains similar information to the original error, but without the source
  • Provide an entirely new type which contains the name of the source type, but doesn't contain an instance of the source type (just like we do for Dst right now)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions