I'm currently trying to get a flatten iterator out of a const size array of locked Vecs, to my surprise I was able to do so but with a catch.
#[derive(Clone, Debug)]
pub(crate) struct LockVec<T, L: RawRwLock> {
data: Arc<RwLock<L, Vec<T>>>,
}
pub(crate) struct RawStorage<T, L: RawRwLock, const N: usize> {
data: [LockVec<T, L>; N],
len: AtomicUsize,
}
impl<T: std::fmt::Debug + Default, L: RawRwLock, const N: usize> RawStorage<T, L, N> {
pub(crate) fn items<'a>(&'a self) -> IterableGuards<'a, T, L> {
let guards = self.data.iter().map(|vec| vec.data.read()).collect();
IterableGuards { guards }
}
}
// To my surprise this works but I have to create a Vec of guards
pub(crate) struct IterableGuards<'a, T: 'static, L: RawRwLock> {
guards: Vec<lock_api::RwLockReadGuard<'a, L, Vec<T>>>,
}
impl<'a, T: 'static, L: RawRwLock> HasIter<'a, T> for IterableGuards<'a, T, L> {
fn iter(&'a self) -> Box<dyn Iterator<Item = &'a T> + 'a> {
let iter = self.guards.iter().flat_map(|guard| guard.iter());
Box::new(iter)
}
}
code in detail here
Is there any way to rewrite IterableGuards to take a slice or an iterator for me to avoid having to get all the locks in a vec?