Skip to main content

Mutex

Struct Mutex 

Source
pub struct Mutex<T: ?Sized> { /* private fields */ }
Expand description

Mutex protecting a value of type T. std::sync::Mutex<T> / parking_lot::Mutex<T> analog.

The lock and the data live together in one type. Acquiring the lock via Mutex::lock returns a MutexGuard<T> that Derefs to T, so the only way to touch the data is to hold the lock first — the borrow checker enforces the contract that lock then *g = … is the only legal access pattern.

§Differences from std::sync::Mutex

  • No poisoning. lock() returns Result<MutexGuard<T>, Error>, not std’s LockResult<MutexGuard<T>>. Mirrors parking_lot’s choice — forcing callers to unwrap a never-firing PoisonError is worse ergonomics than reporting only the backend errors that can actually occur.
  • try_lock_for / try_lock_until variants for bounded waits. Parking_lot has these; std does not. Return Result<MutexGuard, Error> (with Error::Timeout on deadline elapsed), not parking_lot’s Option<MutexGuard> — we have backend errors that aren’t simply “could not acquire”.

Implementations§

Source§

impl<T> Mutex<T>

Source

pub fn new(val: T) -> Result<Self>

Create a new mutex around val via heap allocation (heap mode only).

Source

pub fn create(storage: &'static MutexStorage, val: T) -> Result<Self>

Create a mutex around val that works in both heap and zero-heap modes: heap mode ignores storage and allocates; zero-heap mode backs the handle with the caller-provided storage (which must outlive the Mutex). This is the mode-agnostic constructor downstream crates use — new/from_static are gated to one mode and from_static needs the crate-private storage type, so neither is callable portably.

Source

pub fn into_inner(self) -> Result<T>

Consume the mutex and return the protected value.

Available in heap mode only (zero-heap mode lacks a destroy path for the handle — the handle’s storage outlives the Mutex anyway).

Source§

impl<T: ?Sized> Mutex<T>

Source

pub fn get_mut(&mut self) -> &mut T

Get a mutable reference to the protected value without locking.

Safe because &mut self proves the caller has exclusive access — no other thread can hold a guard at this moment. Matches std::sync::Mutex::get_mut.

Source

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

Acquire the mutex, blocking indefinitely. Returns an RAII guard that releases the lock on drop.

§Errors

Returns the substrate’s error code if the mutex handle is invalid (programming error — same failure mode as in C/C++).

Source

pub fn try_lock(&self) -> Result<MutexGuard<'_, T>>

Attempt to acquire the mutex without blocking. std::sync::Mutex::try_lock analog.

§Errors

Returns Error::WouldBlock if the mutex is currently held by another thread.

Source

pub fn try_lock_for(&self, d: Duration) -> Result<MutexGuard<'_, T>>

Attempt to acquire the mutex, waiting up to d. parking_lot::Mutex::try_lock_for analog.

§Errors

Returns Error::Timeout if the lock cannot be acquired within the duration.

Source

pub fn try_lock_until(&self, deadline: Instant) -> Result<MutexGuard<'_, T>>

Attempt to acquire the mutex by the given deadline. parking_lot::Mutex::try_lock_until analog. Use Instant::FOREVER for an indefinite wait.

§Errors

Returns Error::Timeout if the deadline elapses before the lock is acquired.

Trait Implementations§

Source§

impl<T: ?Sized + Debug> Debug for Mutex<T>

Source§

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

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

impl<T: ?Sized> Drop for Mutex<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T: ?Sized + Send> Send for Mutex<T>

Source§

impl<T: ?Sized + Send> Sync for Mutex<T>

Auto Trait Implementations§

§

impl<T> !Freeze for Mutex<T>

§

impl<T> !RefUnwindSafe for Mutex<T>

§

impl<T> Unpin for Mutex<T>
where T: Unpin + ?Sized,

§

impl<T> UnsafeUnpin for Mutex<T>
where T: UnsafeUnpin + ?Sized,

§

impl<T> UnwindSafe for Mutex<T>
where T: UnwindSafe + ?Sized,

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.