@@ -367,27 +367,15 @@ impl<T, S: Storage> QueueInner<T, S> {
367
367
///
368
368
/// # Examples
369
369
///
370
+ /// Create a queue and split it at runtime
371
+ ///
370
372
/// ```
371
373
/// # use heapless::spsc::Queue;
372
374
/// let mut queue: Queue<(), 4> = Queue::new();
373
375
/// let (mut producer, mut consumer) = queue.split();
374
376
/// producer.enqueue(()).unwrap();
375
377
/// assert_eq!(consumer.dequeue(), Some(()));
376
378
/// ```
377
- pub fn split ( & mut self ) -> ( Producer < ' _ , T > , Consumer < ' _ , T > ) {
378
- (
379
- Producer { rb : self . as_view ( ) } ,
380
- Consumer { rb : self . as_view ( ) } ,
381
- )
382
- }
383
- }
384
-
385
- impl < T , const N : usize > Queue < T , N > {
386
- /// Splits a queue into producer and consumer endpoints.
387
- ///
388
- /// Unlike [`Queue::split`](), this method can be used in a `const` context
389
- ///
390
- /// # Examples
391
379
///
392
380
/// Create a queue at compile time, split it at runtime,
393
381
/// and pass it to an interrupt handler via a mutex.
@@ -439,6 +427,20 @@ impl<T, const N: usize> Queue<T, N> {
439
427
/// consumer.dequeue().unwrap();
440
428
/// }
441
429
/// ```
430
+ pub fn split ( & mut self ) -> ( Producer < ' _ , T > , Consumer < ' _ , T > ) {
431
+ (
432
+ Producer { rb : self . as_view ( ) } ,
433
+ Consumer { rb : self . as_view ( ) } ,
434
+ )
435
+ }
436
+ }
437
+
438
+ impl < T , const N : usize > Queue < T , N > {
439
+ /// Splits a queue into producer and consumer endpoints.
440
+ ///
441
+ /// Unlike [`Queue::split`](), this method can be used in a `const` context
442
+ ///
443
+ /// # Example
442
444
///
443
445
/// Create and split a queue at compile time, and pass it to the main
444
446
/// function and an interrupt handler via a mutex at runtime.
@@ -497,58 +499,7 @@ impl<T> QueueView<T> {
497
499
///
498
500
/// Unlike [`Queue::split`](), this method can be used in a `const` context
499
501
///
500
- /// # Examples
501
- ///
502
- /// Create a queue at compile time, split it at runtime,
503
- /// and pass it to an interrupt handler via a mutex.
504
- ///
505
- /// ```
506
- /// use core::cell::RefCell;
507
- /// use critical_section::Mutex;
508
- /// use heapless::spsc::{Producer, Queue};
509
- ///
510
- /// static PRODUCER: Mutex<RefCell<Option<Producer<'static, ()>>>> = Mutex::new(RefCell::new(None));
511
- ///
512
- /// fn interrupt() {
513
- /// let mut producer = {
514
- /// static mut P: Option<Producer<'static, ()>> = None;
515
- /// // SAFETY: Mutable access to `P` is allowed exclusively in this scope
516
- /// // and `interrupt` cannot be called directly or preempt itself.
517
- /// unsafe { &mut P }
518
- /// }
519
- /// .get_or_insert_with(|| {
520
- /// critical_section::with(|cs| PRODUCER.borrow_ref_mut(cs).take().unwrap())
521
- /// });
522
- ///
523
- /// producer.enqueue(()).unwrap();
524
- /// }
525
- ///
526
- /// fn main() {
527
- /// let mut consumer = {
528
- /// let (p, c) = {
529
- /// static mut Q: Queue<(), 4> = Queue::new();
530
- /// // SAFETY: `Q` is only accessible in this scope
531
- /// // and `main` is only called once.
532
- /// #[allow(static_mut_refs)]
533
- /// unsafe {
534
- /// Q.split()
535
- /// }
536
- /// };
537
- ///
538
- /// critical_section::with(move |cs| {
539
- /// let mut producer = PRODUCER.borrow_ref_mut(cs);
540
- /// *producer = Some(p);
541
- /// });
542
- ///
543
- /// c
544
- /// };
545
- ///
546
- /// // Interrupt occurs.
547
- /// # interrupt();
548
- ///
549
- /// consumer.dequeue().unwrap();
550
- /// }
551
- /// ```
502
+ /// # Example
552
503
///
553
504
/// Create and split a queue at compile time, and pass it to the main
554
505
/// function and an interrupt handler via a mutex at runtime.
@@ -557,13 +508,13 @@ impl<T> QueueView<T> {
557
508
/// use core::cell::RefCell;
558
509
///
559
510
/// use critical_section::Mutex;
560
- /// use heapless::spsc::{Consumer, Producer, Queue};
511
+ /// use heapless::spsc::{Consumer, Producer, Queue, QueueView };
561
512
///
562
513
/// static PC: (
563
514
/// Mutex<RefCell<Option<Producer<'_, ()>>>>,
564
515
/// Mutex<RefCell<Option<Consumer<'_, ()>>>>,
565
516
/// ) = {
566
- /// static mut Q: Queue <(), 4 > = Queue::new();
517
+ /// static mut Q: &mut QueueView <()> = &mut Queue::<(), 4> ::new();
567
518
/// // SAFETY: `Q` is only accessible in this scope.
568
519
/// #[allow(static_mut_refs)]
569
520
/// let (p, c) = unsafe { Q.split_const() };
0 commit comments