Remover

Struct Remover 

Source
pub struct Remover<'a, T> { /* private fields */ }
Expand description

An object that can remove multiple elements from a Vec in a single scan through the Vec.

The Remover has a position in the Vec at which it inserts, called the index. Initially, the index is at the start of the Vec. This index can only be moved further towards end of the Vec, meaning that the indices of the removed objects must be monotonically increasing (i.e. the next index must be larger than the previous index).

Dropping the Remover 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 Remover (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::Remover;

let mut items = vec!['a', 'b', 'c'];

let mut remover = Remover::new(&mut items);

assert_eq!(remover.index(), 0); // Initial index is at start of the vec
assert_eq!(remover.remove(), 'a');

assert_eq!(remover.index(), 1);  // Removing an element increments the index
remover.move_to(2);

assert_eq!(remover.index(), 2);
assert_eq!(remover.remove(), 'c');

assert_eq!(remover.index(), 3);

drop(remover);

assert_eq!(items, ['b']);

As calling Remover::remove will increment the index, it can be used to remove items in a row:

use scanmut::Remover;

let mut items = vec!['a', 'b', 'c', 'd'];

let mut remover = Remover::new(&mut items);

remover.move_to(1);
assert_eq!(remover.remove(), 'b');
assert_eq!(remover.remove(), 'c');
drop(remover);

assert_eq!(items, ['a', 'd']);

Implementations§

Source§

impl<'a, T> Remover<'a, T>

Source

pub fn new(vec: &'a mut Vec<T>) -> Remover<'a, T>

Create a new new Remover for removing elements from a Vec.

Source

pub fn index(&self) -> usize

The current index of this remover.

Source

pub fn current(&self) -> Option<&T>

Returns a reference to the item at the current index or None if the remover is past the end of the underlying Vec.

Source

pub fn current_mut(&mut self) -> Option<&mut T>

Returns a mutable reference to the item at the current index or None if the remover is past the end of the underlying Vec.

Source

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

Returns a pair of slices representing the current state of the Vec being removed from.

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 removed from.

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 move_to(&mut self, index: usize)

Moves this Remover to the given index in the Vec.

§Panics

Panics if the index is less than or equal to the current index.

Source

pub fn remove(&mut self) -> T

Removes an element from the Vec at the current index.

This also increments the index to the next position.

§Panics

Panics if the index is at the end of the vec.

Trait Implementations§

Source§

impl<'a, T: Debug> Debug for Remover<'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 Remover<'a, T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<'a, T> !UnwindSafe for Remover<'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.