Add the matches!( $expr, $pat ) -> bool macro#65479
Merged
bors merged 4 commits intorust-lang:masterfrom Oct 24, 2019
Merged
Conversation
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
This macro is:
As such, I feel it is a good candidate for inclusion in the standard library.
In fact I already felt that way five years ago: #14685 (Although the proof of popularity was not as strong at the time.)
API
Details
Back then, the main concern was that this macro may not be quite universally-enough useful to belong in the prelude.
Therefore, this PR adds the macro such that using it requires one of:
Like arms of a
matchexpression, the macro supports multiple patterns separated by|and optionally followed byifand a guard expression:Details
Implementation constraints
A combination of reasons make it tricky for a standard library macro not to be in the prelude.
Currently, all public
macro_rulesmacros in the standard library macros end up “in the prelude” of every crate not throughuse std::prelude::v1::*;like for other kinds of items, but through#[macro_use]onextern crate std;. (Both are injected bysrc/libsyntax_ext/standard_library_imports.rs.)#[macro_use]seems to import every macro that is available at the top-level of a crate, even if through apub usere-export.Therefore, for
matches!not to be in the prelude, we need it to be inside of a module rather than at the root ofcoreorstd.However, the only way to make a
macro_rulesmacro public outside of the crate where it is defined appears to be#[macro_export]. This exports the macro at the root of the crate regardless of which module defines it. See macro scoping in the reference.Therefore, the macro needs to be defined in a crate that is not
coreorstd.Implementation
This PR adds a new
matches_macrocrate as a private implementation detail of the standard library. This crate is#![no_core]so that libcore can depend on it. It contains amacro_rulesdefinition with#[macro_export].libcore and libstd each have a new public
macrosmodule that contains apub usere-export of the macro. Both the module and the macro are unstable, for now.The existing private
macrosmodules are renamedprelude_macros, though their respective source remains inmacros.rsfiles.