Skip to content

Commit 62b122e

Browse files
bors[bot]japaric
andauthored
Merge #323
323: spsc::Queue: make example less unsafe r=japaric a=japaric closes #314 Co-authored-by: Jorge Aparicio <[email protected]>
2 parents 8dcee5c + 3e4a253 commit 62b122e

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

src/spsc.rs

+17-12
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,30 @@
2222
//! assert_eq!(rb.dequeue(), Some(0));
2323
//! ```
2424
//!
25-
//! - `Queue` can be `split` and then be used in Single Producer Single Consumer mode
25+
//! - `Queue` can be `split` and then be used in Single Producer Single Consumer mode.
2626
//!
27-
//! ```
28-
//! use heapless::spsc::Queue;
27+
//! "no alloc" applications can create a `&'static mut` reference to a `Queue` -- using a static
28+
//! variable -- and then `split` it: this consumes the static reference. The resulting `Consumer`
29+
//! and `Producer` can then be moved into different execution contexts (threads, interrupt handlers,
30+
//! etc.)
2931
//!
30-
//! // Notice, type signature needs to be explicit for now.
31-
//! // (min_const_eval, does not allow for default type assignments)
32-
//! static mut Q: Queue<Event, 4> = Queue::new();
32+
//! ```
33+
//! use heapless::spsc::{Producer, Queue};
3334
//!
3435
//! enum Event { A, B }
3536
//!
3637
//! fn main() {
37-
//! // NOTE(unsafe) beware of aliasing the `consumer` end point
38-
//! let mut consumer = unsafe { Q.split().1 };
38+
//! let queue: &'static mut Queue<Event, 4> = {
39+
//! static mut Q: Queue<Event, 4> = Queue::new();
40+
//! unsafe { &mut Q }
41+
//! };
42+
//!
43+
//! let (producer, mut consumer) = queue.split();
44+
//!
45+
//! // `producer` can be moved into `interrupt_handler` using a static mutex or the mechanism
46+
//! // provided by the concurrency framework you are using (e.g. a resource in RTIC)
3947
//!
4048
//! loop {
41-
//! // `dequeue` is a lockless operation
4249
//! match consumer.dequeue() {
4350
//! Some(Event::A) => { /* .. */ },
4451
//! Some(Event::B) => { /* .. */ },
@@ -49,9 +56,7 @@
4956
//! }
5057
//!
5158
//! // this is a different execution context that can preempt `main`
52-
//! fn interrupt_handler() {
53-
//! // NOTE(unsafe) beware of aliasing the `producer` end point
54-
//! let mut producer = unsafe { Q.split().0 };
59+
//! fn interrupt_handler(producer: &mut Producer<'static, Event, 4>) {
5560
//! # let condition = true;
5661
//!
5762
//! // ..

0 commit comments

Comments
 (0)