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;
// 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>;
// 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 Associated Types§
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]);Provided Methods§
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]);