Skip to content

Commit a33989f

Browse files
committed
Add recent_index, oldest_index, oldest, and is_filled to HistoryBuffer API
1 parent 644653b commit a33989f

File tree

1 file changed

+89
-2
lines changed

1 file changed

+89
-2
lines changed

src/histbuf.rs

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ impl<T, const N: usize> HistoryBuffer<T, N> {
124124
N
125125
}
126126

127+
/// Returns whether the buffer is filled
128+
#[inline]
129+
pub fn is_filled(&self) -> bool {
130+
self.filled
131+
}
132+
127133
/// Writes an element to the buffer, overwriting the oldest value.
128134
pub fn write(&mut self, t: T) {
129135
if self.filled {
@@ -165,14 +171,72 @@ impl<T, const N: usize> HistoryBuffer<T, N> {
165171
/// assert_eq!(x.recent(), Some(&10));
166172
/// ```
167173
pub fn recent(&self) -> Option<&T> {
174+
self.recent_index()
175+
.map(|i| unsafe { &*self.data[i].as_ptr() })
176+
}
177+
178+
/// Returns index of the most recently written value in the underlying slice.
179+
///
180+
/// # Examples
181+
///
182+
/// ```
183+
/// use heapless::HistoryBuffer;
184+
///
185+
/// let mut x: HistoryBuffer<u8, 16> = HistoryBuffer::new();
186+
/// x.write(4);
187+
/// x.write(10);
188+
/// assert_eq!(x.recent_index(), Some(1));
189+
/// ```
190+
pub fn recent_index(&self) -> Option<usize> {
168191
if self.write_at == 0 {
169192
if self.filled {
170-
Some(unsafe { &*self.data[self.capacity() - 1].as_ptr() })
193+
Some(self.capacity() - 1)
171194
} else {
172195
None
173196
}
174197
} else {
175-
Some(unsafe { &*self.data[self.write_at - 1].as_ptr() })
198+
Some(self.write_at - 1)
199+
}
200+
}
201+
202+
/// Returns a reference to the oldest value in the buffer.
203+
///
204+
/// # Examples
205+
///
206+
/// ```
207+
/// use heapless::HistoryBuffer;
208+
///
209+
/// let mut x: HistoryBuffer<u8, 16> = HistoryBuffer::new();
210+
/// x.write(4);
211+
/// x.write(10);
212+
/// assert_eq!(x.oldest(), Some(&4));
213+
/// ```
214+
pub fn oldest(&self) -> Option<&T> {
215+
self.oldest_index()
216+
.map(|i| unsafe { &*self.data[i].as_ptr() })
217+
}
218+
219+
/// Returns index of the oldest value in the underlying slice.
220+
///
221+
/// # Examples
222+
///
223+
/// ```
224+
/// use heapless::HistoryBuffer;
225+
///
226+
/// let mut x: HistoryBuffer<u8, 16> = HistoryBuffer::new();
227+
/// x.write(4);
228+
/// x.write(10);
229+
/// assert_eq!(x.oldest_index(), Some(0));
230+
/// ```
231+
pub fn oldest_index(&self) -> Option<usize> {
232+
if self.filled {
233+
Some(self.write_at)
234+
} else {
235+
if self.write_at == 0 {
236+
None
237+
} else {
238+
Some(0)
239+
}
176240
}
177241
}
178242

@@ -365,9 +429,11 @@ mod tests {
365429
assert_eq!(x.len(), 4);
366430
assert_eq!(x.as_slice(), [1; 4]);
367431
assert_eq!(*x, [1; 4]);
432+
assert!(x.is_filled());
368433

369434
let x: HistoryBuffer<u8, 4> = HistoryBuffer::new();
370435
assert_eq!(x.as_slice(), []);
436+
assert!(!x.is_filled());
371437
}
372438

373439
#[test]
@@ -441,18 +507,39 @@ mod tests {
441507
#[test]
442508
fn recent() {
443509
let mut x: HistoryBuffer<u8, 4> = HistoryBuffer::new();
510+
assert_eq!(x.recent_index(), None);
444511
assert_eq!(x.recent(), None);
445512

446513
x.write(1);
447514
x.write(4);
515+
assert_eq!(x.recent_index(), Some(1));
448516
assert_eq!(x.recent(), Some(&4));
449517

450518
x.write(5);
451519
x.write(6);
452520
x.write(10);
521+
assert_eq!(x.recent_index(), Some(0));
453522
assert_eq!(x.recent(), Some(&10));
454523
}
455524

525+
#[test]
526+
fn oldest() {
527+
let mut x: HistoryBuffer<u8, 4> = HistoryBuffer::new();
528+
assert_eq!(x.oldest_index(), None);
529+
assert_eq!(x.oldest(), None);
530+
531+
x.write(1);
532+
x.write(4);
533+
assert_eq!(x.oldest_index(), Some(0));
534+
assert_eq!(x.oldest(), Some(&1));
535+
536+
x.write(5);
537+
x.write(6);
538+
x.write(10);
539+
assert_eq!(x.oldest_index(), Some(1));
540+
assert_eq!(x.oldest(), Some(&4));
541+
}
542+
456543
#[test]
457544
fn as_slice() {
458545
let mut x: HistoryBuffer<u8, 4> = HistoryBuffer::new();

0 commit comments

Comments
 (0)