Skip to content

Commit 2e5a76c

Browse files
committed
Use cfg_match in core
1 parent 19cab6b commit 2e5a76c

File tree

8 files changed

+53
-118
lines changed

8 files changed

+53
-118
lines changed

library/core/src/ffi/primitives.rs

+16-12
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type_alias! { "c_float.md", c_float = f32; }
3535
type_alias! { "c_double.md", c_double = f64; }
3636

3737
mod c_char_definition {
38-
cfg_if! {
38+
crate::cfg_match! {
3939
// These are the targets on which c_char is unsigned. Usually the
4040
// signedness is the same for all target_os values on a given architecture
4141
// but there are some exceptions (see isSignedCharDefault() in clang).
@@ -105,7 +105,7 @@ mod c_char_definition {
105105
// architecture defaults). As we only have a target for userspace apps so there are no
106106
// special cases for L4Re below.
107107
// https://github.com/rust-lang/rust/pull/132975#issuecomment-2484645240
108-
if #[cfg(all(
108+
all(
109109
not(windows),
110110
not(target_vendor = "apple"),
111111
not(target_os = "vita"),
@@ -122,24 +122,27 @@ mod c_char_definition {
122122
target_arch = "s390x",
123123
target_arch = "xtensa",
124124
)
125-
))] {
125+
) => {
126126
pub(super) type c_char = u8;
127-
} else {
128-
// On every other target, c_char is signed.
127+
}
128+
// On every other target, c_char is signed.
129+
_ => {
129130
pub(super) type c_char = i8;
130131
}
131132
}
132133
}
133134

134135
mod c_long_definition {
135-
cfg_if! {
136-
if #[cfg(any(
136+
crate::cfg_match! {
137+
any(
137138
all(target_pointer_width = "64", not(windows)),
138139
// wasm32 Linux ABI uses 64-bit long
139-
all(target_arch = "wasm32", target_os = "linux")))] {
140+
all(target_arch = "wasm32", target_os = "linux")
141+
) => {
140142
pub(super) type c_long = i64;
141143
pub(super) type c_ulong = u64;
142-
} else {
144+
}
145+
_ => {
143146
// The minimal size of `long` in the C standard is 32 bits
144147
pub(super) type c_long = i32;
145148
pub(super) type c_ulong = u32;
@@ -169,11 +172,12 @@ pub type c_ptrdiff_t = isize;
169172
pub type c_ssize_t = isize;
170173

171174
mod c_int_definition {
172-
cfg_if! {
173-
if #[cfg(any(target_arch = "avr", target_arch = "msp430"))] {
175+
crate::cfg_match! {
176+
any(target_arch = "avr", target_arch = "msp430") => {
174177
pub(super) type c_int = i16;
175178
pub(super) type c_uint = u16;
176-
} else {
179+
}
180+
_ => {
177181
pub(super) type c_int = i32;
178182
pub(super) type c_uint = u32;
179183
}

library/core/src/internal_macros.rs

-77
Original file line numberDiff line numberDiff line change
@@ -120,80 +120,3 @@ macro_rules! impl_fn_for_zst {
120120
)+
121121
}
122122
}
123-
124-
/// A macro for defining `#[cfg]` if-else statements.
125-
///
126-
/// `cfg_if` is similar to the `if/elif` C preprocessor macro by allowing definition of a cascade
127-
/// of `#[cfg]` cases, emitting the implementation which matches first.
128-
///
129-
/// This allows you to conveniently provide a long list `#[cfg]`'d blocks of code without having to
130-
/// rewrite each clause multiple times.
131-
///
132-
/// # Example
133-
///
134-
/// ```ignore(cannot-test-this-because-non-exported-macro)
135-
/// cfg_if! {
136-
/// if #[cfg(unix)] {
137-
/// fn foo() { /* unix specific functionality */ }
138-
/// } else if #[cfg(target_pointer_width = "32")] {
139-
/// fn foo() { /* non-unix, 32-bit functionality */ }
140-
/// } else {
141-
/// fn foo() { /* fallback implementation */ }
142-
/// }
143-
/// }
144-
///
145-
/// # fn main() {}
146-
/// ```
147-
// This is a copy of `cfg_if!` from the `cfg_if` crate.
148-
// The recursive invocations should use $crate if this is ever exported.
149-
macro_rules! cfg_if {
150-
// match if/else chains with a final `else`
151-
(
152-
$(
153-
if #[cfg( $i_meta:meta )] { $( $i_tokens:tt )* }
154-
) else+
155-
else { $( $e_tokens:tt )* }
156-
) => {
157-
cfg_if! {
158-
@__items () ;
159-
$(
160-
(( $i_meta ) ( $( $i_tokens )* )) ,
161-
)+
162-
(() ( $( $e_tokens )* )) ,
163-
}
164-
};
165-
166-
// Internal and recursive macro to emit all the items
167-
//
168-
// Collects all the previous cfgs in a list at the beginning, so they can be
169-
// negated. After the semicolon is all the remaining items.
170-
(@__items ( $( $_:meta , )* ) ; ) => {};
171-
(
172-
@__items ( $( $no:meta , )* ) ;
173-
(( $( $yes:meta )? ) ( $( $tokens:tt )* )) ,
174-
$( $rest:tt , )*
175-
) => {
176-
// Emit all items within one block, applying an appropriate #[cfg]. The
177-
// #[cfg] will require all `$yes` matchers specified and must also negate
178-
// all previous matchers.
179-
#[cfg(all(
180-
$( $yes , )?
181-
not(any( $( $no ),* ))
182-
))]
183-
cfg_if! { @__identity $( $tokens )* }
184-
185-
// Recurse to emit all other items in `$rest`, and when we do so add all
186-
// our `$yes` matchers to the list of `$no` matchers as future emissions
187-
// will have to negate everything we just matched as well.
188-
cfg_if! {
189-
@__items ( $( $no , )* $( $yes , )? ) ;
190-
$( $rest , )*
191-
}
192-
};
193-
194-
// Internal macro to make __apply work out right for different match types,
195-
// because of how macros match/expand stuff.
196-
(@__identity $( $tokens:tt )* ) => {
197-
$( $tokens )*
198-
};
199-
}

library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
#![feature(bigint_helper_methods)]
101101
#![feature(bstr)]
102102
#![feature(bstr_internals)]
103+
#![feature(cfg_match)]
103104
#![feature(closure_track_caller)]
104105
#![feature(const_carrying_mul_add)]
105106
#![feature(const_eval_select)]

library/core/src/num/f32.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use crate::convert::FloatToInt;
1515
use crate::num::FpCategory;
1616
use crate::panic::const_assert;
17-
use crate::{intrinsics, mem};
17+
use crate::{cfg_match, intrinsics, mem};
1818

1919
/// The radix or base of the internal representation of `f32`.
2020
/// Use [`f32::RADIX`] instead.
@@ -996,21 +996,22 @@ impl f32 {
996996
#[stable(feature = "num_midpoint", since = "1.85.0")]
997997
#[rustc_const_stable(feature = "num_midpoint", since = "1.85.0")]
998998
pub const fn midpoint(self, other: f32) -> f32 {
999-
cfg_if! {
999+
cfg_match! {
10001000
// Allow faster implementation that have known good 64-bit float
10011001
// implementations. Falling back to the branchy code on targets that don't
10021002
// have 64-bit hardware floats or buggy implementations.
10031003
// https://github.com/rust-lang/rust/pull/121062#issuecomment-2123408114
1004-
if #[cfg(any(
1005-
target_arch = "x86_64",
1006-
target_arch = "aarch64",
1007-
all(any(target_arch = "riscv32", target_arch = "riscv64"), target_feature = "d"),
1008-
all(target_arch = "arm", target_feature = "vfp2"),
1009-
target_arch = "wasm32",
1010-
target_arch = "wasm64",
1011-
))] {
1004+
any(
1005+
target_arch = "x86_64",
1006+
target_arch = "aarch64",
1007+
all(any(target_arch = "riscv32", target_arch = "riscv64"), target_feature = "d"),
1008+
all(target_arch = "arm", target_feature = "vfp2"),
1009+
target_arch = "wasm32",
1010+
target_arch = "wasm64",
1011+
) => {
10121012
((self as f64 + other as f64) / 2.0) as f32
1013-
} else {
1013+
}
1014+
_ => {
10141015
const LO: f32 = f32::MIN_POSITIVE * 2.;
10151016
const HI: f32 = f32::MAX / 2.;
10161017

library/core/src/slice/sort/select.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//! for pivot selection. Using this as a fallback ensures O(n) worst case running time with
77
//! better performance than one would get using heapsort as fallback.
88
9+
use crate::cfg_match;
910
use crate::mem::{self, SizedTypeProperties};
1011
#[cfg(not(feature = "optimize_for_size"))]
1112
use crate::slice::sort::shared::pivot::choose_pivot;
@@ -41,10 +42,11 @@ where
4142
let min_idx = min_index(v, &mut is_less).unwrap();
4243
v.swap(min_idx, index);
4344
} else {
44-
cfg_if! {
45-
if #[cfg(feature = "optimize_for_size")] {
45+
cfg_match! {
46+
feature = "optimize_for_size" => {
4647
median_of_medians(v, &mut is_less, index);
47-
} else {
48+
}
49+
_ => {
4850
partition_at_index_loop(v, index, None, &mut is_less);
4951
}
5052
}

library/core/src/slice/sort/stable/mod.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
33
#[cfg(not(any(feature = "optimize_for_size", target_pointer_width = "16")))]
44
use crate::cmp;
5-
use crate::intrinsics;
65
use crate::mem::{MaybeUninit, SizedTypeProperties};
76
#[cfg(not(any(feature = "optimize_for_size", target_pointer_width = "16")))]
87
use crate::slice::sort::shared::smallsort::{
98
SMALL_SORT_GENERAL_SCRATCH_LEN, StableSmallSortTypeImpl, insertion_sort_shift_left,
109
};
10+
use crate::{cfg_match, intrinsics};
1111

1212
pub(crate) mod merge;
1313

@@ -39,17 +39,18 @@ pub fn sort<T, F: FnMut(&T, &T) -> bool, BufT: BufGuard<T>>(v: &mut [T], is_less
3939
return;
4040
}
4141

42-
cfg_if! {
43-
if #[cfg(any(feature = "optimize_for_size", target_pointer_width = "16"))] {
42+
cfg_match! {
43+
any(feature = "optimize_for_size", target_pointer_width = "16") => {
4444
// Unlike driftsort, mergesort only requires len / 2,
4545
// not len - len / 2.
4646
let alloc_len = len / 2;
4747

48-
cfg_if! {
49-
if #[cfg(target_pointer_width = "16")] {
48+
cfg_match! {
49+
target_pointer_width = "16" => {
5050
let mut heap_buf = BufT::with_capacity(alloc_len);
5151
let scratch = heap_buf.as_uninit_slice_mut();
52-
} else {
52+
}
53+
_ => {
5354
// For small inputs 4KiB of stack storage suffices, which allows us to avoid
5455
// calling the (de-)allocator. Benchmarks showed this was quite beneficial.
5556
let mut stack_buf = AlignedStorage::<T, 4096>::new();
@@ -65,7 +66,8 @@ pub fn sort<T, F: FnMut(&T, &T) -> bool, BufT: BufGuard<T>>(v: &mut [T], is_less
6566
}
6667

6768
tiny::mergesort(v, scratch, is_less);
68-
} else {
69+
}
70+
_ => {
6971
// More advanced sorting methods than insertion sort are faster if called in
7072
// a hot loop for small inputs, but for general-purpose code the small
7173
// binary size of insertion sort is more important. The instruction cache in

library/core/src/slice/sort/unstable/mod.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
//! This module contains the entry points for `slice::sort_unstable`.
22
3-
use crate::intrinsics;
43
use crate::mem::SizedTypeProperties;
54
#[cfg(not(any(feature = "optimize_for_size", target_pointer_width = "16")))]
65
use crate::slice::sort::shared::find_existing_run;
76
#[cfg(not(any(feature = "optimize_for_size", target_pointer_width = "16")))]
87
use crate::slice::sort::shared::smallsort::insertion_sort_shift_left;
8+
use crate::{cfg_match, intrinsics};
99

1010
pub(crate) mod heapsort;
1111
pub(crate) mod quicksort;
@@ -30,10 +30,11 @@ pub fn sort<T, F: FnMut(&T, &T) -> bool>(v: &mut [T], is_less: &mut F) {
3030
return;
3131
}
3232

33-
cfg_if! {
34-
if #[cfg(any(feature = "optimize_for_size", target_pointer_width = "16"))] {
33+
cfg_match! {
34+
any(feature = "optimize_for_size", target_pointer_width = "16") => {
3535
heapsort::heapsort(v, is_less);
36-
} else {
36+
}
37+
_ => {
3738
// More advanced sorting methods than insertion sort are faster if called in
3839
// a hot loop for small inputs, but for general-purpose code the small
3940
// binary size of insertion sort is more important. The instruction cache in

library/core/src/slice/sort/unstable/quicksort.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::slice::sort::shared::pivot::choose_pivot;
99
use crate::slice::sort::shared::smallsort::UnstableSmallSortTypeImpl;
1010
#[cfg(not(feature = "optimize_for_size"))]
1111
use crate::slice::sort::unstable::heapsort;
12-
use crate::{intrinsics, ptr};
12+
use crate::{cfg_match, intrinsics, ptr};
1313

1414
/// Sorts `v` recursively.
1515
///
@@ -142,10 +142,11 @@ const fn inst_partition<T, F: FnMut(&T, &T) -> bool>() -> fn(&mut [T], &T, &mut
142142
if size_of::<T>() <= MAX_BRANCHLESS_PARTITION_SIZE {
143143
// Specialize for types that are relatively cheap to copy, where branchless optimizations
144144
// have large leverage e.g. `u64` and `String`.
145-
cfg_if! {
146-
if #[cfg(feature = "optimize_for_size")] {
145+
cfg_match! {
146+
feature = "optimize_for_size" => {
147147
partition_lomuto_branchless_simple::<T, F>
148-
} else {
148+
}
149+
_ => {
149150
partition_lomuto_branchless_cyclic::<T, F>
150151
}
151152
}

0 commit comments

Comments
 (0)