From 6f6e030ea7edb9d155ebf21b5d42936c20801b50 Mon Sep 17 00:00:00 2001 From: Mica White Date: Sun, 16 Aug 2026 12:43:05 -0400 Subject: Implement LockContext --- src/iterator/iterator.rs | 81 +++++++++++++++++++++++++++--------------------- 1 file changed, 45 insertions(+), 36 deletions(-) (limited to 'src/iterator/iterator.rs') 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 LockingIterator { - pub(crate) const fn new(key: Key, lockable: L) -> Self { - Self { key, lockable } - } - - fn with_iterator(self, f: impl FnOnce(L) -> M) -> LockingIterator { +impl<'l, I> LockingIterator<'l, I> { + fn with_iterator(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, L: 'a + Lockable + RawLock, Key: Keyable> - LockingIterator -{ - pub fn lock_next(&'a mut self) -> Option::Guard<'a>, Key>> { - if let Some(lock) = self.lockable.next() { +impl<'c, L: 'c + Iterator, I: 'c + RawLock + Lockable> LockingIterator<'c, L> { + pub fn lock_next( + &mut self, + ) -> Option::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, L: 'a + Lockable + RawLock, Key: Keyable> } } - pub fn lock_last(self) -> Option::Guard<'a>, Key>> { - self.lockable.last().map(|lock| unsafe { + pub fn lock_last(self) -> Option::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, L: 'a + Sharable + RawLock, Key: Keyable> - LockingIterator -{ +impl<'c, L: 'c + Iterator, I: 'c + RawLock + Sharable> LockingIterator<'c, L> { pub fn read_next( - &'a mut self, - ) -> Option::ReadGuard<'a>, Key>> { - if let Some(lock) = self.lockable.next() { + &mut self, + ) -> Option::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, L: 'a + Sharable + RawLock, Key: Keyable> } } - pub fn read_last(self) -> Option::ReadGuard<'a>, Key>> { - self.lockable.last().map(|lock| unsafe { + pub fn read_last(self) -> Option::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 LockingIterator { +impl<'l, L: Iterator> LockingIterator<'l, L> { + pub fn skip_next(&mut self) -> Option { + 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) { - self.lockable.size_hint() + self.iterator.size_hint() } - pub fn enumerate(self) -> LockingIterator, Key> { + #[must_use] + pub fn enumerate(self) -> LockingIterator<'l, Enumerate> { self.with_iterator(Iterator::enumerate) } - pub fn skip(self, n: usize) -> LockingIterator, Key> { + #[must_use] + pub fn skip(self, n: usize) -> LockingIterator<'l, Skip> { self.with_iterator(|i| i.skip(n)) } - pub fn take(self, n: usize) -> LockingIterator, Key> { + #[must_use] + pub fn take(self, n: usize) -> LockingIterator<'l, Take> { self.with_iterator(|i| i.take(n)) } - pub fn fuse(self) -> LockingIterator, Key> { + #[must_use] + pub fn fuse(self) -> LockingIterator<'l, Fuse> { self.with_iterator(Iterator::fuse) } } -- cgit v1.3.1