SortMutex

Struct SortMutex 

Source
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§

Source§

impl<T> SortMutex<T>

Source

pub fn new(value: T) -> Self

Creates a new SortLock.

  • value - The value of the lock.
Source

pub fn lock(&self) -> SortMutexGuard<'_, T>

Requests to lock this lock. This method returns a guard which can be used with lock_all to perform a sorted lock.

§Panicking

The guard will panic when locked if this lock becomes poisoned.

Trait Implementations§

Source§

impl<T: Debug> Debug for SortMutex<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Default> Default for SortMutex<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T: Display> Display for SortMutex<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.