pub struct AutoCfg { /* private fields */ }
Expand description
Helper to detect compiler features for cfg
output in build scripts.
Implementations§
source§impl AutoCfg
impl AutoCfg
sourcepub fn new() -> Result<Self, Error>
pub fn new() -> Result<Self, Error>
Creates a new AutoCfg
instance.
§Common errors
rustc
can’t be executed, fromRUSTC
or in thePATH
.- The version output from
rustc
can’t be parsed. OUT_DIR
is not set in the environment, or is not a writable directory.
sourcepub fn with_dir<T: Into<PathBuf>>(dir: T) -> Result<Self, Error>
pub fn with_dir<T: Into<PathBuf>>(dir: T) -> Result<Self, Error>
Creates a new AutoCfg
instance with the specified output directory.
§Common errors
rustc
can’t be executed, fromRUSTC
or in thePATH
.- The version output from
rustc
can’t be parsed. dir
is not a writable directory.
Examples found in repository?
More examples
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
fn main() {
// Normally, cargo will set `OUT_DIR` for build scripts.
let ac = autocfg::AutoCfg::with_dir("target").unwrap();
// When this feature was stabilized, it also renamed the method to
// `chunk_by`, so it's important to *use* the feature in your probe.
let code = r#"
#![feature(slice_group_by)]
pub fn probe(slice: &[i32]) -> impl Iterator<Item = &[i32]> {
slice.group_by(|a, b| a == b)
}
"#;
if ac.probe_raw(code).is_ok() {
autocfg::emit("has_slice_group_by");
}
}
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
fn main() {
// Normally, cargo will set `OUT_DIR` for build scripts.
let ac = autocfg::AutoCfg::with_dir("target").unwrap();
// since ancient times...
ac.emit_has_path("std::vec::Vec");
ac.emit_path_cfg("std::vec::Vec", "has_vec");
// rustc 1.10.0
ac.emit_has_path("std::panic::PanicInfo");
ac.emit_path_cfg("std::panic::PanicInfo", "has_panic_info");
// rustc 1.20.0
ac.emit_has_path("std::mem::ManuallyDrop");
ac.emit_path_cfg("std::mem::ManuallyDrop", "has_manually_drop");
// rustc 1.25.0
ac.emit_has_path("std::ptr::NonNull");
ac.emit_path_cfg("std::ptr::NonNull", "has_non_null");
}
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
fn main() {
// Normally, cargo will set `OUT_DIR` for build scripts.
let ac = autocfg::AutoCfg::with_dir("target").unwrap();
// since ancient times...
ac.emit_has_trait("std::ops::Add");
ac.emit_trait_cfg("std::ops::Add", "has_ops");
// trait parameters have to be provided
ac.emit_has_trait("std::borrow::Borrow<str>");
ac.emit_trait_cfg("std::borrow::Borrow<str>", "has_borrow");
// rustc 1.8.0
ac.emit_has_trait("std::ops::AddAssign");
ac.emit_trait_cfg("std::ops::AddAssign", "has_assign_ops");
// rustc 1.12.0
ac.emit_has_trait("std::iter::Sum");
ac.emit_trait_cfg("std::iter::Sum", "has_sum");
// rustc 1.28.0
ac.emit_has_trait("std::alloc::GlobalAlloc");
ac.emit_trait_cfg("std::alloc::GlobalAlloc", "has_global_alloc");
}
sourcepub fn no_std(&self) -> bool
pub fn no_std(&self) -> bool
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
.
sourcepub fn set_no_std(&mut self, no_std: bool)
pub fn set_no_std(&mut self, no_std: bool)
Sets whether AutoCfg
should use #![no_std]
in its probes.
See also no_std
.
sourcepub fn probe_rustc_version(&self, major: usize, minor: usize) -> bool
pub fn probe_rustc_version(&self, major: usize, minor: usize) -> bool
Tests whether the current rustc
reports a version greater than
or equal to “major
.minor
”.
sourcepub fn emit_rustc_version(&self, major: usize, minor: usize)
pub fn emit_rustc_version(&self, major: usize, minor: usize)
Sets a cfg
value of the form rustc_major_minor
, like rustc_1_29
,
if the current rustc
is at least that version.
sourcepub fn probe_raw(&self, code: &str) -> Result<(), Error>
pub fn probe_raw(&self, code: &str) -> Result<(), Error>
Tests whether the given code can be compiled as a Rust library.
This will only return Ok
if the compiler ran and exited successfully,
per ExitStatus::success()
.
The code is passed to the compiler exactly as-is, notably not even
adding the #![no_std]
attribute like other probes.
Raw probes are useful for testing functionality that’s not yet covered
by the rest of the AutoCfg
API. For example, the following attribute
must be used at the crate level, so it wouldn’t work within the code
templates used by other probe_*
methods.
let ac = autocfg::new();
assert!(ac.probe_raw("#![no_builtins]").is_ok());
Rust nightly features could be tested as well – ideally including a
code sample to ensure the unstable feature still works as expected.
For example, slice::group_by
was renamed to chunk_by
when it was
stabilized, even though the feature name was unchanged, so testing the
#![feature(..)]
alone wouldn’t reveal that. For larger snippets,
include_str!
may be useful to load them from separate files.
let ac = autocfg::new();
let code = r#"
#![feature(slice_group_by)]
pub fn probe(slice: &[i32]) -> impl Iterator<Item = &[i32]> {
slice.group_by(|a, b| a == b)
}
"#;
if ac.probe_raw(code).is_ok() {
autocfg::emit("has_slice_group_by");
}
Examples found in repository?
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
fn main() {
// Normally, cargo will set `OUT_DIR` for build scripts.
let ac = autocfg::AutoCfg::with_dir("target").unwrap();
// When this feature was stabilized, it also renamed the method to
// `chunk_by`, so it's important to *use* the feature in your probe.
let code = r#"
#![feature(slice_group_by)]
pub fn probe(slice: &[i32]) -> impl Iterator<Item = &[i32]> {
slice.group_by(|a, b| a == b)
}
"#;
if ac.probe_raw(code).is_ok() {
autocfg::emit("has_slice_group_by");
}
}
sourcepub fn probe_sysroot_crate(&self, name: &str) -> bool
pub fn probe_sysroot_crate(&self, name: &str) -> bool
Tests whether the given sysroot crate can be used.
The test code is subject to change, but currently looks like:
extern crate CRATE as probe;
sourcepub fn emit_sysroot_crate(&self, name: &str)
pub fn emit_sysroot_crate(&self, name: &str)
Emits a config value has_CRATE
if probe_sysroot_crate
returns true.
sourcepub fn probe_path(&self, path: &str) -> bool
pub fn probe_path(&self, path: &str) -> bool
Tests whether the given path can be used.
The test code is subject to change, but currently looks like:
pub use PATH;
sourcepub fn emit_has_path(&self, path: &str)
pub fn emit_has_path(&self, path: &str)
Emits a config value has_PATH
if probe_path
returns true.
Any non-identifier characters in the path
will be replaced with
_
in the generated config value.
Examples found in repository?
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
fn main() {
// Normally, cargo will set `OUT_DIR` for build scripts.
let ac = autocfg::AutoCfg::with_dir("target").unwrap();
// since ancient times...
ac.emit_has_path("std::vec::Vec");
ac.emit_path_cfg("std::vec::Vec", "has_vec");
// rustc 1.10.0
ac.emit_has_path("std::panic::PanicInfo");
ac.emit_path_cfg("std::panic::PanicInfo", "has_panic_info");
// rustc 1.20.0
ac.emit_has_path("std::mem::ManuallyDrop");
ac.emit_path_cfg("std::mem::ManuallyDrop", "has_manually_drop");
// rustc 1.25.0
ac.emit_has_path("std::ptr::NonNull");
ac.emit_path_cfg("std::ptr::NonNull", "has_non_null");
}
sourcepub fn emit_path_cfg(&self, path: &str, cfg: &str)
pub fn emit_path_cfg(&self, path: &str, cfg: &str)
Emits the given cfg
value if probe_path
returns true.
Examples found in repository?
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
fn main() {
// Normally, cargo will set `OUT_DIR` for build scripts.
let ac = autocfg::AutoCfg::with_dir("target").unwrap();
// since ancient times...
ac.emit_has_path("std::vec::Vec");
ac.emit_path_cfg("std::vec::Vec", "has_vec");
// rustc 1.10.0
ac.emit_has_path("std::panic::PanicInfo");
ac.emit_path_cfg("std::panic::PanicInfo", "has_panic_info");
// rustc 1.20.0
ac.emit_has_path("std::mem::ManuallyDrop");
ac.emit_path_cfg("std::mem::ManuallyDrop", "has_manually_drop");
// rustc 1.25.0
ac.emit_has_path("std::ptr::NonNull");
ac.emit_path_cfg("std::ptr::NonNull", "has_non_null");
}
sourcepub fn probe_trait(&self, name: &str) -> bool
pub fn probe_trait(&self, name: &str) -> bool
Tests whether the given trait can be used.
The test code is subject to change, but currently looks like:
pub trait Probe: TRAIT + Sized {}
sourcepub fn emit_has_trait(&self, name: &str)
pub fn emit_has_trait(&self, name: &str)
Emits a config value has_TRAIT
if probe_trait
returns true.
Any non-identifier characters in the trait name
will be replaced with
_
in the generated config value.
Examples found in repository?
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
fn main() {
// Normally, cargo will set `OUT_DIR` for build scripts.
let ac = autocfg::AutoCfg::with_dir("target").unwrap();
// since ancient times...
ac.emit_has_trait("std::ops::Add");
ac.emit_trait_cfg("std::ops::Add", "has_ops");
// trait parameters have to be provided
ac.emit_has_trait("std::borrow::Borrow<str>");
ac.emit_trait_cfg("std::borrow::Borrow<str>", "has_borrow");
// rustc 1.8.0
ac.emit_has_trait("std::ops::AddAssign");
ac.emit_trait_cfg("std::ops::AddAssign", "has_assign_ops");
// rustc 1.12.0
ac.emit_has_trait("std::iter::Sum");
ac.emit_trait_cfg("std::iter::Sum", "has_sum");
// rustc 1.28.0
ac.emit_has_trait("std::alloc::GlobalAlloc");
ac.emit_trait_cfg("std::alloc::GlobalAlloc", "has_global_alloc");
}
sourcepub fn emit_trait_cfg(&self, name: &str, cfg: &str)
pub fn emit_trait_cfg(&self, name: &str, cfg: &str)
Emits the given cfg
value if probe_trait
returns true.
Examples found in repository?
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
fn main() {
// Normally, cargo will set `OUT_DIR` for build scripts.
let ac = autocfg::AutoCfg::with_dir("target").unwrap();
// since ancient times...
ac.emit_has_trait("std::ops::Add");
ac.emit_trait_cfg("std::ops::Add", "has_ops");
// trait parameters have to be provided
ac.emit_has_trait("std::borrow::Borrow<str>");
ac.emit_trait_cfg("std::borrow::Borrow<str>", "has_borrow");
// rustc 1.8.0
ac.emit_has_trait("std::ops::AddAssign");
ac.emit_trait_cfg("std::ops::AddAssign", "has_assign_ops");
// rustc 1.12.0
ac.emit_has_trait("std::iter::Sum");
ac.emit_trait_cfg("std::iter::Sum", "has_sum");
// rustc 1.28.0
ac.emit_has_trait("std::alloc::GlobalAlloc");
ac.emit_trait_cfg("std::alloc::GlobalAlloc", "has_global_alloc");
}
sourcepub fn probe_type(&self, name: &str) -> bool
pub fn probe_type(&self, name: &str) -> bool
Tests whether the given type can be used.
The test code is subject to change, but currently looks like:
pub type Probe = TYPE;
sourcepub fn emit_has_type(&self, name: &str)
pub fn emit_has_type(&self, name: &str)
Emits a config value has_TYPE
if probe_type
returns true.
Any non-identifier characters in the type name
will be replaced with
_
in the generated config value.
sourcepub fn emit_type_cfg(&self, name: &str, cfg: &str)
pub fn emit_type_cfg(&self, name: &str, cfg: &str)
Emits the given cfg
value if probe_type
returns true.
sourcepub fn probe_expression(&self, expr: &str) -> bool
pub fn probe_expression(&self, expr: &str) -> bool
Tests whether the given expression can be used.
The test code is subject to change, but currently looks like:
pub fn probe() { let _ = EXPR; }
sourcepub fn emit_expression_cfg(&self, expr: &str, cfg: &str)
pub fn emit_expression_cfg(&self, expr: &str, cfg: &str)
Emits the given cfg
value if probe_expression
returns true.
sourcepub fn probe_constant(&self, expr: &str) -> bool
pub fn probe_constant(&self, expr: &str) -> bool
Tests whether the given constant expression can be used.
The test code is subject to change, but currently looks like:
pub const PROBE: () = ((), EXPR).0;
sourcepub fn emit_constant_cfg(&self, expr: &str, cfg: &str)
pub fn emit_constant_cfg(&self, expr: &str, cfg: &str)
Emits the given cfg
value if probe_constant
returns true.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for AutoCfg
impl RefUnwindSafe for AutoCfg
impl Send for AutoCfg
impl Sync for AutoCfg
impl Unpin for AutoCfg
impl UnwindSafe for AutoCfg
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)