#cfg #proc-macro #features #token-stream #foo-bar

no-std is_not

一个类似于 #[cfg(...)] 的过程宏,可以根据启用的功能从库中重新导出

1个不稳定版本

0.1.0 2020年10月27日

#591 in 配置

Download history 183/week @ 2024-03-29 87/week @ 2024-04-05 330/week @ 2024-04-12 126/week @ 2024-04-19 273/week @ 2024-04-26 263/week @ 2024-05-03 190/week @ 2024-05-10 122/week @ 2024-05-17 306/week @ 2024-05-24 430/week @ 2024-05-31 210/week @ 2024-06-07 173/week @ 2024-06-14 121/week @ 2024-06-21 133/week @ 2024-06-28 209/week @ 2024-07-05 276/week @ 2024-07-12

每月下载 830次

MIT/Apache

7KB

is_not

此crate导出两个非常简单的属性宏。 is 返回原始的令牌流,而 not 返回一个空的令牌流。

它们与 #[cfg(...)] 属性非常相似,但它们可以从crate中导出,用于需要启用特定功能的情况。它还有另一个优点,即它还进行类型检查。如果在 #[cfg(mis-spelled)] 内拼写错误,它将始终禁用,而这些属性必须在命名空间中存在。

例如,假设你想要在库内部允许不同的算法或实现,这些算法或实现可以打开和关闭,并且由于依赖关系或将在应用程序主循环的热路径中而导致昂贵的编译时间。

// algo crate
// This crate ensures that at least one of foo and bar is active using a build script or more fancy cfg attributes.

#[cfg(feature = "foo")]
pub use is_not::{is as is_foo, not as not_foo};
#[cfg(not(feature = "foo"))]
pub use is_not::{not as is_foo, is as not_foo};
#[cfg(feature = "bar")]
pub use is_not::{is as is_bar, not as not_bar};
#[cfg(not(feature = "bar"))]
pub use is_not::{not as is_bar, is as not_bar};

#[cfg(feature = "foo")]
mod foo {
    fn foo(a: Foo) -> i32 {
        // algorithm
    }
    struct Foo {
        // fields
    }
}

#[cfg(feature = "foo")]
mod bar {
    fn bar(a: Bar) -> i32 {
        // algorithm
    }
    struct Bar {
        // fields
    }
}
// app crate
// both of these functions will exist, though compile time flags given to the algo crate will change how they are compiled in the app crate.

// You can ensure that at least one is turned on
#[not_foo]
#[not_bar]
compile_error!("Either foo or bar or both should be enabled in algo crate");

#[is_foo]
fn call_foo() -> i32 {
  algo::foo::foo(algo::foo::Foo{})
}

#[not_foo]
fn call_foo() -> i32 {
  call_bar()
}

#[is_bar]
fn call_bar() -> i32 {
  algo::bar::bar(algo::bar::Bar{})
}

#[not_bar]
fn call_bar() -> i32 {
  call_foo()
}

fn call_algo() -> i32 {
  if crate::use_foo() {
    call_foo()
  } else {
    call_bar()
  }
}

无运行时依赖