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

Add a macro that evaluates to Option<_> #8

Closed
1 task
yvt opened this issue May 3, 2023 · 2 comments
Closed
1 task

Add a macro that evaluates to Option<_> #8

yvt opened this issue May 3, 2023 · 2 comments
Labels
enhancement New feature or request help wanted Extra attention is needed tracking issue

Comments

@yvt
Copy link
Owner

yvt commented May 3, 2023

This issue proposes to add a variation of try_match! that evaluates to Option instead of Result.

Prior Arts

Issues

  • What should it be called? A few candidates:
    • try_match_ok!($expr, $pat) (try_match! + Result::ok)
    • match_ok!($expr, $pat) (match + Result::ok)
    • get_match!($expr, $pat)

Examples

Iterator::filter_map

let array = [Var1(42), Var2, Var1(10)];
let filtered: Vec<_> = array
    .iter()
    .filter_map(|x| try_match_ok!(x, &Var1(_0) if _0 > 20))
    .collect();
assert_eq!(filtered, [42]);

Combined with #3:

let array = [Var1(42), Var2, Var1(10)];
let filtered: Vec<_> = array
    .iter()
    .filter_map(try_match_ok!(, &Var1(_0) if _0 > 20))
    .collect();
assert_eq!(filtered, [42]);

Extracting Variants

impl<T> Enum<T> {
    fn var1(&self) -> Option<&T> {
        try_match_ok!(self, Var1(_0))
    }

    fn is_var2(&self) -> bool {
        matches!(self, Var2)
    }
}
@yvt yvt added enhancement New feature or request tracking issue labels May 3, 2023
@yvt yvt mentioned this issue May 3, 2023
3 tasks
@yvt
Copy link
Owner Author

yvt commented May 4, 2023

Unstably added in commit f3137df

@yvt yvt added the help wanted Extra attention is needed label May 4, 2023
@yvt
Copy link
Owner Author

yvt commented May 4, 2023

Change of plan: Insta-stabilized the feature in commit 293993e.

@yvt yvt closed this as completed May 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed tracking issue
Projects
None yet
Development

No branches or pull requests

1 participant