[−][src]Function smart_access::detached_at
pub fn detached_at<View: ?Sized, I>(i: I) -> AT<DetachedRoot<View>, I>
Creates a detached path. Requires detach
feature.
A type of the return value of detached_at::<V>
implements Attach<CPS: Cps<View=V>, View=V>
.
Present only on detach
.
Usage example
A simple case when detached paths could be helpful: creating a detached path and cloning it several times.
use smart_access::Cps; let reference_path = smart_access::detached_at(()).at(()).at(()); let mut items = vec![ Some(Some(Ok(1))), Some(None), Some(Some(Err(2))) ]; let sum = items.iter_mut().map(|wrapped| { wrapped.attach(reference_path.clone()) .access(|x| *x) .into_iter() .sum::<i32>() }).sum::<i32>(); assert!(sum == 1);
A more convoluted example: a functional index combinator.
use smart_access::{Cps, Attach}; type Mat = Vec<Vec<f64>>; fn mat_index<'a>(i: usize, j: usize) -> impl Attach<&'a mut Mat, View=f64> { smart_access::detached_at(i).at(j) } let mut mat = vec![ vec![1., 2.], vec![3., 4.] ]; assert!(mat.attach(mat_index(1,1)).replace(0.) == Some(4.));
But note that a more idiomatic approach would be
use smart_access::{Cps, At}; struct Mat { numbers: Vec<Vec<f64>> }; impl At<(usize, usize)> for Mat { type View = f64; fn access_at<R,F>(&mut self, ij: (usize, usize), f: F) -> Option<R> where F: FnOnce(&mut f64) -> R { let (i, j) = ij; self.numbers.at(i).at(j).access(f) } } let mut mat = Mat { numbers: vec![ vec![1., 2.], vec![3., 4.] ]}; assert!(mat.at( (1,1) ).replace(0.) == Some(4.));