Skip to content

Commit

Permalink
swap dequeue and push
Browse files Browse the repository at this point in the history
  • Loading branch information
jdonszelmann committed Sep 27, 2023
1 parent 05b4975 commit ce4b26e
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,7 @@ mod tests {

#[test]
fn run_test_skip() {
#[allow(deprecated)]
fn test_skip(mut b: impl RingBuffer<i32>) {
b.push(0);
b.push(1);
Expand All @@ -991,6 +992,7 @@ mod tests {

#[test]
fn run_test_skip_2() {
#[allow(deprecated)]
fn test_skip2(mut rb: impl RingBuffer<i32>) {
rb.skip();
rb.skip();
Expand Down
17 changes: 11 additions & 6 deletions src/ringbuffer_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,25 @@ pub unsafe trait RingBuffer<T>:
#[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<T>;

/// 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();
}
Expand Down
2 changes: 1 addition & 1 deletion src/with_alloc/alloc_ringbuffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ unsafe impl<T> RingBuffer<T> for AllocRingBuffer<T> {
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 =
Expand Down
2 changes: 1 addition & 1 deletion src/with_alloc/vecdeque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ unsafe impl<T> RingBuffer<T> for GrowableAllocRingBuffer<T> {
self.pop_front()
}

fn push(&mut self, value: T) {
fn enqueue(&mut self, value: T) {
self.push_back(value);
}

Expand Down
2 changes: 1 addition & 1 deletion src/with_const_generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ unsafe impl<T, const CAP: usize> RingBuffer<T> for ConstGenericRingBuffer<T, CAP
impl_ringbuffer!(readptr, writeptr);

#[inline]
fn push(&mut self, value: T) {
fn enqueue(&mut self, value: T) {
if self.is_full() {
let previous_value = mem::replace(
&mut self.buf[crate::mask_modulo(CAP, self.readptr)],
Expand Down

1 comment on commit ce4b26e

@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.