pub trait SharedExt<T: ?Sized> {
// Required methods
fn rd(&self) -> RwLockReadGuard<'_, T>;
fn wr(&self) -> RwLockWriteGuard<'_, T>;
}Expand description
A convenience trait around RwLock.
This trait makes using RwLock less verbose. It has two benefits. One is obviously that it saves a lot of typing. Another one is that one-liners are particularly powerful in Rust due to when variables are freed, see https://xrcf.org/blog/iterators/ for more information.
Regarding the lost access to error handling (now it just always crashes), that’s for now the default behavior until the project starts to support multithreading. Until that feature is introduced, any blocking should crash since it will hang anyway.
§Example
With this trait and with Shared:
use std::sync::Arc;
use std::sync::RwLock;
use xrcf::shared::Shared;
use xrcf::shared::SharedExt;
let lock = Shared::new(42.into());
assert_eq!(*lock.rd(), 42);Without this trait:
use std::sync::Arc;
use std::sync::RwLock;
let lock = Arc::new(RwLock::new(42));
assert_eq!(*lock.try_read().unwrap(), 42);Required Methods§
Sourcefn rd(&self) -> RwLockReadGuard<'_, T>
fn rd(&self) -> RwLockReadGuard<'_, T>
Convenience method for reading.
Sourcefn wr(&self) -> RwLockWriteGuard<'_, T>
fn wr(&self) -> RwLockWriteGuard<'_, T>
Convenience method for writing.