ArcGuard

Struct ArcGuard 

Source
pub struct ArcGuard<T> { /* private fields */ }

Implementations§

Source§

impl<T> ArcGuard<T>

Source

pub fn new(t: T) -> Self

Constructs a new ArcGuard<T>.

§Example
use arc_guard::ArcGuard;

let indicator = ArcGuard::new(Indicator::new());
Source

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();
});
Source

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();
Source

pub fn clone(&self) -> Self

Returns new ArcGuard with a clone of the inner Arc<Mutex<T>>.

§Example
use arc_guard::ArcGuard;

let indicator = ArcGuard::new(Indicator::new());

let indicator_clone = indicator.clone();

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> 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.