Idea: Wrap &[&(mut) [T]] to allow access like &(mut) [T]

It seems common to have no-continuous bytes (or other things) that need to be read or written continuously one by one, and copying them into new continuously memory requires additional allocation and copy overhead.

isn't Iterator::flat_map() already does that? just a quick note, to get a &mut T, you must use &mut &mut T, not & &mut T, so your example should use &mut [&mut [T] if you want to an iterator for &mut T.

fn for_each_nested<'a, T: 'a>(seq: &mut [&mut [T]], mut f: impl FnMut(&mut T)) {
	for x in seq.iter_mut().flat_map(|slice| slice.iter_mut()) {
		f(x)
	}
}

fn main() {
	let mut a1 = [0, 1, 2];
	let mut a2 = [3, 4];
	fn inc(x: &mut isize) {
		*x += 1;
	}
	for_each_nested(&mut [&mut a1[..], &mut a2[..]], inc);
}
1 Like