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/context.rs | |
| parent | 482b47f4ed786946acb324b60d9f7ae7dd8cc075 (diff) | |
Implement LockContext
Diffstat (limited to 'src/iterator/context.rs')
| -rw-r--r-- | src/iterator/context.rs | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/iterator/context.rs b/src/iterator/context.rs new file mode 100644 index 0000000..a5c9123 --- /dev/null +++ b/src/iterator/context.rs @@ -0,0 +1,52 @@ +use std::marker::PhantomData; + +use crate::{ + iterator::{LockContext, LockingIterator, LockingTuple}, + lockable::Lockable, + ThreadKey, +}; + +impl<'l, L> LockContext<'l, L> { + pub(crate) const fn new(lockable: &'l L) -> Self { + Self { + key: None, + lockable, + } + } +} + +impl<L: Lockable> LockContext<'_, L> { + pub fn tuple(&mut self, key: ThreadKey) -> LockingTuple<'_, L, L> { + unsafe { + self.key = Some(key); + + LockingTuple { + _lockable: PhantomData, + // safety: we just inserted a key + key: self.key.as_ref().unwrap_unchecked(), + tuple: self.lockable, + } + } + } +} + +impl<'l, L> LockContext<'l, L> +where + &'l L: IntoIterator, +{ + #[expect(clippy::iter_not_returning_iterator)] + pub fn iter( + &mut self, + key: ThreadKey, + ) -> LockingIterator<'_, <&'l L as IntoIterator>::IntoIter> { + unsafe { + self.key = Some(key); + + LockingIterator { + // safety: we just inserted a key + key: self.key.as_ref().unwrap_unchecked(), + iterator: self.lockable.into_iter(), + } + } + } +} |
