Skip to content

Commit b553b4f

Browse files
committed
Use const_in_array_repeat_expressions
1 parent 0e14971 commit b553b4f

File tree

6 files changed

+41
-109
lines changed

6 files changed

+41
-109
lines changed

crossbeam-channel/src/flavors/list.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ struct Slot<T> {
4949
}
5050

5151
impl<T> Slot<T> {
52+
const UNINIT: Self = Self {
53+
msg: UnsafeCell::new(MaybeUninit::uninit()),
54+
state: AtomicUsize::new(0),
55+
};
56+
5257
/// Waits until a message is written into the slot.
5358
fn wait_write(&self) {
5459
let backoff = Backoff::new();
@@ -72,13 +77,10 @@ struct Block<T> {
7277
impl<T> Block<T> {
7378
/// Creates an empty block.
7479
fn new() -> Block<T> {
75-
// SAFETY: This is safe because:
76-
// [1] `Block::next` (AtomicPtr) may be safely zero initialized.
77-
// [2] `Block::slots` (Array) may be safely zero initialized because of [3, 4].
78-
// [3] `Slot::msg` (UnsafeCell) may be safely zero initialized because it
79-
// holds a MaybeUninit.
80-
// [4] `Slot::state` (AtomicUsize) may be safely zero initialized.
81-
unsafe { MaybeUninit::zeroed().assume_init() }
80+
Self {
81+
next: AtomicPtr::new(ptr::null_mut()),
82+
slots: [Slot::UNINIT; BLOCK_CAP],
83+
}
8284
}
8385

8486
/// Waits until the next pointer is set.

crossbeam-deque/src/deque.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,11 @@ struct Slot<T> {
11191119
}
11201120

11211121
impl<T> Slot<T> {
1122+
const UNINIT: Self = Self {
1123+
task: UnsafeCell::new(MaybeUninit::uninit()),
1124+
state: AtomicUsize::new(0),
1125+
};
1126+
11221127
/// Waits until a task is written into the slot.
11231128
fn wait_write(&self) {
11241129
let backoff = Backoff::new();
@@ -1142,13 +1147,10 @@ struct Block<T> {
11421147
impl<T> Block<T> {
11431148
/// Creates an empty block that starts at `start_index`.
11441149
fn new() -> Block<T> {
1145-
// SAFETY: This is safe because:
1146-
// [1] `Block::next` (AtomicPtr) may be safely zero initialized.
1147-
// [2] `Block::slots` (Array) may be safely zero initialized because of [3, 4].
1148-
// [3] `Slot::task` (UnsafeCell) may be safely zero initialized because it
1149-
// holds a MaybeUninit.
1150-
// [4] `Slot::state` (AtomicUsize) may be safely zero initialized.
1151-
unsafe { MaybeUninit::zeroed().assume_init() }
1150+
Self {
1151+
next: AtomicPtr::new(ptr::null_mut()),
1152+
slots: [Slot::UNINIT; BLOCK_CAP],
1153+
}
11521154
}
11531155

11541156
/// Waits until the next pointer is set.

crossbeam-epoch/src/deferred.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ impl fmt::Debug for Deferred {
2929
}
3030

3131
impl Deferred {
32+
pub(crate) const NO_OP: Self = {
33+
fn no_op_call(_raw: *mut u8) {}
34+
Self {
35+
call: no_op_call,
36+
data: MaybeUninit::uninit(),
37+
_marker: PhantomData,
38+
}
39+
};
40+
3241
/// Constructs a new `Deferred` from a `FnOnce()`.
3342
pub(crate) fn new<F: FnOnce()>(f: F) -> Self {
3443
let size = mem::size_of::<F>();

crossbeam-epoch/src/internal.rs

Lines changed: 4 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -106,95 +106,19 @@ impl Bag {
106106
}
107107

108108
impl Default for Bag {
109-
#[rustfmt::skip]
110109
fn default() -> Self {
111-
// TODO: [no_op; MAX_OBJECTS] syntax blocked by https://github.com/rust-lang/rust/issues/49147
112-
#[cfg(not(crossbeam_sanitize))]
113-
return Bag {
110+
Bag {
114111
len: 0,
115-
deferreds: [
116-
Deferred::new(no_op_func),
117-
Deferred::new(no_op_func),
118-
Deferred::new(no_op_func),
119-
Deferred::new(no_op_func),
120-
Deferred::new(no_op_func),
121-
Deferred::new(no_op_func),
122-
Deferred::new(no_op_func),
123-
Deferred::new(no_op_func),
124-
Deferred::new(no_op_func),
125-
Deferred::new(no_op_func),
126-
Deferred::new(no_op_func),
127-
Deferred::new(no_op_func),
128-
Deferred::new(no_op_func),
129-
Deferred::new(no_op_func),
130-
Deferred::new(no_op_func),
131-
Deferred::new(no_op_func),
132-
Deferred::new(no_op_func),
133-
Deferred::new(no_op_func),
134-
Deferred::new(no_op_func),
135-
Deferred::new(no_op_func),
136-
Deferred::new(no_op_func),
137-
Deferred::new(no_op_func),
138-
Deferred::new(no_op_func),
139-
Deferred::new(no_op_func),
140-
Deferred::new(no_op_func),
141-
Deferred::new(no_op_func),
142-
Deferred::new(no_op_func),
143-
Deferred::new(no_op_func),
144-
Deferred::new(no_op_func),
145-
Deferred::new(no_op_func),
146-
Deferred::new(no_op_func),
147-
Deferred::new(no_op_func),
148-
Deferred::new(no_op_func),
149-
Deferred::new(no_op_func),
150-
Deferred::new(no_op_func),
151-
Deferred::new(no_op_func),
152-
Deferred::new(no_op_func),
153-
Deferred::new(no_op_func),
154-
Deferred::new(no_op_func),
155-
Deferred::new(no_op_func),
156-
Deferred::new(no_op_func),
157-
Deferred::new(no_op_func),
158-
Deferred::new(no_op_func),
159-
Deferred::new(no_op_func),
160-
Deferred::new(no_op_func),
161-
Deferred::new(no_op_func),
162-
Deferred::new(no_op_func),
163-
Deferred::new(no_op_func),
164-
Deferred::new(no_op_func),
165-
Deferred::new(no_op_func),
166-
Deferred::new(no_op_func),
167-
Deferred::new(no_op_func),
168-
Deferred::new(no_op_func),
169-
Deferred::new(no_op_func),
170-
Deferred::new(no_op_func),
171-
Deferred::new(no_op_func),
172-
Deferred::new(no_op_func),
173-
Deferred::new(no_op_func),
174-
Deferred::new(no_op_func),
175-
Deferred::new(no_op_func),
176-
Deferred::new(no_op_func),
177-
Deferred::new(no_op_func),
178-
],
179-
};
180-
#[cfg(crossbeam_sanitize)]
181-
return Bag {
182-
len: 0,
183-
deferreds: [
184-
Deferred::new(no_op_func),
185-
Deferred::new(no_op_func),
186-
Deferred::new(no_op_func),
187-
Deferred::new(no_op_func),
188-
],
189-
};
112+
deferreds: [Deferred::NO_OP; MAX_OBJECTS],
113+
}
190114
}
191115
}
192116

193117
impl Drop for Bag {
194118
fn drop(&mut self) {
195119
// Call all deferred functions.
196120
for deferred in &mut self.deferreds[..self.len] {
197-
let no_op = Deferred::new(no_op_func);
121+
let no_op = Deferred::NO_OP;
198122
let owned_deferred = mem::replace(deferred, no_op);
199123
owned_deferred.call();
200124
}
@@ -210,8 +134,6 @@ impl fmt::Debug for Bag {
210134
}
211135
}
212136

213-
fn no_op_func() {}
214-
215137
/// A pair of an epoch and a bag.
216138
#[derive(Default, Debug)]
217139
struct SealedBag {

crossbeam-queue/src/seg_queue.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ struct Slot<T> {
3535
}
3636

3737
impl<T> Slot<T> {
38+
const UNINIT: Self = Self {
39+
value: UnsafeCell::new(MaybeUninit::uninit()),
40+
state: AtomicUsize::new(0),
41+
};
42+
3843
/// Waits until a value is written into the slot.
3944
fn wait_write(&self) {
4045
let backoff = Backoff::new();
@@ -58,13 +63,10 @@ struct Block<T> {
5863
impl<T> Block<T> {
5964
/// Creates an empty block that starts at `start_index`.
6065
fn new() -> Block<T> {
61-
// SAFETY: This is safe because:
62-
// [1] `Block::next` (AtomicPtr) may be safely zero initialized.
63-
// [2] `Block::slots` (Array) may be safely zero initialized because of [3, 4].
64-
// [3] `Slot::value` (UnsafeCell) may be safely zero initialized because it
65-
// holds a MaybeUninit.
66-
// [4] `Slot::state` (AtomicUsize) may be safely zero initialized.
67-
unsafe { MaybeUninit::zeroed().assume_init() }
66+
Self {
67+
next: AtomicPtr::new(ptr::null_mut()),
68+
slots: [Slot::UNINIT; BLOCK_CAP],
69+
}
6870
}
6971

7072
/// Waits until the next pointer is set.

crossbeam-utils/src/atomic/atomic_cell.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -902,12 +902,7 @@ fn lock(addr: usize) -> &'static SeqLock {
902902
const LEN: usize = 97;
903903
#[allow(clippy::declare_interior_mutable_const)]
904904
const L: SeqLock = SeqLock::new();
905-
static LOCKS: [SeqLock; LEN] = [
906-
L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L,
907-
L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L,
908-
L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L,
909-
L, L, L, L, L, L, L,
910-
];
905+
static LOCKS: [SeqLock; LEN] = [L; LEN];
911906

912907
// If the modulus is a constant number, the compiler will use crazy math to transform this into
913908
// a sequence of cheap arithmetic operations rather than using the slow modulo instruction.

0 commit comments

Comments
 (0)