Skip to content

Commit b6d10f6

Browse files
committed
Remove Q* type aliases for MpMcQueue, and rename it to just Queue
1 parent a849a60 commit b6d10f6

File tree

3 files changed

+49
-67
lines changed

3 files changed

+49
-67
lines changed

src/mpmc.rs

Lines changed: 40 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
//! use cortex_m::{asm, peripheral::syst::SystClkSource};
1818
//! use cortex_m_rt::{entry, exception};
1919
//! use cortex_m_semihosting::hprintln;
20-
//! use heapless::mpmc::Q2;
20+
//! use heapless::mpmc::Queue::<_, 2>;
2121
//!
22-
//! static Q: Q2<u8> = Q2::new();
22+
//! static Q: Queue::<_, 2><u8> = Q2::new();
2323
//!
2424
//! #[entry]
2525
//! fn main() -> ! {
@@ -112,29 +112,11 @@ type IntSize = isize;
112112
#[cfg(not(feature = "mpmc_large"))]
113113
type IntSize = i8;
114114

115-
/// MPMC queue with a capability for 2 elements.
116-
pub type Q2<T> = MpMcQueue<T, 2>;
117-
118-
/// MPMC queue with a capability for 4 elements.
119-
pub type Q4<T> = MpMcQueue<T, 4>;
120-
121-
/// MPMC queue with a capability for 8 elements.
122-
pub type Q8<T> = MpMcQueue<T, 8>;
123-
124-
/// MPMC queue with a capability for 16 elements.
125-
pub type Q16<T> = MpMcQueue<T, 16>;
126-
127-
/// MPMC queue with a capability for 32 elements.
128-
pub type Q32<T> = MpMcQueue<T, 32>;
129-
130-
/// MPMC queue with a capability for 64 elements.
131-
pub type Q64<T> = MpMcQueue<T, 64>;
132-
133-
/// Base struct for [`MpMcQueue`] and [`MpMcQueueView`], generic over the [`Storage`].
115+
/// Base struct for [`Queue`] and [`QueueView`], generic over the [`Storage`].
134116
///
135-
/// In most cases you should use [`MpMcQueue`] or [`MpMcQueueView`] directly. Only use this
117+
/// In most cases you should use [`Queue`] or [`QueueView`] directly. Only use this
136118
/// struct if you want to write code that's generic over both.
137-
pub struct MpMcQueueInner<T, S: Storage> {
119+
pub struct QueueInner<T, S: Storage> {
138120
dequeue_pos: AtomicTargetSize,
139121
enqueue_pos: AtomicTargetSize,
140122
buffer: UnsafeCell<S::Buffer<Cell<T>>>,
@@ -143,14 +125,14 @@ pub struct MpMcQueueInner<T, S: Storage> {
143125
/// MPMC queue with a capacity for N elements
144126
/// N must be a power of 2
145127
/// The max value of N is `u8::MAX` - 1 if `mpmc_large` feature is not enabled.
146-
pub type MpMcQueue<T, const N: usize> = MpMcQueueInner<T, OwnedStorage<N>>;
128+
pub type Queue<T, const N: usize> = QueueInner<T, OwnedStorage<N>>;
147129

148130
/// MPMC queue with a capacity for N elements
149131
/// N must be a power of 2
150132
/// The max value of N is `u8::MAX` - 1 if `mpmc_large` feature is not enabled.
151-
pub type MpMcQueueView<T> = MpMcQueueInner<T, ViewStorage>;
133+
pub type QueueView<T> = QueueInner<T, ViewStorage>;
152134

153-
impl<T, const N: usize> MpMcQueue<T, N> {
135+
impl<T, const N: usize> Queue<T, N> {
154136
/// Creates an empty queue
155137
pub const fn new() -> Self {
156138
const {
@@ -175,54 +157,54 @@ impl<T, const N: usize> MpMcQueue<T, N> {
175157
}
176158

177159
/// Used in `Storage` implementation
178-
pub(crate) fn as_view_private(&self) -> &MpMcQueueView<T> {
160+
pub(crate) fn as_view_private(&self) -> &QueueView<T> {
179161
self
180162
}
181163
/// Used in `Storage` implementation
182-
pub(crate) fn as_view_mut_private(&mut self) -> &mut MpMcQueueView<T> {
164+
pub(crate) fn as_view_mut_private(&mut self) -> &mut QueueView<T> {
183165
self
184166
}
185167
}
186168

187-
impl<T, S: Storage> MpMcQueueInner<T, S> {
188-
/// Get a reference to the `MpMcQueue`, erasing the `N` const-generic.
169+
impl<T, S: Storage> QueueInner<T, S> {
170+
/// Get a reference to the `Queue`, erasing the `N` const-generic.
189171
///
190172
///
191173
/// ```rust
192-
/// # use heapless::mpmc::{MpMcQueue, MpMcQueueView};
193-
/// let queue: MpMcQueue<u8, 2> = MpMcQueue::new();
194-
/// let view: &MpMcQueueView<u8> = queue.as_view();
174+
/// # use heapless::mpmc::{Queue, QueueView};
175+
/// let queue: Queue<u8, 2> = Queue::new();
176+
/// let view: &QueueView<u8> = queue.as_view();
195177
/// ```
196178
///
197-
/// It is often preferable to do the same through type coerction, since `MpMcQueue<T, N>` implements `Unsize<MpMcQueueView<T>>`:
179+
/// It is often preferable to do the same through type coerction, since `Queue<T, N>` implements `Unsize<QueueView<T>>`:
198180
///
199181
/// ```rust
200-
/// # use heapless::mpmc::{MpMcQueue, MpMcQueueView};
201-
/// let queue: MpMcQueue<u8, 2> = MpMcQueue::new();
202-
/// let view: &MpMcQueueView<u8> = &queue;
182+
/// # use heapless::mpmc::{Queue, QueueView};
183+
/// let queue: Queue<u8, 2> = Queue::new();
184+
/// let view: &QueueView<u8> = &queue;
203185
/// ```
204186
#[inline]
205-
pub fn as_view(&self) -> &MpMcQueueView<T> {
187+
pub fn as_view(&self) -> &QueueView<T> {
206188
S::as_mpmc_view(self)
207189
}
208190

209-
/// Get a mutable reference to the `MpMcQueue`, erasing the `N` const-generic.
191+
/// Get a mutable reference to the `Queue`, erasing the `N` const-generic.
210192
///
211193
/// ```rust
212-
/// # use heapless::mpmc::{MpMcQueue, MpMcQueueView};
213-
/// let mut queue: MpMcQueue<u8, 2> = MpMcQueue::new();
214-
/// let view: &mut MpMcQueueView<u8> = queue.as_mut_view();
194+
/// # use heapless::mpmc::{Queue, QueueView};
195+
/// let mut queue: Queue<u8, 2> = Queue::new();
196+
/// let view: &mut QueueView<u8> = queue.as_mut_view();
215197
/// ```
216198
///
217-
/// It is often preferable to do the same through type coerction, since `MpMcQueue<T, N>` implements `Unsize<MpMcQueueView<T>>`:
199+
/// It is often preferable to do the same through type coerction, since `Queue<T, N>` implements `Unsize<QueueView<T>>`:
218200
///
219201
/// ```rust
220-
/// # use heapless::mpmc::{MpMcQueue, MpMcQueueView};
221-
/// let mut queue: MpMcQueue<u8, 2> = MpMcQueue::new();
222-
/// let view: &mut MpMcQueueView<u8> = &mut queue;
202+
/// # use heapless::mpmc::{Queue, QueueView};
203+
/// let mut queue: Queue<u8, 2> = Queue::new();
204+
/// let view: &mut QueueView<u8> = &mut queue;
223205
/// ```
224206
#[inline]
225-
pub fn as_mut_view(&mut self) -> &mut MpMcQueueView<T> {
207+
pub fn as_mut_view(&mut self) -> &mut QueueView<T> {
226208
S::as_mpmc_mut_view(self)
227209
}
228210

@@ -250,20 +232,20 @@ impl<T, S: Storage> MpMcQueueInner<T, S> {
250232
}
251233
}
252234

253-
impl<T, const N: usize> Default for MpMcQueue<T, N> {
235+
impl<T, const N: usize> Default for Queue<T, N> {
254236
fn default() -> Self {
255237
Self::new()
256238
}
257239
}
258240

259-
impl<T, S: Storage> Drop for MpMcQueueInner<T, S> {
241+
impl<T, S: Storage> Drop for QueueInner<T, S> {
260242
fn drop(&mut self) {
261243
// drop all contents currently in the queue
262244
while self.dequeue().is_some() {}
263245
}
264246
}
265247

266-
unsafe impl<T, S: Storage> Sync for MpMcQueueInner<T, S> where T: Send {}
248+
unsafe impl<T, S: Storage> Sync for QueueInner<T, S> where T: Send {}
267249

268250
struct Cell<T> {
269251
data: MaybeUninit<T>,
@@ -370,16 +352,16 @@ unsafe fn enqueue<T>(
370352
mod tests {
371353
use static_assertions::assert_not_impl_any;
372354

373-
use super::{MpMcQueue, Q2};
355+
use super::Queue;
374356

375-
// Ensure a `MpMcQueue` containing `!Send` values stays `!Send` itself.
376-
assert_not_impl_any!(MpMcQueue<*const (), 4>: Send);
357+
// Ensure a `Queue` containing `!Send` values stays `!Send` itself.
358+
assert_not_impl_any!(Queue<*const (), 4>: Send);
377359

378360
#[test]
379361
fn memory_leak() {
380362
droppable!();
381363

382-
let q = Q2::new();
364+
let q = Queue::<_, 2>::new();
383365
q.enqueue(Droppable::new()).unwrap_or_else(|_| panic!());
384366
q.enqueue(Droppable::new()).unwrap_or_else(|_| panic!());
385367
drop(q);
@@ -389,7 +371,7 @@ mod tests {
389371

390372
#[test]
391373
fn sanity() {
392-
let q = Q2::new();
374+
let q = Queue::<_, 2>::new();
393375
q.enqueue(0).unwrap();
394376
q.enqueue(1).unwrap();
395377
assert!(q.enqueue(2).is_err());
@@ -401,7 +383,7 @@ mod tests {
401383

402384
#[test]
403385
fn drain_at_pos255() {
404-
let q = Q2::new();
386+
let q = Queue::<_, 2>::new();
405387
for _ in 0..255 {
406388
assert!(q.enqueue(0).is_ok());
407389
assert_eq!(q.dequeue(), Some(0));
@@ -413,7 +395,7 @@ mod tests {
413395

414396
#[test]
415397
fn full_at_wrapped_pos0() {
416-
let q = Q2::new();
398+
let q = Queue::<_, 2>::new();
417399
for _ in 0..254 {
418400
assert!(q.enqueue(0).is_ok());
419401
assert_eq!(q.dequeue(), Some(0));
@@ -432,7 +414,7 @@ mod tests {
432414
#[cfg(feature = "mpmc_large")]
433415
const CAPACITY: usize = 256;
434416

435-
let q: MpMcQueue<u8, CAPACITY> = MpMcQueue::new();
417+
let q: Queue<u8, CAPACITY> = Queue::new();
436418

437419
for _ in 0..CAPACITY {
438420
q.enqueue(0xAA).unwrap();

src/storage.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::spsc;
1414
all(feature = "mpmc_large", target_has_atomic = "ptr"),
1515
all(not(feature = "mpmc_large"), target_has_atomic = "8")
1616
))]
17-
use crate::mpmc::{MpMcQueue, MpMcQueueInner, MpMcQueueView};
17+
use crate::mpmc;
1818

1919
pub(crate) trait SealedStorage {
2020
type Buffer<T>: ?Sized + Borrow<[T]> + BorrowMut<[T]>;
@@ -30,15 +30,15 @@ pub(crate) trait SealedStorage {
3030
all(feature = "mpmc_large", target_has_atomic = "ptr"),
3131
all(not(feature = "mpmc_large"), target_has_atomic = "8")
3232
))]
33-
fn as_mpmc_view<T>(this: &MpMcQueueInner<T, Self>) -> &MpMcQueueView<T>
33+
fn as_mpmc_view<T>(this: &mpmc::QueueInner<T, Self>) -> &mpmc::QueueView<T>
3434
where
3535
Self: Storage + Sized;
3636
#[cfg(any(
3737
feature = "portable-atomic",
3838
all(feature = "mpmc_large", target_has_atomic = "ptr"),
3939
all(not(feature = "mpmc_large"), target_has_atomic = "8")
4040
))]
41-
fn as_mpmc_mut_view<T>(this: &mut MpMcQueueInner<T, Self>) -> &mut MpMcQueueView<T>
41+
fn as_mpmc_mut_view<T>(this: &mut mpmc::QueueInner<T, Self>) -> &mut mpmc::QueueView<T>
4242
where
4343
Self: Storage + Sized;
4444

@@ -100,7 +100,7 @@ impl<const N: usize> SealedStorage for OwnedStorage<N> {
100100
all(feature = "mpmc_large", target_has_atomic = "ptr"),
101101
all(not(feature = "mpmc_large"), target_has_atomic = "8")
102102
))]
103-
fn as_mpmc_view<T>(this: &MpMcQueue<T, N>) -> &MpMcQueueView<T>
103+
fn as_mpmc_view<T>(this: &mpmc::Queue<T, N>) -> &mpmc::QueueView<T>
104104
where
105105
Self: Storage + Sized,
106106
{
@@ -112,7 +112,7 @@ impl<const N: usize> SealedStorage for OwnedStorage<N> {
112112
all(feature = "mpmc_large", target_has_atomic = "ptr"),
113113
all(not(feature = "mpmc_large"), target_has_atomic = "8")
114114
))]
115-
fn as_mpmc_mut_view<T>(this: &mut MpMcQueue<T, N>) -> &mut MpMcQueueView<T>
115+
fn as_mpmc_mut_view<T>(this: &mut mpmc::Queue<T, N>) -> &mut mpmc::QueueView<T>
116116
where
117117
Self: Storage + Sized,
118118
{
@@ -165,7 +165,7 @@ impl SealedStorage for ViewStorage {
165165
all(feature = "mpmc_large", target_has_atomic = "ptr"),
166166
all(not(feature = "mpmc_large"), target_has_atomic = "8")
167167
))]
168-
fn as_mpmc_view<T>(this: &MpMcQueueInner<T, Self>) -> &MpMcQueueView<T>
168+
fn as_mpmc_view<T>(this: &mpmc::QueueInner<T, Self>) -> &mpmc::QueueView<T>
169169
where
170170
Self: Storage + Sized,
171171
{
@@ -177,7 +177,7 @@ impl SealedStorage for ViewStorage {
177177
all(feature = "mpmc_large", target_has_atomic = "ptr"),
178178
all(not(feature = "mpmc_large"), target_has_atomic = "8")
179179
))]
180-
fn as_mpmc_mut_view<T>(this: &mut MpMcQueueInner<T, Self>) -> &mut MpMcQueueView<T>
180+
fn as_mpmc_mut_view<T>(this: &mut mpmc::QueueInner<T, Self>) -> &mut mpmc::QueueView<T>
181181
where
182182
Self: Storage + Sized,
183183
{

tests/tsan.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,11 @@ fn contention() {
119119
fn mpmc_contention() {
120120
use std::sync::mpsc;
121121

122-
use heapless::mpmc::Q64;
122+
use heapless::mpmc::Queue;
123123

124124
const N: u32 = 64;
125125

126-
static Q: Q64<u32> = Q64::new();
126+
static Q: Queue<u32, 64> = Queue::new();
127127

128128
let (s, r) = mpsc::channel();
129129
thread::scope(|scope| {

0 commit comments

Comments
 (0)