Skip to main content

Synced

Struct Synced 

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

Implementations§

Source§

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.

Source

pub fn with_lock<F, R>( &self, func: F, ) -> Result<R, PoisonError<MutexGuard<'_, T>>>
where F: FnOnce(&mut T) -> R,

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 to T and returns R
§Returns
  • Ok(R) - The result of the function if the lock was acquired successfully
  • Err(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);
Source

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 last Arc reference and the mutex was not poisoned
  • None - If there are other references to the Arc or 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§

Source§

impl<T> Clone for Synced<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for Synced<T>

Source§

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

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

impl<T: Default> Default for Synced<T>

Source§

fn default() -> Self

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

impl<T> From<T> for Synced<T>

Source§

fn from(value: T) -> Self

Converts to this type from the input type.

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> 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<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
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> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.