diff --git a/src/lib.rs b/src/lib.rs index fc70842..8c03366a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1376,7 +1376,7 @@ mod tests { parent: Option>, } - impl<'a> Drop for Dropee<'a> { + impl Drop for Dropee<'_> { fn drop(&mut self) { if let Some(parent) = &mut self.parent { parent.flag = true; diff --git a/src/ringbuffer_trait.rs b/src/ringbuffer_trait.rs index 1a549fe..c621b28 100644 --- a/src/ringbuffer_trait.rs +++ b/src/ringbuffer_trait.rs @@ -86,7 +86,6 @@ pub unsafe trait RingBuffer: /// /// Cycles around if capacity is reached. /// Forms a more natural counterpart to [`dequeue`](RingBuffer::dequeue). - /// An alias is provided with [`push`](RingBuffer::push). fn enqueue(&mut self, value: T) -> Option; /// dequeues the top item off the ringbuffer, and moves this item out. @@ -237,6 +236,18 @@ pub unsafe trait RingBuffer: RingBufferIterator::new(self) } + /// Extends the ringbuffer with elements from a slice. + fn extend_from_slice(&mut self, elements: &[T]) + where + T: Clone, + { + // Default implementation. + // For performance reasons, specific RingBuffers should use an optimized implementation. + for element in elements { + let _ = self.enqueue(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 f21769e..3af05f3 100644 --- a/src/with_alloc/alloc_ringbuffer.rs +++ b/src/with_alloc/alloc_ringbuffer.rs @@ -474,4 +474,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 978bbe5..8bc57af 100644 --- a/src/with_alloc/vecdeque.rs +++ b/src/with_alloc/vecdeque.rs @@ -282,3 +282,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 fc9857d..200ff60 100644 --- a/src/with_const_generics.rs +++ b/src/with_const_generics.rs @@ -496,4 +496,12 @@ 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); + } }