Struct heapless::HistoryBuffer[][src]

pub struct HistoryBuffer<T, const N: usize> { /* fields omitted */ }
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::HistoryBuffer;

// Initialize a new buffer with 8 elements.
let mut buf = HistoryBuffer::<_, 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);

Implementations

impl<T, const N: usize> HistoryBuffer<T, N>[src]

pub const fn new() -> Self[src]

Constructs a new history buffer.

The construction of a HistoryBuffer works in const contexts.

Examples

use heapless::HistoryBuffer;

// Allocate a 16-element buffer on the stack
let x: HistoryBuffer<u8, 16> = HistoryBuffer::new();
assert_eq!(x.len(), 0);

pub fn clear(&mut self)[src]

Clears the buffer, replacing every element with the default value of type T.

impl<T, const N: usize> HistoryBuffer<T, N> where
    T: Copy + Clone
[src]

pub fn new_with(t: T) -> Self[src]

Constructs a new history buffer, where every element is the given value.

Examples

use heapless::HistoryBuffer;

// Allocate a 16-element buffer on the stack
let mut x: HistoryBuffer<u8, 16> = HistoryBuffer::new_with(4);
// All elements are four
assert_eq!(x.as_slice(), [4; 16]);

pub fn clear_with(&mut self, t: T)[src]

Clears the buffer, replacing every element with the given value.

impl<T, const N: usize> HistoryBuffer<T, N>[src]

pub fn len(&self) -> usize[src]

Returns the current fill level of the buffer.

pub fn capacity(&self) -> usize[src]

Returns the capacity of the buffer, which is the length of the underlying backing array.

pub fn write(&mut self, t: T)[src]

Writes an element to the buffer, overwriting the oldest value.

pub fn extend_from_slice(&mut self, other: &[T]) where
    T: Clone
[src]

Clones and writes all elements in a slice to the buffer.

If the slice is longer than the buffer, only the last self.len() elements will actually be stored.

pub fn recent(&self) -> Option<&T>[src]

Returns a reference to the most recently written value.

Examples

use heapless::HistoryBuffer;

let mut x: HistoryBuffer<u8, 16> = HistoryBuffer::new();
x.write(4);
x.write(10);
assert_eq!(x.recent(), Some(&10));

pub fn as_slice(&self) -> &[T][src]

Returns the array slice backing the buffer, without keeping track of the write position. Therefore, the element order is unspecified.

Trait Implementations

impl<T, const N: usize> AsRef<[T]> for HistoryBuffer<T, N>[src]

fn as_ref(&self) -> &[T][src]

Performs the conversion.

impl<T, const N: usize> Debug for HistoryBuffer<T, N> where
    T: Debug
[src]

fn fmt(&self, f: &mut Formatter<'_>) -> Result[src]

Formats the value using the given formatter. Read more

impl<T, const N: usize> Deref for HistoryBuffer<T, N>[src]

type Target = [T]

The resulting type after dereferencing.

fn deref(&self) -> &[T][src]

Dereferences the value.

impl<T, const N: usize> Drop for HistoryBuffer<T, N>[src]

fn drop(&mut self)[src]

Executes the destructor for this type. Read more

impl<'a, T, const N: usize> Extend<&'a T> for HistoryBuffer<T, N> where
    T: 'a + Clone
[src]

fn extend<I>(&mut self, iter: I) where
    I: IntoIterator<Item = &'a T>, 
[src]

Extends a collection with the contents of an iterator. Read more

fn extend_one(&mut self, item: A)[src]

🔬 This is a nightly-only experimental API. (extend_one)

Extends a collection with exactly one element.

fn extend_reserve(&mut self, additional: usize)[src]

🔬 This is a nightly-only experimental API. (extend_one)

Reserves capacity in a collection for the given number of additional elements. Read more

impl<T, const N: usize> Extend<T> for HistoryBuffer<T, N>[src]

fn extend<I>(&mut self, iter: I) where
    I: IntoIterator<Item = T>, 
[src]

Extends a collection with the contents of an iterator. Read more

fn extend_one(&mut self, item: A)[src]

🔬 This is a nightly-only experimental API. (extend_one)

Extends a collection with exactly one element.

fn extend_reserve(&mut self, additional: usize)[src]

🔬 This is a nightly-only experimental API. (extend_one)

Reserves capacity in a collection for the given number of additional elements. Read more

Auto Trait Implementations

impl<T, const N: usize> Send for HistoryBuffer<T, N> where
    T: Send

impl<T, const N: usize> Sync for HistoryBuffer<T, N> where
    T: Sync

impl<T, const N: usize> Unpin for HistoryBuffer<T, N> where
    T: Unpin

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]

Performs the conversion.