diff options
| author | Mica White <botahamec@outlook.com> | 2026-09-13 15:12:08 -0400 |
|---|---|---|
| committer | Mica White <botahamec@outlook.com> | 2026-09-13 15:12:08 -0400 |
| commit | 16d6bd7ece4c4b9b72f6bd51c9b034270b745525 (patch) | |
| tree | 4ff744fc41319ce73abe06416fabeaa7c17fbd46 /src/collection | |
| parent | 9fd7b94cd7c20abe66cea875a0e83e76a7a00fbc (diff) | |
Support no_stdno-std
Diffstat (limited to 'src/collection')
| -rwxr-xr-x | src/collection/boxed.rs | 38 | ||||
| -rwxr-xr-x | src/collection/guard.rs | 18 | ||||
| -rwxr-xr-x | src/collection/owned.rs | 28 | ||||
| -rwxr-xr-x | src/collection/ref.rs | 35 | ||||
| -rwxr-xr-x | src/collection/retry.rs | 52 | ||||
| -rwxr-xr-x | src/collection/utils.rs | 6 |
6 files changed, 141 insertions, 36 deletions
diff --git a/src/collection/boxed.rs b/src/collection/boxed.rs index 66e6df0..88f3073 100755 --- a/src/collection/boxed.rs +++ b/src/collection/boxed.rs @@ -1,5 +1,8 @@ -use std::cell::UnsafeCell; -use std::fmt::Debug; +use core::cell::UnsafeCell; +use core::fmt::Debug; + +use alloc::boxed::Box; +use alloc::vec::Vec; use crate::lockable::{Lockable, LockableIntoInner, OwnedLockable, RawLock, Sharable}; use crate::{Keyable, ThreadKey}; @@ -169,7 +172,7 @@ impl<T: ?Sized, L: AsRef<T>> AsRef<T> for BoxedLockCollection<L> { #[mutants::skip] #[cfg(not(tarpaulin_include))] impl<L: Debug> Debug for BoxedLockCollection<L> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { f.debug_struct(stringify!(BoxedLockCollection)) .field("data", &self.child) // there's not much reason to show the sorted locks @@ -207,11 +210,11 @@ impl<L> BoxedLockCollection<L> { pub fn into_child(mut self) -> L { unsafe { // safety: this collection will never be used again - std::ptr::drop_in_place(&raw mut self.locks); + core::ptr::drop_in_place(&raw mut self.locks); // safety: this was allocated using a box, and is now unique let boxed: Box<UnsafeCell<L>> = Box::from_raw(self.child.cast_mut()); // to prevent a double free - std::mem::forget(self); + core::mem::forget(self); boxed.into_inner() } @@ -327,7 +330,7 @@ impl<L: Lockable> BoxedLockCollection<L> { locks.sort_by_key(|lock| (&raw const **lock).cast::<()>() as usize); // safety: we're just changing the lifetimes - let locks: Vec<&'static dyn RawLock> = unsafe { std::mem::transmute(locks) }; + let locks: Vec<&'static dyn RawLock> = unsafe { core::mem::transmute(locks) }; let data = &raw const *data; Self { child: data, locks } } @@ -770,9 +773,12 @@ where #[cfg(test)] mod tests { use super::*; - use crate::{Mutex, RwLock, ThreadKey}; + use crate::Mutex; + #[cfg(feature = "std")] + use crate::{RwLock, ThreadKey}; #[test] + #[cfg(feature = "std")] fn from_iterator() { let key = ThreadKey::get().unwrap(); let collection: BoxedLockCollection<Vec<Mutex<&str>>> = @@ -786,6 +792,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn from() { let key = ThreadKey::get().unwrap(); let collection = @@ -805,6 +812,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn into_ref_iterator() { let mut key = ThreadKey::get().unwrap(); let collection = BoxedLockCollection::new([Mutex::new(0), Mutex::new(1), Mutex::new(2)]); @@ -814,6 +822,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn ref_iterator() { let mut key = ThreadKey::get().unwrap(); let collection = BoxedLockCollection::new([Mutex::new(0), Mutex::new(1), Mutex::new(2)]); @@ -847,6 +856,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn scoped_read_sees_changes() { let mut key = ThreadKey::get().unwrap(); let mutexes = [RwLock::new(24), RwLock::new(42)]; @@ -863,6 +873,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn scoped_try_lock_can_fail() { let key = ThreadKey::get().unwrap(); let collection = BoxedLockCollection::new([Mutex::new(1), Mutex::new(2)]); @@ -880,6 +891,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn scoped_try_read_can_fail() { let key = ThreadKey::get().unwrap(); let collection = BoxedLockCollection::new([RwLock::new(1), RwLock::new(2)]); @@ -897,6 +909,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn try_lock_works() { let key = ThreadKey::get().unwrap(); let collection = BoxedLockCollection::new([Mutex::new(1), Mutex::new(2)]); @@ -914,6 +927,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn try_read_works() { let key = ThreadKey::get().unwrap(); let collection = BoxedLockCollection::new([RwLock::new(1), RwLock::new(2)]); @@ -931,6 +945,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn try_lock_fails_with_one_exclusive_lock() { let key = ThreadKey::get().unwrap(); let locks = [Mutex::new(1), Mutex::new(2)]; @@ -949,6 +964,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn try_read_fails_during_exclusive_lock() { let key = ThreadKey::get().unwrap(); let collection = BoxedLockCollection::new([RwLock::new(1), RwLock::new(2)]); @@ -966,6 +982,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn try_read_fails_with_one_exclusive_lock() { let key = ThreadKey::get().unwrap(); let locks = [RwLock::new(1), RwLock::new(2)]; @@ -984,6 +1001,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn unlock_collection_works() { let key = ThreadKey::get().unwrap(); let mutex1 = Mutex::new("foo"); @@ -996,6 +1014,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn read_unlock_collection_works() { let key = ThreadKey::get().unwrap(); let lock1 = RwLock::new("foo"); @@ -1014,6 +1033,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn works_in_collection() { let key = ThreadKey::get().unwrap(); let mutex1 = RwLock::new(0); @@ -1043,7 +1063,7 @@ mod tests { let mutexes = [Mutex::new(0), Mutex::new(1)]; let collection = BoxedLockCollection::new_ref(&mutexes); - assert!(std::ptr::addr_eq(&raw const mutexes, collection.as_ref())) + assert!(core::ptr::addr_eq(&raw const mutexes, collection.as_ref())) } #[test] @@ -1051,6 +1071,6 @@ mod tests { let mutexes = [Mutex::new(0), Mutex::new(1)]; let collection = BoxedLockCollection::new_ref(&mutexes); - assert!(std::ptr::addr_eq(&raw const mutexes, *collection.child())) + assert!(core::ptr::addr_eq(&raw const mutexes, *collection.child())) } } diff --git a/src/collection/guard.rs b/src/collection/guard.rs index ab66ffe..a9bf66e 100755 --- a/src/collection/guard.rs +++ b/src/collection/guard.rs @@ -1,13 +1,13 @@ -use std::fmt::{Debug, Display}; -use std::hash::Hash; -use std::ops::{Deref, DerefMut}; +use core::fmt::{Debug, Display}; +use core::hash::Hash; +use core::ops::{Deref, DerefMut}; use super::LockGuard; #[mutants::skip] // hashing involves RNG and is hard to test #[cfg(not(tarpaulin_include))] impl<Guard: Hash> Hash for LockGuard<Guard> { - fn hash<H: std::hash::Hasher>(&self, state: &mut H) { + fn hash<H: core::hash::Hasher>(&self, state: &mut H) { self.guard.hash(state) } } @@ -20,13 +20,13 @@ impl<Guard: Hash> Hash for LockGuard<Guard> { #[mutants::skip] #[cfg(not(tarpaulin_include))] impl<Guard: Debug> Debug for LockGuard<Guard> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { Debug::fmt(&**self, f) } } impl<Guard: Display> Display for LockGuard<Guard> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { Display::fmt(&**self, f) } } @@ -59,10 +59,13 @@ impl<Guard> AsMut<Guard> for LockGuard<Guard> { #[cfg(test)] mod tests { + #[cfg(feature = "std")] use crate::collection::OwnedLockCollection; + #[cfg(feature = "std")] use crate::{LockCollection, Mutex, RwLock, ThreadKey}; #[test] + #[cfg(feature = "std")] fn guard_display_works() { let key = ThreadKey::get().unwrap(); let lock = OwnedLockCollection::new(RwLock::new("Hello, world!")); @@ -71,6 +74,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn deref_mut_works() { let key = ThreadKey::get().unwrap(); let locks = (Mutex::new(1), Mutex::new(2)); @@ -88,6 +92,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn as_ref_works() { let key = ThreadKey::get().unwrap(); let locks = (Mutex::new(1), Mutex::new(2)); @@ -105,6 +110,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn as_mut_works() { let key = ThreadKey::get().unwrap(); let locks = (Mutex::new(1), Mutex::new(2)); diff --git a/src/collection/owned.rs b/src/collection/owned.rs index ab45e86..e53feb7 100755 --- a/src/collection/owned.rs +++ b/src/collection/owned.rs @@ -1,3 +1,5 @@ +use alloc::vec::Vec; + use crate::context::LockContext; use crate::lockable::{ Lockable, LockableGetMut, LockableIntoInner, OwnedLockable, RawLock, Sharable, @@ -676,10 +678,15 @@ impl<L: LockableIntoInner> OwnedLockCollection<L> { #[cfg(test)] mod tests { + use alloc::{string::String, vec}; + use super::*; - use crate::{LockCollection, Mutex, RwLock, ThreadKey}; + use crate::{LockCollection, Mutex}; + #[cfg(feature = "std")] + use crate::{RwLock, ThreadKey}; #[test] + #[cfg(feature = "std")] fn get_mut_applies_changes() { let key = ThreadKey::get().unwrap(); let mut collection = OwnedLockCollection::new([Mutex::new("foo"), Mutex::new("bar")]); @@ -693,6 +700,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn into_inner_works() { let key = ThreadKey::get().unwrap(); let collection = OwnedLockCollection::from([Mutex::new("foo")]); @@ -727,6 +735,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn scoped_read_works() { let mut key = ThreadKey::get().unwrap(); let collection = OwnedLockCollection::new([RwLock::new(24), RwLock::new(42)]); @@ -735,6 +744,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn scoped_lock_works() { let mut key = ThreadKey::get().unwrap(); let collection = OwnedLockCollection::new([RwLock::new(24), RwLock::new(42)]); @@ -750,6 +760,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn scoped_try_lock_can_fail() { let key = ThreadKey::get().unwrap(); let collection = OwnedLockCollection::new([Mutex::new(1), Mutex::new(2)]); @@ -767,6 +778,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn scoped_try_read_can_fail() { let key = ThreadKey::get().unwrap(); let collection = OwnedLockCollection::new([RwLock::new(1), RwLock::new(2)]); @@ -784,6 +796,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn try_lock_works_on_unlocked() { let key = ThreadKey::get().unwrap(); let collection = OwnedLockCollection::new((Mutex::new(0), Mutex::new(1))); @@ -793,6 +806,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn try_lock_fails_on_locked() { let key = ThreadKey::get().unwrap(); let collection = OwnedLockCollection::new((Mutex::new(0), Mutex::new(1))); @@ -801,7 +815,7 @@ mod tests { s.spawn(|| { let key = ThreadKey::get().unwrap(); let guard = collection.lock(key); - std::mem::forget(guard); + core::mem::forget(guard); }); }); @@ -809,6 +823,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn try_read_succeeds_for_unlocked_collection() { let key = ThreadKey::get().unwrap(); let mutexes = [RwLock::new(24), RwLock::new(42)]; @@ -819,6 +834,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn try_read_fails_on_locked() { let key = ThreadKey::get().unwrap(); let collection = OwnedLockCollection::new((RwLock::new(0), RwLock::new(1))); @@ -827,7 +843,7 @@ mod tests { s.spawn(|| { let key = ThreadKey::get().unwrap(); let guard = collection.lock(key); - std::mem::forget(guard); + core::mem::forget(guard); }); }); @@ -835,6 +851,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn can_read_twice_on_different_threads() { let key = ThreadKey::get().unwrap(); let mutexes = [RwLock::new(24), RwLock::new(42)]; @@ -846,7 +863,7 @@ mod tests { let guard = collection.read(key); assert_eq!(*guard[0], 24); assert_eq!(*guard[1], 42); - std::mem::forget(guard); + core::mem::forget(guard); }); }); @@ -856,6 +873,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn unlock_collection_works() { let key = ThreadKey::get().unwrap(); let collection = OwnedLockCollection::new((Mutex::new("foo"), Mutex::new("bar"))); @@ -866,6 +884,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn read_unlock_collection_works() { let key = ThreadKey::get().unwrap(); let collection = OwnedLockCollection::new((RwLock::new("foo"), RwLock::new("bar"))); @@ -897,6 +916,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn works_in_collection() { let key = ThreadKey::get().unwrap(); let collection = diff --git a/src/collection/ref.rs b/src/collection/ref.rs index a422bb2..8ab7120 100755 --- a/src/collection/ref.rs +++ b/src/collection/ref.rs @@ -1,4 +1,6 @@ -use std::fmt::Debug; +use core::fmt::Debug; + +use alloc::vec::Vec; use crate::lockable::{Lockable, OwnedLockable, RawLock, Sharable}; use crate::{Keyable, ThreadKey}; @@ -114,7 +116,7 @@ impl<T: ?Sized, L: AsRef<T>> AsRef<T> for RefLockCollection<'_, L> { #[mutants::skip] #[cfg(not(tarpaulin_include))] impl<L: Debug> Debug for RefLockCollection<'_, L> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { f.debug_struct(stringify!(RefLockCollection)) .field("data", self.child) // there's not much reason to show the sorting order @@ -640,7 +642,9 @@ where #[cfg(test)] mod tests { use super::*; - use crate::{Mutex, RwLock, ThreadKey}; + use crate::Mutex; + #[cfg(feature = "std")] + use crate::{RwLock, ThreadKey}; #[test] fn non_duplicates_allowed() { @@ -656,6 +660,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn from() { let key = ThreadKey::get().unwrap(); let mutexes = [Mutex::new("foo"), Mutex::new("bar"), Mutex::new("baz")]; @@ -667,6 +672,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn scoped_lock_changes_collection() { let mut key = ThreadKey::get().unwrap(); let mutexes = [Mutex::new(24), Mutex::new(42)]; @@ -684,6 +690,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn scoped_read_sees_changes() { let mut key = ThreadKey::get().unwrap(); let mutexes = [RwLock::new(24), RwLock::new(42)]; @@ -702,6 +709,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn scoped_try_lock_can_fail() { let key = ThreadKey::get().unwrap(); let locks = [Mutex::new(1), Mutex::new(2)]; @@ -720,6 +728,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn scoped_try_read_can_fail() { let key = ThreadKey::get().unwrap(); let locks = [RwLock::new(1), RwLock::new(2)]; @@ -738,6 +747,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn try_lock_succeeds_for_unlocked_collection() { let key = ThreadKey::get().unwrap(); let mutexes = [Mutex::new(24), Mutex::new(42)]; @@ -748,6 +758,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn try_lock_fails_for_locked_collection() { let key = ThreadKey::get().unwrap(); let mutexes = [Mutex::new(24), Mutex::new(42)]; @@ -758,7 +769,7 @@ mod tests { let key = ThreadKey::get().unwrap(); let guard = mutexes[1].lock(key); assert_eq!(*guard, 42); - std::mem::forget(guard); + core::mem::forget(guard); }); }); @@ -767,6 +778,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn try_read_succeeds_for_unlocked_collection() { let key = ThreadKey::get().unwrap(); let mutexes = [RwLock::new(24), RwLock::new(42)]; @@ -777,6 +789,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn try_read_fails_for_locked_collection() { let key = ThreadKey::get().unwrap(); let mutexes = [RwLock::new(24), RwLock::new(42)]; @@ -787,7 +800,7 @@ mod tests { let key = ThreadKey::get().unwrap(); let guard = mutexes[1].write(key); assert_eq!(*guard, 42); - std::mem::forget(guard); + core::mem::forget(guard); }); }); @@ -796,6 +809,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn can_read_twice_on_different_threads() { let key = ThreadKey::get().unwrap(); let mutexes = [RwLock::new(24), RwLock::new(42)]; @@ -807,7 +821,7 @@ mod tests { let guard = collection.read(key); assert_eq!(*guard[0], 24); assert_eq!(*guard[1], 42); - std::mem::forget(guard); + core::mem::forget(guard); }); }); @@ -817,6 +831,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn into_ref_iterator() { let mut key = ThreadKey::get().unwrap(); let mutexes = [Mutex::new(0), Mutex::new(1), Mutex::new(2)]; @@ -827,6 +842,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn ref_iterator() { let mut key = ThreadKey::get().unwrap(); let mutexes = [Mutex::new(0), Mutex::new(1), Mutex::new(2)]; @@ -837,6 +853,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn works_in_collection() { let key = ThreadKey::get().unwrap(); let mutex1 = RwLock::new(0); @@ -862,6 +879,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn unlock_collection_works() { let key = ThreadKey::get().unwrap(); let mutexes = (Mutex::new("foo"), Mutex::new("bar")); @@ -873,6 +891,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn read_unlock_collection_works() { let key = ThreadKey::get().unwrap(); let locks = (RwLock::new("foo"), RwLock::new("bar")); @@ -888,7 +907,7 @@ mod tests { let mutexes = [Mutex::new(0), Mutex::new(1)]; let collection = RefLockCollection::new(&mutexes); - assert!(std::ptr::addr_eq(&raw const mutexes, collection.as_ref())) + assert!(core::ptr::addr_eq(&raw const mutexes, collection.as_ref())) } #[test] @@ -896,6 +915,6 @@ mod tests { let mutexes = [Mutex::new(0), Mutex::new(1)]; let collection = RefLockCollection::new(&mutexes); - assert!(std::ptr::addr_eq(&raw const mutexes, collection.child())) + assert!(core::ptr::addr_eq(&raw const mutexes, collection.child())) } } diff --git a/src/collection/retry.rs b/src/collection/retry.rs index b9ac530..1cce663 100755 --- a/src/collection/retry.rs +++ b/src/collection/retry.rs @@ -1,5 +1,6 @@ -use std::cell::Cell; -use std::collections::HashSet; +use core::cell::Cell; + +use alloc::vec::Vec; use crate::collection::utils; use crate::handle_unwind::handle_unwind; @@ -14,6 +15,20 @@ use super::utils::{ }; use super::{LockGuard, RetryingLockCollection}; +cfg_select! { + feature = "std" => { + fn set_with_capacity<T>(capacity: usize) -> std::collections::HashSet<T> { + std::collections::HashSet::with_capacity(capacity) + } + }, + _ => { + #[expect(clippy::missing_const_for_fn)] + fn set_with_capacity<T>(_capacity: usize) -> alloc::collections::btree_set::BTreeSet<T> { + alloc::collections::btree_set::BTreeSet::new() + } + }, +} + /// Checks that a collection contains no duplicate references to a lock. fn contains_duplicates<L: Lockable>(data: L) -> bool { let mut locks = Vec::new(); @@ -21,7 +36,7 @@ fn contains_duplicates<L: Lockable>(data: L) -> bool { // cast to *const () so that the v-table pointers are not used for hashing let locks = locks.into_iter().map(|l| (&raw const *l).cast::<()>()); - let mut locks_set = HashSet::with_capacity(locks.len()); + let mut locks_set = set_with_capacity(locks.len()); for lock in locks { if !locks_set.insert(lock) { return true; @@ -1004,9 +1019,14 @@ where #[cfg(test)] mod tests { + use alloc::vec; + use super::*; + #[cfg(feature = "std")] use crate::collection::BoxedLockCollection; - use crate::{Mutex, RwLock, ThreadKey}; + #[cfg(feature = "std")] + use crate::ThreadKey; + use crate::{Mutex, RwLock}; #[test] fn nonduplicate_lock_references_are_allowed() { @@ -1033,6 +1053,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn from() { let key = ThreadKey::get().unwrap(); let collection = @@ -1044,6 +1065,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn new_ref_works() { let key = ThreadKey::get().unwrap(); let mutexes = [Mutex::new(0), Mutex::new(1)]; @@ -1055,6 +1077,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn scoped_read_sees_changes() { let mut key = ThreadKey::get().unwrap(); let mutexes = [RwLock::new(24), RwLock::new(42)]; @@ -1071,6 +1094,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn get_mut_affects_scoped_read() { let mut key = ThreadKey::get().unwrap(); let mutexes = [RwLock::new(24), RwLock::new(42)]; @@ -1088,6 +1112,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn scoped_try_lock_can_fail() { let key = ThreadKey::get().unwrap(); let collection = RetryingLockCollection::new([Mutex::new(1), Mutex::new(2)]); @@ -1105,6 +1130,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn scoped_try_read_can_fail() { let key = ThreadKey::get().unwrap(); let collection = RetryingLockCollection::new([RwLock::new(1), RwLock::new(2)]); @@ -1122,6 +1148,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn try_lock_works() { let key = ThreadKey::get().unwrap(); let collection = RetryingLockCollection::new([Mutex::new(1), Mutex::new(2)]); @@ -1139,6 +1166,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn try_read_works() { let key = ThreadKey::get().unwrap(); let collection = RetryingLockCollection::new([RwLock::new(1), RwLock::new(2)]); @@ -1156,6 +1184,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn try_read_fails_for_locked_collection() { let key = ThreadKey::get().unwrap(); let mutexes = [RwLock::new(24), RwLock::new(42)]; @@ -1166,7 +1195,7 @@ mod tests { let key = ThreadKey::get().unwrap(); let guard = mutexes[1].write(key); assert_eq!(*guard, 42); - std::mem::forget(guard); + core::mem::forget(guard); }); }); @@ -1175,6 +1204,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn locks_all_inner_mutexes() { let key = ThreadKey::get().unwrap(); let mutex1 = Mutex::new(0); @@ -1190,6 +1220,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn locks_all_inner_rwlocks() { let key = ThreadKey::get().unwrap(); let rwlock1 = RwLock::new(0); @@ -1205,6 +1236,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn works_with_other_collections() { let key = ThreadKey::get().unwrap(); let mutex1 = Mutex::new(0); @@ -1222,6 +1254,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn from_iterator() { let key = ThreadKey::get().unwrap(); let collection: RetryingLockCollection<Vec<Mutex<&str>>> = @@ -1243,6 +1276,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn into_ref_iterator() { let mut key = ThreadKey::get().unwrap(); let collection = RetryingLockCollection::new([Mutex::new(0), Mutex::new(1), Mutex::new(2)]); @@ -1252,6 +1286,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn ref_iterator() { let mut key = ThreadKey::get().unwrap(); let collection = RetryingLockCollection::new([Mutex::new(0), Mutex::new(1), Mutex::new(2)]); @@ -1261,6 +1296,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn mut_iterator() { let mut key = ThreadKey::get().unwrap(); let mut collection = @@ -1282,6 +1318,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn lock_empty_lock_collection() { let key = ThreadKey::get().unwrap(); let collection: RetryingLockCollection<[RwLock<i32>; 0]> = RetryingLockCollection::new([]); @@ -1295,6 +1332,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn read_empty_lock_collection() { let key = ThreadKey::get().unwrap(); let collection: RetryingLockCollection<[RwLock<i32>; 0]> = RetryingLockCollection::new([]); @@ -1312,7 +1350,7 @@ mod tests { let mutexes = [Mutex::new(0), Mutex::new(1)]; let collection = RetryingLockCollection::new_ref(&mutexes); - assert!(std::ptr::addr_eq(&raw const mutexes, collection.as_ref())) + assert!(core::ptr::addr_eq(&raw const mutexes, collection.as_ref())) } #[test] @@ -1330,7 +1368,7 @@ mod tests { let mutexes = [Mutex::new(0), Mutex::new(1)]; let collection = RetryingLockCollection::new_ref(&mutexes); - assert!(std::ptr::addr_eq(&raw const mutexes, *collection.child())) + assert!(core::ptr::addr_eq(&raw const mutexes, *collection.child())) } #[test] diff --git a/src/collection/utils.rs b/src/collection/utils.rs index e79b78b..236853c 100755 --- a/src/collection/utils.rs +++ b/src/collection/utils.rs @@ -1,4 +1,6 @@ -use std::cell::Cell; +use core::cell::Cell; + +use alloc::vec::Vec; use crate::handle_unwind::handle_unwind; use crate::lockable::{Lockable, RawLock, Sharable}; @@ -32,7 +34,7 @@ pub fn ordered_contains_duplicates(l: &[&dyn RawLock]) -> bool { l.windows(2) // NOTE: addr_eq is necessary because eq would also compare the v-table pointers - .any(|window| std::ptr::addr_eq(window[0], window[1])) + .any(|window| core::ptr::addr_eq(window[0], window[1])) } /// Lock a set of locks in the given order. It's UB to call this without a `ThreadKey` |
