#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum LockMode {
Shared,
Exclusive,
}
impl LockMode {
#[inline]
#[must_use]
pub const fn compatible_with(self, other: LockMode) -> bool {
matches!((self, other), (LockMode::Shared, LockMode::Shared))
}
#[inline]
#[must_use]
pub const fn covers(self, other: LockMode) -> bool {
matches!(
(self, other),
(LockMode::Exclusive, _) | (LockMode::Shared, LockMode::Shared)
)
}
#[inline]
#[must_use]
pub const fn is_exclusive(self) -> bool {
matches!(self, LockMode::Exclusive)
}
}
#[cfg(test)]
mod tests {
use super::LockMode::{Exclusive, Shared};
#[test]
fn test_compatible_matrix_only_shared_shared_is_true() {
assert!(Shared.compatible_with(Shared));
assert!(!Shared.compatible_with(Exclusive));
assert!(!Exclusive.compatible_with(Shared));
assert!(!Exclusive.compatible_with(Exclusive));
}
#[test]
fn test_compatible_is_symmetric() {
for a in [Shared, Exclusive] {
for b in [Shared, Exclusive] {
assert_eq!(a.compatible_with(b), b.compatible_with(a));
}
}
}
#[test]
fn test_covers_reflexive() {
for m in [Shared, Exclusive] {
assert!(m.covers(m));
}
}
#[test]
fn test_covers_exclusive_covers_everything() {
assert!(Exclusive.covers(Shared));
assert!(Exclusive.covers(Exclusive));
}
#[test]
fn test_covers_shared_does_not_cover_exclusive() {
assert!(!Shared.covers(Exclusive));
}
#[test]
fn test_is_exclusive() {
assert!(Exclusive.is_exclusive());
assert!(!Shared.is_exclusive());
}
#[test]
fn test_ordering_shared_below_exclusive() {
assert!(Shared < Exclusive);
}
}