Inserter

Struct Inserter 

Source
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>

Source

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.

Source

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.

Source

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.

Source

pub fn remaining_inserts(&self) -> usize

Returns the remaining number of allowed inserts in this Inserter.

Source

pub fn index(&self) -> usize

Returns the current index of the inserter.

Source

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.

Source

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.

Source

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

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

Source

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.

Source§

impl<'a, T: Clone> Inserter<'a, T>

Source

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

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

Source§

impl<'a, T: Copy> Inserter<'a, T>

Source

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

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

Trait Implementations§

Source§

impl<'a, T: Debug> Debug for Inserter<'a, T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a, T> Drop for Inserter<'a, T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'a, T> Freeze for Inserter<'a, T>

§

impl<'a, T> RefUnwindSafe for Inserter<'a, T>
where T: RefUnwindSafe,

§

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

§

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

§

impl<'a, T> Unpin for Inserter<'a, T>

§

impl<'a, T> !UnwindSafe for Inserter<'a, T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.