Skip to content

Commit c0649c4

Browse files
authored
Merge pull request #347 from YuhanLiin/histbuf-api-additions
Add `recent_index`, `oldest_index`, `oldest,` and `is_filled` to HistoryBuffer API
2 parents fc099ec + 2cafa8c commit c0649c4

File tree

2 files changed

+88
-2
lines changed

2 files changed

+88
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1111

1212
- Added `format` macro.
1313
- Added `String::from_utf16`.
14+
- Added `is_full`, `recent_index`, `oldest`, and `oldest_index` to `HistoryBuffer`
1415

1516
### Changed
1617

src/histbuf.rs

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@ impl<T, const N: usize> HistoryBuffer<T, N> {
141141
N
142142
}
143143

144+
/// Returns whether the buffer is full
145+
#[inline]
146+
pub fn is_full(&self) -> bool {
147+
self.filled
148+
}
149+
144150
/// Writes an element to the buffer, overwriting the oldest value.
145151
pub fn write(&mut self, t: T) {
146152
if self.filled {
@@ -182,14 +188,70 @@ impl<T, const N: usize> HistoryBuffer<T, N> {
182188
/// assert_eq!(x.recent(), Some(&10));
183189
/// ```
184190
pub fn recent(&self) -> Option<&T> {
191+
self.recent_index()
192+
.map(|i| unsafe { &*self.data[i].as_ptr() })
193+
}
194+
195+
/// Returns index of the most recently written value in the underlying slice.
196+
///
197+
/// # Examples
198+
///
199+
/// ```
200+
/// use heapless::HistoryBuffer;
201+
///
202+
/// let mut x: HistoryBuffer<u8, 16> = HistoryBuffer::new();
203+
/// x.write(4);
204+
/// x.write(10);
205+
/// assert_eq!(x.recent_index(), Some(1));
206+
/// ```
207+
pub fn recent_index(&self) -> Option<usize> {
185208
if self.write_at == 0 {
186209
if self.filled {
187-
Some(unsafe { &*self.data[self.capacity() - 1].as_ptr() })
210+
Some(self.capacity() - 1)
188211
} else {
189212
None
190213
}
191214
} else {
192-
Some(unsafe { &*self.data[self.write_at - 1].as_ptr() })
215+
Some(self.write_at - 1)
216+
}
217+
}
218+
219+
/// Returns a reference to the oldest value in the buffer.
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(), Some(&4));
230+
/// ```
231+
pub fn oldest(&self) -> Option<&T> {
232+
self.oldest_index()
233+
.map(|i| unsafe { &*self.data[i].as_ptr() })
234+
}
235+
236+
/// Returns index of the oldest value in the underlying slice.
237+
///
238+
/// # Examples
239+
///
240+
/// ```
241+
/// use heapless::HistoryBuffer;
242+
///
243+
/// let mut x: HistoryBuffer<u8, 16> = HistoryBuffer::new();
244+
/// x.write(4);
245+
/// x.write(10);
246+
/// assert_eq!(x.oldest_index(), Some(0));
247+
/// ```
248+
pub fn oldest_index(&self) -> Option<usize> {
249+
if self.filled {
250+
Some(self.write_at)
251+
} else if self.write_at == 0 {
252+
None
253+
} else {
254+
Some(0)
193255
}
194256
}
195257

@@ -381,9 +443,11 @@ mod tests {
381443
assert_eq!(x.len(), 4);
382444
assert_eq!(x.as_slice(), [1; 4]);
383445
assert_eq!(*x, [1; 4]);
446+
assert!(x.is_full());
384447

385448
let x: HistoryBuffer<u8, 4> = HistoryBuffer::new();
386449
assert_eq!(x.as_slice(), []);
450+
assert!(!x.is_full());
387451
}
388452

389453
#[test]
@@ -457,18 +521,39 @@ mod tests {
457521
#[test]
458522
fn recent() {
459523
let mut x: HistoryBuffer<u8, 4> = HistoryBuffer::new();
524+
assert_eq!(x.recent_index(), None);
460525
assert_eq!(x.recent(), None);
461526

462527
x.write(1);
463528
x.write(4);
529+
assert_eq!(x.recent_index(), Some(1));
464530
assert_eq!(x.recent(), Some(&4));
465531

466532
x.write(5);
467533
x.write(6);
468534
x.write(10);
535+
assert_eq!(x.recent_index(), Some(0));
469536
assert_eq!(x.recent(), Some(&10));
470537
}
471538

539+
#[test]
540+
fn oldest() {
541+
let mut x: HistoryBuffer<u8, 4> = HistoryBuffer::new();
542+
assert_eq!(x.oldest_index(), None);
543+
assert_eq!(x.oldest(), None);
544+
545+
x.write(1);
546+
x.write(4);
547+
assert_eq!(x.oldest_index(), Some(0));
548+
assert_eq!(x.oldest(), Some(&1));
549+
550+
x.write(5);
551+
x.write(6);
552+
x.write(10);
553+
assert_eq!(x.oldest_index(), Some(1));
554+
assert_eq!(x.oldest(), Some(&4));
555+
}
556+
472557
#[test]
473558
fn as_slice() {
474559
let mut x: HistoryBuffer<u8, 4> = HistoryBuffer::new();

0 commit comments

Comments
 (0)