[][src]Struct scanmut::Inserter

pub struct Inserter<'a, T> { /* fields omitted */ }

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

impl<'a, T> Inserter<'a, T>[src]

pub fn new(vec: &'a mut Vec<T>, additional: usize) -> Inserter<'a, T>[src]

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.

pub fn as_slices(&self) -> (&[T], &[T])[src]

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.

pub fn as_mut_slices(&mut self) -> (&mut [T], &mut [T])[src]

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.

pub fn remaining_inserts(&self) -> usize[src]

Returns the remaining number of allowed inserts in this Inserter.

pub fn index(&self) -> usize[src]

Returns the current index of the inserter.

pub fn move_to(&mut self, index: usize)[src]

Moves this Inserter to the given index in the Vec.

Panics

Panics if the index to move to is larger than the current index.

pub fn insert(&mut self, item: T)[src]

Inserts an element into the Vec at the specified index.

Panics

Panics if there are no more inserts available for this Inserter.

pub fn insert_all<I>(&mut self, items: I) where
    I: IntoIterator<Item = T>,
    I::IntoIter: DoubleEndedIterator
[src]

Inserts all items from an iterator into the underlying Vec at the current index.

pub fn insert_all_rev<I>(&mut self, items: I) where
    I: IntoIterator<Item = T>, 
[src]

Inserts all items from an iterator into the underlying Vec at the current index in reverse.

impl<'a, T: Clone> Inserter<'a, T>[src]

pub fn insert_slice_clone(&mut self, items: &[T])[src]

Inserts items at the current index by cloning them from a slice

impl<'a, T: Copy> Inserter<'a, T>[src]

pub fn insert_slice_copy(&mut self, items: &[T])[src]

Inserts items at the current index by copying them from a slice

Trait Implementations

impl<'a, T: Debug> Debug for Inserter<'a, T>[src]

impl<'a, T> Drop for Inserter<'a, T>[src]

Auto Trait Implementations

impl<'a, T> Send for Inserter<'a, T> where
    T: Send
[src]

impl<'a, T> Sync for Inserter<'a, T> where
    T: Sync
[src]

impl<'a, T> Unpin for Inserter<'a, T>[src]

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.