Fix MPMC size asserts

This commit is contained in:
chemicstry 2021-09-22 17:48:53 +03:00
parent 1c1dd43e08
commit bd3cbaba61
2 changed files with 13 additions and 1 deletions

View File

@ -122,6 +122,7 @@ pub type Q32<T> = MpMcQueue<T, 32>;
pub type Q64<T> = MpMcQueue<T, 64>;
/// MPMC queue with a capacity for N elements
/// N must be a power of 2
/// The max value of N is u8::MAX - 1 if `mpmc_large` feature is not enabled.
pub struct MpMcQueue<T, const N: usize> {
buffer: UnsafeCell<[Cell<T>; N]>,
@ -138,7 +139,8 @@ impl<T, const N: usize> MpMcQueue<T, N> {
/// Creates an empty queue
pub const fn new() -> Self {
// Const assert
crate::sealed::greater_than_0::<N>();
crate::sealed::greater_than_1::<N>();
crate::sealed::power_of_two::<N>();
// Const assert on size.
Self::ASSERT[!(N < (IntSize::MAX as usize)) as usize];

View File

@ -64,6 +64,13 @@ pub(crate) const fn greater_than_1<const N: usize>() {
Assert::<N, 1>::GREATER;
}
#[allow(dead_code)]
#[allow(path_statements)]
pub(crate) const fn power_of_two<const N: usize>() {
Assert::<N, 0>::GREATER;
Assert::<N, 0>::POWER_OF_TWO;
}
#[allow(dead_code)]
/// Const assert hack
pub struct Assert<const L: usize, const R: usize>;
@ -87,4 +94,7 @@ impl<const L: usize, const R: usize> Assert<L, R> {
/// Const assert hack
pub const LESS: usize = R - L - 1;
/// Const assert hack
pub const POWER_OF_TWO: usize = 0 - (L & (L - 1));
}