Type-level CPU feature detection using a tag dispatch model.
The following example uses a type that proves AVX support to make an AVX function safe to call:
use arch_types::{impl_features, new_features_type, Features};
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[target_feature(enable = "avx")]
unsafe fn foo_unsafe() {
println!("hello from AVX!");
}
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
fn foo_safe(_: impl_features!("avx")) {
unsafe { foo_unsafe() } // the trait bound ensures we support AVX
}
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
fn main() {
new_features_type! { Avx => "avx" }
if let Some(handle) = Avx::new() {
foo_safe(handle)
}
}
The following fails to compile due to missing AVX support:
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
fn main() {
new_features_type! { NotAvx => "sse" }
if let Some(handle) = NotAvx::new() {
foo_safe(handle)
}
}
arch-types is distributed under the terms of both the MIT license and the Apache License (Version 2.0).
See LICENSE-APACHE and LICENSE-MIT for details.