Skip to content

Commit 5e62ce9

Browse files
authored
rndr: minor tweaks (#586)
The most notable change is that `is_aarch64_feature_detected!` now takes precedence over `asm!`-based detection.
1 parent 3d0c01c commit 5e62ce9

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed

src/backends/rndr.rs

+27-26
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,21 @@ unsafe fn rndr_fill(dest: &mut [MaybeUninit<u8>]) -> Option<()> {
6060
Some(())
6161
}
6262

63+
#[cfg(target_feature = "rand")]
6364
fn is_rndr_available() -> bool {
65+
true
66+
}
67+
68+
#[cfg(not(target_feature = "rand"))]
69+
fn is_rndr_available() -> bool {
70+
#[path = "../lazy.rs"]
71+
mod lazy;
72+
static RNDR_GOOD: lazy::LazyBool = lazy::LazyBool::new();
73+
6474
cfg_if::cfg_if! {
65-
if #[cfg(target_feature = "rand")] {
66-
true
75+
if #[cfg(feature = "std")] {
76+
extern crate std;
77+
RNDR_GOOD.unsync_init(|| std::arch::is_aarch64_feature_detected!("rand"))
6778
} else if #[cfg(target_os = "linux")] {
6879
/// Check whether FEAT_RNG is available on the system
6980
///
@@ -87,14 +98,7 @@ fn is_rndr_available() -> bool {
8798
(id_aa64isar0 >> 60) & 0xf >= 1
8899
}
89100

90-
#[path = "../lazy.rs"] mod lazy;
91-
static RNDR_GOOD: lazy::LazyBool = lazy::LazyBool::new();
92101
RNDR_GOOD.unsync_init(mrs_check)
93-
} else if #[cfg(feature = "std")] {
94-
extern crate std;
95-
#[path = "../lazy.rs"] mod lazy;
96-
static RNDR_GOOD: lazy::LazyBool = lazy::LazyBool::new();
97-
RNDR_GOOD.unsync_init(|| std::arch::is_aarch64_feature_detected!("rand"))
98102
} else {
99103
compile_error!(
100104
"RNDR `no_std` runtime detection is currently supported only on Linux targets. \
@@ -105,32 +109,29 @@ fn is_rndr_available() -> bool {
105109
}
106110

107111
pub fn inner_u32() -> Result<u32, Error> {
108-
if is_rndr_available() {
109-
// SAFETY: after this point, we know the `rand` target feature is enabled
110-
let res = unsafe { rndr() };
111-
res.map(truncate).ok_or(Error::RNDR_FAILURE)
112-
} else {
113-
Err(Error::RNDR_NOT_AVAILABLE)
112+
if !is_rndr_available() {
113+
return Err(Error::RNDR_NOT_AVAILABLE);
114114
}
115+
// SAFETY: after this point, we know the `rand` target feature is enabled
116+
let res = unsafe { rndr() };
117+
res.map(truncate).ok_or(Error::RNDR_FAILURE)
115118
}
116119

117120
pub fn inner_u64() -> Result<u64, Error> {
118-
if is_rndr_available() {
119-
// SAFETY: after this point, we know the `rand` target feature is enabled
120-
let res = unsafe { rndr() };
121-
res.ok_or(Error::RNDR_FAILURE)
122-
} else {
123-
Err(Error::RNDR_NOT_AVAILABLE)
121+
if !is_rndr_available() {
122+
return Err(Error::RNDR_NOT_AVAILABLE);
124123
}
124+
// SAFETY: after this point, we know the `rand` target feature is enabled
125+
let res = unsafe { rndr() };
126+
res.ok_or(Error::RNDR_FAILURE)
125127
}
126128

127129
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
128-
if is_rndr_available() {
129-
// SAFETY: after this point, we know the `rand` target feature is enabled
130-
unsafe { rndr_fill(dest).ok_or(Error::RNDR_FAILURE) }
131-
} else {
132-
Err(Error::RNDR_NOT_AVAILABLE)
130+
if !is_rndr_available() {
131+
return Err(Error::RNDR_NOT_AVAILABLE);
133132
}
133+
// SAFETY: after this point, we know the `rand` target feature is enabled
134+
unsafe { rndr_fill(dest).ok_or(Error::RNDR_FAILURE) }
134135
}
135136

136137
impl Error {

0 commit comments

Comments
 (0)