summaryrefslogtreecommitdiff
path: root/src/iterator
diff options
context:
space:
mode:
Diffstat (limited to 'src/iterator')
-rw-r--r--src/iterator/context.rs52
-rw-r--r--src/iterator/guard.rs58
-rw-r--r--src/iterator/iterator.rs118
-rw-r--r--src/iterator/tuple.rs202
4 files changed, 0 insertions, 430 deletions
diff --git a/src/iterator/context.rs b/src/iterator/context.rs
deleted file mode 100644
index a5c9123..0000000
--- a/src/iterator/context.rs
+++ /dev/null
@@ -1,52 +0,0 @@
-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(),
- }
- }
- }
-}
diff --git a/src/iterator/guard.rs b/src/iterator/guard.rs
deleted file mode 100644
index cb220ac..0000000
--- a/src/iterator/guard.rs
+++ /dev/null
@@ -1,58 +0,0 @@
-use std::fmt::{Debug, Display};
-use std::hash::Hash;
-use std::ops::{Deref, DerefMut};
-
-use super::IteratorGuard;
-
-#[mutants::skip] // hashing involves RNG and is hard to test
-#[cfg(not(tarpaulin_include))]
-impl<Guard: Hash, Key> Hash for IteratorGuard<'_, Guard, Key> {
- fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
- self.guard.hash(state)
- }
-}
-
-// No implementations of Eq, PartialEq, PartialOrd, or Ord
-// You can't implement both PartialEq<Self> and PartialEq<T>
-// It's easier to just implement neither and ask users to dereference
-// This is less of a problem when using the scoped lock API
-
-#[mutants::skip]
-#[cfg(not(tarpaulin_include))]
-impl<Guard: Debug, Key> Debug for IteratorGuard<'_, Guard, Key> {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- Debug::fmt(&**self, f)
- }
-}
-
-impl<Guard: Display, Key> Display for IteratorGuard<'_, Guard, Key> {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- Display::fmt(&**self, f)
- }
-}
-
-impl<Guard, Key> Deref for IteratorGuard<'_, Guard, Key> {
- type Target = Guard;
-
- fn deref(&self) -> &Self::Target {
- &self.guard
- }
-}
-
-impl<Guard, Key> DerefMut for IteratorGuard<'_, Guard, Key> {
- fn deref_mut(&mut self) -> &mut Self::Target {
- &mut self.guard
- }
-}
-
-impl<Guard, Key> AsRef<Guard> for IteratorGuard<'_, Guard, Key> {
- fn as_ref(&self) -> &Guard {
- &self.guard
- }
-}
-
-impl<Guard, Key> AsMut<Guard> for IteratorGuard<'_, Guard, Key> {
- fn as_mut(&mut self) -> &mut Guard {
- &mut self.guard
- }
-}
diff --git a/src/iterator/iterator.rs b/src/iterator/iterator.rs
deleted file mode 100644
index 316a276..0000000
--- a/src/iterator/iterator.rs
+++ /dev/null
@@ -1,118 +0,0 @@
-use std::iter::{Enumerate, Fuse, Skip, Take};
-
-use super::{IteratorGuard, LockingIterator};
-
-use crate::{
- lockable::{Lockable, RawLock, Sharable},
- ThreadKey,
-};
-
-impl<'l, I> LockingIterator<'l, I> {
- fn with_iterator<M>(self, f: impl FnOnce(I) -> M) -> LockingIterator<'l, M> {
- LockingIterator {
- key: self.key,
- iterator: f(self.iterator),
- }
- }
-}
-
-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,
- guard,
- })
- }
- } else {
- None
- }
- }
-
- 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();
-
- IteratorGuard {
- _key: self.key,
- guard,
- }
- })
- }
-}
-
-impl<'c, L: 'c + Iterator<Item = &'c I>, I: 'c + RawLock + Sharable> LockingIterator<'c, L> {
- pub fn read_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,
- guard,
- })
- }
- } else {
- None
- }
- }
-
- 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();
-
- IteratorGuard {
- _key: self.key,
- guard,
- }
- })
- }
-}
-
-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.iterator.size_hint()
- }
-
- #[must_use]
- pub fn enumerate(self) -> LockingIterator<'l, Enumerate<L>> {
- self.with_iterator(Iterator::enumerate)
- }
-
- #[must_use]
- pub fn skip(self, n: usize) -> LockingIterator<'l, Skip<L>> {
- self.with_iterator(|i| i.skip(n))
- }
-
- #[must_use]
- pub fn take(self, n: usize) -> LockingIterator<'l, Take<L>> {
- self.with_iterator(|i| i.take(n))
- }
-
- #[must_use]
- pub fn fuse(self) -> LockingIterator<'l, Fuse<L>> {
- self.with_iterator(Iterator::fuse)
- }
-}
diff --git a/src/iterator/tuple.rs b/src/iterator/tuple.rs
deleted file mode 100644
index 855cdc7..0000000
--- a/src/iterator/tuple.rs
+++ /dev/null
@@ -1,202 +0,0 @@
-use std::marker::PhantomData;
-
-use crate::{
- iterator::{IteratorGuard, LockingTuple},
- lockable::{Lockable, RawLock},
- ThreadKey,
-};
-
-impl<'c, A, B> LockingTuple<'c, A, B> {
- const fn transmute<C>(self) -> LockingTuple<'c, C, B> {
- LockingTuple {
- _lockable: PhantomData,
- key: self.key,
- tuple: self.tuple,
- }
- }
-}
-
-macro_rules! lock_impl {
- ($self: expr, $field: tt) => {
- unsafe {
- $self.tuple.$field.raw_write();
- (
- IteratorGuard {
- _key: &$self.key,
- guard: $self.tuple.$field.guard(),
- },
- $self.transmute(),
- )
- }
- };
-}
-
-macro_rules! recurse_impl {
- ($self: expr, $field: tt) => {
- LockingTuple {
- _lockable: PhantomData,
- key: $self.key,
- tuple: &$self.tuple.$field,
- }
- };
-}
-
-type LockReturn<'a, 'context, Guarded, L, C> = (
- IteratorGuard<'a, <Guarded as Lockable>::Guard<'a>, ThreadKey>,
- LockingTuple<'context, L, C>,
-);
-
-impl<'context, A: RawLock + Lockable> LockingTuple<'context, (A,), (A,)> {
- #[must_use]
- pub fn lock_0<'a>(self) -> LockReturn<'a, 'context, A, ((),), (A,)>
- where
- 'context: 'a,
- {
- lock_impl!(self, 0)
- }
-}
-
-impl<'context, A> LockingTuple<'context, (A,), (A,)> {
- #[must_use]
- pub const fn recurse_0(self) -> LockingTuple<'context, A, A> {
- recurse_impl!(self, 0)
- }
-}
-
-impl<'context, A: RawLock + Lockable, B, B0> LockingTuple<'context, (A, B), (A, B0)> {
- #[must_use]
- pub fn lock_0<'a>(self) -> LockReturn<'a, 'context, A, ((), B), (A, B0)>
- where
- 'context: 'a,
- {
- lock_impl!(self, 0)
- }
-}
-
-impl<'context, A, B, B0> LockingTuple<'context, (A, B), (A, B0)> {
- #[must_use]
- pub const fn recurse_0(self) -> LockingTuple<'context, A, A> {
- recurse_impl!(self, 0)
- }
-}
-
-impl<'context, A: Lockable + RawLock, B> LockingTuple<'context, (A, B), (A, B)> {
- #[must_use]
- pub fn lock_and_recurse<'a>(
- self,
- ) -> (
- IteratorGuard<'a, <A as Lockable>::Guard<'a>, ThreadKey>,
- LockingTuple<'context, B, B>,
- )
- where
- 'context: 'a,
- {
- unsafe {
- self.tuple.0.raw_write();
- (
- IteratorGuard {
- _key: self.key,
- guard: self.tuple.0.guard(),
- },
- LockingTuple {
- _lockable: PhantomData,
- key: self.key,
- tuple: &self.tuple.1,
- },
- )
- }
- }
-}
-
-impl<'context, A, A0, B: RawLock + Lockable> LockingTuple<'context, (A, B), (A0, B)> {
- #[must_use]
- pub fn lock_1<'a>(self) -> LockReturn<'a, 'context, B, ((), ()), (A0, B)>
- where
- 'context: 'a,
- {
- lock_impl!(self, 1)
- }
-}
-
-impl<'context, A, A0, B> LockingTuple<'context, (A, B), (A0, B)> {
- #[must_use]
- pub const fn recurse_1(self) -> LockingTuple<'context, B, B> {
- recurse_impl!(self, 1)
- }
-}
-
-impl<'context, A: RawLock + Lockable, B, B0, C, C0> LockingTuple<'context, (A, B, C), (A, B0, C0)> {
- #[must_use]
- pub fn lock_0<'a>(self) -> LockReturn<'a, 'context, A, ((), B, C), (A, B0, C0)>
- where
- 'context: 'a,
- {
- lock_impl!(self, 0)
- }
-}
-
-impl<'context, A, B, B0, C, C0> LockingTuple<'context, (A, B, C), (A, B0, C0)> {
- #[must_use]
- pub const fn recurse_0(self) -> LockingTuple<'context, A, A> {
- recurse_impl!(self, 0)
- }
-}
-
-impl<'context, A, A0, B: RawLock + Lockable, C, C0> LockingTuple<'context, (A, B, C), (A0, B, C0)> {
- #[must_use]
- pub fn lock_1<'a>(self) -> LockReturn<'a, 'context, B, ((), (), C), (A0, B, C0)>
- where
- 'context: 'a,
- {
- lock_impl!(self, 1)
- }
-}
-
-impl<'context, A, A0, B, C, C0> LockingTuple<'context, (A, B, C), (A0, B, C0)> {
- #[must_use]
- pub const fn recurse_1(self) -> LockingTuple<'context, B, B> {
- recurse_impl!(self, 1)
- }
-}
-
-impl<'context, A, A0, B, B0, C: RawLock + Lockable> LockingTuple<'context, (A, B, C), (A0, B0, C)> {
- #[must_use]
- pub fn lock_2<'a>(self) -> LockReturn<'a, 'context, C, ((), (), ()), (A0, B0, C)>
- where
- 'context: 'a,
- {
- lock_impl!(self, 2)
- }
-}
-
-impl<'context, A, A0, B, B0, C> LockingTuple<'context, (A, B, C), (A0, B0, C)> {
- #[must_use]
- pub const fn recurse_2(self) -> LockingTuple<'context, C, C> {
- recurse_impl!(self, 2)
- }
-}
-
-impl<'context, A, B, B0, C, C0, D, D0> LockingTuple<'context, (A, B, C, D), (A, B0, C0, D0)> {
- #[must_use]
- pub const fn recurse_0(self) -> LockingTuple<'context, A, A> {
- recurse_impl!(self, 0)
- }
-}
-impl<'context, A, A0, B, C, C0, D, D0> LockingTuple<'context, (A, B, C, D), (A0, B, C0, D0)> {
- #[must_use]
- pub const fn recurse_1(self) -> LockingTuple<'context, B, B> {
- recurse_impl!(self, 1)
- }
-}
-impl<'context, A, A0, B, B0, C, D, D0> LockingTuple<'context, (A, B, C, D), (A0, B0, C, D0)> {
- #[must_use]
- pub const fn recurse_2(self) -> LockingTuple<'context, C, C> {
- recurse_impl!(self, 2)
- }
-}
-impl<'context, A, A0, B, B0, C, C0, D> LockingTuple<'context, (A, B, C, D), (A0, B0, C0, D)> {
- #[must_use]
- pub const fn recurse_3(self) -> LockingTuple<'context, D, D> {
- recurse_impl!(self, 3)
- }
-}