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

An attribute for TryUnwrap to simplify its return type #257

Open
rassvet2 opened this issue Apr 15, 2023 · 1 comment
Open

An attribute for TryUnwrap to simplify its return type #257

rassvet2 opened this issue Apr 15, 2023 · 1 comment

Comments

@rassvet2
Copy link
Contributor

I made TryUnwrap returns a dedicated error as Err, but in practice, I guess there are cases where this is not desired. Sometimes, you will only be interested in the input value returned. (And it's exactly me)
I think it's a good idea to add an attribute such as #[try_unwrap(simple_return_type)] to make try_unwrap_thing returns Result<(A, B, ...), Self> instead of Result<(A, B, ...), TryUnwrapError<Self>>.

Example

#[derive(TryUnwrap)]
#[try_unwrap(ref, simple_return_type)]
// or #[try_unwrap(simple_error)]
// or #[try_unwrap(return_type = simple)]
// or... #[try_unwrap(return_type = detailed)] to enable TryUnwrapError?
enum Maybe<T> {
    Just(T),
    Nothing
}

fn main() {
    match Maybe::Just("lorem").try_unwrap_nothing() {
        Ok(()) => println!("nothing"),
        Err(val) => println!("{}", val.try_unwrap_just().unwrap()), // => "lorem"
    }
}
@JelteF
Copy link
Owner

JelteF commented Dec 19, 2023

Is it really that big of a deal to access the .input field of the error? I'm not opposed to this though feel free to propose a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants