pub struct SortMutex<T> { /* private fields */ }Expand description
A sortable lock that ensures exclusive access to a resource.
This is a sortable version of rust’s Mutex type.
Locking looks a little different to Mutex, as this lock allows sorting with other locks
through the use of lock_all.
use sortlock::{SortMutex, LockGroup};
let lock = SortMutex::new("some value");
let guard = lock.lock().lock_all();
println!("{}", *guard);With multiple locks this ensures that locks are always locked in the same order:
use sortlock::{SortMutex, LockGroup};
let lock1 = SortMutex::new("some value");
let lock2 = SortMutex::new("some other value");
// Here lock1 is locked then lock2.
let (guard1, guard2) = (lock1.lock(), lock2.lock()).lock_all();
println!("{}", *guard1);
println!("{}", *guard2);
// Unlock so we can lock again.
drop(guard1);
drop(guard2);
// Despite the order change the same is true here.
let (guard2, guard1) = (lock2.lock(), lock1.lock()).lock_all();
println!("{}", *guard1);
println!("{}", *guard2);Implementations§
Trait Implementations§
Auto Trait Implementations§
impl<T> !Freeze for SortMutex<T>
impl<T> RefUnwindSafe for SortMutex<T>
impl<T> Send for SortMutex<T>where
T: Send,
impl<T> Sync for SortMutex<T>where
T: Send,
impl<T> Unpin for SortMutex<T>where
T: Unpin,
impl<T> UnwindSafe for SortMutex<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more