andreacorbellini/rust-circular-buffer

`push_front` and `push_back` should return dropped element

kKdH opened this issue · 3 comments

kKdH commented

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());
}

Hi, this feature was already contributed in #3. Because this is a backward-incompatible change, I could not release it in 0.1.x, so I'm holding it off until a future 0.2.x release. I plan to make such 0.2.x release within the next 2-3 months, so stay tuned!

kKdH commented

Thank you! Sounds great. That in combination with #8 would be awesome!