Trait toad_array::Indexed

source ·
pub trait Indexed<T>where
    Self: Len,{
    type Ref<'a>: Deref<Target = T>
       where Self: 'a,
             T: 'a;
    type RefMut<'a>: Deref<Target = T> + DerefMut
       where Self: 'a,
             T: 'a;

    const SUPPORTS_SLICE_OPS: bool;
Show 13 methods // Required methods fn get<'a>(&'a self, ix: usize) -> Option<Self::Ref<'a>>; fn get_mut<'a>(&'a mut self, ix: usize) -> Option<Self::RefMut<'a>>; fn insert(&mut self, ix: usize, t: T); fn remove(&mut self, ix: usize) -> Option<T>; fn idx<I>(&self, idx: I) -> &I::Output where I: SliceIndex<[T]>; fn idx_mut<I>(&mut self, idx: I) -> &mut I::Output where I: SliceIndex<[T]>; // Provided methods fn iter<'a>(&'a self) -> IndexedIter<'a, Self, T> where Self: Sized { ... } fn iter_mut<'a>(&'a mut self) -> IndexedIterMut<'a, Self, T> where Self: Sized { ... } 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 Associated Types§

source

type Ref<'a>: Deref<Target = T> where Self: 'a, T: 'a

A reference to an element T in the collection

source

type RefMut<'a>: Deref<Target = T> + DerefMut where Self: 'a, T: 'a

A mutable reference to an element T in the collection

Required Associated Constants§

source

const SUPPORTS_SLICE_OPS: bool

If this is false, Indexed::idx and Indexed::idx_mut will panic!

Required Methods§

source

fn get<'a>(&'a self, ix: usize) -> Option<Self::Ref<'a>>

Get an immutable reference to element at ix

source

fn get_mut<'a>(&'a mut self, ix: usize) -> Option<Self::RefMut<'a>>

Get a mutable reference to element at ix

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]);
source

fn idx<I>(&self, idx: I) -> &I::Outputwhere I: SliceIndex<[T]>,

Get one or more elements using any SliceIndex.

Note that not all Indexed collections support contiguous slice operations, for collections that have Indexed::SUPPORTS_SLICE_OPS = false, this method will panic.

use tinyvec::{array_vec, ArrayVec};
use toad_array::Indexed;

let vec: Vec<usize> = vec![1, 2, 3, 4];
let avec: ArrayVec<[usize; 16]> = array_vec![1, 2, 3, 4];

fn foo<I: Indexed<usize>>(i: &I) {
  assert_eq!(i.idx(1..), &[2, 3, 4])
}

foo(&vec);
foo(&avec);
source

fn idx_mut<I>(&mut self, idx: I) -> &mut I::Outputwhere I: SliceIndex<[T]>,

Mutably get one or more elements using any SliceIndex.

Note that not all Indexed collections support contiguous slice operations, for collections that have Indexed::SUPPORTS_SLICE_OPS = false, this method will panic.

use tinyvec::{array_vec, ArrayVec};
use toad_array::Indexed;

let mut vec: Vec<usize> = vec![1, 2, 3, 4];
let mut avec: ArrayVec<[usize; 16]> = array_vec![1, 2, 3, 4];

fn foo<I: Indexed<usize>>(i: &mut I) {
  *i.idx_mut(1) = 10;
  assert_eq!(i.idx(..), &[1, 10, 3, 4])
}

foo(&mut vec);
foo(&mut avec);

Provided Methods§

source

fn iter<'a>(&'a self) -> IndexedIter<'a, Self, T> where Self: Sized,

Iterate over the elements in the collection

use core::ops::Deref;

use tinyvec::{array_vec, ArrayVec};
use toad_array::Indexed;

let v: Vec<usize> = vec![1, 2, 3, 4];
let av: ArrayVec<[usize; 16]> = array_vec![1, 2, 3, 4];

fn foo<I>(i: &I)
  where I: Indexed<usize>
{
  assert_eq!(i.iter().map(|n| *n.deref()).sum::<usize>(), 10)
}

foo(&v);
foo(&av);
source

fn iter_mut<'a>(&'a mut self) -> IndexedIterMut<'a, Self, T> where Self: Sized,

Iterate mutably over the elements in the collection

use core::ops::{Deref, DerefMut};

use tinyvec::{array_vec, ArrayVec};
use toad_array::Indexed;

let mut v: Vec<usize> = vec![1, 2, 3, 4];
let mut av: ArrayVec<[usize; 16]> = array_vec![1, 2, 3, 4];

fn foo<I>(i: &mut I)
  where I: Indexed<usize>
{
  i.iter_mut().for_each(|mut n| *n.deref_mut() = 0);
  assert!(i.iter()
           .map(|n| *n.deref())
           .eq(vec![0usize, 0, 0, 0].into_iter()));
}

foo(&mut v);
foo(&mut av);
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]);

Implementations on Foreign Types§

source§

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

source§

const SUPPORTS_SLICE_OPS: bool = true

§

type Ref<'a> where A::Item: 'a, Self: 'a = &'a <A as Array>::Item

§

type RefMut<'a> where A::Item: 'a, Self: 'a = &'a mut <A as Array>::Item

source§

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

source§

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

source§

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

source§

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

source§

fn idx<I>(&self, idx: I) -> &I::Outputwhere I: SliceIndex<[A::Item]>,

source§

fn idx_mut<I>(&mut self, idx: I) -> &mut I::Outputwhere I: SliceIndex<[A::Item]>,

source§

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

source§

const SUPPORTS_SLICE_OPS: bool = true

§

type Ref<'a> where T: 'a = &'a T

§

type RefMut<'a> where T: 'a = &'a mut T

source§

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

source§

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

source§

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

source§

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

source§

fn idx<I>(&self, idx: I) -> &I::Outputwhere I: SliceIndex<[T]>,

source§

fn idx_mut<I>(&mut self, idx: I) -> &mut I::Outputwhere I: SliceIndex<[T]>,

Implementors§