From 16d6bd7ece4c4b9b72f6bd51c9b034270b745525 Mon Sep 17 00:00:00 2001 From: Mica White Date: Sun, 13 Sep 2026 15:12:08 -0400 Subject: Support no_std --- src/collection/boxed.rs | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) (limited to 'src/collection/boxed.rs') 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> AsRef for BoxedLockCollection { #[mutants::skip] #[cfg(not(tarpaulin_include))] impl Debug for BoxedLockCollection { - 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 BoxedLockCollection { 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> = 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 BoxedLockCollection { 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>> = @@ -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())) } } -- cgit v1.3.1