Skip to content

Commit b8aecbf

Browse files
committed
Override the features detected using an env::var
Fixes: #804
1 parent 9fe42b0 commit b8aecbf

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
lines changed

crates/std_detect/Cargo.toml

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ auxv = "0.3.3"
3131
cupid = "0.6.0"
3232

3333
[features]
34-
default = [ "std_detect_dlsym_getauxval", "std_detect_file_io" ]
34+
default = [ "std_detect_dlsym_getauxval", "std_detect_file_io", "std_detect_env_override" ]
3535
std_detect_file_io = []
36-
std_detect_dlsym_getauxval = [ "libc" ]
36+
std_detect_dlsym_getauxval = [ "libc" ]
37+
std_detect_env_override = []

crates/std_detect/src/detect/cache.rs

+24-1
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,25 @@ impl Cache {
162162
self.1.store(hi, Ordering::Relaxed);
163163
}
164164
}
165+
cfg_if! {
166+
if #[cfg(feature = "std_detect_env_override")] {
167+
fn env_override(mut value: Initializer) -> Initializer {
168+
if let Ok(disable) = crate::env::var("RUST_STD_DETECT_UNSTABLE") {
169+
for v in disable.split(" ") {
170+
let _ = super::Feature::from_str(v).map(|v| value.unset(v as u32));
171+
}
172+
value
173+
} else {
174+
value
175+
}
176+
}
177+
} else {
178+
#[inline]
179+
fn env_override(value: Initializer) -> Initializer {
180+
value
181+
}
182+
}
183+
}
165184

166185
/// Tests the `bit` of the storage. If the storage has not been initialized,
167186
/// initializes it with the result of `f()`.
@@ -171,13 +190,17 @@ impl Cache {
171190
///
172191
/// It uses the `Feature` variant to index into this variable as a bitset. If
173192
/// the bit is set, the feature is enabled, and otherwise it is disabled.
193+
///
194+
/// If the feature `std_detect_env_override` is enabled looks for the env
195+
/// variable `RUST_STD_DETECT_UNSTABLE` and uses its its content to disable
196+
/// Features that would had been otherwise detected.
174197
#[inline]
175198
pub(crate) fn test<F>(bit: u32, f: F) -> bool
176199
where
177200
F: FnOnce() -> Initializer,
178201
{
179202
if CACHE.is_uninitialized() {
180-
CACHE.initialize(f());
203+
CACHE.initialize(env_override(f()));
181204
}
182205
CACHE.test(bit)
183206
}

crates/std_detect/src/detect/macros.rs

+6
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ macro_rules! features {
6969
Feature::_last => unreachable!(),
7070
}
7171
}
72+
pub fn from_str(s: &str) -> Result<Feature, ()> {
73+
match s {
74+
$($feature_lit => Ok(Feature::$feature),)*
75+
_ => Err(())
76+
}
77+
}
7278
}
7379

7480
/// Each function performs run-time feature detection for a single

crates/std_detect/src/lib.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,23 @@
2424
extern crate cfg_if;
2525

2626
cfg_if! {
27-
if #[cfg(feature = "std_detect_file_io")] {
27+
if #[cfg(all(feature = "std_detect_file_io", feature = "std_detect_env_override"))] {
28+
#[cfg_attr(test, macro_use(println))]
29+
extern crate std;
30+
31+
#[allow(unused_imports)]
32+
use std::{arch, env, fs, io, mem, sync};
33+
} else if #[cfg(feature = "std_detect_file_io")] {
2834
#[cfg_attr(test, macro_use(println))]
2935
extern crate std;
3036

3137
#[allow(unused_imports)]
3238
use std::{arch, fs, io, mem, sync};
39+
} else if #[cfg(feature = "std_detect_env_override")] {
40+
#[cfg_attr(test, macro_use(println))]
41+
extern crate std;
42+
43+
use std::env;
3344
} else {
3445
#[cfg(test)]
3546
#[macro_use(println)]

0 commit comments

Comments
 (0)