-
Notifications
You must be signed in to change notification settings - Fork 565
cfg_select! macro
#2103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
folkertdev
wants to merge
2
commits into
rust-lang:master
Choose a base branch
from
folkertdev:cfg-select
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+67
−2
Open
cfg_select! macro
#2103
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,7 +31,7 @@ r[cfg.general] | |
| *Conditionally compiled source code* is source code that is compiled only under certain conditions. | ||
|
|
||
| r[cfg.attributes-macro] | ||
| Source code can be made conditionally compiled using the [`cfg`] and [`cfg_attr`] [attributes] and the built-in [`cfg` macro]. | ||
| Source code can be made conditionally compiled using the [`cfg`] and [`cfg_attr`] [attributes] and the built-in [`cfg!`] and [`cfg_select!`] [macros]. | ||
|
|
||
| r[cfg.conditional] | ||
| Whether to compile can depend on the target architecture of the compiled crate, arbitrary values passed to the compiler, and other things further described below. | ||
|
|
@@ -466,11 +466,75 @@ let machine_kind = if cfg!(unix) { | |
| println!("I'm running on a {} machine!", machine_kind); | ||
| ``` | ||
|
|
||
| r[cfg.cfg_select] | ||
| ### The `cfg_select` macro | ||
|
|
||
| r[cfg.cfg_select.syntax] | ||
| ```grammar,configuration | ||
| CfgSelect -> | ||
| cfg_select! `{` CfgSelectBranch* `}` | ||
|
|
||
| CfgSelectConfigurationPredicate -> | ||
| ConfigurationPredicate | `_` | ||
|
|
||
| CfgSelectBranch -> | ||
| CfgSelectConfigurationPredicate `=>` `{` TokenTree `}` | ||
| | CfgSelectConfigurationPredicate `=>` TokenTree `,` | ||
| ``` | ||
|
|
||
| r[cfg.cfg_select.general] | ||
| The built-in `cfg_select` macro expands to the `TokenTree` on the right-hand side of the first configuration predicate that evaluates to `true`. | ||
|
|
||
| For example: | ||
|
|
||
| ```rust | ||
| cfg_select! { | ||
| unix => { | ||
| fn foo() { /* unix specific functionality */ } | ||
| } | ||
| target_pointer_width = "32" => { | ||
| fn foo() { /* non-unix, 32-bit functionality */ } | ||
| } | ||
| _ => { | ||
| fn foo() { /* fallback implementation */ } | ||
| } | ||
| } | ||
| ``` | ||
| The `cfg_select` macro can also be used in expression position: | ||
|
|
||
| ```rust | ||
| let is_unix_str = cfg_select! { | ||
| unix => "unix", | ||
| _ => "not unix", | ||
| }; | ||
| ``` | ||
|
|
||
| r[cfg.cfg_select.wildcard] | ||
| A `_` can be used to write a configuration predicate that always evaluates to `true`. | ||
|
|
||
| r[cfg.cfg_select.fallthrough] | ||
| If none of the predicates evaluates to `true`, a compiler error is emitted. | ||
|
|
||
| r[cfg.cfg_select.positions] | ||
| The `cfg_select!` macro is accepted in the following macro expansion positions | ||
|
|
||
| - items | ||
| - statements | ||
| - expression | ||
| - impl items | ||
| - trait impl items | ||
| - trait items | ||
| - foreign items | ||
|
Comment on lines
+518
to
+527
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't find the positions in which macro expansion is allowed in the reference, actually. |
||
|
|
||
| r[cfg.cfg_select.well-formed] | ||
| Each right-hand side must syntactically be valid expansion for the position that the macro is invoked in. | ||
|
|
||
| [Testing]: attributes/testing.md | ||
| [`--cfg`]: ../rustc/command-line-arguments.html#--cfg-configure-the-compilation-environment | ||
| [`--test`]: ../rustc/command-line-arguments.html#--test-build-a-test-harness | ||
| [`cfg`]: #the-cfg-attribute | ||
| [`cfg` macro]: #the-cfg-macro | ||
| [`cfg!`]: #the-cfg-macro | ||
| [`cfg_select!`]: #the-cfg_select-macro | ||
| [`cfg_attr`]: #the-cfg_attr-attribute | ||
| [`crate_name`]: crates-and-source-files.md#the-crate_name-attribute | ||
| [`crate_type`]: linkage.md | ||
|
|
@@ -479,4 +543,5 @@ println!("I'm running on a {} machine!", machine_kind); | |
| [attributes]: attributes.md | ||
| [cargo-feature]: ../cargo/reference/features.html | ||
| [crate type]: linkage.md | ||
| [macros]: macros.md | ||
| [static C runtime]: linkage.md#static-and-dynamic-c-runtimes | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it clear enough from the grammer and the language here that the
{ /* ... */ }are dropped?