pub struct Inserter<'a, T> { /* private fields */ }Expand description
An object which can insert multiple elements into a Vec in a single scan through the Vec.
The Inserter has a position in the Vec at which it inserts, called the index. Initially, the index is at the end of the Vec. This index can only be moved further towards front of the Vec, meaning that the indices of the inserted objects must be monotonically non-increasing (i.e. the next index must be less than or equal to the previous index).
Dropping the Inserter without using the entire insertion capacity has a runtime cost proportional to the size of the merged elements. Dropping after using the entire insertion capacity has no runtime cost.
Leaking the Inserter (through std::mem::forget or similar) may leak some items of the Vec and will leave the Vec in an unspecified but valid state
§Example
use scanmut::Inserter;
let mut items = vec!['a', 'b', 'c'];
let mut inserter = Inserter::new(&mut items, 2);
assert_eq!(inserter.index(), 3); // Initial index is at end of vec
inserter.insert('d');
inserter.move_to(1);
inserter.insert('e');
drop(inserter);
assert_eq!(items, ['a', 'e', 'b', 'c', 'd']);It is also possible to insert multiple items at the same position, but keep in mind that, since the index does not shift with the new element, the order will be the reverse of insertion order.
use scanmut::Inserter;
let mut items = vec![1, 2, 3];
let mut inserter = Inserter::new(&mut items, 2);
inserter.move_to(1);
inserter.insert(4);
inserter.insert(5);
drop(inserter);
assert_eq!(items, [1, 5, 4, 2, 3]);Implementations§
Source§impl<'a, T> Inserter<'a, T>
impl<'a, T> Inserter<'a, T>
Sourcepub fn new(vec: &'a mut Vec<T>, additional: usize) -> Inserter<'a, T>
pub fn new(vec: &'a mut Vec<T>, additional: usize) -> Inserter<'a, T>
Create a new Inserter with the specified maximum number of inserts
§Panics
Panics if the length plus the additional number of inserts exceeds isize::MAX bytes.
Sourcepub fn as_slices(&self) -> (&[T], &[T])
pub fn as_slices(&self) -> (&[T], &[T])
Returns a pair of slices representing the current state of the Vecbeing inserted into.
The first slice is the part of the Vec below the index. The second slice is the part of
the Vec above or equal to the index.
Sourcepub fn as_mut_slices(&mut self) -> (&mut [T], &mut [T])
pub fn as_mut_slices(&mut self) -> (&mut [T], &mut [T])
Returns a pair of mutable slices representing the current state of the Vec being inserted
into.
The first slice is the part of the Vec below the index. The second slice is the part of
the Vec above or equal to the index.
Sourcepub fn remaining_inserts(&self) -> usize
pub fn remaining_inserts(&self) -> usize
Returns the remaining number of allowed inserts in this Inserter.
Sourcepub fn move_to(&mut self, index: usize)
pub fn move_to(&mut self, index: usize)
Moves this Inserter to the given index in the Vec.
§Panics
Panics if the index to move to is larger than the current index.
Sourcepub fn insert(&mut self, item: T)
pub fn insert(&mut self, item: T)
Inserts an element into the Vec at the specified index.
§Panics
Panics if there are no more inserts available for this Inserter.
Sourcepub fn insert_all<I>(&mut self, items: I)
pub fn insert_all<I>(&mut self, items: I)
Inserts all items from an iterator into the underlying Vec at the current index.
Sourcepub fn insert_all_rev<I>(&mut self, items: I)where
I: IntoIterator<Item = T>,
pub fn insert_all_rev<I>(&mut self, items: I)where
I: IntoIterator<Item = T>,
Inserts all items from an iterator into the underlying Vec at the current index in
reverse.