mirror of
https://github.com/rust-embedded/heapless.git
synced 2025-09-27 04:20:24 +00:00
Fix MPMC size asserts
This commit is contained in:
parent
1c1dd43e08
commit
bd3cbaba61
@ -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];
|
||||
|
@ -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));
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user