Skip to content

Commit

Permalink
Add target_cfg_f macro
Browse files Browse the repository at this point in the history
  • Loading branch information
calebzulawski committed Dec 10, 2022
1 parent 3c682fc commit ac5fcb8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
5 changes: 5 additions & 0 deletions multiversion-macros/src/dispatcher.rs
Expand Up @@ -104,13 +104,18 @@ impl Dispatcher {
{ [$cfg:meta, $attr:meta] $($attached:tt)* } => { #[multiversion::target::target_cfg_attr_impl(#features, $cfg, $attr)] $($attached)* };
}

macro_rules! target_cfg_f {
{ $cfg:meta } => { multiversion::target::target_cfg_f_impl!(#features, $cfg) };
}

macro_rules! match_target {
{ $($arms:tt)* } => { multiversion::target::match_target_impl!{ #features $($arms)* } }
}

pub(crate) use inherit_target;
pub(crate) use target_cfg;
pub(crate) use target_cfg_attr;
pub(crate) use target_cfg_f;
pub(crate) use match_target;
}
#block
Expand Down
20 changes: 20 additions & 0 deletions multiversion-macros/src/lib.rs
Expand Up @@ -87,6 +87,15 @@ pub fn target_cfg_attr(
.into()
}

#[proc_macro]
pub fn target_cfg_f(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = TokenStream::from(input);
quote! {
__multiversion::target_cfg_f!{ #input }
}
.into()
}

#[proc_macro_attribute]
pub fn target_cfg_impl(
attr: proc_macro::TokenStream,
Expand Down Expand Up @@ -120,6 +129,17 @@ pub fn target_cfg_attr_impl(
.into()
}

#[proc_macro]
pub fn target_cfg_f_impl(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let meta = parse_macro_input!(input with Punctuated::parse_terminated);

let meta = cfg::transform(meta);
quote! {
cfg!(#meta)
}
.into()
}

#[proc_macro]
pub fn match_target(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = TokenStream::from(input);
Expand Down
9 changes: 8 additions & 1 deletion multiversion/src/lib.rs
Expand Up @@ -218,8 +218,15 @@ pub mod target {
/// ```
pub use multiversion_macros::match_target;

/// Equivalent to `cfg!`, but considers `target_feature`s detected at runtime.
///
/// This macro only works in a function marked with [`multiversion`].
pub use multiversion_macros::target_cfg_f;

#[doc(hidden)]
pub use multiversion_macros::{match_target_impl, target_cfg_attr_impl, target_cfg_impl};
pub use multiversion_macros::{
match_target_impl, target_cfg_attr_impl, target_cfg_f_impl, target_cfg_impl,
};

#[doc(no_inline)]
pub use target_features::Target;
Expand Down
15 changes: 14 additions & 1 deletion multiversion/tests/cfg.rs
@@ -1,6 +1,6 @@
use multiversion::{
multiversion,
target::{match_target, selected_target, target_cfg, target_cfg_attr},
target::{match_target, selected_target, target_cfg, target_cfg_attr, target_cfg_f},
};

#[test]
Expand Down Expand Up @@ -49,6 +49,19 @@ fn cfg_attr() {
foo();
}

#[test]
fn cfg_f() {
#[multiversion(targets = "simd")]
fn foo() {
let cfg_avx = target_cfg_f!(all(target_arch = "x86_64", target_feature = "avx"));
let has_avx =
std::env::consts::ARCH == "x86_64" && selected_target!().supports_feature_str("avx");
assert_eq!(cfg_avx, has_avx);
}

foo();
}

#[test]
fn match_target() {
#[multiversion(targets = "simd")]
Expand Down

0 comments on commit ac5fcb8

Please sign in to comment.