summaryrefslogtreecommitdiffstats
path: root/vendor/cfg-if/src/lib.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /vendor/cfg-if/src/lib.rs
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/cfg-if/src/lib.rs')
-rw-r--r--vendor/cfg-if/src/lib.rs176
1 files changed, 176 insertions, 0 deletions
diff --git a/vendor/cfg-if/src/lib.rs b/vendor/cfg-if/src/lib.rs
new file mode 100644
index 000000000..52bbbe0f3
--- /dev/null
+++ b/vendor/cfg-if/src/lib.rs
@@ -0,0 +1,176 @@
+//! A macro for defining `#[cfg]` if-else statements.
+//!
+//! The macro provided by this crate, `cfg_if`, is similar to the `if/elif` C
+//! preprocessor macro by allowing definition of a cascade of `#[cfg]` cases,
+//! emitting the implementation which matches first.
+//!
+//! This allows you to conveniently provide a long list `#[cfg]`'d blocks of code
+//! without having to rewrite each clause multiple times.
+//!
+//! # Example
+//!
+//! ```
+//! cfg_if::cfg_if! {
+//! if #[cfg(unix)] {
+//! fn foo() { /* unix specific functionality */ }
+//! } else if #[cfg(target_pointer_width = "32")] {
+//! fn foo() { /* non-unix, 32-bit functionality */ }
+//! } else {
+//! fn foo() { /* fallback implementation */ }
+//! }
+//! }
+//!
+//! # fn main() {}
+//! ```
+
+#![no_std]
+#![doc(html_root_url = "https://docs.rs/cfg-if")]
+#![deny(missing_docs)]
+#![cfg_attr(test, deny(warnings))]
+
+/// The main macro provided by this crate. See crate documentation for more
+/// information.
+#[macro_export]
+macro_rules! cfg_if {
+ // match if/else chains with a final `else`
+ ($(
+ if #[cfg($meta:meta)] { $($tokens:tt)* }
+ ) else * else {
+ $($tokens2:tt)*
+ }) => {
+ $crate::cfg_if! {
+ @__items
+ () ;
+ $( ( ($meta) ($($tokens)*) ), )*
+ ( () ($($tokens2)*) ),
+ }
+ };
+
+ // match if/else chains lacking a final `else`
+ (
+ if #[cfg($i_met:meta)] { $($i_tokens:tt)* }
+ $(
+ else if #[cfg($e_met:meta)] { $($e_tokens:tt)* }
+ )*
+ ) => {
+ $crate::cfg_if! {
+ @__items
+ () ;
+ ( ($i_met) ($($i_tokens)*) ),
+ $( ( ($e_met) ($($e_tokens)*) ), )*
+ ( () () ),
+ }
+ };
+
+ // 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),*) ($($tokens:tt)*) ), $($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(all($($m,)* not(any($($not),*))))] $crate::cfg_if! { @__identity $($tokens)* }
+
+ // 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.
+ $crate::cfg_if! { @__items ($($not,)* $($m,)*) ; $($rest)* }
+ };
+
+ // Internal macro to make __apply work out right for different match types,
+ // because of how macros matching/expand stuff.
+ (@__identity $($tokens:tt)*) => {
+ $($tokens)*
+ };
+}
+
+#[cfg(test)]
+mod tests {
+ cfg_if! {
+ if #[cfg(test)] {
+ use core::option::Option as Option2;
+ fn works1() -> Option2<u32> { Some(1) }
+ } else {
+ fn works1() -> Option<u32> { None }
+ }
+ }
+
+ cfg_if! {
+ if #[cfg(foo)] {
+ fn works2() -> bool { false }
+ } else if #[cfg(test)] {
+ fn works2() -> bool { true }
+ } else {
+ fn works2() -> bool { false }
+ }
+ }
+
+ cfg_if! {
+ if #[cfg(foo)] {
+ fn works3() -> bool { false }
+ } else {
+ fn works3() -> bool { true }
+ }
+ }
+
+ cfg_if! {
+ if #[cfg(test)] {
+ use core::option::Option as Option3;
+ fn works4() -> Option3<u32> { Some(1) }
+ }
+ }
+
+ cfg_if! {
+ if #[cfg(foo)] {
+ fn works5() -> bool { false }
+ } else if #[cfg(test)] {
+ fn works5() -> bool { true }
+ }
+ }
+
+ #[test]
+ fn it_works() {
+ assert!(works1().is_some());
+ assert!(works2());
+ assert!(works3());
+ assert!(works4().is_some());
+ assert!(works5());
+ }
+
+ #[test]
+ #[allow(clippy::assertions_on_constants)]
+ fn test_usage_within_a_function() {
+ cfg_if! {if #[cfg(debug_assertions)] {
+ // we want to put more than one thing here to make sure that they
+ // all get configured properly.
+ assert!(cfg!(debug_assertions));
+ assert_eq!(4, 2+2);
+ } else {
+ assert!(works1().is_some());
+ assert_eq!(10, 5+5);
+ }}
+ }
+
+ trait Trait {
+ fn blah(&self);
+ }
+
+ #[allow(dead_code)]
+ struct Struct;
+
+ impl Trait for Struct {
+ cfg_if! {
+ if #[cfg(feature = "blah")] {
+ fn blah(&self) {
+ unimplemented!();
+ }
+ } else {
+ fn blah(&self) {
+ unimplemented!();
+ }
+ }
+ }
+ }
+}