Skip to content

Commit

Permalink
fix more lints
Browse files Browse the repository at this point in the history
  • Loading branch information
NULLx76 committed May 15, 2024
1 parent 3adfdc3 commit 4ed20d2
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 30 deletions.
27 changes: 12 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn benchmark_push<T: RingBuffer<i32>, 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(());
}

Expand All @@ -20,25 +20,25 @@ fn benchmark_push_dequeue<T: RingBuffer<i32>, 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));
Expand All @@ -54,7 +54,7 @@ fn benchmark_various<T: RingBuffer<i32>, 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());
}
Expand Down
8 changes: 6 additions & 2 deletions src/with_alloc/alloc_ringbuffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ impl<T> Drop for AllocRingBuffer<T> {

let layout = alloc::alloc::Layout::array::<T>(self.size).unwrap();
unsafe {
alloc::alloc::dealloc(self.buf as *mut u8, layout);
alloc::alloc::dealloc(self.buf.cast(), layout);
}
}
}
Expand Down Expand Up @@ -187,6 +187,8 @@ impl<T> IntoIterator for AllocRingBuffer<T> {
}
}

#[allow(clippy::into_iter_without_iter)]
// iter() is implemented on the trait
impl<'a, T> IntoIterator for &'a AllocRingBuffer<T> {
type Item = &'a T;
type IntoIter = RingBufferIterator<'a, T, AllocRingBuffer<T>>;
Expand All @@ -196,6 +198,8 @@ impl<'a, T> IntoIterator for &'a AllocRingBuffer<T> {
}
}

#[allow(clippy::into_iter_without_iter)]
// iter_mut() is implemented on the trait
impl<'a, T> IntoIterator for &'a mut AllocRingBuffer<T> {
type Item = &'a mut T;
type IntoIter = RingBufferMutIterator<'a, T, AllocRingBuffer<T>>;
Expand Down Expand Up @@ -320,7 +324,7 @@ impl<T> AllocRingBuffer<T> {
assert_ne!(capacity, 0, "Capacity must be greater than 0");
let size = capacity.next_power_of_two();
let layout = alloc::alloc::Layout::array::<T>(size).unwrap();
let buf = unsafe { alloc::alloc::alloc(layout) as *mut T };
let buf = unsafe { alloc::alloc::alloc(layout).cast() };
Self {
buf,
size,
Expand Down
8 changes: 4 additions & 4 deletions src/with_alloc/vecdeque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,9 @@ unsafe impl<T> RingBuffer<T> for GrowableAllocRingBuffer<T> {
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;

Expand All @@ -231,11 +231,11 @@ unsafe impl<T> RingBuffer<T> for GrowableAllocRingBuffer<T> {
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;

Expand Down
6 changes: 5 additions & 1 deletion src/with_const_generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ impl<T, const CAP: usize> ConstGenericRingBuffer<T, CAP> {
ConstGenericRingBuffer<T, CAP>: From<ConstGenericRingBuffer<T, N>>,
{
#[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<T>
// which explicitly *is* defined behavior
Expand Down Expand Up @@ -223,6 +223,8 @@ impl<T, const CAP: usize> IntoIterator for ConstGenericRingBuffer<T, CAP> {
}
}

#[allow(clippy::into_iter_without_iter)]
// iter() is implemented on the trait
impl<'a, T, const CAP: usize> IntoIterator for &'a ConstGenericRingBuffer<T, CAP> {
type Item = &'a T;
type IntoIter = RingBufferIterator<'a, T, ConstGenericRingBuffer<T, CAP>>;
Expand All @@ -232,6 +234,8 @@ impl<'a, T, const CAP: usize> IntoIterator for &'a ConstGenericRingBuffer<T, CAP
}
}

#[allow(clippy::into_iter_without_iter)]
// iter_mut() is implemented on the trait
impl<'a, T, const CAP: usize> IntoIterator for &'a mut ConstGenericRingBuffer<T, CAP> {
type Item = &'a mut T;
type IntoIter = RingBufferMutIterator<'a, T, ConstGenericRingBuffer<T, CAP>>;
Expand Down

1 comment on commit 4ed20d2

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.