From 129c13c21254ca104bddf020170edaca1fb7107d Mon Sep 17 00:00:00 2001 From: Mica White Date: Wed, 25 Dec 2024 22:49:42 -0500 Subject: Implement common traits --- src/mutex/guard.rs | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 2 deletions(-) (limited to 'src/mutex') diff --git a/src/mutex/guard.rs b/src/mutex/guard.rs index c255996..b79d90b 100644 --- a/src/mutex/guard.rs +++ b/src/mutex/guard.rs @@ -1,4 +1,5 @@ use std::fmt::{Debug, Display}; +use std::hash::Hash; use std::marker::PhantomData; use std::ops::{Deref, DerefMut}; @@ -9,9 +10,34 @@ use crate::lockable::RawLock; use super::{Mutex, MutexGuard, MutexRef}; +impl PartialEq for MutexRef<'_, T, R> { + fn eq(&self, other: &Self) -> bool { + self.deref().eq(&**other) + } +} + +impl Eq for MutexRef<'_, T, R> {} + +impl PartialOrd for MutexRef<'_, T, R> { + fn partial_cmp(&self, other: &Self) -> Option { + self.deref().partial_cmp(&**other) + } +} + +impl Ord for MutexRef<'_, T, R> { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + self.deref().cmp(&**other) + } +} + +impl Hash for MutexRef<'_, T, R> { + fn hash(&self, state: &mut H) { + self.deref().hash(state) + } +} + // This makes things slightly easier because now you can use -// `println!("{guard}")` instead of `println!("{}", *guard)`. I wonder if I -// should implement some other standard library traits like this too? +// `println!("{guard}")` instead of `println!("{}", *guard)` impl<'a, T: Debug + ?Sized + 'a, R: RawMutex> Debug for MutexRef<'a, T, R> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { Debug::fmt(&**self, f) @@ -76,6 +102,34 @@ impl<'a, T: ?Sized + 'a, R: RawMutex> MutexRef<'a, T, R> { // it's kinda annoying to re-implement some of this stuff on guards // there's nothing i can do about that +impl PartialEq for MutexGuard<'_, '_, T, Key, R> { + fn eq(&self, other: &Self) -> bool { + self.deref().eq(&**other) + } +} + +impl Eq for MutexGuard<'_, '_, T, Key, R> {} + +impl PartialOrd + for MutexGuard<'_, '_, T, Key, R> +{ + fn partial_cmp(&self, other: &Self) -> Option { + self.deref().partial_cmp(&**other) + } +} + +impl Ord for MutexGuard<'_, '_, T, Key, R> { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + self.deref().cmp(&**other) + } +} + +impl Hash for MutexGuard<'_, '_, T, Key, R> { + fn hash(&self, state: &mut H) { + self.deref().hash(state) + } +} + impl<'a, 'key, T: Debug + ?Sized + 'a, Key: Keyable + 'key, R: RawMutex> Debug for MutexGuard<'a, 'key, T, Key, R> { -- cgit v1.2.3