Skip to content

Commit

Permalink
ringbuffer_trait: add extend_from_slice
Browse files Browse the repository at this point in the history
This adds an extend_from_slice function to the RingBuffer trait. The default
implementation calls push() for each element. However, by creating specialized
implementations for the various buffers, one can do various performance
optimizations in a follow-up.
  • Loading branch information
phip1611 committed May 12, 2024
1 parent 0b7f2c1 commit 825971b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/ringbuffer_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,13 @@ pub unsafe trait RingBuffer<T>:
RingBufferIterator::new(self)
}

/// Extends the ringbuffer with elements from a slice by cloning them.
fn extend_from_slice(&mut self, elements: &[T]) where T: Clone {
for element in elements {
self.push(element.clone())
}
}

/// Converts the buffer to a vector. This Copies all elements in the ringbuffer.
#[cfg(feature = "alloc")]
fn to_vec(&self) -> Vec<T>
Expand Down
8 changes: 8 additions & 0 deletions src/with_alloc/alloc_ringbuffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,4 +469,12 @@ mod tests {
assert_eq!(buf.capacity, 4);
assert_eq!(buf.to_vec(), alloc::vec![1, 2, 3, 4]);
}

#[test]
fn test_extend_from_slice() {
let mut buf = AllocRingBuffer::new(3);
let elems = [1, 2, 3];
buf.extend_from_slice(&elems);
assert_eq!(buf.to_vec().as_slice(), elems)
}
}
13 changes: 13 additions & 0 deletions src/with_alloc/vecdeque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,16 @@ impl<T> FromIterator<T> for GrowableAllocRingBuffer<T> {
Self(VecDeque::from_iter(iter))
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_extend_from_slice() {
let mut buf = GrowableAllocRingBuffer::new();
let elems = [1, 2, 3];
buf.extend_from_slice(&elems);
assert_eq!(buf.to_vec().as_slice(), elems)
}
}
8 changes: 8 additions & 0 deletions src/with_const_generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,5 +492,13 @@ mod tests {
vec![1, 2, 3]
);
}

#[test]
fn test_extend_from_slice() {
let mut buf = ConstGenericRingBuffer::<i32, 3>::new();
let elems = [1, 2, 3];
buf.extend_from_slice(&elems);
assert_eq!(buf.to_vec().as_slice(), elems)
}
}
}

0 comments on commit 825971b

Please sign in to comment.