Skip to content

Commit 45fdfe1

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

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-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/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/detect/mod.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,31 @@ cfg_if! {
9797
}
9898
}
9999

100+
cfg_if! {
101+
if #[cfg(feature = "std_detect_env_override")] {
102+
fn detect_features() -> cache::Initializer {
103+
let value = self::os::detect_features();
104+
if let Ok(disable) = crate::env::var("RUST_STD_DETECT_UNSTABLE") {
105+
let mut value = value;
106+
for v in disable.split(" ") {
107+
let _ = Feature::from_str(v).map(|v| value.unset(v as u32));
108+
}
109+
value
110+
} else {
111+
value
112+
}
113+
}
114+
} else {
115+
fn detect_features() -> cache::Initializer {
116+
self::os::detect_features()
117+
}
118+
}
119+
}
120+
100121
/// Performs run-time feature detection.
101122
#[inline]
102123
pub fn check_for(x: Feature) -> bool {
103-
cache::test(x as u32, self::os::detect_features)
124+
cache::test(x as u32, detect_features)
104125
}
105126

106127
/// Returns an `Iterator<Item=(&'static str, bool)>` where

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)