Skip to main content

steel_utils/
locks.rs

1#![expect(
2    clippy::disallowed_types,
3    reason = "this module is the canonical definition of the allowed lock types"
4)]
5//! Lock wrappers for debug checks and deadlock prevention.
6
7use std::sync::Arc;
8
9use tokio::sync::{Mutex, RwLock};
10
11/// A synchronous mutex.
12pub type SyncMutex<T> = parking_lot::Mutex<T>;
13/// A synchronous read-write lock.
14pub type SyncRwLock<T> = parking_lot::RwLock<T>;
15
16/// An asynchronous mutex.
17pub type AsyncMutex<T> = Mutex<T>;
18/// An asynchronous read-write lock.
19pub type AsyncRwLock<T> = RwLock<T>;
20
21/// A value shared across threads behind the crate's standard lock.
22pub type Shared<T> = Arc<SyncMutex<T>>;
23
24/// Wraps `value` in the standard shared lock handle.
25pub fn shared<T>(value: T) -> Shared<T> {
26    Arc::new(SyncMutex::new(value))
27}
28
29/// Wraps this value in an `Arc<SyncMutex<>>`
30pub trait IntoShared: Sized {
31    /// Wraps this value in an `Arc<SyncMutex<>>`
32    fn into_shared(self) -> Shared<Self> {
33        Arc::new(SyncMutex::new(self))
34    }
35}
36
37impl<T> IntoShared for T {}