Skip to content

Commit d61ab75

Browse files
authored
Merge pull request #1348 from tmfink/feature-refactor. r=emilio
Track Rust target features with declaration macro
2 parents 651d1a7 + b0a1752 commit d61ab75

File tree

1 file changed

+73
-43
lines changed

1 file changed

+73
-43
lines changed

src/features.rs

+73-43
Original file line numberDiff line numberDiff line change
@@ -110,72 +110,74 @@ pub const LATEST_STABLE_RUST: RustTarget = RustTarget::Stable_1_21;
110110

111111
/// Create RustFeatures struct definition, new(), and a getter for each field
112112
macro_rules! rust_feature_def {
113-
( $( $( #[$attr:meta] )* => $feature:ident; )* ) => {
113+
(
114+
$( $rust_target:ident {
115+
$( $( #[$attr:meta] )* => $feature:ident; )*
116+
} )*
117+
) => {
114118
/// Features supported by a rust target
115119
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
116120
pub struct RustFeatures {
117-
$(
121+
$( $(
118122
$(
119123
#[$attr]
120124
)*
121125
pub $feature: bool,
122-
)*
126+
)* )*
123127
}
124128

125129
impl RustFeatures {
126130
/// Gives a RustFeatures struct with all features disabled
127131
fn new() -> Self {
128132
RustFeatures {
129-
$(
133+
$( $(
130134
$feature: false,
131-
)*
135+
)* )*
132136
}
133137
}
134138
}
135-
}
136-
}
137139

138-
rust_feature_def!(
139-
/// Untagged unions ([RFC 1444](https://github.com/rust-lang/rfcs/blob/master/text/1444-union.md))
140-
=> untagged_union;
141-
/// `thiscall` calling convention ([Tracking issue](https://github.com/rust-lang/rust/issues/42202))
142-
=> thiscall_abi;
143-
/// builtin impls for `Clone` ([PR](https://github.com/rust-lang/rust/pull/43690))
144-
=> builtin_clone_impls;
145-
/// repr(align) https://github.com/rust-lang/rust/pull/47006
146-
=> repr_align;
147-
/// associated constants https://github.com/rust-lang/rust/issues/29646
148-
=> associated_const;
149-
);
140+
impl From<RustTarget> for RustFeatures {
141+
fn from(rust_target: RustTarget) -> Self {
142+
let mut features = RustFeatures::new();
150143

151-
impl From<RustTarget> for RustFeatures {
152-
fn from(rust_target: RustTarget) -> Self {
153-
let mut features = RustFeatures::new();
154-
155-
if rust_target >= RustTarget::Stable_1_19 {
156-
features.untagged_union = true;
157-
}
158-
159-
if rust_target >= RustTarget::Stable_1_20 {
160-
features.associated_const = true;
161-
}
162-
163-
if rust_target >= RustTarget::Stable_1_21 {
164-
features.builtin_clone_impls = true;
165-
}
166-
167-
if rust_target >= RustTarget::Stable_1_25 {
168-
features.repr_align = true;
169-
}
144+
$(
145+
if rust_target >= RustTarget::$rust_target {
146+
$(
147+
features.$feature = true;
148+
)*
149+
}
150+
)*
170151

171-
if rust_target >= RustTarget::Nightly {
172-
features.thiscall_abi = true;
152+
features
153+
}
173154
}
174-
175-
features
176155
}
177156
}
178157

158+
rust_feature_def!(
159+
Stable_1_19 {
160+
/// Untagged unions ([RFC 1444](https://github.com/rust-lang/rfcs/blob/master/text/1444-union.md))
161+
=> untagged_union;
162+
}
163+
Stable_1_20 {
164+
/// associated constants ([PR](https://github.com/rust-lang/rust/pull/42809))
165+
=> associated_const;
166+
}
167+
Stable_1_21 {
168+
/// builtin impls for `Clone` ([PR](https://github.com/rust-lang/rust/pull/43690))
169+
=> builtin_clone_impls;
170+
}
171+
Stable_1_25 {
172+
/// repr(align) ([PR](https://github.com/rust-lang/rust/pull/47006))
173+
=> repr_align;
174+
}
175+
Nightly {
176+
/// `thiscall` calling convention ([Tracking issue](https://github.com/rust-lang/rust/issues/42202))
177+
=> thiscall_abi;
178+
}
179+
);
180+
179181
impl Default for RustFeatures {
180182
fn default() -> Self {
181183
let default_rust_target: RustTarget = Default::default();
@@ -185,9 +187,37 @@ impl Default for RustFeatures {
185187

186188
#[cfg(test)]
187189
mod test {
188-
#![allow(unused_imports)]
190+
#![allow(unused_imports)]
189191
use super::*;
190192

193+
#[test]
194+
fn target_features() {
195+
let f_1_0 = RustFeatures::from(RustTarget::Stable_1_0);
196+
assert!(
197+
!f_1_0.untagged_union
198+
&& !f_1_0.associated_const
199+
&& !f_1_0.builtin_clone_impls
200+
&& !f_1_0.repr_align
201+
&& !f_1_0.thiscall_abi
202+
);
203+
let f_1_21 = RustFeatures::from(RustTarget::Stable_1_21);
204+
assert!(
205+
f_1_21.untagged_union
206+
&& f_1_21.associated_const
207+
&& f_1_21.builtin_clone_impls
208+
&& !f_1_21.repr_align
209+
&& !f_1_21.thiscall_abi
210+
);
211+
let f_nightly = RustFeatures::from(RustTarget::Nightly);
212+
assert!(
213+
f_nightly.untagged_union
214+
&& f_nightly.associated_const
215+
&& f_nightly.builtin_clone_impls
216+
&& f_nightly.repr_align
217+
&& f_nightly.thiscall_abi
218+
);
219+
}
220+
191221
fn test_target(target_str: &str, target: RustTarget) {
192222
let target_string: String = target.into();
193223
assert_eq!(target_str, target_string);

0 commit comments

Comments
 (0)