Crates.io | strict_result |
lib.rs | strict_result |
version | 1.2.0 |
source | src |
created_at | 2022-12-08 22:38:37.681506 |
updated_at | 2023-11-08 02:07:48.930658 |
description | Adds a `Result::strict()?` function to help with type inference |
homepage | |
repository | https://github.com/Kyuuhachi/strict_result |
max_upload_size | |
id | 732866 |
size | 7,296 |
The ?
operator on Result
has an implicit .into()
on the error type, to make it easier to
upcast errors into broader error types. This however can sometimes cause problems for type
inference in highly generic contexts, since it effectively turns into .into().into()
, which is
ambiguous. To combat this, this crate defines a separate .strict()?
operator, which does not
perform this implicit .into()
.
For an example, let's define a simple generic function:
fn passthrough<T>(f: impl FnOnce() -> T) -> T {
f()
}
If we try to use this combined with the ?
operator, we will get an error because the generic
<T>
cannot be determined.
passthrough(|| {
std::fs::create_dir("example")?;
Ok(()) // cannot infer type of the type parameter `E` declared on the enum `Result`
})?;
In this case we can use .strict()?
to require that the error type is equal to the outer one.
use strict_result::Strict;
passthrough(|| {
std::fs::create_dir("example")?;
Ok(())
}).strict()?;
This crate uses the try_trait_v2
feature, and thus requires nightly.