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§
Required Associated Constants§
sourceconst SUPPORTS_SLICE_OPS: bool
const SUPPORTS_SLICE_OPS: bool
If this is false, Indexed::idx and Indexed::idx_mut will panic!
Required Methods§
sourcefn get<'a>(&'a self, ix: usize) -> Option<Self::Ref<'a>>
fn get<'a>(&'a self, ix: usize) -> Option<Self::Ref<'a>>
Get an immutable reference to element at ix
sourcefn get_mut<'a>(&'a mut self, ix: usize) -> Option<Self::RefMut<'a>>
fn get_mut<'a>(&'a mut self, ix: usize) -> Option<Self::RefMut<'a>>
Get a mutable reference to element at ix
sourcefn insert(&mut self, ix: usize, t: T)
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![]);sourcefn remove(&mut self, ix: usize) -> Option<T>
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]);sourcefn idx<I>(&self, idx: I) -> &I::Outputwhere
I: SliceIndex<[T]>,
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);sourcefn idx_mut<I>(&mut self, idx: I) -> &mut I::Outputwhere
I: SliceIndex<[T]>,
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§
sourcefn iter<'a>(&'a self) -> IndexedIter<'a, Self, T> ⓘwhere
Self: Sized,
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);sourcefn iter_mut<'a>(&'a mut self) -> IndexedIterMut<'a, Self, T> ⓘwhere
Self: Sized,
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);sourcefn push(&mut self, t: T)
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![]);sourcefn append(&mut self, t: T)
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![]);sourcefn drop_front(&mut self, ct: usize)
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![]);sourcefn drop_back(&mut self, ct: usize)
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![]);sourcefn drop_while<F>(&mut self, f: F)where
F: for<'a> Fn(&'a T) -> bool,
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]);