1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
/// A version of the locks from /// [`parking_lot`](https://github.com/Amanieu/parking_lot) which can be used in /// a static (e.g. without lazy_static, or OnceCell). /// /// The only thing preventing this was the fact that the API was implemented in /// a generic way in `lock_api`, and const trait bounds aren't fully supported /// yet. /// /// All this crate does is manually monomorphize (expand) the generic /// implementation, allowing `Mutex::new`, `RwLock::new`, and /// `ReentrantMutex::new`, etc to be used in statics. Eventually this will work /// without this crate, at which point there will probably be no reason to use /// it. /// /// # Example: /// /// ``` /// // Or RwLock, ReentrantMutex, ... /// use static_locks::Mutex; /// /// # struct MyType(String); /// // Avoid lazy_static / OnceCell / and just use a static mutex! /// static MUTEXED: Mutex<Option<MyType>> = Mutex::new(None); /// ``` mod mutex; mod remutex; mod rwlock; pub use mutex::*; pub use remutex::*; pub use rwlock::*; /// Our version of parking_lot, re-exported should you need it. pub use parking_lot; // Used to avoid having to change the lock_api code where I don't need to. (It // uses this crate too). #[macro_use] extern crate scopeguard; #[cfg(test)] mod test { use super::*; // Ensure we can make statics of these. #[allow(dead_code)] static MUTEXED: Mutex<()> = Mutex::new(()); #[allow(dead_code)] static REMUTEXED: ReentrantMutex<()> = ReentrantMutex::new(()); #[allow(dead_code)] static RWLOCKED: RwLock<()> = RwLock::new(()); macro_rules! assert_impls_traits { ($ty:ty : $trait0:ident $(+ $trait_:ident)* $(,)?) => {{ fn check<T: ?Sized + $trait0 $(+ $trait_)*>() {} check::<$ty>(); }}; } // ensure the parking lot types actually do implement what i assumed // they did when manually monomorphizing things. #[test] fn check_parkinglot_traits() { use parking_lot::lock_api::*; assert_impls_traits!(parking_lot::RawMutex: Send + Sync + RawMutexFair + RawMutexTimed); assert_impls_traits!(parking_lot::RawRwLock: Send + Sync + RawRwLock + RawRwLockFair + RawRwLockDowngrade + RawRwLockTimed + RawRwLockRecursive + RawRwLockRecursiveTimed + RawRwLockUpgrade + RawRwLockUpgradeFair + RawRwLockUpgradeDowngrade + RawRwLockUpgradeTimed); } }