QueueView

Type Alias QueueView 

Source
pub type QueueView<T> = QueueInner<T, ViewStorage>;
Expand description

A Queue with dynamic capacity.

Queue coerces to QueueView. QueueView is !Sized, meaning it can only ever be used by reference.

Aliased Type§

pub struct QueueView<T> { /* private fields */ }

Implementations§

Source§

impl<T> QueueView<T>

Source

pub const fn split_const(&mut self) -> (Producer<'_, T>, Consumer<'_, T>)

Splits a queue into producer and consumer endpoints.

Unlike Queue::split, this method can be used in a const context

§Example

Create and split a queue at compile time, and pass it to the main function and an interrupt handler via a mutex at runtime.

use core::cell::RefCell;

use critical_section::Mutex;
use heapless::spsc::{Consumer, Producer, Queue, QueueView};

static PC: (
    Mutex<RefCell<Option<Producer<'_, ()>>>>,
    Mutex<RefCell<Option<Consumer<'_, ()>>>>,
) = {
    static mut Q: &mut QueueView<()> = &mut Queue::<(), 4>::new();
    // SAFETY: `Q` is only accessible in this scope.
    #[allow(static_mut_refs)]
    let (p, c) = unsafe { Q.split_const() };

    (
        Mutex::new(RefCell::new(Some(p))),
        Mutex::new(RefCell::new(Some(c))),
    )
};

fn interrupt() {
    let mut producer = {
        static mut P: Option<Producer<'_, ()>> = None;
        // SAFETY: Mutable access to `P` is allowed exclusively in this scope
        // and `interrupt` cannot be called directly or preempt itself.
        unsafe { &mut P }
    }
    .get_or_insert_with(|| {
        critical_section::with(|cs| PC.0.borrow_ref_mut(cs).take().unwrap())
    });

    producer.enqueue(()).unwrap();
}

fn main() {
    let mut consumer = critical_section::with(|cs| PC.1.borrow_ref_mut(cs).take().unwrap());

    // Interrupt occurs.

    consumer.dequeue().unwrap();
}