diff options
| author | Mica White <botahamec@outlook.com> | 2026-08-16 12:43:05 -0400 |
|---|---|---|
| committer | Mica White <botahamec@outlook.com> | 2026-08-16 12:43:05 -0400 |
| commit | 6f6e030ea7edb9d155ebf21b5d42936c20801b50 (patch) | |
| tree | f201f6536e3cb21786dc037570fba3f3ed2ff0e7 /src/iterator/iterator.rs | |
| parent | 482b47f4ed786946acb324b60d9f7ae7dd8cc075 (diff) | |
Implement LockContext
Diffstat (limited to 'src/iterator/iterator.rs')
| -rw-r--r-- | src/iterator/iterator.rs | 81 |
1 files changed, 45 insertions, 36 deletions
diff --git a/src/iterator/iterator.rs b/src/iterator/iterator.rs index 1e389a4..316a276 100644 --- a/src/iterator/iterator.rs +++ b/src/iterator/iterator.rs @@ -1,36 +1,32 @@ use std::iter::{Enumerate, Fuse, Skip, Take}; -use super::{ConsumedIteratorGuard, IteratorGuard, LockingIterator}; +use super::{IteratorGuard, LockingIterator}; use crate::{ lockable::{Lockable, RawLock, Sharable}, - Keyable, + ThreadKey, }; -impl<L, Key> LockingIterator<L, Key> { - pub(crate) const fn new(key: Key, lockable: L) -> Self { - Self { key, lockable } - } - - fn with_iterator<M>(self, f: impl FnOnce(L) -> M) -> LockingIterator<M, Key> { +impl<'l, I> LockingIterator<'l, I> { + fn with_iterator<M>(self, f: impl FnOnce(I) -> M) -> LockingIterator<'l, M> { LockingIterator { key: self.key, - lockable: f(self.lockable), + iterator: f(self.iterator), } } } -impl<'a, I: Iterator<Item = &'a L>, L: 'a + Lockable + RawLock, Key: Keyable> - LockingIterator<I, Key> -{ - pub fn lock_next(&'a mut self) -> Option<IteratorGuard<'a, <L as Lockable>::Guard<'a>, Key>> { - if let Some(lock) = self.lockable.next() { +impl<'c, L: 'c + Iterator<Item = &'c I>, I: 'c + RawLock + Lockable> LockingIterator<'c, L> { + pub fn lock_next( + &mut self, + ) -> Option<IteratorGuard<'c, <I as Lockable>::Guard<'c>, ThreadKey>> { + if let Some(lock) = self.iterator.next() { unsafe { lock.raw_write(); let guard = lock.guard(); Some(IteratorGuard { - _key: &self.key, + _key: self.key, guard, }) } @@ -39,32 +35,30 @@ impl<'a, I: Iterator<Item = &'a L>, L: 'a + Lockable + RawLock, Key: Keyable> } } - pub fn lock_last(self) -> Option<ConsumedIteratorGuard<<L as Lockable>::Guard<'a>, Key>> { - self.lockable.last().map(|lock| unsafe { + pub fn lock_last(self) -> Option<IteratorGuard<'c, <I as Lockable>::Guard<'c>, ThreadKey>> { + self.iterator.last().map(|lock| unsafe { lock.raw_write(); let guard = lock.guard(); - ConsumedIteratorGuard { - key: self.key, + IteratorGuard { + _key: self.key, guard, } }) } } -impl<'a, I: Iterator<Item = &'a L>, L: 'a + Sharable + RawLock, Key: Keyable> - LockingIterator<I, Key> -{ +impl<'c, L: 'c + Iterator<Item = &'c I>, I: 'c + RawLock + Sharable> LockingIterator<'c, L> { pub fn read_next( - &'a mut self, - ) -> Option<IteratorGuard<'a, <L as Sharable>::ReadGuard<'a>, Key>> { - if let Some(lock) = self.lockable.next() { + &mut self, + ) -> Option<IteratorGuard<'c, <I as Sharable>::ReadGuard<'c>, ThreadKey>> { + if let Some(lock) = self.iterator.next() { unsafe { lock.raw_read(); let guard = lock.read_guard(); Some(IteratorGuard { - _key: &self.key, + _key: self.key, guard, }) } @@ -73,37 +67,52 @@ impl<'a, I: Iterator<Item = &'a L>, L: 'a + Sharable + RawLock, Key: Keyable> } } - pub fn read_last(self) -> Option<ConsumedIteratorGuard<<L as Sharable>::ReadGuard<'a>, Key>> { - self.lockable.last().map(|lock| unsafe { + pub fn read_last(self) -> Option<IteratorGuard<'c, <I as Sharable>::ReadGuard<'c>, ThreadKey>> { + self.iterator.last().map(|lock| unsafe { lock.raw_read(); let guard = lock.read_guard(); - ConsumedIteratorGuard { - key: self.key, + IteratorGuard { + _key: self.key, guard, } }) } } -impl<L: Iterator, Key> LockingIterator<L, Key> { +impl<'l, L: Iterator> LockingIterator<'l, L> { + pub fn skip_next(&mut self) -> Option<L::Item> { + self.iterator.next() + } + + pub fn skip_mut(&mut self, n: usize) { + for _ in 0..n { + self.iterator.next(); + } + } + + #[must_use] pub fn size_hint(&self) -> (usize, Option<usize>) { - self.lockable.size_hint() + self.iterator.size_hint() } - pub fn enumerate(self) -> LockingIterator<Enumerate<L>, Key> { + #[must_use] + pub fn enumerate(self) -> LockingIterator<'l, Enumerate<L>> { self.with_iterator(Iterator::enumerate) } - pub fn skip(self, n: usize) -> LockingIterator<Skip<L>, Key> { + #[must_use] + pub fn skip(self, n: usize) -> LockingIterator<'l, Skip<L>> { self.with_iterator(|i| i.skip(n)) } - pub fn take(self, n: usize) -> LockingIterator<Take<L>, Key> { + #[must_use] + pub fn take(self, n: usize) -> LockingIterator<'l, Take<L>> { self.with_iterator(|i| i.take(n)) } - pub fn fuse(self) -> LockingIterator<Fuse<L>, Key> { + #[must_use] + pub fn fuse(self) -> LockingIterator<'l, Fuse<L>> { self.with_iterator(Iterator::fuse) } } |
