SortRwLock

Struct SortRwLock 

Source
pub struct SortRwLock<T> { /* private fields */ }
Expand description

A sortable lock that allows either exclusive write access or shared read access. This is a sortable version of rust’s RwLock type.

Locking looks a little different to RwLock, as this lock allows sorting with other locks through the use of lock_all. Locking for reading can be performed with read while locking for writing can be performed with write.

use sortlock::{SortRwLock, LockGroup};

let lock = SortRwLock::new("some value");

let guard = lock.read().lock_all();
println!("{}", *guard);
use sortlock::{SortRwLock, LockGroup};

let lock = SortRwLock::new(1);

let mut guard = lock.write().lock_all();
*guard += 1;
assert_eq!(2, *guard);

With multiple locks this ensures that locks are always locked in the same order. This occurs regardless of whether the lock was locked for reading or writing.

use sortlock::{SortRwLock, LockGroup};

let lock1 = SortRwLock::new(100);
let lock2 = SortRwLock::new(200);

// Here lock1 is locked then lock2.
let (guard1, mut guard2) = (lock1.read(), lock2.write()).lock_all();
println!("{}", *guard1);
*guard2 += 1;

// Unlock so we can lock again.
drop(guard1);
drop(guard2);

// Despite the order change the same is true here.
let (guard2, mut guard1) = (lock2.read(), lock1.write()).lock_all();
*guard1 += 1;
println!("{}", *guard2);

Implementations§

Source§

impl<T> SortRwLock<T>

Source

pub fn new(value: T) -> Self

Creates a new SortRwLock.

  • value - The value of the lock.
Source

pub fn read(&self) -> SortReadGuard<'_, T>

Requests to lock this lock for reading. 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.

Source

pub fn write(&self) -> SortWriteGuard<'_, T>

Requests to lock this lock for writing. 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 SortRwLock<T>

Source§

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

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

impl<T: Default> Default for SortRwLock<T>

Source§

fn default() -> Self

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

impl<T: Display> Display for SortRwLock<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 SortRwLock<T>

§

impl<T> RefUnwindSafe for SortRwLock<T>

§

impl<T> Send for SortRwLock<T>
where T: Send,

§

impl<T> Sync for SortRwLock<T>
where T: Send + Sync,

§

impl<T> Unpin for SortRwLock<T>
where T: Unpin,

§

impl<T> UnwindSafe for SortRwLock<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.