From bd1572c632592a34e2b45464fefa00cb94bd9d98 Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Thu, 2 Sep 2021 22:37:09 +0200 Subject: [PATCH] SequenceView: add .iter() --- nested/src/sequence/mod.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/nested/src/sequence/mod.rs b/nested/src/sequence/mod.rs index 42f7f9e..7bc8849 100644 --- a/nested/src/sequence/mod.rs +++ b/nested/src/sequence/mod.rs @@ -19,6 +19,40 @@ pub trait SequenceView : View { //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> +pub trait SequenceViewExt : SequenceView { + fn iter<'a>(&'a self) -> SequenceViewIter<'a, Self> { + SequenceViewIter { + view: self, + cur: 0 + } + } +} + +impl SequenceViewExt for V {} + +//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> + +pub struct SequenceViewIter<'a, V> +where V: SequenceView + ?Sized +{ + view: &'a V, + cur: usize +} + +impl<'a, V> Iterator for SequenceViewIter<'a, V> +where V: SequenceView + ?Sized +{ + type Item = V::Item; + + fn next(&mut self) -> Option { + let i = self.cur; + self.cur += 1; + self.view.get(&i) + } +} + +//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> + use std::{ sync::Arc, ops::{Deref}