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>;