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.
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");
}
- 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.