1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-01-18 04:23:14 +02:00

Expand "thiserror and anyhow" slide (#1600)

Explain what traits `thiserror::Error` derives.

Explain how `.context()` and `.with_context()` operate on types that are
not aware of `anyhow`.

Resolves #1597.
This commit is contained in:
Pavel Roskin 2023-12-20 07:37:22 -08:00 committed by GitHub
parent 26c088497f
commit e60f56e8f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -41,13 +41,22 @@ fn main() {
<details>
## `thiserror`
* The `Error` derive macro is provided by `thiserror`, and has lots of useful
attributes like `#[error]` to help define a useful error type.
* `anyhow::Result<V>` is a type alias for `Result<V, anyhow::Error>`.
attributes to help define error types in a compact way.
* The `std::error::Error` trait is derived automatically.
* The message from `#[error]` is used to derive the `Display` trait.
## `anyhow`
* `anyhow::Error` is essentially a wrapper around `Box<dyn Error>`. As such it's again generally not
a good choice for the public API of a library, but is widely used in applications.
* `anyhow::Result<V>` is a type alias for `Result<V, anyhow::Error>`.
* Actual error type inside of it can be extracted for examination if necessary.
* Functionality provided by `anyhow::Result<T>` may be familiar to Go developers, as it provides
similar usage patterns and ergonomics to `(T, error)` from Go.
* `anyhow::Context` is a trait implemented for the standard `Result` and `Option` types.
`use anyhow::Context` is necessary to enable `.context()` and `.with_context()` on those types.
</details>