mirror of
https://gitee.com/openharmony/third_party_rust_cfg-if
synced 2024-11-22 22:51:23 +00:00
Initial commit
This commit is contained in:
commit
3e9f521352
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
target
|
||||
Cargo.lock
|
14
Cargo.toml
Normal file
14
Cargo.toml
Normal file
@ -0,0 +1,14 @@
|
||||
[package]
|
||||
name = "cfg-if"
|
||||
version = "0.1.0"
|
||||
authors = ["Alex Crichton <alex@alexcrichton.com>"]
|
||||
license = "MIT/Apache-2.0"
|
||||
readme = "README.md"
|
||||
repository = "https://github.com/alexcrichton/cfg-if"
|
||||
homepage = "https://github.com/alexcrichton/cfg-if"
|
||||
documentation = "http://alexcrichton.com/cfg-if"
|
||||
description = """
|
||||
A macro to ergonomically define an item depending on a large number of #[cfg]
|
||||
parameters. Structured like an if-else chain, the first matching branch is the
|
||||
item that gets emitted.
|
||||
"""
|
40
README.md
Normal file
40
README.md
Normal file
@ -0,0 +1,40 @@
|
||||
# cfg-if
|
||||
|
||||
[![Build Status](https://travis-ci.org/alexcrichton/cfg-if.svg?branch=master)](https://travis-ci.org/alexcrichton/cfg-if)
|
||||
|
||||
[Documentation](http://alexcrichton.com/cfg-if)
|
||||
|
||||
A macro to ergonomically define an item depending on a large number of #[cfg]
|
||||
parameters. Structured like an if-else chain, the first matching branch is the
|
||||
item that gets emitted.
|
||||
|
||||
```toml
|
||||
[dependencies]
|
||||
cfg-if = "0.1"
|
||||
```
|
||||
|
||||
## Example
|
||||
|
||||
```rust
|
||||
#[macro_use]
|
||||
extern crate 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 */ }
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
# License
|
||||
|
||||
`cfg-if` is primarily distributed under the terms of both the MIT license and
|
||||
the Apache License (Version 2.0), with portions covered by various BSD-like
|
||||
licenses.
|
||||
|
||||
See LICENSE-APACHE, and LICENSE-MIT for details.
|
||||
|
101
src/lib.rs
Normal file
101
src/lib.rs
Normal file
@ -0,0 +1,101 @@
|
||||
//! 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
|
||||
//!
|
||||
//! ```
|
||||
//! #[macro_use]
|
||||
//! extern crate 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() {}
|
||||
//! ```
|
||||
|
||||
#![doc(html_root_url = "http://alexcrichton.com/cfg-if")]
|
||||
#![deny(missing_docs)]
|
||||
#![cfg_attr(test, deny(warnings))]
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! cfg_if {
|
||||
($(
|
||||
if #[cfg($($meta:meta),*)] { $($it:item)* }
|
||||
) else * else {
|
||||
$($it2:item)*
|
||||
}) => {
|
||||
__cfg_if_items! {
|
||||
() ;
|
||||
$( ( ($($meta),*) ($($it)*) ), )*
|
||||
( () ($($it2)*) ),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
#[doc(hidden)]
|
||||
macro_rules! __cfg_if_items {
|
||||
(($($not:meta,)*) ; ) => {};
|
||||
(($($not:meta,)*) ; ( ($($m:meta),*) ($($it:item)*) ), $($rest:tt)*) => {
|
||||
__cfg_if_apply! { cfg(all($($m,)* not(any($($not),*)))), $($it)* }
|
||||
__cfg_if_items! { ($($not,)* $($m,)*) ; $($rest)* }
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
#[doc(hidden)]
|
||||
macro_rules! __cfg_if_apply {
|
||||
($m:meta, $($it:item)*) => {
|
||||
$(#[$m] $it)*
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
cfg_if! {
|
||||
if #[cfg(test)] {
|
||||
use std::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 }
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn it_works() {
|
||||
assert!(works1().is_some());
|
||||
assert!(works2());
|
||||
assert!(works3());
|
||||
}
|
||||
}
|
17
tests/xcrate.rs
Normal file
17
tests/xcrate.rs
Normal file
@ -0,0 +1,17 @@
|
||||
#[macro_use]
|
||||
extern crate cfg_if;
|
||||
|
||||
cfg_if! {
|
||||
if #[cfg(foo)] {
|
||||
fn works() -> bool { false }
|
||||
} else if #[cfg(test)] {
|
||||
fn works() -> bool { true }
|
||||
} else {
|
||||
fn works() -> bool { false }
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn smoke() {
|
||||
assert!(works());
|
||||
}
|
Loading…
Reference in New Issue
Block a user