Skip to content

Commit f01eb29

Browse files
bors[bot]hulthekorken89
authored
Merge #327
327: Add HistoryBuffer::as_slices() r=korken89 a=hulthe This method returns the contents of a HistoryBuffer as two ordered slices, similar to [Deque::as_slices()](https://docs.rs/heapless/0.7.16/heapless/struct.Deque.html#method.as_slices). Co-authored-by: Joakim Hulthe <joakim@hulthe.net> Co-authored-by: Emil Fresk <emil.fresk@gmail.com>
2 parents b3ddffb + 61e6896 commit f01eb29

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
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
- Add `Clone` and `PartialEq` implementations to `HistoryBuffer`.
1313
- Added an object pool API. see the `pool::object` module level doc for details
14+
- Add `HistoryBuffer::as_slices()`
1415
- Implemented `retain` for `IndexMap` and `IndexSet`.
1516

1617
### Changed

src/histbuf.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,28 @@ impl<T, const N: usize> HistoryBuffer<T, N> {
182182
unsafe { slice::from_raw_parts(self.data.as_ptr() as *const _, self.len()) }
183183
}
184184

185+
/// Returns a pair of slices which contain, in order, the contents of the buffer.
186+
///
187+
/// # Examples
188+
///
189+
/// ```
190+
/// use heapless::HistoryBuffer;
191+
///
192+
/// let mut buffer: HistoryBuffer<u8, 6> = HistoryBuffer::new();
193+
/// buffer.extend([0, 0, 0]);
194+
/// buffer.extend([1, 2, 3, 4, 5, 6]);
195+
/// assert_eq!(buffer.as_slices(), (&[1, 2, 3][..], &[4, 5, 6][..]));
196+
/// ```
197+
pub fn as_slices(&self) -> (&[T], &[T]) {
198+
let buffer = self.as_slice();
199+
200+
if !self.filled {
201+
(buffer, &[])
202+
} else {
203+
(&buffer[self.write_at..], &buffer[..self.write_at])
204+
}
205+
}
206+
185207
/// Returns an iterator for iterating over the buffer from oldest to newest.
186208
///
187209
/// # Examples
@@ -442,6 +464,37 @@ mod tests {
442464
assert_eq!(x.as_slice(), [5, 2, 3, 4]);
443465
}
444466

467+
/// Test whether .as_slices() behaves as expected.
468+
#[test]
469+
fn as_slices() {
470+
let mut buffer: HistoryBuffer<u8, 4> = HistoryBuffer::new();
471+
let mut extend_then_assert = |extend: &[u8], assert: (&[u8], &[u8])| {
472+
buffer.extend(extend);
473+
assert_eq!(buffer.as_slices(), assert);
474+
};
475+
476+
extend_then_assert(b"a", (b"a", b""));
477+
extend_then_assert(b"bcd", (b"abcd", b""));
478+
extend_then_assert(b"efg", (b"d", b"efg"));
479+
extend_then_assert(b"h", (b"efgh", b""));
480+
extend_then_assert(b"123456", (b"34", b"56"));
481+
}
482+
483+
/// Test whether .as_slices() and .oldest_ordered() produce elements in the same order.
484+
#[test]
485+
fn as_slices_equals_ordered() {
486+
let mut buffer: HistoryBuffer<u8, 6> = HistoryBuffer::new();
487+
488+
for n in 0..20 {
489+
buffer.write(n);
490+
let (head, tail) = buffer.as_slices();
491+
assert_eq_iter(
492+
[head, tail].iter().copied().flatten(),
493+
buffer.oldest_ordered(),
494+
)
495+
}
496+
}
497+
445498
#[test]
446499
fn ordered() {
447500
// test on an empty buffer

0 commit comments

Comments
 (0)