Trait Indexed

Source
pub trait Indexed<T>
where Self: Len + Deref<Target = [T]>,
{ // Required methods fn insert(&mut self, ix: usize, t: T); fn remove(&mut self, ix: usize) -> Option<T>; // Provided methods fn push(&mut self, t: T) { ... } fn append(&mut self, t: T) { ... } fn drop_front(&mut self, ct: usize) { ... } fn drop_back(&mut self, ct: usize) { ... } fn drop_while<F>(&mut self, f: F) where F: for<'a> Fn(&'a T) -> bool { ... } }
Expand description

Operations on ordered indexed collections

Required Methods§

Source

fn insert(&mut self, ix: usize, t: T)

Insert a new element at ix, shifting all other elements to the right.

use toad_array::Indexed;

fn do_stuff<I: Indexed<u32> + AsRef<Vec<u32>>>(mut i: I) {
  i.insert(0, 2);
  assert_eq!(i.as_ref(), &vec![2]);

  i.insert(0, 1);
  assert_eq!(i.as_ref(), &vec![1, 2]);

  i.insert(2, 3);
  assert_eq!(i.as_ref(), &vec![1, 2, 3]);
}

do_stuff(vec![]);
Source

fn remove(&mut self, ix: usize) -> Option<T>

Remove element at ix, shifting all other elements to the left.

use toad_array::Indexed;

fn do_stuff<I: Indexed<u32> + AsRef<Vec<u32>>>(mut i: I) {
  i.remove(1);
  assert_eq!(i.as_ref(), &vec![1]);

  i.remove(0);
  assert_eq!(i.as_ref(), &vec![]);

  i.remove(0);
  assert_eq!(i.as_ref(), &vec![]);
}

do_stuff(vec![1, 2]);

Provided Methods§

Source

fn push(&mut self, t: T)

Insert an element at the front of the collection

use toad_array::Indexed;

fn do_stuff<I: Indexed<u32> + AsRef<Vec<u32>>>(mut i: I) {
  i.push(3);
  assert_eq!(i.as_ref(), &vec![3]);

  i.push(2);
  assert_eq!(i.as_ref(), &vec![2, 3]);

  i.push(1);
  assert_eq!(i.as_ref(), &vec![1, 2, 3]);
}

do_stuff(vec![]);
Source

fn append(&mut self, t: T)

Insert an element at the end of the collection

use toad_array::Indexed;

fn do_stuff<I: Indexed<u32> + AsRef<Vec<u32>>>(mut i: I) {
  i.append(3);
  assert_eq!(i.as_ref(), &vec![3]);

  i.append(2);
  assert_eq!(i.as_ref(), &vec![3, 2]);

  i.append(1);
  assert_eq!(i.as_ref(), &vec![3, 2, 1]);
}

do_stuff(vec![]);
Source

fn drop_front(&mut self, ct: usize)

Drop ct elements from the front of the collection

use toad_array::Indexed;

let mut v: Vec<u32> = vec![1, 2, 3, 4];

v.drop_front(2);
assert_eq!(v, vec![3, 4]);

v.drop_front(3);
assert_eq!(v, vec![]);

v.drop_front(1);
assert_eq!(v, vec![]);
Source

fn drop_back(&mut self, ct: usize)

Drop ct elements from the back of the collection

use toad_array::Indexed;

let mut v: Vec<u32> = vec![1, 2, 3, 4];

v.drop_back(2);
assert_eq!(v, vec![1, 2]);

v.drop_back(2);
assert_eq!(v, vec![]);

v.drop_back(1);
assert_eq!(v, vec![]);
Source

fn drop_while<F>(&mut self, f: F)
where F: for<'a> Fn(&'a T) -> bool,

Drop elements from the front of the collection until the collection is emptied or the predicate returns false.

use toad_array::Indexed;

let mut v: Vec<u32> = vec![2, 4, 6, 5];

v.drop_while(|n| n % 2 == 0);
assert_eq!(v, vec![5]);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<A> Indexed<<A as Array>::Item> for ArrayVec<A>
where Self: Filled<A::Item> + Trunc, A: Array,

Source§

fn insert(&mut self, ix: usize, t: A::Item)

Source§

fn remove(&mut self, ix: usize) -> Option<A::Item>

Source§

impl<T> Indexed<T> for Vec<T>

Source§

fn insert(&mut self, index: usize, value: T)

Source§

fn remove(&mut self, index: usize) -> Option<T>

Implementors§