SyncWrapper

Struct SyncWrapper 

Source
#[non_exhaustive]
pub struct SyncWrapper<T> { /* private fields */ }

Implementations§

Source§

impl<T> SyncWrapper<T>
where T: Clone,

Source

pub fn new(data: T) -> Self

 use std::sync::Arc;
 use wrapit::SyncWrapper;
 let wrapper = SyncWrapper::new("java");
 assert_eq!(wrapper.get(), SyncWrapper::new("java").get());
Source

pub fn reset(&self, ndata: T) -> &Self

 use wrapit::SyncWrapper;
 let wrapper = SyncWrapper::new("java");
 let res = wrapper.reset("clojure");
 let got = SyncWrapper::new("clojure");
 assert_eq!(*got.lock(), res.get());
Source

pub fn get(&self) -> T

 use wrapit::SyncWrapper;
 use std::sync::Arc;
 let wrapper = SyncWrapper::new("java");
 assert_eq!(wrapper.get(), "java");
Source

pub fn lock(&self) -> MutexGuard<'_, T>

Acquire a lock on the underlying data.

Returns a MutexGuard, allowing mutable access.

§Panics

Panics if the mutex is poisoned (another thread panicked while holding the lock).

§Examples
use wrapit::SyncWrapper;
let w = SyncWrapper::new(vec![1, 2, 3]);
{
    let mut guard = w.lock();
    guard.push(4);
} // lock released here
assert_eq!(w.get(), vec![1, 2, 3, 4]);
Source

pub fn map<F, U>(&self, f: F) -> U
where F: FnOnce(&T) -> U,

Apply a function to the locked value without exposing the guard.

The lock is automatically released when the function returns.

§Panics

Panics if the mutex is poisoned.

§Examples
use wrapit::SyncWrapper;
let w = SyncWrapper::new(10i32);
let doubled = w.map(|x| x * 2);
assert_eq!(doubled, 20);
assert_eq!(w.get(), 10); // original unchanged

Trait Implementations§

Source§

impl<T: Clone> Clone for SyncWrapper<T>

Source§

fn clone(&self) -> SyncWrapper<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for SyncWrapper<T>
where T: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Default> Default for SyncWrapper<T>

Source§

fn default() -> SyncWrapper<T>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for SyncWrapper<T>

§

impl<T> RefUnwindSafe for SyncWrapper<T>

§

impl<T> Send for SyncWrapper<T>
where T: Send,

§

impl<T> Sync for SyncWrapper<T>
where T: Send,

§

impl<T> Unpin for SyncWrapper<T>

§

impl<T> UnwindSafe for SyncWrapper<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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.