From 2f37030588fb4db77ca37a7ccb0155ec18e2f4bb Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Fri, 22 Mar 2024 18:32:43 +0100 Subject: [PATCH] add iter() for ListView --- src/view/list/mod.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/view/list/mod.rs b/src/view/list/mod.rs index c3fa0f2..f4ea200 100644 --- a/src/view/list/mod.rs +++ b/src/view/list/mod.rs @@ -21,6 +21,46 @@ where Item: Clone + Send + Sync + 'static //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> +pub trait ListViewExt: ListView +where T: Clone + Send + Sync + 'static +{ + fn iter<'a>(&'a self) -> ListViewIter<'a, T, Self> { + ListViewIter { _phantom: std::marker::PhantomData, view: self, cur: 0 } + } +} + +impl + ?Sized> ListViewExt for V +where T: Clone + Send + Sync + 'static +{} + +//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> + +pub struct ListViewIter<'a, T, V> +where + T: Clone + Send + Sync + 'static, + V: ListView + ?Sized, +{ + _phantom: std::marker::PhantomData, + view: &'a V, + cur: usize, +} + +impl<'a, T, V> Iterator for ListViewIter<'a, T, V> +where + T: Clone + Send + Sync + 'static, + V: ListView + ?Sized, +{ + type Item = T; + + fn next(&mut self) -> Option { + let i = self.cur; + self.cur += 1; + self.view.get(&i) + } +} + +//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> + use std::sync::RwLock; use std::{ops::Deref, sync::Arc};