Skip to content

Commit 4a95ffe

Browse files
Forward HistoryBuf implementations to HistoryBufView
1 parent 35a3e6b commit 4a95ffe

File tree

1 file changed

+47
-80
lines changed

1 file changed

+47
-80
lines changed

src/histbuf.rs

Lines changed: 47 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -80,29 +80,29 @@ pub type HistoryBuffer<T, const N: usize> = HistoryBufferInner<[MaybeUninit<T>;
8080
///
8181
/// # Examples
8282
/// ```
83-
/// use heapless::HistoryBuffer;
83+
/// use heapless::{HistoryBuffer, HistoryBufferView};
8484
///
8585
/// // Initialize a new buffer with 8 elements.
8686
/// let mut buf = HistoryBuffer::<_, 8>::new();
8787
/// let buf_view: &mut HistoryBufferView<_> = &mut buf;
8888
///
8989
/// // Starts with no data
90-
/// assert_eq!(buf.recent(), None);
90+
/// assert_eq!(buf_view.recent(), None);
9191
///
92-
/// buf.write(3);
93-
/// buf.write(5);
94-
/// buf.extend(&[4, 4]);
92+
/// buf_view.write(3);
93+
/// buf_view.write(5);
94+
/// buf_view.extend(&[4, 4]);
9595
///
9696
/// // The most recent written element is a four.
97-
/// assert_eq!(buf.recent(), Some(&4));
97+
/// assert_eq!(buf_view.recent(), Some(&4));
9898
///
9999
/// // To access all elements in an unspecified order, use `as_slice()`.
100-
/// for el in buf.as_slice() {
100+
/// for el in buf_view.as_slice() {
101101
/// println!("{:?}", el);
102102
/// }
103103
///
104104
/// // Now we can prepare an average of all values, which comes out to 4.
105-
/// let avg = buf.as_slice().iter().sum::<usize>() / buf.len();
105+
/// let avg = buf_view.as_slice().iter().sum::<usize>() / buf_view.len();
106106
/// assert_eq!(avg, 4);
107107
/// ```
108108
pub type HistoryBufferView<T> = HistoryBufferInner<[MaybeUninit<T>]>;
@@ -299,12 +299,13 @@ impl<T> HistoryBufferView<T> {
299299
/// # Examples
300300
///
301301
/// ```
302-
/// use heapless::HistoryBuffer;
302+
/// use heapless::{HistoryBuffer, HistoryBufferView};
303303
///
304304
/// let mut x: HistoryBuffer<u8, 16> = HistoryBuffer::new();
305-
/// x.write(4);
306-
/// x.write(10);
307-
/// assert_eq!(x.recent(), Some(&10));
305+
/// let mut x_view = &mut x;
306+
/// x_view.write(4);
307+
/// x_view.write(10);
308+
/// assert_eq!(x_view.recent(), Some(&10));
308309
/// ```
309310
pub fn recent(&self) -> Option<&T> {
310311
self.recent_index()
@@ -316,12 +317,13 @@ impl<T> HistoryBufferView<T> {
316317
/// # Examples
317318
///
318319
/// ```
319-
/// use heapless::HistoryBuffer;
320+
/// use heapless::{HistoryBuffer, HistoryBufferView};
320321
///
321322
/// let mut x: HistoryBuffer<u8, 16> = HistoryBuffer::new();
322-
/// x.write(4);
323-
/// x.write(10);
324-
/// assert_eq!(x.recent_index(), Some(1));
323+
/// let mut x_view = &mut x;
324+
/// x_view.write(4);
325+
/// x_view.write(10);
326+
/// assert_eq!(x_view.recent_index(), Some(1));
325327
/// ```
326328
pub fn recent_index(&self) -> Option<usize> {
327329
if self.write_at == 0 {
@@ -340,12 +342,13 @@ impl<T> HistoryBufferView<T> {
340342
/// # Examples
341343
///
342344
/// ```
343-
/// use heapless::HistoryBuffer;
345+
/// use heapless::{HistoryBuffer, HistoryBufferView};
344346
///
345347
/// let mut x: HistoryBuffer<u8, 16> = HistoryBuffer::new();
346-
/// x.write(4);
347-
/// x.write(10);
348-
/// assert_eq!(x.oldest(), Some(&4));
348+
/// let mut x_view = &mut x;
349+
/// x_view.write(4);
350+
/// x_view.write(10);
351+
/// assert_eq!(x_view.oldest(), Some(&4));
349352
/// ```
350353
pub fn oldest(&self) -> Option<&T> {
351354
self.oldest_index()
@@ -357,12 +360,13 @@ impl<T> HistoryBufferView<T> {
357360
/// # Examples
358361
///
359362
/// ```
360-
/// use heapless::HistoryBuffer;
363+
/// use heapless::{HistoryBuffer, HistoryBufferView};
361364
///
362365
/// let mut x: HistoryBuffer<u8, 16> = HistoryBuffer::new();
363-
/// x.write(4);
364-
/// x.write(10);
365-
/// assert_eq!(x.oldest_index(), Some(0));
366+
/// let mut x_view = &mut x;
367+
/// x_view.write(4);
368+
/// x_view.write(10);
369+
/// assert_eq!(x_view.oldest_index(), Some(0));
366370
/// ```
367371
pub fn oldest_index(&self) -> Option<usize> {
368372
if self.filled {
@@ -385,12 +389,12 @@ impl<T> HistoryBufferView<T> {
385389
/// # Examples
386390
///
387391
/// ```
388-
/// use heapless::HistoryBuffer;
389-
///
392+
/// use heapless::{HistoryBuffer, HistoryBufferView};
390393
/// let mut buffer: HistoryBuffer<u8, 6> = HistoryBuffer::new();
391-
/// buffer.extend([0, 0, 0]);
392-
/// buffer.extend([1, 2, 3, 4, 5, 6]);
393-
/// assert_eq!(buffer.as_slices(), (&[1, 2, 3][..], &[4, 5, 6][..]));
394+
/// let mut buffer_view: &mut HistoryBufferView<u8> = &mut buffer;
395+
/// buffer_view.extend([0, 0, 0]);
396+
/// buffer_view.extend([1, 2, 3, 4, 5, 6]);
397+
/// assert_eq!(buffer_view.as_slices(), (&[1, 2, 3][..], &[4, 5, 6][..]));
394398
/// ```
395399
pub fn as_slices(&self) -> (&[T], &[T]) {
396400
let buffer = self.as_slice();
@@ -407,12 +411,13 @@ impl<T> HistoryBufferView<T> {
407411
/// # Examples
408412
///
409413
/// ```
410-
/// use heapless::HistoryBuffer;
414+
/// use heapless::{HistoryBuffer, HistoryBufferView};
411415
///
412416
/// let mut buffer: HistoryBuffer<u8, 6> = HistoryBuffer::new();
413-
/// buffer.extend([0, 0, 0, 1, 2, 3, 4, 5, 6]);
417+
/// let mut buffer_view: &mut HistoryBufferView<u8> = &mut buffer;
418+
/// buffer_view.extend([0, 0, 0, 1, 2, 3, 4, 5, 6]);
414419
/// let expected = [1, 2, 3, 4, 5, 6];
415-
/// for (x, y) in buffer.oldest_ordered().zip(expected.iter()) {
420+
/// for (x, y) in buffer_view.oldest_ordered().zip(expected.iter()) {
416421
/// assert_eq!(x, y)
417422
/// }
418423
/// ```
@@ -426,11 +431,7 @@ impl<T, const N: usize> HistoryBuffer<T, N> {
426431
/// Returns the current fill level of the buffer.
427432
#[inline]
428433
pub fn len(&self) -> usize {
429-
if self.filled {
430-
N
431-
} else {
432-
self.write_at
433-
}
434+
self.as_view().len()
434435
}
435436

436437
/// Returns true if the buffer is empty.
@@ -445,7 +446,7 @@ impl<T, const N: usize> HistoryBuffer<T, N> {
445446
/// ```
446447
#[inline]
447448
pub fn is_empty(&self) -> bool {
448-
self.len() == 0
449+
self.as_view().is_empty()
449450
}
450451

451452
/// Returns the capacity of the buffer, which is the length of the
@@ -463,17 +464,7 @@ impl<T, const N: usize> HistoryBuffer<T, N> {
463464

464465
/// Writes an element to the buffer, overwriting the oldest value.
465466
pub fn write(&mut self, t: T) {
466-
if self.filled {
467-
// Drop the old before we overwrite it.
468-
unsafe { ptr::drop_in_place(self.data[self.write_at].as_mut_ptr()) }
469-
}
470-
self.data[self.write_at] = MaybeUninit::new(t);
471-
472-
self.write_at += 1;
473-
if self.write_at == self.capacity() {
474-
self.write_at = 0;
475-
self.filled = true;
476-
}
467+
self.as_mut_view().write(t)
477468
}
478469

479470
/// Clones and writes all elements in a slice to the buffer.
@@ -484,9 +475,7 @@ impl<T, const N: usize> HistoryBuffer<T, N> {
484475
where
485476
T: Clone,
486477
{
487-
for item in other {
488-
self.write(item.clone());
489-
}
478+
self.as_mut_view().extend_from_slice(other)
490479
}
491480

492481
/// Returns a reference to the most recently written value.
@@ -502,8 +491,7 @@ impl<T, const N: usize> HistoryBuffer<T, N> {
502491
/// assert_eq!(x.recent(), Some(&10));
503492
/// ```
504493
pub fn recent(&self) -> Option<&T> {
505-
self.recent_index()
506-
.map(|i| unsafe { &*self.data[i].as_ptr() })
494+
self.as_view().recent()
507495
}
508496

509497
/// Returns index of the most recently written value in the underlying slice.
@@ -519,15 +507,7 @@ impl<T, const N: usize> HistoryBuffer<T, N> {
519507
/// assert_eq!(x.recent_index(), Some(1));
520508
/// ```
521509
pub fn recent_index(&self) -> Option<usize> {
522-
if self.write_at == 0 {
523-
if self.filled {
524-
Some(self.capacity() - 1)
525-
} else {
526-
None
527-
}
528-
} else {
529-
Some(self.write_at - 1)
530-
}
510+
self.as_view().recent_index()
531511
}
532512

533513
/// Returns a reference to the oldest value in the buffer.
@@ -543,8 +523,7 @@ impl<T, const N: usize> HistoryBuffer<T, N> {
543523
/// assert_eq!(x.oldest(), Some(&4));
544524
/// ```
545525
pub fn oldest(&self) -> Option<&T> {
546-
self.oldest_index()
547-
.map(|i| unsafe { &*self.data[i].as_ptr() })
526+
self.as_view().oldest()
548527
}
549528

550529
/// Returns index of the oldest value in the underlying slice.
@@ -560,19 +539,13 @@ impl<T, const N: usize> HistoryBuffer<T, N> {
560539
/// assert_eq!(x.oldest_index(), Some(0));
561540
/// ```
562541
pub fn oldest_index(&self) -> Option<usize> {
563-
if self.filled {
564-
Some(self.write_at)
565-
} else if self.write_at == 0 {
566-
None
567-
} else {
568-
Some(0)
569-
}
542+
self.as_view().oldest_index()
570543
}
571544

572545
/// Returns the array slice backing the buffer, without keeping track
573546
/// of the write position. Therefore, the element order is unspecified.
574547
pub fn as_slice(&self) -> &[T] {
575-
unsafe { slice::from_raw_parts(self.data.as_ptr() as *const _, self.len()) }
548+
self.as_view().as_slice()
576549
}
577550

578551
/// Returns a pair of slices which contain, in order, the contents of the buffer.
@@ -588,13 +561,7 @@ impl<T, const N: usize> HistoryBuffer<T, N> {
588561
/// assert_eq!(buffer.as_slices(), (&[1, 2, 3][..], &[4, 5, 6][..]));
589562
/// ```
590563
pub fn as_slices(&self) -> (&[T], &[T]) {
591-
let buffer = self.as_slice();
592-
593-
if !self.filled {
594-
(buffer, &[])
595-
} else {
596-
(&buffer[self.write_at..], &buffer[..self.write_at])
597-
}
564+
self.as_view().as_slices()
598565
}
599566

600567
/// Returns an iterator for iterating over the buffer from oldest to newest.

0 commit comments

Comments
 (0)