MCP: Deref Patterns
See original GitHub issueProposal
Allow pattern matching through types that impl Deref
or DerefMut
.
Summary and problem statement
Currently in rust, matching is blocked by bounderies like smart pointers, containers, and some wrappers. To solve this problem you would need to either use if let guards (unstable), or nested match/if-let. The former is limited to one such level, and the latter can become excessive for deeply nested types. To solve this, I propose that “deref patterns” be added, to allow for such matching to be performed.
An exception to the above problem, is that Box<T>
can be matched with feature(box_patterns)
. However, this is magic behaviour of box, and I am not a fan of this kind of magic.
Motivation, use-cases, and solution sketches
Recursive types necessarily include smart pointers, even when you could normally match through them.
For example, in a proc-macro I worked on to support restricted variadic generics, I wanted to match “fold expressions”, which take the form (<pattern> <op> ...)
, so I would need to match against Expr::Paren(ParenExpr{expr: Expr::Binary(ExprBinary{ left, op, right: Expr::Verbaitum(t), ..}), ..})
. However, this is currently not possible, and required nested matches. This generalizes to any case where you need to check some pattern, but hit a deref boundery.
Prioritization
I do not believe this fits into any of the listed priorities. It may be considered “Targeted ergonomic wins and extensions”, however, I believe it is a larger than is intended for the category.
Links and related work
This has been discussed on the Rust Internals Forum at https://internals.rust-lang.org/t/somewhat-random-idea-deref-patterns/13813, as well as on zulip at https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/Deref.20patterns.
A tracking document of all currently discussed questions and potential answers can be found here https://hackmd.io/GBTt4ptjTh219SBhDCPO4A.
Prior discussions raised on the IRLO thread:
- https://github.com/rust-lang/rfcs/pull/462
- https://github.com/rust-lang/rfcs/issues/2099
- https://github.com/rust-lang/rfcs/blob/master/text/0809-box-and-in-for-stdlib.md
Initial people involved
I would be involved initially, as well as Nadreiril on zulip. I would be open to anyone who wished to helping with it.
This issue is not meant to be used for technical discussion. There is a Zulip stream for that. Use this issue to leave procedural comments, such as volunteering to review, indicating that you second the proposal (or third, etc), or raising a concern that you would like to be addressed.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:9
- Comments:13 (9 by maintainers)
Team member @nikomatsakis has proposed to merge this. The next step is review by the rest of the tagged team members:
No concerns currently listed.
Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn’t been raised at any point in this process, please speak up!
See this document for info about what commands tagged team members can give me.
well we can do it by hand for now