puffin-client: rejigger error type (#1102)

This PR changes the error type to be boxed internally so that it uses
less size on the stack. This makes functions returning `Result<T,
Error>`, in particular, return something much smaller.

The specific thing that motivated this was Clippy lints firing when I
tried to refactor code in this crate.

I chose to achieve boxing by splitting the enum out into a separate
type, and then wiring up the necessary `From` impl to make error
conversions easy, and then making `Error` itself opaque. We could expose
the `Box`, but there isn't a ton of benefit in doing so because one
cannot pattern match through a `Box`.

This required using more explicit error conversions in several places.
And as a result, I was able to remove all `#[from]` attributes on
non-transparent error variants.
This commit is contained in:
Andrew Gallant 2024-01-25 13:13:21 -05:00 committed by GitHub
parent 3e86c80874
commit 067acfe79e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 194 additions and 109 deletions

View file

@ -105,17 +105,17 @@ impl<'a, Context: BuildContext + Send + Sync> ResolverProvider
self.flat_index.get(package_name).cloned(),
self.no_binary,
)),
Err(
err @ (puffin_client::Error::PackageNotFound(_)
| puffin_client::Error::NoIndex(_)),
) => {
if let Some(flat_index) = self.flat_index.get(package_name).cloned() {
Ok(VersionMap::from(flat_index))
} else {
Err(err)
Err(err) => match err.into_kind() {
kind @ (puffin_client::ErrorKind::PackageNotFound(_)
| puffin_client::ErrorKind::NoIndex(_)) => {
if let Some(flat_index) = self.flat_index.get(package_name).cloned() {
Ok(VersionMap::from(flat_index))
} else {
Err(kind.into())
}
}
}
Err(err) => Err(err),
kind => Err(kind.into()),
},
})
}