Struct heapless::ring_buffer::RingBuffer[][src]

pub struct RingBuffer<T, N, U = usize> where
    N: Add<U1> + Unsigned,
    Sum<N, U1>: ArrayLength<T>,
    U: Uxx
{ /* fields omitted */ }

A statically allocated ring buffer with a capacity of N

By default RingBuffer will use usize integers to hold the indices to its head and tail. For small ring buffers usize may be overkill. However, RingBuffer's index type is generic and can be changed to u8 or u16 to reduce its footprint. The easiest to construct a RingBuffer with a smaller index type is to use the u8 and u16 constructors.

Examples

use heapless::RingBuffer;
use heapless::consts::*;

let mut rb: RingBuffer<u8, U3> = RingBuffer::new();

assert!(rb.enqueue(0).is_ok());
assert!(rb.enqueue(1).is_ok());
assert!(rb.enqueue(2).is_ok());
assert!(rb.enqueue(3).is_err()); // full

assert_eq!(rb.dequeue(), Some(0));

Single producer single consumer mode

use heapless::RingBuffer;
use heapless::consts::*;

// static mut RB: RingBuffer<Event, U4> = RingBuffer::new(); // requires feature `const-fn`

static mut RB: Option<RingBuffer<Event, U4>>  = None;

enum Event { A, B }

fn main() {
    unsafe { RB = Some(RingBuffer::new()) };
    // NOTE(unsafe) beware of aliasing the `consumer` end point
    let mut consumer = unsafe { RB.as_mut().unwrap().split().1 };

    loop {
        // `dequeue` is a lockless operation
        match consumer.dequeue() {
            Some(Event::A) => { /* .. */ },
            Some(Event::B) => { /* .. */ },
            None => { /* sleep */},
        }
    }
}

// this is a different execution context that can preempt `main`
fn interrupt_handler() {
    // NOTE(unsafe) beware of aliasing the `producer` end point
    let mut producer = unsafe { RB.as_mut().unwrap().split().0 };

    // ..

    if condition {
        producer.enqueue(Event::A).ok().unwrap();
    } else {
        producer.enqueue(Event::B).ok().unwrap();
    }

    // ..
}

Methods

impl<T, N, U> RingBuffer<T, N, U> where
    N: Add<U1> + Unsigned,
    Sum<N, U1>: ArrayLength<T>,
    U: Uxx
[src]

Splits a statically allocated ring buffer into producer and consumer end points

impl<T, N, U> RingBuffer<T, N, U> where
    N: Add<U1> + Unsigned,
    Sum<N, U1>: ArrayLength<T>,
    U: Uxx
[src]

Returns the maximum number of elements the ring buffer can hold

Returns true if the ring buffer has a length of 0

Important traits for Iter<'a, T, N, U>

Iterates from the front of the queue to the back

Important traits for IterMut<'a, T, N, U>

Returns an iterator that allows modifying each value.

impl<T, N> RingBuffer<T, N, usize> where
    N: Add<U1> + Unsigned,
    Sum<N, U1>: ArrayLength<T>, 
[src]

impl<T, N> RingBuffer<T, N, u8> where
    N: Add<U1> + Unsigned,
    Sum<N, U1>: ArrayLength<T>, 
[src]

Creates an empty ring buffer with a fixed capacity of N

Returns the item in the front of the queue, or None if the queue is empty

Adds an item to the end of the queue

Returns back the item if the queue is full

Adds an item to the end of the queue, without checking if it's full

WARNING If the queue is full this operation will make the queue appear empty to the Consumer, thus leaking (destructors won't run) all the elements that were in the queue.

Returns the number of elements in the queue

impl<T, N> RingBuffer<T, N, u16> where
    N: Add<U1> + Unsigned,
    Sum<N, U1>: ArrayLength<T>, 
[src]

Creates an empty ring buffer with a fixed capacity of N

Returns the item in the front of the queue, or None if the queue is empty

Adds an item to the end of the queue

Returns back the item if the queue is full

Adds an item to the end of the queue, without checking if it's full

WARNING If the queue is full this operation will make the queue appear empty to the Consumer, thus leaking (destructors won't run) all the elements that were in the queue.

Returns the number of elements in the queue

impl<T, N> RingBuffer<T, N, usize> where
    N: Add<U1> + Unsigned,
    Sum<N, U1>: ArrayLength<T>, 
[src]

Creates an empty ring buffer with a fixed capacity of N

Returns the item in the front of the queue, or None if the queue is empty

Adds an item to the end of the queue

Returns back the item if the queue is full

Adds an item to the end of the queue, without checking if it's full

WARNING If the queue is full this operation will make the queue appear empty to the Consumer, thus leaking (destructors won't run) all the elements that were in the queue.

Returns the number of elements in the queue

Trait Implementations

impl<T, N, U> Drop for RingBuffer<T, N, U> where
    N: Add<U1> + Unsigned,
    Sum<N, U1>: ArrayLength<T>,
    U: Uxx
[src]

Executes the destructor for this type. Read more

impl<'a, T, N, U> IntoIterator for &'a RingBuffer<T, N, U> where
    N: Add<U1> + Unsigned,
    Sum<N, U1>: ArrayLength<T>,
    U: Uxx
[src]

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

impl<'a, T, N, U> IntoIterator for &'a mut RingBuffer<T, N, U> where
    N: Add<U1> + Unsigned,
    Sum<N, U1>: ArrayLength<T>,
    U: Uxx
[src]

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

Auto Trait Implementations

impl<T, N, U> Send for RingBuffer<T, N, U> where
    T: Send

impl<T, N, U = usize> !Sync for RingBuffer<T, N, U>