Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getter no_std() and setter set_no_std(bool) #27

Merged
merged 2 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ pub fn rerun_env(var: &str) {
println!("cargo:rerun-if-env-changed={}", var);
}

/// Create a new `AutoCfg` instance.
/// Creates a new `AutoCfg` instance.
///
/// # Panics
///
Expand All @@ -129,7 +129,7 @@ pub fn new() -> AutoCfg {
}

impl AutoCfg {
/// Create a new `AutoCfg` instance.
/// Creates a new `AutoCfg` instance.
///
/// # Common errors
///
Expand All @@ -144,7 +144,7 @@ impl AutoCfg {
}
}

/// Create a new `AutoCfg` instance with the specified output directory.
/// Creates a new `AutoCfg` instance with the specified output directory.
///
/// # Common errors
///
Expand Down Expand Up @@ -188,7 +188,34 @@ impl AutoCfg {
Ok(ac)
}

/// Test whether the current `rustc` reports a version greater than
/// Returns whether `AutoCfg` is using `#![no_std]` in its probes.
///
/// This is automatically detected during construction -- if an empty probe
/// fails while one with `#![no_std]` succeeds, then the attribute will be
/// used for all further probes. This is usually only necessary when the
/// `TARGET` lacks `std` altogether. If neither succeeds, `no_std` is not
/// set, but that `AutoCfg` will probably only work for version checks.
///
/// This attribute changes the implicit [prelude] from `std` to `core`,
/// which may affect the paths you need to use in other probes. It also
/// restricts some types that otherwise get additional methods in `std`,
/// like floating-point trigonometry and slice sorting.
///
/// See also [`set_no_std`](#method.set_no_std).
///
/// [prelude]: https://doc.rust-lang.org/reference/crates-and-source-files.html#preludes-and-no_std
pub fn no_std(&self) -> bool {
self.no_std
}

/// Sets whether `AutoCfg` should use `#![no_std]` in its probes.
///
/// See also [`no_std`](#method.no_std).
pub fn set_no_std(&mut self, no_std: bool) {
self.no_std = no_std;
}

/// Tests whether the current `rustc` reports a version greater than
/// or equal to "`major`.`minor`".
pub fn probe_rustc_version(&self, major: usize, minor: usize) -> bool {
self.rustc_version >= Version::new(major, minor, 0)
Expand Down
27 changes: 27 additions & 0 deletions tests/no_std.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
extern crate autocfg;

use std::env;

/// Tests that we can control the use of `#![no_std]`.
#[test]
fn no_std() {
// Clear the CI `TARGET`, if any, so we're just dealing with the
// host target which always has `std` available.
env::remove_var("TARGET");

// Use the same path as this test binary.
let dir = env::current_exe().unwrap().parent().unwrap().to_path_buf();
env::set_var("OUT_DIR", &format!("{}", dir.display()));

let mut ac = autocfg::AutoCfg::new().unwrap();
assert!(!ac.no_std());
assert!(ac.probe_path("std::mem"));

// `#![no_std]` was stabilized in Rust 1.6
if ac.probe_rustc_version(1, 6) {
ac.set_no_std(true);
assert!(ac.no_std());
assert!(!ac.probe_path("std::mem"));
assert!(ac.probe_path("core::mem"));
}
}