-
Notifications
You must be signed in to change notification settings - Fork 85
define cfg_if! and cfg_if_expr! internal macros #438
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
cosmicexplorer
wants to merge
1
commit into
zip-rs:master
Choose a base branch
from
cosmicexplorer:cfg-if-internal
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.
Open
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
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 |
---|---|---|
@@ -0,0 +1,162 @@ | ||
//! Macros used internally. | ||
//! | ||
//! These may technically be exported, but that's only to make them available to internal | ||
//! project dependencies. The `#[doc(hidden)]` mark indicates that these are not stable or supported | ||
//! APIs, and should not be relied upon by external dependees. | ||
/// The single macro export of the [`cfg-if`](https://docs.rs/cfg-if) crate. | ||
/// | ||
/// It is packaged here to avoid pulling in another dependency. The stdlib does the same[^1]. | ||
/// | ||
/// [^1]: https://github.com/rust-lang/rust/blob/a2db9280539229a3b8a084a09886670a57bc7e9c/library/compiler-builtins/libm/src/math/support/macros.rs#L1 | ||
cosmicexplorer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#[doc(hidden)] | ||
#[macro_export] | ||
macro_rules! cfg_if { | ||
// match if/else chains with a final `else` | ||
( | ||
$( | ||
if #[cfg( $i_meta:meta )] { $( $i_tokens:tt )* } | ||
) else+ | ||
else { $( $e_tokens:tt )* } | ||
) => { | ||
$crate::cfg_if! { | ||
@__items () ; | ||
$( | ||
(( $i_meta ) ( $( $i_tokens )* )) , | ||
)+ | ||
(() ( $( $e_tokens )* )) , | ||
}; | ||
}; | ||
|
||
// match if/else chains lacking a final `else` | ||
( | ||
if #[cfg( $i_meta:meta )] { $( $i_tokens:tt )* } | ||
$( | ||
else if #[cfg( $e_meta:meta )] { $( $e_tokens:tt )* } | ||
)* | ||
) => { | ||
$crate::cfg_if! { | ||
@__items () ; | ||
(( $i_meta ) ( $( $i_tokens )* )) , | ||
$( | ||
(( $e_meta ) ( $( $e_tokens )* )) , | ||
)* | ||
}; | ||
}; | ||
|
||
// Internal and recursive macro to emit all the items | ||
// | ||
// Collects all the previous cfgs in a list at the beginning, so they can be | ||
// negated. After the semicolon are all the remaining items. | ||
(@__items ( $( $_:meta , )* ) ; ) => {}; | ||
( | ||
@__items ( $( $no:meta , )* ) ; | ||
(( $( $yes:meta )? ) ( $( $tokens:tt )* )) , | ||
$( $rest:tt , )* | ||
) => { | ||
// Emit all items within one block, applying an appropriate #[cfg]. The | ||
// #[cfg] will require all `$yes` matchers specified and must also negate | ||
// all previous matchers. | ||
#[cfg(all( | ||
$( $yes , )? | ||
not(any( $( $no ),* )) | ||
))] | ||
$crate::cfg_if! { @__identity $( $tokens )* } | ||
|
||
// Recurse to emit all other items in `$rest`, and when we do so add all | ||
// our `$yes` matchers to the list of `$no` matchers as future emissions | ||
// will have to negate everything we just matched as well. | ||
$crate::cfg_if! { | ||
@__items ( $( $no , )* $( $yes , )? ) ; | ||
$( $rest , )* | ||
}; | ||
}; | ||
|
||
// Internal macro to make __apply work out right for different match types, | ||
// because of how macros match/expand stuff. | ||
(@__identity $( $tokens:tt )* ) => { | ||
$( $tokens )* | ||
}; | ||
} | ||
|
||
/// Similar to [`cfg_if`](cfg_if), but accepts a list of expressions, and generates an internal | ||
/// closure to return each value. | ||
/// | ||
/// The main reason this is necessary is because attaching `#[cfg(...)]` annotations to certain | ||
/// types of statements requires a nightly feature, or `cfg_if` would be enough on its own. This | ||
/// macro's restricted interface allows it to generate a closure as a circumlocution that is legal | ||
/// on stable rust. | ||
/// | ||
/// Note that any `return` operation within the expressions provided to this macro will apply to the | ||
/// generated closure, not the enclosing scope--it cannot be used to interfere with external | ||
/// control flow. | ||
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. Can this gotcha be checked at build-time, by raising an error when a branch expression starts with |
||
/// | ||
/// The generated closure is non-[`const`](const@keyword), so cannot be used inside `const` methods. | ||
#[doc(hidden)] | ||
#[macro_export] | ||
macro_rules! cfg_if_expr { | ||
// Match =>, chains, maybe with a final _ => catchall clause. | ||
( | ||
$( $ret_ty:ty : )? | ||
$( | ||
#[cfg( $i_meta:meta )] => $i_val:expr | ||
),+ , | ||
_ => $rem_val:expr $(,)? | ||
) => { | ||
(|| $( -> $ret_ty )? { | ||
$crate::cfg_if_expr! { | ||
@__items (); | ||
$( | ||
(( $i_meta ) ( | ||
#[allow(unreachable_code)] | ||
return $i_val ; | ||
)) , | ||
)+ | ||
(() ( | ||
#[allow(unreachable_code)] | ||
return $rem_val ; | ||
)) , | ||
} | ||
})() | ||
}; | ||
// Match =>, chains *without* any _ => clause. | ||
( | ||
$( $ret_ty:ty : )? | ||
$( | ||
#[cfg( $i_meta:meta )] => $i_val:expr | ||
),+ $(,)? | ||
) => { | ||
(|| $( -> $ret_ty )? { | ||
$crate::cfg_if_expr! { | ||
@__items (); | ||
$( | ||
(( $i_meta ) ( | ||
#[allow(unreachable_code)] | ||
return $i_val ; | ||
)) , | ||
)+ | ||
} | ||
})() | ||
}; | ||
|
||
(@__items ( $( $_:meta , )* ) ; ) => {}; | ||
( | ||
@__items ( $( $no:meta , )* ) ; | ||
(( $( $yes:meta )? ) ( $( $tokens:tt )* )) , | ||
$( $rest:tt , )* | ||
) => { | ||
#[cfg(all( | ||
$( $yes , )? | ||
not(any( $( $no ),* )) | ||
))] | ||
$crate::cfg_if_expr! { @__identity $( $tokens )* } | ||
|
||
$crate::cfg_if_expr! { | ||
@__items ( $( $no , )* $( $yes , )? ) ; | ||
$( $rest , )* | ||
}; | ||
}; | ||
(@__identity $( $tokens:tt )* ) => { | ||
$( $tokens )* | ||
}; | ||
} |
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.