snafu icon indicating copy to clipboard operation
snafu copied to clipboard

Consider adding simple pattern matching to the ensure macros

Open shepmaster opened this issue 1 year ago • 1 comments

@Kyuuhachi says:

I like to include that clause when I make a project-local ensure macro

"That clause" means something like this:

ensure!(let Some(x) = y, SomeSnafu);

This could expand into something like

let Some(x) = y else { return SomeSnafu.fail() };

To consider is if there's a way to do it with something besides let-else, which only stabilized in 1.65.

shepmaster avatar Aug 13 '24 15:08 shepmaster

To consider is if there's a way to do it with something besides let-else, which only stabilized in 1.65.

(Just passing through, leaving my 2 cents) Isn't let Some(x) = y else { return SomeSnafu.fail() }; just sugar for

let x = match y {
  Some(x) => x,
  _ =>  return SomeSnafu.fail(),
}

But that would probably require a more sophisticated macro definition, as it would need to deconstruct the "that clause"

kelko avatar Sep 18 '24 19:09 kelko