From 825971b797b1d7fc51dd734221889347bd6d614a Mon Sep 17 00:00:00 2001 From: Philipp Schuster Date: Sun, 12 May 2024 22:11:30 +0200 Subject: [PATCH] ringbuffer_trait: add extend_from_slice 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. --- src/ringbuffer_trait.rs | 7 +++++++ src/with_alloc/alloc_ringbuffer.rs | 8 ++++++++ src/with_alloc/vecdeque.rs | 13 +++++++++++++ src/with_const_generics.rs | 8 ++++++++ 4 files changed, 36 insertions(+) diff --git a/src/ringbuffer_trait.rs b/src/ringbuffer_trait.rs index ae92f09..3e2f02c 100644 --- a/src/ringbuffer_trait.rs +++ b/src/ringbuffer_trait.rs @@ -230,6 +230,13 @@ pub unsafe trait RingBuffer: 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 diff --git a/src/with_alloc/alloc_ringbuffer.rs b/src/with_alloc/alloc_ringbuffer.rs index 730b67d..e2d84e6 100644 --- a/src/with_alloc/alloc_ringbuffer.rs +++ b/src/with_alloc/alloc_ringbuffer.rs @@ -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) + } } diff --git a/src/with_alloc/vecdeque.rs b/src/with_alloc/vecdeque.rs index 23adeb1..f340f7e 100644 --- a/src/with_alloc/vecdeque.rs +++ b/src/with_alloc/vecdeque.rs @@ -280,3 +280,16 @@ impl FromIterator for GrowableAllocRingBuffer { 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) + } +} diff --git a/src/with_const_generics.rs b/src/with_const_generics.rs index e4b6570..e4e4b0e 100644 --- a/src/with_const_generics.rs +++ b/src/with_const_generics.rs @@ -492,5 +492,13 @@ mod tests { vec![1, 2, 3] ); } + + #[test] + fn test_extend_from_slice() { + let mut buf = ConstGenericRingBuffer::::new(); + let elems = [1, 2, 3]; + buf.extend_from_slice(&elems); + assert_eq!(buf.to_vec().as_slice(), elems) + } } }