pub struct ArcGuard<T> { /* private fields */ }Implementations§
Source§impl<T> ArcGuard<T>
impl<T> ArcGuard<T>
Sourcepub fn new(t: T) -> Self
pub fn new(t: T) -> Self
Constructs a new ArcGuard<T>.
§Example
use arc_guard::ArcGuard;
let indicator = ArcGuard::new(Indicator::new());Sourcepub fn execute<R>(&self, callback: impl FnMut(Arc<Mutex<T>>) -> R) -> R
pub fn execute<R>(&self, callback: impl FnMut(Arc<Mutex<T>>) -> R) -> R
Executes a closure passed as an argument.
This is exactly what helps us avoid the boilerplate code,
execute passes an Arc<Mutex<T>> clone and when the Closure finishes,
the clone is automatically dropped.
§Example
use arc_guard::ArcGuard;
let indicator = ArcGuard::new(Indicator::new());
indicator.execute(|indicator| {
let indicator = indicator.lock().expect("Unable to lock indicator.");
indicator.do_something();
});execute takes the return type of the Closure as its own,
so you are able to return from your closure into a variable.
§Example
use arc_guard::ArcGuard;
let indicator = ArcGuard::new(Indicator::new());
let some_string: String = indicator.execute(|indicator| -> String {
let indicator = indicator.lock().expect("Unable to lock indicator.");
return indicator.something();
});Sourcepub fn arc(&self) -> Arc<Mutex<T>>
pub fn arc(&self) -> Arc<Mutex<T>>
In some cases it is convenient to use Arc<Mutex<T>>, instead of ArcGuard<T>.
With this method you are able to get a clone of the inner Arc<Mutex<T>>.
§Example
use arc_guard::ArcGuard;
let indicator = ArcGuard::new(Indicator::new());
let inner_arc = indicator.arc();Auto Trait Implementations§
impl<T> Freeze for ArcGuard<T>
impl<T> RefUnwindSafe for ArcGuard<T>
impl<T> Send for ArcGuard<T>where
T: Send,
impl<T> Sync for ArcGuard<T>where
T: Send,
impl<T> Unpin for ArcGuard<T>
impl<T> UnwindSafe for ArcGuard<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more