-
Notifications
You must be signed in to change notification settings - Fork 355
Replace cfg_if! macro with cfg_select! fallback
#740
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
Merged
+42
−65
Merged
Changes from all commits
Commits
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
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
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
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 |
|---|---|---|
| @@ -1,70 +1,42 @@ | ||
| // See the cfg-if crate. | ||
| #[expect(unused_macro_rules)] | ||
| macro_rules! cfg_if { | ||
| // match if/else chains with a final `else` | ||
| ($( | ||
| if #[cfg($($meta:meta),*)] { $($it:item)* } | ||
| ) else * else { | ||
| $($it2:item)* | ||
| }) => { | ||
| cfg_if! { | ||
| @__items | ||
| () ; | ||
| $( ( ($($meta),*) ($($it)*) ), )* | ||
| ( () ($($it2)*) ), | ||
| } | ||
| // FIXME: remove once we bump MSRV >= 1.95.0 | ||
| // Originally by @folkertdev on Zulip: | ||
| // https://rust-lang.zulipchat.com/#narrow/channel/219381-t-libs/topic/compiler-builtins.20msrv.20policy.3F/near/606320965 | ||
| /// Fallback `cfg_select!` on older Rust. | ||
| /// | ||
| /// Requires braces around output, but works for items and expressions. | ||
| #[cfg(not(feature = "nightly"))] | ||
| #[macro_export] | ||
| macro_rules! cfg_select { | ||
| ({ $($tt:tt)* }) => {{ | ||
| $crate::cfg_select! { $($tt)* } | ||
| }}; | ||
| (_ => { $($output:tt)* }) => { | ||
| $($output)* | ||
| }; | ||
|
|
||
| // match if/else chains lacking a final `else` | ||
|
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. We actually never used this branch, but I guess we wanted it for completeness. |
||
| ( | ||
| if #[cfg($($i_met:meta),*)] { $($i_it:item)* } | ||
| $( | ||
| else if #[cfg($($e_met:meta),*)] { $($e_it:item)* } | ||
| )* | ||
| $cfg:meta => $output:tt | ||
| $($( $rest:tt )+)? | ||
| ) => { | ||
| cfg_if! { | ||
| @__items | ||
| () ; | ||
| ( ($($i_met),*) ($($i_it)*) ), | ||
| $( ( ($($e_met),*) ($($e_it)*) ), )* | ||
| ( () () ), | ||
| } | ||
| }; | ||
|
|
||
| // Internal and recursive macro to emit all the items | ||
| // | ||
| // Collects all the negated cfgs in a list at the beginning and after the | ||
| // semicolon is all the remaining items | ||
| (@__items ($($not:meta,)*) ; ) => {}; | ||
| (@__items ($($not:meta,)*) ; ( ($($m:meta),*) ($($it:item)*) ), $($rest:tt)*) => { | ||
| // Emit all items within one block, applying an appropriate #[cfg]. The | ||
| // #[cfg] will require all `$m` matchers specified and must also negate | ||
| // all previous matchers. | ||
| cfg_if! { @__apply cfg(all($($m,)* not(any($($not),*)))), $($it)* } | ||
|
|
||
| // Recurse to emit all other items in `$rest`, and when we do so add all | ||
| // our `$m` matchers to the list of `$not` matchers as future emissions | ||
| // will have to negate everything we just matched as well. | ||
| cfg_if! { @__items ($($not,)* $($m,)*) ; $($rest)* } | ||
| }; | ||
|
|
||
| // Internal macro to Apply a cfg attribute to a list of items | ||
| (@__apply $m:meta, $($it:item)*) => { | ||
| $(#[$m] $it)* | ||
| }; | ||
| #[cfg($cfg)] | ||
| $crate::cfg_select! { _ => $output } | ||
| $( | ||
| #[cfg(not($cfg))] | ||
| $crate::cfg_select! { $($rest)+ } | ||
| )? | ||
| } | ||
| } | ||
|
|
||
| // Helper macro for specialization. This also helps avoid parse errors if the | ||
| // default fn syntax for specialization changes in the future. | ||
| #[cfg(feature = "nightly")] | ||
| macro_rules! default_fn { | ||
| (#[$($a:tt)*] $($tt:tt)*) => { | ||
| (#[$($a:tt)*] $($tt:tt)*) => { | ||
| #[$($a)*] default $($tt)* | ||
| } | ||
| } | ||
| #[cfg(not(feature = "nightly"))] | ||
| macro_rules! default_fn { | ||
| ($($tt:tt)*) => { | ||
| ($($tt:tt)*) => { | ||
| $($tt)* | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.