mirror of
https://github.com/rust-embedded/heapless.git
synced 2025-10-02 14:54:30 +00:00
HistoryBuffer::{clone, eq}.
This commit is contained in:
parent
97ac129ba4
commit
340e0cec99
@ -238,6 +238,17 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T, const N: usize> Clone for HistoryBuffer<T, N>
|
||||||
|
where
|
||||||
|
T: Clone,
|
||||||
|
{
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
let mut ret = Self::new();
|
||||||
|
ret.extend(self.iter().cloned());
|
||||||
|
ret
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T, const N: usize> Drop for HistoryBuffer<T, N> {
|
impl<T, const N: usize> Drop for HistoryBuffer<T, N> {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
unsafe {
|
unsafe {
|
||||||
@ -279,6 +290,15 @@ impl<T, const N: usize> Default for HistoryBuffer<T, N> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T, const N: usize> PartialEq for HistoryBuffer<T, N>
|
||||||
|
where
|
||||||
|
T: PartialEq,
|
||||||
|
{
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
self.oldest_ordered().eq(other.oldest_ordered())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// An iterator on the underlying buffer ordered from oldest data to newest
|
/// An iterator on the underlying buffer ordered from oldest data to newest
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct OldestOrdered<'a, T, const N: usize> {
|
pub struct OldestOrdered<'a, T, const N: usize> {
|
||||||
@ -311,6 +331,7 @@ impl<'a, T, const N: usize> Iterator for OldestOrdered<'a, T, N> {
|
|||||||
mod tests {
|
mod tests {
|
||||||
use crate::HistoryBuffer;
|
use crate::HistoryBuffer;
|
||||||
use core::fmt::Debug;
|
use core::fmt::Debug;
|
||||||
|
use core::sync::atomic::{AtomicUsize, Ordering};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn new() {
|
fn new() {
|
||||||
@ -350,6 +371,47 @@ mod tests {
|
|||||||
assert_eq!(x.as_slice(), [1; 4]);
|
assert_eq!(x.as_slice(), [1; 4]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn clone() {
|
||||||
|
let mut x: HistoryBuffer<u8, 3> = HistoryBuffer::new();
|
||||||
|
for i in 0..10 {
|
||||||
|
assert_eq!(x.as_slice(), x.clone().as_slice());
|
||||||
|
x.write(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Records number of clones locally and globally.
|
||||||
|
static GLOBAL: AtomicUsize = AtomicUsize::new(0);
|
||||||
|
#[derive(Default, PartialEq, Debug)]
|
||||||
|
struct InstrumentedClone(usize);
|
||||||
|
|
||||||
|
impl Clone for InstrumentedClone {
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
GLOBAL.fetch_add(1, Ordering::Relaxed);
|
||||||
|
Self(self.0 + 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut y: HistoryBuffer<InstrumentedClone, 2> = HistoryBuffer::new();
|
||||||
|
let _ = y.clone();
|
||||||
|
assert_eq!(GLOBAL.load(Ordering::Relaxed), 0);
|
||||||
|
y.write(InstrumentedClone(0));
|
||||||
|
assert_eq!(GLOBAL.load(Ordering::Relaxed), 0);
|
||||||
|
assert_eq!(y.clone().as_slice(), [InstrumentedClone(1)]);
|
||||||
|
assert_eq!(GLOBAL.load(Ordering::Relaxed), 1);
|
||||||
|
y.write(InstrumentedClone(0));
|
||||||
|
assert_eq!(GLOBAL.load(Ordering::Relaxed), 1);
|
||||||
|
assert_eq!(
|
||||||
|
y.clone().as_slice(),
|
||||||
|
[InstrumentedClone(1), InstrumentedClone(1)]
|
||||||
|
);
|
||||||
|
assert_eq!(GLOBAL.load(Ordering::Relaxed), 3);
|
||||||
|
assert_eq!(
|
||||||
|
y.clone().clone().clone().as_slice(),
|
||||||
|
[InstrumentedClone(3), InstrumentedClone(3)]
|
||||||
|
);
|
||||||
|
assert_eq!(GLOBAL.load(Ordering::Relaxed), 9);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn recent() {
|
fn recent() {
|
||||||
let mut x: HistoryBuffer<u8, 4> = HistoryBuffer::new();
|
let mut x: HistoryBuffer<u8, 4> = HistoryBuffer::new();
|
||||||
@ -430,4 +492,30 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn partial_eq() {
|
||||||
|
let mut x: HistoryBuffer<u8, 3> = HistoryBuffer::new();
|
||||||
|
let mut y: HistoryBuffer<u8, 3> = HistoryBuffer::new();
|
||||||
|
assert_eq!(x, y);
|
||||||
|
x.write(1);
|
||||||
|
assert_ne!(x, y);
|
||||||
|
y.write(1);
|
||||||
|
assert_eq!(x, y);
|
||||||
|
for _ in 0..4 {
|
||||||
|
x.write(2);
|
||||||
|
assert_ne!(x, y);
|
||||||
|
for i in 0..5 {
|
||||||
|
x.write(i);
|
||||||
|
y.write(i);
|
||||||
|
}
|
||||||
|
assert_eq!(
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
"{:?} {:?}",
|
||||||
|
x.iter().collect::<Vec<_>>(),
|
||||||
|
y.iter().collect::<Vec<_>>()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user