xrcf::shared

Trait SharedExt

Source
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§

Source

fn rd(&self) -> RwLockReadGuard<'_, T>

Convenience method for reading.

Source

fn wr(&self) -> RwLockWriteGuard<'_, T>

Convenience method for writing.

Implementors§

Source§

impl<T: ?Sized> SharedExt<T> for Shared<T>