From 219261b7042fba1a54ecd478b56e902d9ca8787b Mon Sep 17 00:00:00 2001 From: Charlotte Pabst Date: Thu, 7 Mar 2024 21:52:30 +0100 Subject: --- src/entity_iterator.rs | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/entity_iterator.rs (limited to 'src/entity_iterator.rs') diff --git a/src/entity_iterator.rs b/src/entity_iterator.rs new file mode 100644 index 0000000..58cd2a9 --- /dev/null +++ b/src/entity_iterator.rs @@ -0,0 +1,59 @@ +use crate::*; + +pub struct EntityIterator<'tok, 'brand, 'arena, T>(Option<(lens_t!(T), lens_t!(T))>); + +impl<'tok, 'brand, 'arena, T> Clone for EntityIterator<'tok, 'brand, 'arena, T> { + fn clone(&self) -> Self { + Self(self.0) + } +} + +impl<'tok, 'brand, 'arena, T> Copy for EntityIterator<'tok, 'brand, 'arena, T> {} + +impl<'tok, 'brand, 'arena, T: Entity<'brand, 'arena>> EntityIterator<'tok, 'brand, 'arena, T> { + pub(crate) fn new( + start: Option, + token: &'tok impl ReflAsRef>, + ) -> Self { + Self(start.map(|s| { + let l = Lens::new(s, token); + (l, l.prev()) + })) + } +} + +impl<'tok, 'brand, 'arena, T: Entity<'brand, 'arena>> Iterator + for EntityIterator<'tok, 'brand, 'arena, T> +{ + type Item = lens_t!(T); + + fn next(&mut self) -> Option { + let range = self.0.as_mut()?; + let ret = range.0; + + if range.0 == range.1 { + self.0 = None; + } else { + range.0 = range.0.next(); + } + + Some(ret) + } +} + +impl<'tok, 'brand, 'arena, T: Entity<'brand, 'arena>> DoubleEndedIterator + for EntityIterator<'tok, 'brand, 'arena, T> +{ + fn next_back(&mut self) -> Option { + let range = self.0.as_mut()?; + let ret = range.1; + + if range.0 == range.1 { + self.0 = None; + } else { + range.1 = range.1.prev(); + } + + Some(ret) + } +} -- cgit v1.2.3