diff --git a/crossbeam-channel/src/flavors/array.rs b/crossbeam-channel/src/flavors/array.rs index 659fce69a..29aa6a129 100644 --- a/crossbeam-channel/src/flavors/array.rs +++ b/crossbeam-channel/src/flavors/array.rs @@ -114,23 +114,23 @@ impl Channel { // Tail is initialized to `{ lap: 0, mark: 0, index: 0 }`. let tail = 0; - // Allocate a buffer of `cap` slots. + // Allocate a buffer of `cap` slots initialized + // with stamps. let buffer = { - let mut v = Vec::>::with_capacity(cap); + let mut v: Vec> = (0..cap) + .map(|i| { + // Set the stamp to `{ lap: 0, mark: 0, index: i }`. + Slot { + stamp: AtomicUsize::new(i), + msg: UnsafeCell::new(MaybeUninit::uninit()), + } + }) + .collect(); let ptr = v.as_mut_ptr(); mem::forget(v); ptr }; - // Initialize stamps in the slots. - for i in 0..cap { - unsafe { - // Set the stamp to `{ lap: 0, mark: 0, index: i }`. - let slot = buffer.add(i); - ptr::write(&mut (*slot).stamp, AtomicUsize::new(i)); - } - } - Channel { buffer, cap, diff --git a/crossbeam-queue/src/array_queue.rs b/crossbeam-queue/src/array_queue.rs index 45b055abd..bce9d2d69 100644 --- a/crossbeam-queue/src/array_queue.rs +++ b/crossbeam-queue/src/array_queue.rs @@ -13,7 +13,6 @@ use core::cell::UnsafeCell; use core::fmt; use core::marker::PhantomData; use core::mem; -use core::ptr; use core::sync::atomic::{self, AtomicUsize, Ordering}; use crossbeam_utils::{Backoff, CachePadded}; @@ -110,23 +109,23 @@ impl ArrayQueue { let head = 0; let tail = 0; - // Allocate a buffer of `cap` slots. + // Allocate a buffer of `cap` slots initialized + // with stamps. let buffer = { - let mut v = Vec::>::with_capacity(cap); + let mut v: Vec> = (0..cap) + .map(|i| { + // Set the stamp to `{ lap: 0, index: i }`. + Slot { + stamp: AtomicUsize::new(i), + value: UnsafeCell::new(MaybeUninit::uninit()), + } + }) + .collect(); let ptr = v.as_mut_ptr(); mem::forget(v); ptr }; - // Initialize stamps in the slots. - for i in 0..cap { - unsafe { - // Set the stamp to `{ lap: 0, index: i }`. - let slot = buffer.add(i); - ptr::write(&mut (*slot).stamp, AtomicUsize::new(i)); - } - } - // One lap is the smallest power of two greater than `cap`. let one_lap = (cap + 1).next_power_of_two();