From bd3cbaba61f3b8481f227e8d42406bda912a7e1e Mon Sep 17 00:00:00 2001 From: chemicstry Date: Wed, 22 Sep 2021 17:48:53 +0300 Subject: [PATCH] Fix MPMC size asserts --- src/mpmc.rs | 4 +++- src/sealed.rs | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/mpmc.rs b/src/mpmc.rs index 9ab4e4c1..0fbf7ce3 100644 --- a/src/mpmc.rs +++ b/src/mpmc.rs @@ -122,6 +122,7 @@ pub type Q32 = MpMcQueue; pub type Q64 = MpMcQueue; /// 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 { buffer: UnsafeCell<[Cell; N]>, @@ -138,7 +139,8 @@ impl MpMcQueue { /// Creates an empty queue pub const fn new() -> Self { // Const assert - crate::sealed::greater_than_0::(); + crate::sealed::greater_than_1::(); + crate::sealed::power_of_two::(); // Const assert on size. Self::ASSERT[!(N < (IntSize::MAX as usize)) as usize]; diff --git a/src/sealed.rs b/src/sealed.rs index aeabca58..2efb07f2 100644 --- a/src/sealed.rs +++ b/src/sealed.rs @@ -64,6 +64,13 @@ pub(crate) const fn greater_than_1() { Assert::::GREATER; } +#[allow(dead_code)] +#[allow(path_statements)] +pub(crate) const fn power_of_two() { + Assert::::GREATER; + Assert::::POWER_OF_TWO; +} + #[allow(dead_code)] /// Const assert hack pub struct Assert; @@ -87,4 +94,7 @@ impl Assert { /// Const assert hack pub const LESS: usize = R - L - 1; + + /// Const assert hack + pub const POWER_OF_TWO: usize = 0 - (L & (L - 1)); }