Struct heapless::HistoryBuffer [−][src]
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.
The buffer is always fully initialized; depending on the constructor, the initial value is either the default value for the element type or a supplied initial value. This simplifies the API and is mostly irrelevant for the intended use case.
Examples
use heapless::HistoryBuffer; use heapless::consts::*; // Initialize a new buffer with 8 elements, all initially zero. let mut buf = HistoryBuffer::<_, U8>::new(); buf.write(3); buf.write(5); buf.extend(&[4, 4]); // The most recent written element is a four. assert_eq!(buf.recent(), &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 2. let avg = buf.as_slice().iter().sum::<usize>() / buf.len(); assert_eq!(avg, 2);
Implementations
impl<T, N> HistoryBuffer<T, N> where
N: ArrayLength<T>,
T: Default,
[src]
N: ArrayLength<T>,
T: Default,
pub fn new() -> Self
[src]
Constructs a new history buffer, where every element is filled with the
default value of the type T
.
HistoryBuffer
currently cannot be constructed in const
context.
Examples
use heapless::HistoryBuffer; use heapless::consts::*; // Allocate a 16-element buffer on the stack let mut x: HistoryBuffer<u8, U16> = HistoryBuffer::new(); // All elements are zero assert_eq!(x.as_slice(), [0; 16]);
pub fn clear(&mut self)
[src]
Clears the buffer, replacing every element with the default value of
type T
.
impl<T, N> HistoryBuffer<T, N> where
N: ArrayLength<T>,
T: Clone,
[src]
N: ArrayLength<T>,
T: Clone,
pub fn new_with(t: T) -> Self
[src]
Constructs a new history buffer, where every element is the given value.
Examples
use heapless::HistoryBuffer; use heapless::consts::*; // Allocate a 16-element buffer on the stack let mut x: HistoryBuffer<u8, U16> = 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, N> HistoryBuffer<T, N> where
N: ArrayLength<T>,
[src]
N: ArrayLength<T>,
pub fn len(&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]
T: Clone,
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) -> &T
[src]
Returns a reference to the most recently written value.
Examples
use heapless::HistoryBuffer; use heapless::consts::*; let mut x: HistoryBuffer<u8, U16> = HistoryBuffer::new(); x.write(4); x.write(10); assert_eq!(x.recent(), &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: Clone, N: Clone> Clone for HistoryBuffer<T, N> where
N: ArrayLength<T>,
[src]
N: ArrayLength<T>,
fn clone(&self) -> HistoryBuffer<T, N>
[src]
pub fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl<'a, T, N> Extend<&'a T> for HistoryBuffer<T, N> where
T: 'a + Clone,
N: ArrayLength<T>,
[src]
T: 'a + Clone,
N: ArrayLength<T>,
fn extend<I>(&mut self, iter: I) where
I: IntoIterator<Item = &'a T>,
[src]
I: IntoIterator<Item = &'a T>,
pub fn extend_one(&mut self, item: A)
[src]
pub fn extend_reserve(&mut self, additional: usize)
[src]
impl<T, N> Extend<T> for HistoryBuffer<T, N> where
N: ArrayLength<T>,
[src]
N: ArrayLength<T>,
fn extend<I>(&mut self, iter: I) where
I: IntoIterator<Item = T>,
[src]
I: IntoIterator<Item = T>,
pub fn extend_one(&mut self, item: A)
[src]
pub fn extend_reserve(&mut self, additional: usize)
[src]
Auto Trait Implementations
impl<T, N> RefUnwindSafe for HistoryBuffer<T, N> where
<N as ArrayLength<T>>::ArrayType: RefUnwindSafe,
<N as ArrayLength<T>>::ArrayType: RefUnwindSafe,
impl<T, N> Send for HistoryBuffer<T, N> where
T: Send,
T: Send,
impl<T, N> Sync for HistoryBuffer<T, N> where
T: Sync,
T: Sync,
impl<T, N> Unpin for HistoryBuffer<T, N> where
<N as ArrayLength<T>>::ArrayType: Unpin,
<N as ArrayLength<T>>::ArrayType: Unpin,
impl<T, N> UnwindSafe for HistoryBuffer<T, N> where
<N as ArrayLength<T>>::ArrayType: UnwindSafe,
<N as ArrayLength<T>>::ArrayType: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> Same<T> for T
[src]
type Output = T
Should always be Self
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
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]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,