Zerthox / deref-pat

Rust macro for dereferencing in patterns.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

deref_pat

A Rust macro allowing to dereference struct fields in patterns.

You can use the macro to match on fields containing Box, Rc, String, Vec and everything else that implements Deref/DerefMut. In the special case of Box even matching on owned values is supported.

Usage

deref_pat! {
    if let Foo { #[deref] string: bound @ "foo" } = &foo {
        assert_eq!(bound, "foo");
    } else {
        panic!("did not match");
    }
}

The generated code looks something like:

if let Some(bound) = {
    let mut result = None;
    if let Foo { string } = &foo {
        if let bound @ "foo" = PatDeref::pat_deref(string) {
            result = Some(bound);
        }
    }
    result
} {
    assert_eq!(bound, "foo");
} else {
    panic!("did not match");
}

Notes

  • The deref_pat crate must be in scope under this exact name. Supplying a custom name is not yet supported.
  • Only supports if let expressions. match expression are not yet supported.

About

Rust macro for dereferencing in patterns.

License:MIT License


Languages

Language:Rust 100.0%