Skip to content

CPU feature detection types for Rust

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

calebzulawski/arch-types

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

arch-types

Build Status Rustc Version 1.34+

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)
    }
}

License

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.

About

CPU feature detection types for Rust

Topics

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages