Skip to content

push_front and push_back should return dropped element #12

@kKdH

Description

@kKdH

Hallo,

it would be very handy if the push_front() and it's corresponding push_back() method return the dropped element.

I have some logic which needs to clean up other stuff in the case an element gets dropped from a CircularBuffer:

let mut revisions = CircularBuffer::new()

...

if let Err(revision) = revisions.try_push_front(revision) {
    if let Some(oldest_revision) = revisions.pop_back() {
        self.store.remove(oldest_revision.hash());
    }
    revisions.try_push_front(revision)
        .expect("After removing the oldest revision, the buffer should now have space to push a new revision.")
}

A fn push_front(&mut self, item: T) -> Option<T> method would reduce the code above to:

if let Some(oldest_revision) = revisions.push_front(revision) {
    self.store.remove(oldest_revision.hash());
}

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