Skip to content

Commit

Permalink
add split_off_or_default to removal extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
teh-cmc committed Nov 30, 2023
1 parent e637361 commit c41aebb
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions crates/re_log_types/src/vec_deque_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@ pub trait VecDequeRemovalExt<T> {
///
/// Element at index 0 is the front of the queue.
fn swap_remove(&mut self, index: usize) -> Option<T>;

/// Splits the deque into two at the given index.
///
/// Returns a newly allocated `VecDeque`. `self` contains elements `[0, at)`,
/// and the returned deque contains elements `[at, len)`.
///
/// If `at` is equal or greater than the length, the returned `VecDeque` is empty.
///
/// Note that the capacity of `self` does not change.
///
/// Element at index 0 is the front of the queue.
fn split_off_or_default(&mut self, at: usize) -> Self;
}

impl<T: Clone> VecDequeRemovalExt<T> for VecDeque<T> {
Expand All @@ -110,6 +122,14 @@ impl<T: Clone> VecDequeRemovalExt<T> for VecDeque<T> {
self.swap_remove_back(index)
}
}

#[inline]
fn split_off_or_default(&mut self, at: usize) -> Self {
if at >= self.len() {
return Default::default();
}
self.split_off(at)
}
}

#[test]
Expand Down

0 comments on commit c41aebb

Please sign in to comment.