pub trait RwLockExtensions<T: Send + Sync> {
// Required methods
fn read_safe(&self, context: &str) -> DMSCLockResult<RwLockReadGuard<'_, T>>;
fn write_safe(
&self,
context: &str,
) -> DMSCLockResult<RwLockWriteGuard<'_, T>>;
fn try_read_safe(
&self,
context: &str,
) -> DMSCLockResult<Option<RwLockReadGuard<'_, T>>>;
fn try_write_safe(
&self,
context: &str,
) -> DMSCLockResult<Option<RwLockWriteGuard<'_, T>>>;
}Expand description
Extension trait providing safe read lock acquisition for RwLock.
This trait adds a read_safe method to RwLock that returns a Result
instead of panicking when the lock is poisoned or cannot be acquired.
Required Methods§
Sourcefn read_safe(&self, context: &str) -> DMSCLockResult<RwLockReadGuard<'_, T>>
fn read_safe(&self, context: &str) -> DMSCLockResult<RwLockReadGuard<'_, T>>
Acquires a read lock safely, returning a Result instead of panicking.
This method attempts to acquire a read lock on the RwLock. If the lock
is held by a writer or if a previous holder panicked (poisoned), this
method returns an error instead of panicking.
§Arguments
context: A description of what is being locked for error messages
§Returns
Ok(RwLockReadGuard<T>)if the lock was acquired successfullyErr(DMSCLockError)if the lock could not be acquired
§Examples
use std::sync::RwLock;
use dmsc::core::lock::RwLockExtensions;
let lock = RwLock::new(42);
match lock.read_safe("counter") {
Ok(guard) => println!("Value: {}", *guard),
Err(e) => println!("Failed to acquire lock: {}", e),
}Sourcefn write_safe(&self, context: &str) -> DMSCLockResult<RwLockWriteGuard<'_, T>>
fn write_safe(&self, context: &str) -> DMSCLockResult<RwLockWriteGuard<'_, T>>
Acquires a write lock safely, returning a Result instead of panicking.
This method attempts to acquire a write lock on the RwLock. If the lock
is currently held by any readers or if a previous holder panicked (poisoned),
this method returns an error instead of panicking.
§Arguments
context: A description of what is being locked for error messages
§Returns
Ok(RwLockWriteGuard<T>)if the lock was acquired successfullyErr(DMSCLockError)if the lock could not be acquired
§Examples
use std::sync::RwLock;
use dmsc::core::lock::RwLockExtensions;
let lock = RwLock::new(42);
match lock.write_safe("counter") {
Ok(mut guard) => {
*guard += 1;
println!("New value: {}", *guard);
}
Err(e) => println!("Failed to acquire lock: {}", e),
}Sourcefn try_read_safe(
&self,
context: &str,
) -> DMSCLockResult<Option<RwLockReadGuard<'_, T>>>
fn try_read_safe( &self, context: &str, ) -> DMSCLockResult<Option<RwLockReadGuard<'_, T>>>
Attempts to acquire a read lock, returning immediately if unavailable.
This method tries to acquire a read lock without blocking. If the lock is held by a writer, it returns an error immediately.
§Arguments
context: A description of what is being locked for error messages
§Returns
Ok(Some(RwLockReadGuard<T>))if the lock was acquiredOk(None)if the lock is held by a writerErr(DMSCLockError)if the lock is poisoned
Sourcefn try_write_safe(
&self,
context: &str,
) -> DMSCLockResult<Option<RwLockWriteGuard<'_, T>>>
fn try_write_safe( &self, context: &str, ) -> DMSCLockResult<Option<RwLockWriteGuard<'_, T>>>
Attempts to acquire a write lock, returning immediately if unavailable.
This method tries to acquire a write lock without blocking. If the lock is held by any readers, it returns an error immediately.
§Arguments
context: A description of what is being locked for error messages
§Returns
Ok(Some(RwLockWriteGuard<T>))if the lock was acquiredOk(None)if the lock is held by readers or a writerErr(DMSCLockError)if the lock is poisoned