#[non_exhaustive]pub struct SyncWrapper<T> { /* private fields */ }Implementations§
Source§impl<T> SyncWrapper<T>where
T: Clone,
impl<T> SyncWrapper<T>where
T: Clone,
Sourcepub fn new(data: T) -> Self
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());Sourcepub fn reset(&self, ndata: T) -> &Self
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());Sourcepub fn get(&self) -> T
pub fn get(&self) -> T
use wrapit::SyncWrapper;
use std::sync::Arc;
let wrapper = SyncWrapper::new("java");
assert_eq!(wrapper.get(), "java");Sourcepub fn lock(&self) -> MutexGuard<'_, T>
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]);Sourcepub fn map<F, U>(&self, f: F) -> U
pub fn map<F, U>(&self, f: F) -> 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 unchangedTrait Implementations§
Source§impl<T: Clone> Clone for SyncWrapper<T>
impl<T: Clone> Clone for SyncWrapper<T>
Source§fn clone(&self) -> SyncWrapper<T>
fn clone(&self) -> SyncWrapper<T>
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl<T> Debug for SyncWrapper<T>where
T: Debug,
impl<T> Debug for SyncWrapper<T>where
T: Debug,
Source§impl<T: Default> Default for SyncWrapper<T>
impl<T: Default> Default for SyncWrapper<T>
Source§fn default() -> SyncWrapper<T>
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> 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