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/retry.rs | |
| parent | 9fd7b94cd7c20abe66cea875a0e83e76a7a00fbc (diff) | |
Support no_stdno-std
Diffstat (limited to 'src/collection/retry.rs')
| -rwxr-xr-x | src/collection/retry.rs | 52 |
1 files changed, 45 insertions, 7 deletions
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] |
