Skip to main content

MutexKey

Struct MutexKey 

Source
pub struct MutexKey<'scope, Lvl: IsLevel> { /* private fields */ }
Expand description

Scope token for ordered lock acquisition.

Tracks the current lock level as a type parameter Lvl. Consumed by lock and re-emitted at the new level.

  • !Send + !Sync — cannot cross thread boundaries.
  • Branded lifetime 'scope — cannot escape the lock_scope closure.
  • !Clone + !Copy — prevents using the same key twice.

Implementations§

Source§

impl<'scope, Lvl: IsLevel> MutexKey<'scope, Lvl>

Source

pub fn lock<'a, L: Lockable<'a>>( self, target: &'a L, ) -> (L::Guard, MutexKey<'scope, L::MaxLvl>)
where L::MinLvl: LockAfter<Lvl>,

Lock one or more mutexes, returning the guard(s) directly.

Accepts a single &Mutex or a &LockSet. Consumes the key and returns the guard(s) plus a new key at the target’s level.

For a single mutex this is zero-allocation; for a LockSet the sort was already done at construction time.

§Examples
use surelock::{key::lock_scope, level::Level, mutex::Mutex, set::LockSet};

let a: Mutex<u32> = Mutex::new(1);
let b: Mutex<u32, Level<1>> = Mutex::new(2);

lock_scope(|key| {
    // Single mutex
    let (mut guard, key) = key.lock(&a);
    *guard += 10;
    drop(guard);

    // Pre-sorted set
    let set = LockSet::new(&b);
    let (mut guard, _key) = key.lock(&set);
    *guard += 20;
});
Source

pub fn lock_with<'a, L, F, Ret>( self, lockable: &'a L, f: F, ) -> (Ret, MutexKey<'scope, <L as Acquirable<'a>>::MaxLvl>)
where L: Acquirable<'a>, <L as Acquirable<'a>>::MinLvl: LockAfter<Lvl>, F: FnOnce(<L as Acquirable<'a>>::Guard) -> Ret,

Lock one or more mutexes via a closure.

Sorts by LockId and acquires in one call. The guards live inside the closure – no LockSet needed. Convenient for one-shot read-and-release patterns.

For hot paths where the same locks are acquired repeatedly, pre-build a LockSet and use lock instead (sort once, lock many).

Consumes the key. Returns the closure’s result plus a new key at the lock group’s level.

§Panics

Panics if the group contains duplicate locks (same LockId appearing more than once).

§Examples
use surelock::{key_handle::KeyHandle, mutex::Mutex};

let a: Mutex<u32> = Mutex::new(1);
let b: Mutex<u32> = Mutex::new(2);

let mut handle = KeyHandle::claim();
handle.scope(|key| {
    let (sum, _key) = key.lock_with(&(&a, &b), |(ga, gb)| *ga + *gb);
    assert_eq!(sum, 3);
});
Source

pub fn subscope<F, Ret>(self, f: F) -> (Ret, MutexKey<'scope, Lvl>)
where F: for<'inner> FnOnce(MutexKey<'inner, Lvl>) -> Ret,

Create a nested scope that inherits the current level.

The inner key starts at the same level as the outer key, so any subsequent lock calls must still be at higher levels. The outer key is consumed for the duration of the subscope.

After the subscope returns, a key at the same level is returned alongside the closure’s return value. The inner key gets its own branded lifetime, so its guards cannot escape the inner closure.

Trait Implementations§

Source§

impl<Lvl: IsLevel> Debug for MutexKey<'_, Lvl>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'scope, Lvl> Freeze for MutexKey<'scope, Lvl>

§

impl<'scope, Lvl> RefUnwindSafe for MutexKey<'scope, Lvl>
where Lvl: RefUnwindSafe,

§

impl<'scope, Lvl> !Send for MutexKey<'scope, Lvl>

§

impl<'scope, Lvl> !Sync for MutexKey<'scope, Lvl>

§

impl<'scope, Lvl> Unpin for MutexKey<'scope, Lvl>
where Lvl: Unpin,

§

impl<'scope, Lvl> UnsafeUnpin for MutexKey<'scope, Lvl>

§

impl<'scope, Lvl> UnwindSafe for MutexKey<'scope, Lvl>
where Lvl: UnwindSafe,

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, 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.