question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MCP: Deref Patterns

See original GitHub issue

Proposal

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:

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:closed
  • Created 3 years ago
  • Reactions:9
  • Comments:13 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
rfcbotcommented, Mar 2, 2021

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.

1reaction
nikomatsakiscommented, Feb 26, 2021

well we can do it by hand for now

Read more comments on GitHub >

github_iconTop Results From Across the Web

Somewhat Random Idea: Deref patterns - Rust Internals
Currently there is no way to match a type through a deref boundery, without introducing nested matches. The one exception is Box with ......
Read more >
Deref Patterns, requirements and Syntax Tracking Document
Deref Patterns, requirements and Syntax Tracking Document Discussion on irlo: ... Be able to match through a Deref or DerefMut smart pointer ergonomically....
Read more >
Deref Polymorphism - Rust Design Patterns
A catalogue of Rust design patterns, anti-patterns and idioms.
Read more >
C Programming Reference Manual - Unisys
The Transaction Server for ClearPath MCP Programming Guide ... bit patterns in any unused portions or “holes” in the structure. Union to Union....
Read more >
Discovering Patterns and Anomalies in Graphs with Discrete ...
ing typical patterns in graph-based data, which we use as the basis ... LV(j,la), are used to dereference the row and column of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found