Skip to content

Commit bd3cbab

Browse files
committed
Fix MPMC size asserts
1 parent 1c1dd43 commit bd3cbab

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/mpmc.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ pub type Q32<T> = MpMcQueue<T, 32>;
122122
pub type Q64<T> = MpMcQueue<T, 64>;
123123

124124
/// MPMC queue with a capacity for N elements
125+
/// N must be a power of 2
125126
/// The max value of N is u8::MAX - 1 if `mpmc_large` feature is not enabled.
126127
pub struct MpMcQueue<T, const N: usize> {
127128
buffer: UnsafeCell<[Cell<T>; N]>,
@@ -138,7 +139,8 @@ impl<T, const N: usize> MpMcQueue<T, N> {
138139
/// Creates an empty queue
139140
pub const fn new() -> Self {
140141
// Const assert
141-
crate::sealed::greater_than_0::<N>();
142+
crate::sealed::greater_than_1::<N>();
143+
crate::sealed::power_of_two::<N>();
142144

143145
// Const assert on size.
144146
Self::ASSERT[!(N < (IntSize::MAX as usize)) as usize];

src/sealed.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ pub(crate) const fn greater_than_1<const N: usize>() {
6464
Assert::<N, 1>::GREATER;
6565
}
6666

67+
#[allow(dead_code)]
68+
#[allow(path_statements)]
69+
pub(crate) const fn power_of_two<const N: usize>() {
70+
Assert::<N, 0>::GREATER;
71+
Assert::<N, 0>::POWER_OF_TWO;
72+
}
73+
6774
#[allow(dead_code)]
6875
/// Const assert hack
6976
pub struct Assert<const L: usize, const R: usize>;
@@ -87,4 +94,7 @@ impl<const L: usize, const R: usize> Assert<L, R> {
8794

8895
/// Const assert hack
8996
pub const LESS: usize = R - L - 1;
97+
98+
/// Const assert hack
99+
pub const POWER_OF_TWO: usize = 0 - (L & (L - 1));
90100
}

0 commit comments

Comments
 (0)