Module history_buf

Module history_buf 

Source
Expand description

A “history buffer”, similar to a write-only ring buffer of fixed length.

This buffer keeps a fixed number of elements. On write, the oldest element is overwritten. Thus, the buffer is useful to keep a history of values with some desired depth, and for example calculate a rolling average.

§Examples

use heapless::HistoryBuf;

// Initialize a new buffer with 8 elements.
let mut buf = HistoryBuf::<_, 8>::new();

// Starts with no data
assert_eq!(buf.recent(), None);

buf.write(3);
buf.write(5);
buf.extend(&[4, 4]);

// The most recent written element is a four.
assert_eq!(buf.recent(), Some(&4));

// To access all elements in an unspecified order, use `as_slice()`.
for el in buf.as_slice() {
    println!("{:?}", el);
}

// Now we can prepare an average of all values, which comes out to 4.
let avg = buf.as_slice().iter().sum::<usize>() / buf.len();
assert_eq!(avg, 4);

Structs§

HistoryBufInner
Base struct for HistoryBuf and HistoryBufView, generic over the HistoryBufStorage.
OldestOrdered
Double ended iterator on the underlying buffer ordered from the oldest data to the newest.

Traits§

HistoryBufStorage
Trait defining how data for a container is stored.

Type Aliases§

HistoryBuf
A “history buffer”, similar to a write-only ring buffer of fixed length.
HistoryBufView
A “view” into a HistoryBuf
OwnedHistoryBufStorage
Implementation of HistoryBufStorage that stores the data in an array [T; N] whose size is known at compile time.
ViewHistoryBufStorage
Implementation of HistoryBufStorage that stores the data in an unsized [T].