1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#[derive(Debug)]
pub struct RdRand(());
impl RdRand {
#[cfg(target_arch = "x86_64")]
pub fn new() -> Option<Self> {
let cpuid = raw_cpuid::CpuId::new();
let has_rdrand = match cpuid.get_feature_info() {
Some(finfo) => finfo.has_rdrand(),
None => false,
};
match has_rdrand {
true => Some(RdRand(())),
false => None,
}
}
#[inline]
#[cfg(target_arch = "x86_64")]
pub fn get(&self) -> u64 {
let res: u64;
unsafe {
asm!("rdrand %rax" : "={rax}"(res) ::: "volatile");
}
res
}
}