Indexing

Trait Indexing 

Source
pub trait Indexing<T, E> {
    // Required methods
    fn get_at<'a>(&self, root: &'a T, idx: usize) -> &'a E;
    fn get_mut_at<'a>(&self, root: &'a mut T, idx: usize) -> &'a mut E;
    fn set_at(&self, root: &mut T, idx: usize, value: E);
    fn set_mut_at(&self, root: &mut T, idx: usize, f: impl FnOnce(&mut E));
    fn set_clone_at(&self, root: &mut T, idx: usize, value: &E)
       where E: Clone;
}
Expand description

Indexing operations for accessors that focus Vec<E>.

Provided as a blanket impl for Accessor<T, Vec<E>>.

Required Methods§

Source

fn get_at<'a>(&self, root: &'a T, idx: usize) -> &'a E

Borrow the element at idx immutably.

use pathmod_core::{Accessor, Indexing};
#[derive(Debug)]
struct Bag { items: Vec<i32> }
fn acc_items() -> Accessor<Bag, Vec<i32>> {
    fn gr(b: &Bag) -> &Vec<i32> { &b.items }
    fn gm(b: &mut Bag) -> &mut Vec<i32> { &mut b.items }
    Accessor::from_fns(gr, gm)
}
let b = Bag { items: vec![1,2,3] };
let acc = acc_items();
assert_eq!(*acc.get_at(&b, 1), 2);
Source

fn get_mut_at<'a>(&self, root: &'a mut T, idx: usize) -> &'a mut E

Borrow the element at idx mutably.

Source

fn set_at(&self, root: &mut T, idx: usize, value: E)

Set the element at idx by moving value in.

Source

fn set_mut_at(&self, root: &mut T, idx: usize, f: impl FnOnce(&mut E))

Mutate the element at idx in-place using the closure.

Source

fn set_clone_at(&self, root: &mut T, idx: usize, value: &E)
where E: Clone,

Set the element at idx by cloning from value.

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.

Implementors§

Source§

impl<T, E> Indexing<T, E> for Accessor<T, Vec<E>>