From ce4b26e071bae4cdf8c66f24e012212f666cdeb8 Mon Sep 17 00:00:00 2001 From: jdonszelmann Date: Wed, 27 Sep 2023 10:03:21 +0200 Subject: [PATCH 1/7] swap dequeue and push --- src/lib.rs | 2 ++ src/ringbuffer_trait.rs | 17 +++++++++++------ src/with_alloc/alloc_ringbuffer.rs | 2 +- src/with_alloc/vecdeque.rs | 2 +- src/with_const_generics.rs | 2 +- 5 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index fa3dd17..e31ef59 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -972,6 +972,7 @@ mod tests { #[test] fn run_test_skip() { + #[allow(deprecated)] fn test_skip(mut b: impl RingBuffer) { b.push(0); b.push(1); @@ -991,6 +992,7 @@ mod tests { #[test] fn run_test_skip_2() { + #[allow(deprecated)] fn test_skip2(mut rb: impl RingBuffer) { rb.skip(); rb.skip(); diff --git a/src/ringbuffer_trait.rs b/src/ringbuffer_trait.rs index ae92f09..3f4beca 100644 --- a/src/ringbuffer_trait.rs +++ b/src/ringbuffer_trait.rs @@ -75,20 +75,25 @@ pub unsafe trait RingBuffer: #[doc(hidden)] unsafe fn ptr_buffer_size(rb: *const Self) -> usize; - /// Pushes a value onto the buffer. Cycles around if capacity is reached. - fn push(&mut self, value: T); - - /// alias for [`push`](RingBuffer::push), forming a more natural counterpart to [`dequeue`](RingBuffer::dequeue) - fn enqueue(&mut self, value: T) { - self.push(value); + /// Alias for [`enqueue`](RingBuffer::enqueue) + fn push(&mut self, value: T) { + self.enqueue(value); } + /// Adds a value onto the buffer. + /// + /// 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); + /// dequeues the top item off the ringbuffer, and moves this item out. fn dequeue(&mut self) -> Option; /// dequeues the top item off the queue, but does not return it. Instead it is dropped. /// If the ringbuffer is empty, this function is a nop. #[inline] + #[deprecated = "use dequeue instead"] fn skip(&mut self) { let _ = self.dequeue(); } diff --git a/src/with_alloc/alloc_ringbuffer.rs b/src/with_alloc/alloc_ringbuffer.rs index 730b67d..cc529ea 100644 --- a/src/with_alloc/alloc_ringbuffer.rs +++ b/src/with_alloc/alloc_ringbuffer.rs @@ -229,7 +229,7 @@ unsafe impl RingBuffer for AllocRingBuffer { impl_ringbuffer!(readptr, writeptr); #[inline] - fn push(&mut self, value: T) { + fn enqueue(&mut self, value: T) { if self.is_full() { // mask with and is allowed here because size is always a power of two let previous_value = diff --git a/src/with_alloc/vecdeque.rs b/src/with_alloc/vecdeque.rs index 23adeb1..bfb1122 100644 --- a/src/with_alloc/vecdeque.rs +++ b/src/with_alloc/vecdeque.rs @@ -186,7 +186,7 @@ unsafe impl RingBuffer for GrowableAllocRingBuffer { self.pop_front() } - fn push(&mut self, value: T) { + fn enqueue(&mut self, value: T) { self.push_back(value); } diff --git a/src/with_const_generics.rs b/src/with_const_generics.rs index e4b6570..7f247c9 100644 --- a/src/with_const_generics.rs +++ b/src/with_const_generics.rs @@ -265,7 +265,7 @@ unsafe impl RingBuffer for ConstGenericRingBuffer Date: Fri, 8 Dec 2023 15:52:48 +0100 Subject: [PATCH 2/7] Update Cargo.toml --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index c883c99..2d43b0b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "ringbuffer" version = "0.15.0" authors = [ - "Victor Roest ", + "Vivian Roest ", "Jonathan Dönszelmann ", ] edition = "2021" From 1a63e1be0455301664f28a36f576860e95f9f180 Mon Sep 17 00:00:00 2001 From: Vivian Date: Wed, 15 May 2024 19:42:19 +0200 Subject: [PATCH 3/7] deprecate push --- src/ringbuffer_trait.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/ringbuffer_trait.rs b/src/ringbuffer_trait.rs index 3f4beca..8e86c66 100644 --- a/src/ringbuffer_trait.rs +++ b/src/ringbuffer_trait.rs @@ -75,9 +75,12 @@ pub unsafe trait RingBuffer: #[doc(hidden)] unsafe fn ptr_buffer_size(rb: *const Self) -> usize; - /// Alias for [`enqueue`](RingBuffer::enqueue) + + /// Alias for [`enqueue`] + #[deprecated = "use enqueue instead"] + #[inline] fn push(&mut self, value: T) { - self.enqueue(value); + self.enqueue(value) } /// Adds a value onto the buffer. From 3cb5ecb5a62198b45927652bfd81be1673b64796 Mon Sep 17 00:00:00 2001 From: Vivian Date: Wed, 15 May 2024 19:58:59 +0200 Subject: [PATCH 4/7] swap push for enqueue in tests --- src/lib.rs | 443 +++++++++--------- src/with_alloc/alloc_ringbuffer.rs | 18 +- src/with_const_generics.rs | 20 +- ...est_const_generic_array_zero_length_new.rs | 2 +- tests/conversions.rs | 8 +- 5 files changed, 246 insertions(+), 245 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e31ef59..e8c8954 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -64,7 +64,7 @@ mod tests { const capacity: usize = 8; fn test_neg_index(mut b: impl RingBuffer) { for i in 0..capacity + 2 { - b.push(i); + b.enqueue(i); assert_eq!(b.get_signed(-1), Some(&i)); } } @@ -114,9 +114,9 @@ mod tests { fn run_test_len() { fn test_len(mut b: impl RingBuffer) { assert_eq!(0, b.len()); - b.push(1); + b.enqueue(1); assert_eq!(1, b.len()); - b.push(2); + b.enqueue(2); assert_eq!(2, b.len()) } @@ -129,14 +129,14 @@ mod tests { fn run_test_len_wrap() { fn test_len_wrap(mut b: impl RingBuffer) { assert_eq!(0, b.len()); - b.push(1); + b.enqueue(1); assert_eq!(1, b.len()); - b.push(2); + b.enqueue(2); assert_eq!(2, b.len()); // Now we are wrapping - b.push(3); + b.enqueue(3); assert_eq!(2, b.len()); - b.push(4); + b.enqueue(4); assert_eq!(2, b.len()); } @@ -146,20 +146,20 @@ mod tests { // the growable ringbuffer actually should grow instead of wrap let mut grb = GrowableAllocRingBuffer::with_capacity(2); assert_eq!(0, grb.len()); - grb.push(0); + grb.enqueue(0); assert_eq!(1, grb.len()); - grb.push(1); + grb.enqueue(1); assert_eq!(2, grb.len()); - grb.push(2); + grb.enqueue(2); assert_eq!(3, grb.len()); } #[test] fn run_test_clear() { fn test_clear(mut b: impl RingBuffer) { - b.push(1); - b.push(2); - b.push(3); + b.enqueue(1); + b.enqueue(2); + b.enqueue(3); b.clear(); assert!(b.is_empty()); @@ -175,9 +175,9 @@ mod tests { fn run_test_empty() { fn test_empty(mut b: impl RingBuffer) { assert!(b.is_empty()); - b.push(1); - b.push(2); - b.push(3); + b.enqueue(1); + b.enqueue(2); + b.enqueue(3); assert!(!b.is_empty()); b.clear(); @@ -193,13 +193,13 @@ mod tests { #[test] fn run_test_iter() { fn test_iter(mut b: impl RingBuffer) { - b.push(1); - b.push(2); - b.push(3); - b.push(4); - b.push(5); - b.push(6); - b.push(7); + b.enqueue(1); + b.enqueue(2); + b.enqueue(3); + b.enqueue(4); + b.enqueue(5); + b.enqueue(6); + b.enqueue(7); let mut iter = b.iter(); assert_eq!(&1, iter.next().unwrap()); @@ -220,13 +220,13 @@ mod tests { #[test] fn run_test_forward_iter_non_power_of_two() { fn test_iter(mut b: impl RingBuffer) { - b.push(1); - b.push(2); - b.push(3); - b.push(4); - b.push(5); - b.push(6); - b.push(7); + b.enqueue(1); + b.enqueue(2); + b.enqueue(3); + b.enqueue(4); + b.enqueue(5); + b.enqueue(6); + b.enqueue(7); let mut iter = b.iter(); assert_eq!(&1, iter.next().unwrap()); @@ -247,13 +247,13 @@ mod tests { #[test] fn run_test_iter_non_power_of_two() { fn test_iter(mut b: impl RingBuffer) { - b.push(1); - b.push(2); - b.push(3); - b.push(4); - b.push(5); - b.push(6); - b.push(7); + b.enqueue(1); + b.enqueue(2); + b.enqueue(3); + b.enqueue(4); + b.enqueue(5); + b.enqueue(6); + b.enqueue(7); let mut iter = b.iter(); assert_eq!(&1, iter.next().unwrap()); @@ -278,13 +278,13 @@ mod tests { B: RingBuffer, for<'a> &'a B: IntoIterator>, { - b.push(1); - b.push(2); - b.push(3); - b.push(4); - b.push(5); - b.push(6); - b.push(7); + b.enqueue(1); + b.enqueue(2); + b.enqueue(3); + b.enqueue(4); + b.enqueue(5); + b.enqueue(6); + b.enqueue(7); let mut iter = (&b).into_iter(); assert_eq!(&1, iter.next().unwrap()); @@ -305,13 +305,13 @@ mod tests { #[test] fn run_test_into_iter() { fn test_iter(mut b: impl RingBuffer) { - b.push(1); - b.push(2); - b.push(3); - b.push(4); - b.push(5); - b.push(6); - b.push(7); + b.enqueue(1); + b.enqueue(2); + b.enqueue(3); + b.enqueue(4); + b.enqueue(5); + b.enqueue(6); + b.enqueue(7); let mut iter = b.into_iter(); assert_eq!(1, iter.next().unwrap()); @@ -333,9 +333,9 @@ mod tests { #[test] fn run_test_iter_with_lifetimes() { fn test_iter<'a>(string: &'a str, mut b: impl RingBuffer<&'a str>) { - b.push(&string[0..1]); - b.push(&string[1..2]); - b.push(&string[2..3]); + b.enqueue(&string[0..1]); + b.enqueue(&string[1..2]); + b.enqueue(&string[2..3]); let mut iter = b.iter(); assert_eq!(&&string[0..1], iter.next().unwrap()); @@ -355,9 +355,9 @@ mod tests { #[test] fn run_test_double_iter() { fn test_double_iter(mut b: impl RingBuffer) { - b.push(1); - b.push(2); - b.push(3); + b.enqueue(1); + b.enqueue(2); + b.enqueue(3); let mut iter1 = b.iter(); let mut iter2 = b.iter(); @@ -378,10 +378,10 @@ mod tests { #[test] fn run_test_iter_wrap() { fn test_iter_wrap(mut b: impl RingBuffer) { - b.push(1); - b.push(2); + b.enqueue(1); + b.enqueue(2); // Wrap - b.push(3); + b.enqueue(3); let mut iter = b.iter(); assert_eq!(&2, iter.next().unwrap()); @@ -394,10 +394,10 @@ mod tests { // the growable ringbuffer shouldn't actually stop growing let mut b = GrowableAllocRingBuffer::with_capacity(2); - b.push(1); - b.push(2); + b.enqueue(1); + b.enqueue(2); // No wrap - b.push(3); + b.enqueue(3); let mut iter = b.iter(); assert_eq!(&1, iter.next().unwrap()); @@ -409,9 +409,9 @@ mod tests { #[test] fn run_test_iter_mut() { fn test_iter_mut(mut b: impl RingBuffer) { - b.push(1); - b.push(2); - b.push(3); + b.enqueue(1); + b.enqueue(2); + b.enqueue(3); for el in b.iter_mut() { *el += 1; @@ -433,9 +433,9 @@ mod tests { for<'a> &'a mut B: IntoIterator>, { - b.push(1); - b.push(2); - b.push(3); + b.enqueue(1); + b.enqueue(2); + b.enqueue(3); for el in &mut b { *el += 1; @@ -452,9 +452,9 @@ mod tests { #[test] fn test_iter_mut_wrap() { fn run_test_iter_mut_wrap(mut b: impl RingBuffer) { - b.push(1); - b.push(2); - b.push(3); + b.enqueue(1); + b.enqueue(2); + b.enqueue(3); for i in b.iter_mut() { *i += 1; @@ -469,9 +469,9 @@ mod tests { // The growable ringbuffer actually shouldn't wrap let mut b = GrowableAllocRingBuffer::with_capacity(2); - b.push(1); - b.push(2); - b.push(3); + b.enqueue(1); + b.enqueue(2); + b.enqueue(3); for i in b.iter_mut() { *i += 1; @@ -483,9 +483,9 @@ mod tests { #[test] fn test_iter_mut_miri_fail() { fn run_test_iter_mut_wrap(mut b: impl RingBuffer) { - b.push(1); - b.push(2); - b.push(3); + b.enqueue(1); + b.enqueue(2); + b.enqueue(3); let buf = b.iter_mut().collect::>(); @@ -501,9 +501,9 @@ mod tests { // the growable ringbuffer actually shouldn't wrap let mut b = GrowableAllocRingBuffer::with_capacity(2); - b.push(1); - b.push(2); - b.push(3); + b.enqueue(1); + b.enqueue(2); + b.enqueue(3); let buf = b.iter_mut().collect::>(); @@ -517,9 +517,9 @@ mod tests { #[test] fn run_test_to_vec() { fn test_to_vec(mut b: impl RingBuffer) { - b.push(1); - b.push(2); - b.push(3); + b.enqueue(1); + b.enqueue(2); + b.enqueue(3); assert_eq!(vec![1, 2, 3], b.to_vec()) } @@ -532,10 +532,10 @@ mod tests { #[test] fn run_test_to_vec_wrap() { fn test_to_vec_wrap(mut b: impl RingBuffer) { - b.push(1); - b.push(2); + b.enqueue(1); + b.enqueue(2); // Wrap - b.push(3); + b.enqueue(3); assert_eq!(vec![2, 3], b.to_vec()) } @@ -546,9 +546,9 @@ mod tests { // The growable ringbuffer should actually remember all items let mut b = GrowableAllocRingBuffer::with_capacity(2); - b.push(1); - b.push(2); - b.push(3); + b.enqueue(1); + b.enqueue(2); + b.enqueue(3); assert_eq!(vec![1, 2, 3], b.to_vec()) } @@ -556,7 +556,7 @@ mod tests { #[test] fn run_test_index() { fn test_index(mut b: impl RingBuffer) { - b.push(2); + b.enqueue(2); assert_eq!(b[0], 2) } @@ -568,14 +568,14 @@ mod tests { #[test] fn run_test_get() { fn test_index(mut b: impl RingBuffer) { - b.push(0); - b.push(1); - b.push(2); - b.push(3); - b.push(4); - b.push(5); - b.push(6); - b.push(7); + b.enqueue(0); + b.enqueue(1); + b.enqueue(2); + b.enqueue(3); + b.enqueue(4); + b.enqueue(5); + b.enqueue(6); + b.enqueue(7); assert_eq!(b.get(0), Some(&0)); assert_eq!(b.get(1), Some(&1)); @@ -595,7 +595,7 @@ mod tests { #[test] fn run_test_index_mut() { fn test_index_mut(mut b: impl RingBuffer) { - b.push(2); + b.enqueue(2); assert_eq!(b[0], 2); @@ -612,8 +612,8 @@ mod tests { #[test] fn run_test_peek_some() { fn test_peek_some(mut b: impl RingBuffer) { - b.push(1); - b.push(2); + b.enqueue(1); + b.enqueue(2); assert_eq!(b.peek(), Some(&1)); } @@ -637,8 +637,8 @@ mod tests { #[test] fn run_test_get_relative() { fn test_get_relative(mut b: impl RingBuffer) { - b.push(0); - b.push(1); + b.enqueue(0); + b.enqueue(1); // get[(index + 1) % len] = 1 assert_eq!(b.get(0).unwrap(), &0); @@ -657,9 +657,9 @@ mod tests { #[test] fn run_test_wrapping_get_relative() { fn test_wrapping_get_relative(mut b: impl RingBuffer) { - b.push(0); - b.push(1); - b.push(2); + b.enqueue(0); + b.enqueue(1); + b.enqueue(2); // [0, ...] // ^ @@ -678,9 +678,9 @@ mod tests { // the growable ringbuffer actually shouldn't wrap let mut b = GrowableAllocRingBuffer::with_capacity(2); - b.push(0); - b.push(1); - b.push(2); + b.enqueue(0); + b.enqueue(1); + b.enqueue(2); assert_eq!(b.get(0).unwrap(), &0); assert_eq!(b.get(1).unwrap(), &1); @@ -701,8 +701,8 @@ mod tests { #[test] fn run_test_get_relative_mut() { fn test_get_relative_mut(mut b: impl RingBuffer) { - b.push(0); - b.push(1); + b.enqueue(0); + b.enqueue(1); // [0, ...] // ^ @@ -725,9 +725,9 @@ mod tests { #[test] fn run_test_wrapping_get_relative_mut() { fn test_wrapping_get_relative_mut(mut b: impl RingBuffer) { - b.push(0); - b.push(1); - b.push(2); + b.enqueue(0); + b.enqueue(1); + b.enqueue(2); *b.get_mut(0).unwrap() = 3; @@ -749,9 +749,9 @@ mod tests { // the growable ringbuffer actually shouldn't wrap let mut b = GrowableAllocRingBuffer::with_capacity(2); - b.push(0); - b.push(1); - b.push(2); + b.enqueue(0); + b.enqueue(1); + b.enqueue(2); *b.get_mut(0).unwrap() = 3; @@ -798,8 +798,8 @@ mod tests { #[test] fn run_test_get_relative_negative() { fn test_get_relative_negative(mut b: impl RingBuffer) { - b.push(0); - b.push(1); + b.enqueue(0); + b.enqueue(1); // [0, ...] // ^ @@ -822,8 +822,8 @@ mod tests { #[test] fn run_test_contains() { fn test_contains(mut b: impl RingBuffer) { - b.push(1); - b.push(2); + b.enqueue(1); + b.enqueue(2); assert!(b.contains(&1)); assert!(b.contains(&2)); @@ -838,9 +838,9 @@ mod tests { fn run_test_is_full() { fn test_is_full(mut b: impl RingBuffer) { assert!(!b.is_full()); - b.push(1); + b.enqueue(1); assert!(!b.is_full()); - b.push(2); + b.enqueue(2); assert!(b.is_full()); } @@ -852,8 +852,8 @@ mod tests { #[test] fn run_test_front_some() { fn test_front_some(mut b: impl RingBuffer) { - b.push(1); - b.push(2); + b.enqueue(1); + b.enqueue(2); assert_eq!(b.front(), Some(&1)); } @@ -877,8 +877,8 @@ mod tests { #[test] fn run_test_back_some() { fn test_back_some(mut b: impl RingBuffer) { - b.push(1); - b.push(2); + b.enqueue(1); + b.enqueue(2); assert_eq!(b.back(), Some(&2)); } @@ -902,8 +902,8 @@ mod tests { #[test] fn run_test_front_some_mut() { fn test_front_some_mut(mut b: impl RingBuffer) { - b.push(1); - b.push(2); + b.enqueue(1); + b.enqueue(2); assert_eq!(b.front_mut(), Some(&mut 1)); } @@ -927,8 +927,8 @@ mod tests { #[test] fn run_test_back_some_mut() { fn test_back_some_mut(mut b: impl RingBuffer) { - b.push(1); - b.push(2); + b.enqueue(1); + b.enqueue(2); assert_eq!(b.back_mut(), Some(&mut 2)); } @@ -952,8 +952,8 @@ mod tests { #[test] fn run_test_dequeue() { fn run_test_dequeue(mut b: impl RingBuffer) { - b.push(0); - b.push(1); + b.enqueue(0); + b.enqueue(1); assert_eq!(b.len(), 2); @@ -974,8 +974,8 @@ mod tests { fn run_test_skip() { #[allow(deprecated)] fn test_skip(mut b: impl RingBuffer) { - b.push(0); - b.push(1); + b.enqueue(0); + b.enqueue(1); assert_eq!(b.len(), 2); @@ -997,7 +997,7 @@ mod tests { rb.skip(); rb.skip(); rb.skip(); - rb.push(1); + rb.enqueue(1); assert_eq!(rb.dequeue(), Some(1)); assert_eq!(rb.dequeue(), None); rb.skip(); @@ -1010,8 +1010,9 @@ mod tests { } #[test] - fn run_test_push_dequeue_push() { - fn test_push_dequeue_push(mut b: impl RingBuffer) { + #[allow(deprecated)] + fn run_test_push_pop() { + fn test_push_pop(mut b: impl RingBuffer) { b.push(0); b.push(1); @@ -1027,14 +1028,14 @@ mod tests { assert_eq!(b.dequeue(), None); } - test_push_dequeue_push(AllocRingBuffer::new(8)); - test_push_dequeue_push(GrowableAllocRingBuffer::with_capacity(8)); - test_push_dequeue_push(ConstGenericRingBuffer::::new()); + test_push_pop(AllocRingBuffer::new(8)); + test_push_pop(GrowableAllocRingBuffer::with_capacity(8)); + test_push_pop(ConstGenericRingBuffer::::new()); } #[test] - fn run_test_enqueue_dequeue_push() { - fn test_enqueue_dequeue_push(mut b: impl RingBuffer) { + fn run_test_enqueue_dequeue_enqueue() { + fn test_enqueue_dequeue_enqueue(mut b: impl RingBuffer) { b.enqueue(0); b.enqueue(1); @@ -1050,16 +1051,16 @@ mod tests { assert_eq!(b.dequeue(), None); } - test_enqueue_dequeue_push(AllocRingBuffer::new(8)); - test_enqueue_dequeue_push(GrowableAllocRingBuffer::with_capacity(8)); - test_enqueue_dequeue_push(ConstGenericRingBuffer::::new()); + test_enqueue_dequeue_enqueue(AllocRingBuffer::new(8)); + test_enqueue_dequeue_enqueue(GrowableAllocRingBuffer::with_capacity(8)); + test_enqueue_dequeue_enqueue(ConstGenericRingBuffer::::new()); } #[test] fn large_negative_index() { fn test_large_negative_index(mut b: impl RingBuffer) { - b.push(1); - b.push(2); + b.enqueue(1); + b.enqueue(2); assert_eq!(b.get_signed(1), Some(&2)); assert_eq!(b.get_signed(0), Some(&1)); assert_eq!(b.get_signed(-1), Some(&2)); @@ -1075,8 +1076,8 @@ mod tests { #[test] fn large_negative_index_mut() { fn test_large_negative_index(mut b: impl RingBuffer) { - b.push(1); - b.push(2); + b.enqueue(1); + b.enqueue(2); assert_eq!(b.get_mut_signed(1), Some(&mut 2)); assert_eq!(b.get_mut_signed(0), Some(&mut 1)); assert_eq!(b.get_mut_signed(-1), Some(&mut 2)); @@ -1090,42 +1091,42 @@ mod tests { } #[test] - fn run_test_push_dequeue_push_full() { - fn test_push_dequeue_push_full(mut b: impl RingBuffer) { - b.push(0); - b.push(1); - b.push(2); + fn run_test_enqueue_dequeue_enqueue_full() { + fn test_enqueue_dequeue_enqueue_full(mut b: impl RingBuffer) { + b.enqueue(0); + b.enqueue(1); + b.enqueue(2); assert_eq!(b.dequeue(), Some(1)); assert_eq!(b.dequeue(), Some(2)); assert_eq!(b.dequeue(), None); - b.push(0); - b.push(1); - b.push(2); + b.enqueue(0); + b.enqueue(1); + b.enqueue(2); assert_eq!(b.dequeue(), Some(1)); assert_eq!(b.dequeue(), Some(2)); assert_eq!(b.dequeue(), None); } - test_push_dequeue_push_full(AllocRingBuffer::new(2)); - test_push_dequeue_push_full(ConstGenericRingBuffer::::new()); + test_enqueue_dequeue_enqueue_full(AllocRingBuffer::new(2)); + test_enqueue_dequeue_enqueue_full(ConstGenericRingBuffer::::new()); // the growable ringbuffer should actually keep growing and dequeue all items let mut b = GrowableAllocRingBuffer::with_capacity(2); - b.push(0); - b.push(1); - b.push(2); + b.enqueue(0); + b.enqueue(1); + b.enqueue(2); assert_eq!(b.dequeue(), Some(0)); assert_eq!(b.dequeue(), Some(1)); assert_eq!(b.dequeue(), Some(2)); assert_eq!(b.dequeue(), None); - b.push(0); - b.push(1); - b.push(2); + b.enqueue(0); + b.enqueue(1); + b.enqueue(2); assert_eq!(b.dequeue(), Some(0)); assert_eq!(b.dequeue(), Some(1)); @@ -1134,60 +1135,60 @@ mod tests { } #[test] - fn run_test_push_dequeue_push_full_get() { - fn test_push_dequeue_push_full_get(mut b: impl RingBuffer) { - b.push(0); - b.push(1); - b.push(2); + fn run_test_enqueue_dequeue_enqueue_full_get() { + fn test_enqueue_dequeue_enqueue_full_get(mut b: impl RingBuffer) { + b.enqueue(0); + b.enqueue(1); + b.enqueue(2); assert_eq!(b.dequeue(), Some(1)); assert_eq!(b.dequeue(), Some(2)); assert_eq!(b.dequeue(), None); - b.push(0); - b.push(1); - b.push(2); + b.enqueue(0); + b.enqueue(1); + b.enqueue(2); assert_eq!(b.dequeue(), Some(1)); assert_eq!(b.dequeue(), Some(2)); assert_eq!(b.dequeue(), None); - b.push(0); - b.push(1); - b.push(2); + b.enqueue(0); + b.enqueue(1); + b.enqueue(2); assert_eq!(b.get_signed(-1), Some(&2)); assert_eq!(b.get_signed(-2), Some(&1)); assert_eq!(b.get_signed(-3), Some(&2)); } - test_push_dequeue_push_full_get(AllocRingBuffer::new(2)); - test_push_dequeue_push_full_get(ConstGenericRingBuffer::::new()); + test_enqueue_dequeue_enqueue_full_get(AllocRingBuffer::new(2)); + test_enqueue_dequeue_enqueue_full_get(ConstGenericRingBuffer::::new()); // the growable ringbuffer should actually keep growing and dequeue all items let mut b = GrowableAllocRingBuffer::with_capacity(2); - b.push(0); - b.push(1); - b.push(2); + b.enqueue(0); + b.enqueue(1); + b.enqueue(2); assert_eq!(b.dequeue(), Some(0)); assert_eq!(b.dequeue(), Some(1)); assert_eq!(b.dequeue(), Some(2)); assert_eq!(b.dequeue(), None); - b.push(0); - b.push(1); - b.push(2); + b.enqueue(0); + b.enqueue(1); + b.enqueue(2); assert_eq!(b.dequeue(), Some(0)); assert_eq!(b.dequeue(), Some(1)); assert_eq!(b.dequeue(), Some(2)); assert_eq!(b.dequeue(), None); - b.push(0); - b.push(1); - b.push(2); + b.enqueue(0); + b.enqueue(1); + b.enqueue(2); assert_eq!(b.get_signed(-1), Some(&2)); assert_eq!(b.get_signed(-2), Some(&1)); @@ -1197,49 +1198,49 @@ mod tests { #[test] #[cfg_attr(miri, ignore)] // this test takes far too long with Miri enabled - fn run_test_push_dequeue_push_full_get_rep() { - fn test_push_dequeue_push_full_get_rep(mut rb: impl RingBuffer) { + fn run_test_enqueue_dequeue_enqueue_full_get_rep() { + fn test_enqueue_dequeue_enqueue_full_get_rep(mut rb: impl RingBuffer) { for _ in 0..100_000 { - rb.push(1); - rb.push(2); + rb.enqueue(1); + rb.enqueue(2); assert_eq!(rb.dequeue(), Some(1)); assert_eq!(rb.dequeue(), Some(2)); - rb.push(1); - rb.push(2); + rb.enqueue(1); + rb.enqueue(2); assert_eq!(rb.dequeue(), Some(1)); assert_eq!(rb.dequeue(), Some(2)); - rb.push(1); - rb.push(2); + rb.enqueue(1); + rb.enqueue(2); assert_eq!(rb.get_signed(-1), Some(&2)); assert_eq!(rb.get_signed(-2), Some(&1)); } } - test_push_dequeue_push_full_get_rep(AllocRingBuffer::new(8)); - test_push_dequeue_push_full_get_rep(GrowableAllocRingBuffer::with_capacity(8)); - test_push_dequeue_push_full_get_rep(ConstGenericRingBuffer::::new()); + test_enqueue_dequeue_enqueue_full_get_rep(AllocRingBuffer::new(8)); + test_enqueue_dequeue_enqueue_full_get_rep(GrowableAllocRingBuffer::with_capacity(8)); + test_enqueue_dequeue_enqueue_full_get_rep(ConstGenericRingBuffer::::new()); } #[test] fn run_test_clone() { fn test_clone(mut rb: impl RingBuffer + Clone + Eq + Debug) { - rb.push(42); - rb.push(32); - rb.push(22); + rb.enqueue(42); + rb.enqueue(32); + rb.enqueue(22); let mut other = rb.clone(); assert_eq!(rb, other); - rb.push(11); - rb.push(12); - other.push(11); - other.push(12); + rb.enqueue(11); + rb.enqueue(12); + other.enqueue(11); + other.enqueue(12); assert_eq!(rb, other); } @@ -1254,7 +1255,7 @@ mod tests { fn test_default_fill(mut rb: impl RingBuffer) { for i in 0..rb.capacity() { for _ in 0..i { - rb.push(1); + rb.enqueue(1); } assert_eq!(rb.len(), i); @@ -1280,12 +1281,12 @@ mod tests { let mut alloc_b = ConstGenericRingBuffer::::new(); assert!(alloc_a.eq(&alloc_b)); - alloc_a.push(1); + alloc_a.enqueue(1); assert!(!alloc_b.eq(&alloc_a)); - alloc_b.push(1); + alloc_b.enqueue(1); assert!(alloc_a.eq(&alloc_b)); - alloc_a.push(4); - alloc_b.push(2); + alloc_a.enqueue(4); + alloc_b.enqueue(2); assert!(!alloc_b.eq(&alloc_a)); } @@ -1293,7 +1294,7 @@ mod tests { fn run_next_back_test() { fn next_back_test(mut rb: impl RingBuffer) { for i in 1..=4 { - rb.push(i); + rb.enqueue(i); } let mut it = rb.iter(); @@ -1313,7 +1314,7 @@ mod tests { fn run_next_back_test_mut() { fn next_back_test_mut(mut rb: impl RingBuffer) { for i in 1..=4 { - rb.push(i); + rb.enqueue(i); } let mut it = rb.iter_mut(); @@ -1342,7 +1343,7 @@ mod tests { fn test_fill(mut rb: impl RingBuffer) { for i in 0..rb.capacity() { for _ in 0..i { - rb.push(1); + rb.enqueue(1); } assert_eq!(rb.len(), i); @@ -1394,8 +1395,8 @@ mod tests { parent: Some(unsafe { dt.as_ref() }.unwrap().borrow_mut()), }; let mut rb = { $constructor }; - rb.push(d); - rb.push(Dropee { parent: None }); + rb.enqueue(d); + rb.enqueue(Dropee { parent: None }); } { // Safety: @@ -1431,13 +1432,13 @@ mod tests { macro_rules! test_clone { ($e: expr) => { let mut e1 = $e; - e1.push(1); - e1.push(2); + e1.enqueue(1); + e1.enqueue(2); let mut e2 = e1.clone(); - e2.push(11); - e2.push(12); + e2.enqueue(11); + e2.enqueue(12); assert_eq!(e1.to_vec(), vec![1, 2]); assert_eq!(e2.to_vec(), vec![1, 2, 11, 12]); diff --git a/src/with_alloc/alloc_ringbuffer.rs b/src/with_alloc/alloc_ringbuffer.rs index cc529ea..199101d 100644 --- a/src/with_alloc/alloc_ringbuffer.rs +++ b/src/with_alloc/alloc_ringbuffer.rs @@ -21,19 +21,19 @@ use core::ptr; /// let mut buffer = AllocRingBuffer::new(2); /// /// // First entry of the buffer is now 5. -/// buffer.push(5); +/// buffer.enqueue(5); /// -/// // The last item we pushed is 5 +/// // The last item we enqueued is 5 /// assert_eq!(buffer.back(), Some(&5)); /// /// // Second entry is now 42. -/// buffer.push(42); +/// buffer.enqueue(42); /// /// assert_eq!(buffer.peek(), Some(&5)); /// assert!(buffer.is_full()); /// -/// // Because capacity is reached the next push will be the first item of the buffer. -/// buffer.push(1); +/// // Because capacity is reached the next enqueue will be the first item of the buffer. +/// buffer.enqueue(1); /// assert_eq!(buffer.to_vec(), vec![42, 1]); /// ``` #[derive(Debug)] @@ -163,7 +163,7 @@ impl Clone for AllocRingBuffer { debug_assert_ne!(self.capacity, 0); let mut new = Self::new(self.capacity); - self.iter().cloned().for_each(|i| new.push(i)); + self.iter().cloned().for_each(|i| new.enqueue(i)); new } } @@ -210,7 +210,7 @@ impl Extend for AllocRingBuffer { let iter = iter.into_iter(); for i in iter { - self.push(i); + self.enqueue(i); } } } @@ -420,7 +420,7 @@ mod tests { #[test] fn test_extend() { let mut buf = AllocRingBuffer::::new(4); - (0..4).for_each(|_| buf.push(0)); + (0..4).for_each(|_| buf.enqueue(0)); let new_data = [0, 1, 2]; buf.extend(new_data); @@ -437,7 +437,7 @@ mod tests { #[test] fn test_extend_with_overflow() { let mut buf = AllocRingBuffer::::new(8); - (0..8).for_each(|_| buf.push(0)); + (0..8).for_each(|_| buf.enqueue(0)); let new_data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; buf.extend(new_data); diff --git a/src/with_const_generics.rs b/src/with_const_generics.rs index 7f247c9..c47148f 100644 --- a/src/with_const_generics.rs +++ b/src/with_const_generics.rs @@ -18,19 +18,19 @@ use core::ops::{Index, IndexMut}; /// let mut buffer = ConstGenericRingBuffer::<_, 2>::new(); /// /// // First entry of the buffer is now 5. -/// buffer.push(5); +/// buffer.enqueue(5); /// -/// // The last item we pushed is 5 +/// // The last item we enqueued is 5 /// assert_eq!(buffer.back(), Some(&5)); /// /// // Second entry is now 42. -/// buffer.push(42); +/// buffer.enqueue(42); /// /// assert_eq!(buffer.peek(), Some(&5)); /// assert!(buffer.is_full()); /// -/// // Because capacity is reached the next push will be the first item of the buffer. -/// buffer.push(1); +/// // Because capacity is reached the next enqueue will be the first item of the buffer. +/// buffer.enqueue(1); /// assert_eq!(buffer.to_vec(), vec![42, 1]); /// ``` #[derive(Debug)] @@ -138,7 +138,7 @@ impl Drop for ConstGenericRingBuffer { impl Clone for ConstGenericRingBuffer { fn clone(&self) -> Self { let mut new = ConstGenericRingBuffer::::new(); - self.iter().cloned().for_each(|i| new.push(i)); + self.iter().cloned().for_each(|i| new.enqueue(i)); new } } @@ -246,7 +246,7 @@ impl Extend for ConstGenericRingBuffer { let iter = iter.into_iter(); for i in iter { - self.push(i); + self.enqueue(i); } } } @@ -331,7 +331,7 @@ impl FromIterator for ConstGenericRingBuffer fn from_iter>(iter: T) -> Self { let mut res = Self::default(); for i in iter { - res.push(i); + res.enqueue(i); } res @@ -387,7 +387,7 @@ mod tests { #[test] fn test_extend() { let mut buf = ConstGenericRingBuffer::::new(); - (0..4).for_each(|_| buf.push(0)); + (0..4).for_each(|_| buf.enqueue(0)); let new_data = [0, 1, 2]; buf.extend(new_data); @@ -404,7 +404,7 @@ mod tests { #[test] fn test_extend_with_overflow() { let mut buf = ConstGenericRingBuffer::::new(); - (0..8).for_each(|_| buf.push(0)); + (0..8).for_each(|_| buf.enqueue(0)); let new_data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; buf.extend(new_data); diff --git a/tests/compile-fail/test_const_generic_array_zero_length_new.rs b/tests/compile-fail/test_const_generic_array_zero_length_new.rs index b080de1..2d2798e 100644 --- a/tests/compile-fail/test_const_generic_array_zero_length_new.rs +++ b/tests/compile-fail/test_const_generic_array_zero_length_new.rs @@ -6,5 +6,5 @@ fn main() { let mut buf = ConstGenericRingBuffer::new::<0>(); //~^ note: the above error was encountered while instantiating `fn ringbuffer::ConstGenericRingBuffer::::new::<0>` // ringbuffer can't be zero length - buf.push(5); + buf.enqueue(5); } diff --git a/tests/conversions.rs b/tests/conversions.rs index e3c13a2..fad6807 100644 --- a/tests/conversions.rs +++ b/tests/conversions.rs @@ -15,7 +15,7 @@ macro_rules! convert_test { let mut b: $to = a.into(); assert_eq!(b.to_vec(), vec!['1', '2']); - b.push('3'); + b.enqueue('3'); assert_eq!(b, b); } }; @@ -123,13 +123,13 @@ fn test_extra_conversions_const() { fn test_const_generic_new_parameter() { // Can we specify size only on the method? let mut a = ConstGenericRingBuffer::new::<2>(); - a.push(5); + a.enqueue(5); // Can we specify size in both positions? let mut a = ConstGenericRingBuffer::::new::<50>(); - a.push(5); + a.enqueue(5); // Can we specify size only on the struct? let mut a = ConstGenericRingBuffer::::new(); - a.push(5); + a.enqueue(5); } From 3adfdc3f3848ed087d2de4e95a06646309a461f6 Mon Sep 17 00:00:00 2001 From: Vivian Date: Wed, 15 May 2024 20:07:59 +0200 Subject: [PATCH 5/7] fix lint --- src/ringbuffer_trait.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/ringbuffer_trait.rs b/src/ringbuffer_trait.rs index 8e86c66..84f0e20 100644 --- a/src/ringbuffer_trait.rs +++ b/src/ringbuffer_trait.rs @@ -75,12 +75,11 @@ pub unsafe trait RingBuffer: #[doc(hidden)] unsafe fn ptr_buffer_size(rb: *const Self) -> usize; - /// Alias for [`enqueue`] #[deprecated = "use enqueue instead"] #[inline] fn push(&mut self, value: T) { - self.enqueue(value) + self.enqueue(value); } /// Adds a value onto the buffer. From 4ed20d22b592573ee2bf7dbe9294d71d394c2f13 Mon Sep 17 00:00:00 2001 From: Vivian Date: Wed, 15 May 2024 21:12:14 +0200 Subject: [PATCH 6/7] fix more lints --- README.md | 27 ++++++++++++--------------- benches/bench.rs | 16 ++++++++-------- src/with_alloc/alloc_ringbuffer.rs | 8 ++++++-- src/with_alloc/vecdeque.rs | 8 ++++---- src/with_const_generics.rs | 6 +++++- 5 files changed, 35 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index e3cd9ae..9c0d455 100644 --- a/README.md +++ b/README.md @@ -27,25 +27,22 @@ MSRV: Rust 1.59 ```rust use ringbuffer::{AllocRingBuffer, RingBuffer}; -fn main() { - let mut buffer = AllocRingBuffer::with_capacity(2); +let mut buffer = AllocRingBuffer::with_capacity(2); - // First entry of the buffer is now 5. - buffer.push(5); +// First entry of the buffer is now 5. +buffer.push(5); - // The last item we pushed is 5 - assert_eq!(buffer.back(), Some(&5)); +// The last item we pushed is 5 +assert_eq!(buffer.back(), Some(&5)); - // Second entry is now 42. - buffer.push(42); - assert_eq!(buffer.peek(), Some(&5)); - assert!(buffer.is_full()); - - // Because capacity is reached the next push will be the first item of the buffer. - buffer.push(1); - assert_eq!(buffer.to_vec(), vec![42, 1]); -} +// Second entry is now 42. +buffer.push(42); +assert_eq!(buffer.peek(), Some(&5)); +assert!(buffer.is_full()); +// Because capacity is reached the next push will be the first item of the buffer. +buffer.push(1); +assert_eq!(buffer.to_vec(), vec![42, 1]); ``` # Features diff --git a/benches/bench.rs b/benches/bench.rs index 6793a40..51c3dfc 100644 --- a/benches/bench.rs +++ b/benches/bench.rs @@ -7,7 +7,7 @@ fn benchmark_push, F: Fn() -> T>(b: &mut Bencher, new: F) { let mut rb = new(); for i in 0..1_000_000 { - rb.push(i); + rb.enqueue(i); black_box(()); } @@ -20,25 +20,25 @@ fn benchmark_push_dequeue, F: Fn() -> T>(b: &mut Bencher, new let mut rb = new(); for _i in 0..100_000 { - rb.push(1); + rb.enqueue(1); black_box(()); - rb.push(2); + rb.enqueue(2); black_box(()); assert_eq!(black_box(rb.dequeue()), Some(1)); assert_eq!(black_box(rb.dequeue()), Some(2)); - rb.push(1); + rb.enqueue(1); black_box(()); - rb.push(2); + rb.enqueue(2); black_box(()); assert_eq!(black_box(rb.dequeue()), Some(1)); assert_eq!(black_box(rb.dequeue()), Some(2)); - rb.push(1); + rb.enqueue(1); black_box(()); - rb.push(2); + rb.enqueue(2); black_box(()); assert_eq!(black_box(rb.get_signed(-1)), Some(&2)); @@ -54,7 +54,7 @@ fn benchmark_various, F: Fn() -> T>(b: &mut Bencher, new: F) let mut rb = new(); for i in 0..100_000 { - rb.push(i); + rb.enqueue(i); black_box(()); black_box(rb.back()); } diff --git a/src/with_alloc/alloc_ringbuffer.rs b/src/with_alloc/alloc_ringbuffer.rs index 199101d..35cfa46 100644 --- a/src/with_alloc/alloc_ringbuffer.rs +++ b/src/with_alloc/alloc_ringbuffer.rs @@ -153,7 +153,7 @@ impl Drop for AllocRingBuffer { let layout = alloc::alloc::Layout::array::(self.size).unwrap(); unsafe { - alloc::alloc::dealloc(self.buf as *mut u8, layout); + alloc::alloc::dealloc(self.buf.cast(), layout); } } } @@ -187,6 +187,8 @@ impl IntoIterator for AllocRingBuffer { } } +#[allow(clippy::into_iter_without_iter)] +// iter() is implemented on the trait impl<'a, T> IntoIterator for &'a AllocRingBuffer { type Item = &'a T; type IntoIter = RingBufferIterator<'a, T, AllocRingBuffer>; @@ -196,6 +198,8 @@ impl<'a, T> IntoIterator for &'a AllocRingBuffer { } } +#[allow(clippy::into_iter_without_iter)] +// iter_mut() is implemented on the trait impl<'a, T> IntoIterator for &'a mut AllocRingBuffer { type Item = &'a mut T; type IntoIter = RingBufferMutIterator<'a, T, AllocRingBuffer>; @@ -320,7 +324,7 @@ impl AllocRingBuffer { assert_ne!(capacity, 0, "Capacity must be greater than 0"); let size = capacity.next_power_of_two(); let layout = alloc::alloc::Layout::array::(size).unwrap(); - let buf = unsafe { alloc::alloc::alloc(layout) as *mut T }; + let buf = unsafe { alloc::alloc::alloc(layout).cast() }; Self { buf, size, diff --git a/src/with_alloc/vecdeque.rs b/src/with_alloc/vecdeque.rs index bfb1122..7f13a54 100644 --- a/src/with_alloc/vecdeque.rs +++ b/src/with_alloc/vecdeque.rs @@ -216,9 +216,9 @@ unsafe impl RingBuffer for GrowableAllocRingBuffer { if self.is_empty() { None } else if index >= 0 { - self.0.get(crate::mask_modulo(self.0.len(), index as usize)) + self.0.get(crate::mask_modulo(self.0.len(), index.unsigned_abs())) } else { - let positive_index = -index as usize - 1; + let positive_index = index.unsigned_abs() - 1; let masked = crate::mask_modulo(self.0.len(), positive_index); let index = self.0.len() - 1 - masked; @@ -231,11 +231,11 @@ unsafe impl RingBuffer for GrowableAllocRingBuffer { if RingBuffer::ptr_len(rb) == 0 { None } else if index >= 0 { - (*rb).0.get_mut(index as usize) + (*rb).0.get_mut(index.unsigned_abs()) } else { let len = Self::ptr_len(rb); - let positive_index = -index as usize + 1; + let positive_index = index.unsigned_abs() + 1; let masked = crate::mask_modulo(len, positive_index); let index = len - 1 - masked; diff --git a/src/with_const_generics.rs b/src/with_const_generics.rs index c47148f..9f26462 100644 --- a/src/with_const_generics.rs +++ b/src/with_const_generics.rs @@ -176,7 +176,7 @@ impl ConstGenericRingBuffer { ConstGenericRingBuffer: From>, { #[allow(clippy::let_unit_value)] - let _ = Self::ERROR_CAPACITY_IS_NOT_ALLOWED_TO_BE_ZERO; + let () = Self::ERROR_CAPACITY_IS_NOT_ALLOWED_TO_BE_ZERO; // allow here since we are constructing an array of MaybeUninit // which explicitly *is* defined behavior @@ -223,6 +223,8 @@ impl IntoIterator for ConstGenericRingBuffer { } } +#[allow(clippy::into_iter_without_iter)] +// iter() is implemented on the trait impl<'a, T, const CAP: usize> IntoIterator for &'a ConstGenericRingBuffer { type Item = &'a T; type IntoIter = RingBufferIterator<'a, T, ConstGenericRingBuffer>; @@ -232,6 +234,8 @@ impl<'a, T, const CAP: usize> IntoIterator for &'a ConstGenericRingBuffer IntoIterator for &'a mut ConstGenericRingBuffer { type Item = &'a mut T; type IntoIter = RingBufferMutIterator<'a, T, ConstGenericRingBuffer>; From 0e4e86f35c2c9fea9a026b9f77f3d91fafa4714b Mon Sep 17 00:00:00 2001 From: Vivian Date: Wed, 15 May 2024 21:18:44 +0200 Subject: [PATCH 7/7] fmt --- src/with_alloc/vecdeque.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/with_alloc/vecdeque.rs b/src/with_alloc/vecdeque.rs index 7f13a54..ff8112a 100644 --- a/src/with_alloc/vecdeque.rs +++ b/src/with_alloc/vecdeque.rs @@ -216,7 +216,8 @@ unsafe impl RingBuffer for GrowableAllocRingBuffer { if self.is_empty() { None } else if index >= 0 { - self.0.get(crate::mask_modulo(self.0.len(), index.unsigned_abs())) + self.0 + .get(crate::mask_modulo(self.0.len(), index.unsigned_abs())) } else { let positive_index = index.unsigned_abs() - 1; let masked = crate::mask_modulo(self.0.len(), positive_index);