use core::fmt;
use core::sync::atomic::{AtomicBool, Ordering};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct MissingFeature(&'static str);
impl fmt::Display for MissingFeature {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"rind requires {} CPU support on {}",
self.0,
std::env::consts::ARCH
)
}
}
impl core::error::Error for MissingFeature {}
pub fn check() -> Result<(), MissingFeature> {
match super::missing_cpu_feature() {
Some(feature) => Err(MissingFeature(feature)),
None => Ok(()),
}
}
pub fn has_aes_hardware() -> bool {
#[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
return super::HaveCryptoExt::check().is_some();
#[cfg(target_arch = "x86_64")]
return true;
}
pub(crate) fn verify_cpu_features() {
static VERIFIED: AtomicBool = AtomicBool::new(false);
#[cold]
fn verify() {
if let Err(missing) = check() {
panic!("{missing}");
}
VERIFIED.store(true, Ordering::Relaxed);
}
if !VERIFIED.load(Ordering::Relaxed) {
verify();
}
}