Skip to content

The RingBuffer trait #3

@bal-e

Description

@bal-e

Here's a sketch of what the RingBuffer trait should look like:

pub trait RingBuffer {
    type Item;

    fn is_full(&self) -> bool;
    fn is_empty(&self) -> bool;

    // Peek ahead from elements to be dequeued.
    fn peek(&self, off: usize) -> Option<&Self::Item>;
    fn peek_mut(&mut self, off: usize) -> Option<&mut Self::Item>;

    // Peek behind to elements recently enqueued.
    fn peek_back(&self, off: usize) -> Option<&Self::Item>;
    fn peek_back_mut(&mut self, off: usize) -> Option<&mut Self::Item>;

    fn enqueue(&self, item: Self::Item) -> Option<Self::Item>;
    fn dequeue(&self) -> Option<Self::Item>;
}

I'm still not sure about the peek methods, but they're here to facilitate discussion on better APIs. Are there any other functions or types we want to include here (e.g. for iterators)?

I think a fairly common use case is ring buffers that are always full - let's call them SaturatedRingBuffers. They can have their own trait, and it might look like this:

pub trait SaturatedRingBuffer {
    type Item;

    // Peek ahead from elements to be dequeued.
    fn peek(&self, off: usize) -> &Self::Item;
    fn peek_mut(&mut self, off: usize) -> &mut Self::Item;

    // Peek behind to elements recently enqueued.
    fn peek_back(&self, off: usize) -> &Self::Item;
    fn peek_back_mut(&mut self, off: usize) -> &mut Self::Item;

    fn enqueue(&self, item: Self::Item) -> Self::Item;
}

Note that there are a few shared/similar items here. Can we unify them between the two traits, and how might we do that?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions