1 //! Contains code for selecting features
3 #![deny(unused_extern_crates)]
4 #![deny(clippy::missing_docs_in_private_items)]
7 use std::cmp::Ordering;
11 /// This macro defines the [`RustTarget`] and [`RustFeatures`] types.
12 macro_rules! define_rust_targets {
14 Nightly => {$($nightly_feature:ident $(: #$issue:literal)?),* $(,)?} $(,)?
17 $variant:ident($minor:literal) => {$($feature:ident $(: #$pull:literal)?),* $(,)?},
21 /// Represents the version of the Rust language to target.
23 /// To support a beta release, use the corresponding stable release.
25 /// This enum will have more variants added as necessary.
26 #[allow(non_camel_case_types)]
27 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
31 "- [`", stringify!($nightly_feature), "`]",
32 "(", $("https://github.com/rust-lang/rust/pull/", stringify!($issue),)* ")",
36 #[doc = concat!("Rust 1.", stringify!($minor))]
38 "- [`", stringify!($feature), "`]",
39 "(", $("https://github.com/rust-lang/rust/pull/", stringify!($pull),)* ")",
47 fn minor(self) -> Option<u64> {
49 $( Self::$variant => Some($minor),)*
54 const fn stable_releases() -> [(Self, u64); [$($minor,)*].len()] {
55 [$((Self::$variant, $minor),)*]
59 #[cfg(feature = "__cli")]
60 /// Strings of allowed `RustTarget` values
61 pub const RUST_TARGET_STRINGS: &[&str] = &[$(concat!("1.", stringify!($minor)),)*];
63 #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
64 pub(crate) struct RustFeatures {
65 $($(pub(crate) $feature: bool,)*)*
66 $(pub(crate) $nightly_feature: bool,)*
69 impl From<RustTarget> for RustFeatures {
70 fn from(target: RustTarget) -> Self {
71 if target == RustTarget::Nightly {
73 $($($feature: true,)*)*
74 $($nightly_feature: true,)*
78 let mut features = Self {
79 $($($feature: false,)*)*
80 $($nightly_feature: false,)*
83 $(if target >= RustTarget::$variant {
84 $(features.$feature = true;)*
93 // NOTE: When adding or removing features here, make sure to add the stabilization PR
94 // number for the feature if it has been stabilized or the tracking issue number if the feature is
96 define_rust_targets! {
100 Stable_1_73(73) => { thiscall_abi: #42202 },
101 Stable_1_71(71) => { c_unwind_abi: #106075 },
102 Stable_1_68(68) => { abi_efiapi: #105795 },
103 Stable_1_64(64) => { core_ffi_c: #94503 },
104 Stable_1_59(59) => { const_cstr: #54745 },
105 Stable_1_47(47) => { larger_arrays: #74060 },
106 Stable_1_40(40) => { non_exhaustive: #44109 },
107 Stable_1_36(36) => { maybe_uninit: #60445 },
108 Stable_1_33(33) => { repr_packed_n: #57049 },
111 core_ffi_c_void: #53910,
112 min_const_fn: #54835,
115 Stable_1_28(28) => { repr_transparent: #51562 },
117 Stable_1_27(27) => { must_use_function: #48925 },
119 Stable_1_26(26) => { i128_and_u128: #49101 },
121 Stable_1_25(25) => { repr_align: #47006 },
123 Stable_1_21(21) => { builtin_clone_impls: #43690 },
125 Stable_1_20(20) => { associated_const: #42809 },
127 Stable_1_19(19) => { untagged_union: #42068 },
129 Stable_1_17(17) => { static_lifetime_elision: #39265 },
134 /// Latest stable release of Rust
135 pub const LATEST_STABLE_RUST: RustTarget = {
136 // FIXME: replace all this code by
138 // RustTarget::stable_releases()
140 // .max_by_key(|(_, m)| m)
142 // .unwrap_or(RustTarget::Nightly)
144 // once those operations can be used in constants.
145 let targets = RustTarget::stable_releases();
148 let mut latest_target = None;
149 let mut latest_minor = 0;
151 while i < targets.len() {
152 let (target, minor) = targets[i];
154 if latest_minor < minor {
155 latest_minor = minor;
156 latest_target = Some(target);
162 match latest_target {
163 Some(target) => target,
164 None => unreachable!(),
168 impl Default for RustTarget {
169 fn default() -> Self {
174 impl PartialOrd for RustTarget {
175 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
176 Some(self.cmp(other))
180 impl Ord for RustTarget {
181 fn cmp(&self, other: &Self) -> Ordering {
182 match (self.minor(), other.minor()) {
183 (Some(a), Some(b)) => a.cmp(&b),
184 (Some(_), None) => Ordering::Less,
185 (None, Some(_)) => Ordering::Greater,
186 (None, None) => Ordering::Equal,
191 impl FromStr for RustTarget {
192 type Err = io::Error;
194 fn from_str(s: &str) -> Result<Self, Self::Err> {
196 return Ok(Self::Nightly);
199 if let Some(("1", str_minor)) = s.split_once('.') {
200 if let Ok(minor) = str_minor.parse::<u64>() {
201 for (target, target_minor) in Self::stable_releases() {
202 if minor == target_minor {
210 io::ErrorKind::InvalidInput,
211 "Got an invalid Rust target. Accepted values are of the form \"1.71\" or \"nightly\"."
216 impl std::fmt::Display for RustTarget {
217 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
219 Some(minor) => write!(f, "1.{}", minor),
220 None => "nightly".fmt(f),
225 impl Default for RustFeatures {
226 fn default() -> Self {
227 RustTarget::default().into()
233 #![allow(unused_imports)]
237 fn target_features() {
238 let f_1_0 = RustFeatures::from(RustTarget::Stable_1_0);
240 !f_1_0.static_lifetime_elision &&
241 !f_1_0.core_ffi_c_void &&
242 !f_1_0.untagged_union &&
243 !f_1_0.associated_const &&
244 !f_1_0.builtin_clone_impls &&
246 !f_1_0.thiscall_abi &&
247 !f_1_0.vectorcall_abi
249 let f_1_21 = RustFeatures::from(RustTarget::Stable_1_21);
251 f_1_21.static_lifetime_elision &&
252 !f_1_21.core_ffi_c_void &&
253 f_1_21.untagged_union &&
254 f_1_21.associated_const &&
255 f_1_21.builtin_clone_impls &&
256 !f_1_21.repr_align &&
257 !f_1_21.thiscall_abi &&
258 !f_1_21.vectorcall_abi
260 let features = RustFeatures::from(RustTarget::Stable_1_71);
262 features.c_unwind_abi &&
263 features.abi_efiapi &&
264 !features.thiscall_abi
266 let f_nightly = RustFeatures::from(RustTarget::Nightly);
268 f_nightly.static_lifetime_elision &&
269 f_nightly.core_ffi_c_void &&
270 f_nightly.untagged_union &&
271 f_nightly.associated_const &&
272 f_nightly.builtin_clone_impls &&
273 f_nightly.maybe_uninit &&
274 f_nightly.repr_align &&
275 f_nightly.thiscall_abi &&
276 f_nightly.vectorcall_abi
280 fn test_target(target_str: &str, target: RustTarget) {
281 let target_string = target.to_string();
282 assert_eq!(target_str, target_string);
283 assert_eq!(target, RustTarget::from_str(target_str).unwrap());
288 test_target("1.0", RustTarget::Stable_1_0);
289 test_target("1.17", RustTarget::Stable_1_17);
290 test_target("1.19", RustTarget::Stable_1_19);
291 test_target("1.21", RustTarget::Stable_1_21);
292 test_target("1.25", RustTarget::Stable_1_25);
293 test_target("1.71", RustTarget::Stable_1_71);
294 test_target("nightly", RustTarget::Nightly);