| 1 |
//! Contains code for selecting features |
| 2 |
|
| 3 |
#![deny(unused_extern_crates)] |
| 4 |
#![deny(clippy::missing_docs_in_private_items)] |
| 5 |
#![allow(deprecated)] |
| 6 |
|
| 7 |
use std::io; |
| 8 |
use std::str::FromStr; |
| 9 |
|
| 10 |
/// Define RustTarget struct definition, Default impl, and conversions |
| 11 |
/// between RustTarget and String. |
| 12 |
macro_rules! rust_target_def { |
| 13 |
( $( $( #[$attr:meta] )* => $release:ident => $value:expr; )* ) => { |
| 14 |
/// Represents the version of the Rust language to target. |
| 15 |
/// |
| 16 |
/// To support a beta release, use the corresponding stable release. |
| 17 |
/// |
| 18 |
/// This enum will have more variants added as necessary. |
| 19 |
#[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Hash)] |
| 20 |
#[allow(non_camel_case_types)] |
| 21 |
pub enum RustTarget { |
| 22 |
$( |
| 23 |
$( |
| 24 |
#[$attr] |
| 25 |
)* |
| 26 |
$release, |
| 27 |
)* |
| 28 |
} |
| 29 |
|
| 30 |
impl Default for RustTarget { |
| 31 |
/// Gives the latest stable Rust version |
| 32 |
fn default() -> RustTarget { |
| 33 |
LATEST_STABLE_RUST |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
impl FromStr for RustTarget { |
| 38 |
type Err = io::Error; |
| 39 |
|
| 40 |
/// Create a `RustTarget` from a string. |
| 41 |
/// |
| 42 |
/// * The stable/beta versions of Rust are of the form "1.0", |
| 43 |
/// "1.19", etc. |
| 44 |
/// * The nightly version should be specified with "nightly". |
| 45 |
fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 46 |
match s.as_ref() { |
| 47 |
$( |
| 48 |
stringify!($value) => Ok(RustTarget::$release), |
| 49 |
)* |
| 50 |
_ => Err( |
| 51 |
io::Error::new( |
| 52 |
io::ErrorKind::InvalidInput, |
| 53 |
concat!( |
| 54 |
"Got an invalid rust target. Accepted values ", |
| 55 |
"are of the form ", |
| 56 |
"\"1.0\" or \"nightly\"."))), |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
impl From<RustTarget> for String { |
| 62 |
fn from(target: RustTarget) -> Self { |
| 63 |
match target { |
| 64 |
$( |
| 65 |
RustTarget::$release => stringify!($value), |
| 66 |
)* |
| 67 |
}.into() |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
/// Defines an array slice with all RustTarget values |
| 74 |
macro_rules! rust_target_values_def { |
| 75 |
( $( $( #[$attr:meta] )* => $release:ident => $value:expr; )* ) => { |
| 76 |
/// Strings of allowed `RustTarget` values |
| 77 |
pub static RUST_TARGET_STRINGS: &'static [&str] = &[ |
| 78 |
$( |
| 79 |
stringify!($value), |
| 80 |
)* |
| 81 |
]; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
/// Defines macro which takes a macro |
| 86 |
macro_rules! rust_target_base { |
| 87 |
( $x_macro:ident ) => { |
| 88 |
$x_macro!( |
| 89 |
/// Rust stable 1.0 |
| 90 |
#[deprecated = "This rust target is deprecated. If you have a good reason to use this target please report it at https://github.com/rust-lang/rust-bindgen/issues"] => Stable_1_0 => 1.0; |
| 91 |
/// Rust stable 1.17 |
| 92 |
/// * Static lifetime elision ([RFC 1623](https://github.com/rust-lang/rfcs/blob/master/text/1623-static.md)) |
| 93 |
#[deprecated = "This rust target is deprecated. If you have a good reason to use this target please report it at https://github.com/rust-lang/rust-bindgen/issues"] => Stable_1_17 => 1.17; |
| 94 |
/// Rust stable 1.19 |
| 95 |
/// * Untagged unions ([RFC 1444](https://github.com/rust-lang/rfcs/blob/master/text/1444-union.md)) |
| 96 |
#[deprecated = "This rust target is deprecated. If you have a good reason to use this target please report it at https://github.com/rust-lang/rust-bindgen/issues"] => Stable_1_19 => 1.19; |
| 97 |
/// Rust stable 1.20 |
| 98 |
/// * Associated constants ([PR](https://github.com/rust-lang/rust/pull/42809)) |
| 99 |
#[deprecated = "This rust target is deprecated. If you have a good reason to use this target please report it at https://github.com/rust-lang/rust-bindgen/issues"] => Stable_1_20 => 1.20; |
| 100 |
/// Rust stable 1.21 |
| 101 |
/// * Builtin impls for `Clone` ([PR](https://github.com/rust-lang/rust/pull/43690)) |
| 102 |
#[deprecated = "This rust target is deprecated. If you have a good reason to use this target please report it at https://github.com/rust-lang/rust-bindgen/issues"] => Stable_1_21 => 1.21; |
| 103 |
/// Rust stable 1.25 |
| 104 |
/// * `repr(align)` ([PR](https://github.com/rust-lang/rust/pull/47006)) |
| 105 |
#[deprecated = "This rust target is deprecated. If you have a good reason to use this target please report it at https://github.com/rust-lang/rust-bindgen/issues"] => Stable_1_25 => 1.25; |
| 106 |
/// Rust stable 1.26 |
| 107 |
/// * [i128 / u128 support](https://doc.rust-lang.org/std/primitive.i128.html) |
| 108 |
#[deprecated = "This rust target is deprecated. If you have a good reason to use this target please report it at https://github.com/rust-lang/rust-bindgen/issues"] => Stable_1_26 => 1.26; |
| 109 |
/// Rust stable 1.27 |
| 110 |
/// * `must_use` attribute on functions ([PR](https://github.com/rust-lang/rust/pull/48925)) |
| 111 |
#[deprecated = "This rust target is deprecated. If you have a good reason to use this target please report it at https://github.com/rust-lang/rust-bindgen/issues"] => Stable_1_27 => 1.27; |
| 112 |
/// Rust stable 1.28 |
| 113 |
/// * `repr(transparent)` ([PR](https://github.com/rust-lang/rust/pull/51562)) |
| 114 |
#[deprecated = "This rust target is deprecated. If you have a good reason to use this target please report it at https://github.com/rust-lang/rust-bindgen/issues"] => Stable_1_28 => 1.28; |
| 115 |
/// Rust stable 1.30 |
| 116 |
/// * `const fn` support for limited cases ([PR](https://github.com/rust-lang/rust/pull/54835/) |
| 117 |
/// * [c_void available in core](https://doc.rust-lang.org/core/ffi/enum.c_void.html) |
| 118 |
#[deprecated = "This rust target is deprecated. If you have a good reason to use this target please report it at https://github.com/rust-lang/rust-bindgen/issues"] => Stable_1_30 => 1.30; |
| 119 |
/// Rust stable 1.33 |
| 120 |
/// * repr(packed(N)) ([PR](https://github.com/rust-lang/rust/pull/57049)) |
| 121 |
=> Stable_1_33 => 1.33; |
| 122 |
/// Rust stable 1.36 |
| 123 |
/// * `MaybeUninit` instead of `mem::uninitialized()` ([PR](https://github.com/rust-lang/rust/pull/60445)) |
| 124 |
=> Stable_1_36 => 1.36; |
| 125 |
/// Rust stable 1.40 |
| 126 |
/// * `non_exhaustive` enums/structs ([Tracking issue](https://github.com/rust-lang/rust/issues/44109)) |
| 127 |
=> Stable_1_40 => 1.40; |
| 128 |
/// Rust stable 1.47 |
| 129 |
/// * `larger_arrays` ([Tracking issue](https://github.com/rust-lang/rust/pull/74060)) |
| 130 |
=> Stable_1_47 => 1.47; |
| 131 |
/// Rust stable 1.64 |
| 132 |
/// * `core_ffi_c` ([Tracking issue](https://github.com/rust-lang/rust/issues/94501)) |
| 133 |
=> Stable_1_64 => 1.64; |
| 134 |
/// Rust stable 1.68 |
| 135 |
/// * `abi_efiapi` calling convention ([Tracking issue](https://github.com/rust-lang/rust/issues/65815)) |
| 136 |
=> Stable_1_68 => 1.68; |
| 137 |
/// Nightly rust |
| 138 |
/// * `thiscall` calling convention ([Tracking issue](https://github.com/rust-lang/rust/issues/42202)) |
| 139 |
/// * `vectorcall` calling convention (no tracking issue) |
| 140 |
/// * `c_unwind` calling convention ([Tracking issue](https://github.com/rust-lang/rust/issues/74990)) |
| 141 |
=> Nightly => nightly; |
| 142 |
); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
rust_target_base!(rust_target_def); |
| 147 |
rust_target_base!(rust_target_values_def); |
| 148 |
|
| 149 |
/// Latest stable release of Rust |
| 150 |
pub const LATEST_STABLE_RUST: RustTarget = RustTarget::Stable_1_68; |
| 151 |
|
| 152 |
/// Create RustFeatures struct definition, new(), and a getter for each field |
| 153 |
macro_rules! rust_feature_def { |
| 154 |
( |
| 155 |
$( $rust_target:ident { |
| 156 |
$( $( #[$attr:meta] )* => $feature:ident; )* |
| 157 |
} )* |
| 158 |
) => { |
| 159 |
/// Features supported by a rust target |
| 160 |
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] |
| 161 |
#[allow(missing_docs)] // Documentation should go into the relevant variants. |
| 162 |
pub(crate) struct RustFeatures { |
| 163 |
$( $( |
| 164 |
$( |
| 165 |
#[$attr] |
| 166 |
)* |
| 167 |
pub $feature: bool, |
| 168 |
)* )* |
| 169 |
} |
| 170 |
|
| 171 |
impl RustFeatures { |
| 172 |
/// Gives a RustFeatures struct with all features disabled |
| 173 |
fn new() -> Self { |
| 174 |
RustFeatures { |
| 175 |
$( $( |
| 176 |
$feature: false, |
| 177 |
)* )* |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
impl From<RustTarget> for RustFeatures { |
| 183 |
fn from(rust_target: RustTarget) -> Self { |
| 184 |
let mut features = RustFeatures::new(); |
| 185 |
|
| 186 |
$( |
| 187 |
if rust_target >= RustTarget::$rust_target { |
| 188 |
$( |
| 189 |
features.$feature = true; |
| 190 |
)* |
| 191 |
} |
| 192 |
)* |
| 193 |
|
| 194 |
features |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
// NOTE(emilio): When adding or removing features here, make sure to update the |
| 201 |
// documentation for the relevant variant in the rust_target_base macro |
| 202 |
// definition. |
| 203 |
rust_feature_def!( |
| 204 |
Stable_1_17 { |
| 205 |
=> static_lifetime_elision; |
| 206 |
} |
| 207 |
Stable_1_19 { |
| 208 |
=> untagged_union; |
| 209 |
} |
| 210 |
Stable_1_20 { |
| 211 |
=> associated_const; |
| 212 |
} |
| 213 |
Stable_1_21 { |
| 214 |
=> builtin_clone_impls; |
| 215 |
} |
| 216 |
Stable_1_25 { |
| 217 |
=> repr_align; |
| 218 |
} |
| 219 |
Stable_1_26 { |
| 220 |
=> i128_and_u128; |
| 221 |
} |
| 222 |
Stable_1_27 { |
| 223 |
=> must_use_function; |
| 224 |
} |
| 225 |
Stable_1_28 { |
| 226 |
=> repr_transparent; |
| 227 |
} |
| 228 |
Stable_1_30 { |
| 229 |
=> min_const_fn; |
| 230 |
=> core_ffi_c_void; |
| 231 |
} |
| 232 |
Stable_1_33 { |
| 233 |
=> repr_packed_n; |
| 234 |
} |
| 235 |
Stable_1_36 { |
| 236 |
=> maybe_uninit; |
| 237 |
} |
| 238 |
Stable_1_40 { |
| 239 |
=> non_exhaustive; |
| 240 |
} |
| 241 |
Stable_1_47 { |
| 242 |
=> larger_arrays; |
| 243 |
} |
| 244 |
Stable_1_64 { |
| 245 |
=> core_ffi_c; |
| 246 |
} |
| 247 |
Stable_1_68 { |
| 248 |
=> abi_efiapi; |
| 249 |
} |
| 250 |
Nightly { |
| 251 |
=> thiscall_abi; |
| 252 |
=> vectorcall_abi; |
| 253 |
=> c_unwind_abi; |
| 254 |
} |
| 255 |
); |
| 256 |
|
| 257 |
impl Default for RustFeatures { |
| 258 |
fn default() -> Self { |
| 259 |
let default_rust_target: RustTarget = Default::default(); |
| 260 |
Self::from(default_rust_target) |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
#[cfg(test)] |
| 265 |
mod test { |
| 266 |
#![allow(unused_imports)] |
| 267 |
use super::*; |
| 268 |
|
| 269 |
#[test] |
| 270 |
fn target_features() { |
| 271 |
let f_1_0 = RustFeatures::from(RustTarget::Stable_1_0); |
| 272 |
assert!( |
| 273 |
!f_1_0.static_lifetime_elision && |
| 274 |
!f_1_0.core_ffi_c_void && |
| 275 |
!f_1_0.untagged_union && |
| 276 |
!f_1_0.associated_const && |
| 277 |
!f_1_0.builtin_clone_impls && |
| 278 |
!f_1_0.repr_align && |
| 279 |
!f_1_0.thiscall_abi && |
| 280 |
!f_1_0.vectorcall_abi |
| 281 |
); |
| 282 |
let f_1_21 = RustFeatures::from(RustTarget::Stable_1_21); |
| 283 |
assert!( |
| 284 |
f_1_21.static_lifetime_elision && |
| 285 |
!f_1_21.core_ffi_c_void && |
| 286 |
f_1_21.untagged_union && |
| 287 |
f_1_21.associated_const && |
| 288 |
f_1_21.builtin_clone_impls && |
| 289 |
!f_1_21.repr_align && |
| 290 |
!f_1_21.thiscall_abi && |
| 291 |
!f_1_21.vectorcall_abi |
| 292 |
); |
| 293 |
let f_nightly = RustFeatures::from(RustTarget::Nightly); |
| 294 |
assert!( |
| 295 |
f_nightly.static_lifetime_elision && |
| 296 |
f_nightly.core_ffi_c_void && |
| 297 |
f_nightly.untagged_union && |
| 298 |
f_nightly.associated_const && |
| 299 |
f_nightly.builtin_clone_impls && |
| 300 |
f_nightly.maybe_uninit && |
| 301 |
f_nightly.repr_align && |
| 302 |
f_nightly.thiscall_abi && |
| 303 |
f_nightly.vectorcall_abi && |
| 304 |
f_nightly.c_unwind_abi |
| 305 |
); |
| 306 |
} |
| 307 |
|
| 308 |
fn test_target(target_str: &str, target: RustTarget) { |
| 309 |
let target_string: String = target.into(); |
| 310 |
assert_eq!(target_str, target_string); |
| 311 |
assert_eq!(target, RustTarget::from_str(target_str).unwrap()); |
| 312 |
} |
| 313 |
|
| 314 |
#[test] |
| 315 |
fn str_to_target() { |
| 316 |
test_target("1.0", RustTarget::Stable_1_0); |
| 317 |
test_target("1.17", RustTarget::Stable_1_17); |
| 318 |
test_target("1.19", RustTarget::Stable_1_19); |
| 319 |
test_target("1.21", RustTarget::Stable_1_21); |
| 320 |
test_target("1.25", RustTarget::Stable_1_25); |
| 321 |
test_target("nightly", RustTarget::Nightly); |
| 322 |
} |
| 323 |
} |