lock_hierarchy/
lib.rs

1//! This crate offers debug assertions for violations of lock hierarchies. No runtime overhead or
2//! protection occurs for release builds.
3//!
4//! Each lock is assigned a level. Locks with higher levels must be acquired before locks with
5//! lower levels.
6//! Both [RwLock] and [Mutex] use the same hierarchy.
7
8mod level;
9mod mutex;
10mod rwlock;
11
12use std::sync::{LockResult, PoisonError};
13
14pub use mutex::{Mutex, MutexGuard};
15pub use rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard};
16
17pub(crate) fn map_guard<G, F>(result: LockResult<G>, f: impl FnOnce(G) -> F) -> LockResult<F> {
18    match result {
19        Ok(guard) => Ok(f(guard)),
20        Err(err) => Err(PoisonError::new(f(err.into_inner()))),
21    }
22}