pub struct Synced<T> { /* private fields */ }Implementations§
Source§impl<T> Synced<T>
A thread-safe wrapper that provides synchronized access to an inner value.
impl<T> Synced<T>
A thread-safe wrapper that provides synchronized access to an inner value.
Synced<T> wraps a value of type T in an Arc<Mutex<T>>, providing
safe concurrent access across multiple threads.
Sourcepub fn with_lock<F, R>(
&self,
func: F,
) -> Result<R, PoisonError<MutexGuard<'_, T>>>
pub fn with_lock<F, R>( &self, func: F, ) -> Result<R, PoisonError<MutexGuard<'_, T>>>
Executes a function with exclusive access to the inner value.
This method acquires a lock on the internal mutex, passes a mutable reference to the inner value to the provided function, and returns the result.
§Arguments
func- A closure that takes a mutable reference toTand returnsR
§Returns
Ok(R)- The result of the function if the lock was acquired successfullyErr(PoisonError)- If the mutex was poisoned (a thread panicked while holding the lock)
§Examples
use timsrust_utils::thread::Synced;
let synced = Synced::from(vec![1, 2, 3]);
let sum = synced.with_lock(|data| {
data.iter().sum::<i32>()
}).unwrap();
assert_eq!(sum, 6);Sourcepub fn try_finalize(self) -> Option<T>
pub fn try_finalize(self) -> Option<T>
Attempts to extract the inner value, consuming the Synced wrapper.
This method will only succeed if this is the last reference to the
internal Arc and the mutex is not poisoned.
§Returns
Some(T)- The inner value if this was the lastArcreference and the mutex was not poisonedNone- If there are other references to theArcor the mutex was poisoned
§Examples
use timsrust_utils::thread::Synced;
let synced = Synced::from(42);
let value = synced.try_finalize();
assert_eq!(value, Some(42));use timsrust_utils::thread::Synced;
let synced = Synced::from(42);
let cloned = synced.clone();
// Cannot finalize because there are multiple references
let value = synced.try_finalize();
assert_eq!(value, None);Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for Synced<T>
impl<T> RefUnwindSafe for Synced<T>
impl<T> Send for Synced<T>where
T: Send,
impl<T> Sync for Synced<T>where
T: Send,
impl<T> Unpin for Synced<T>
impl<T> UnsafeUnpin for Synced<T>
impl<T> UnwindSafe for Synced<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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more