Struct SimpleShare

Source
pub struct SimpleShare<T> { /* private fields */ }
Expand description

Simplified version for simple types

Implementations§

Source§

impl<T> SimpleShare<T>

Source

pub fn new(data: T) -> Self

Creates a new SimpleShare

This method creates a new SimpleShare<T> instance with the provided data. SimpleShare is a simplified version of ThreadShare without change detection.

§Arguments
  • data - The initial data to share between threads
§Returns

A new SimpleShare<T> instance containing the data.

§Example
use thread_share::SimpleShare;

let counter = SimpleShare::new(0);
let message = SimpleShare::new(String::from("Hello"));
let data = SimpleShare::new(vec![1, 2, 3]);
Source

pub fn get(&self) -> T
where T: Clone,

Gets data

This method retrieves a copy of the current data. The operation is safe but involves cloning the data.

§Requirements

The type T must implement Clone trait.

§Returns

A copy of the current data.

§Example
use thread_share::SimpleShare;

let counter = SimpleShare::new(42);
let value = counter.get();
assert_eq!(value, 42);
Source

pub fn set(&self, new_data: T)

Sets data

This method replaces the current data with new data.

§Arguments
  • new_data - The new data to set
§Example
use thread_share::SimpleShare;

let counter = SimpleShare::new(0);
counter.set(100);
assert_eq!(counter.get(), 100);
Source

pub fn update<F>(&self, f: F)
where F: FnOnce(&mut T),

Updates data

This method allows you to modify the data through a closure.

§Arguments
  • f - Closure that receives a mutable reference to the data
§Example
use thread_share::SimpleShare;

let counter = SimpleShare::new(0);

counter.update(|x| *x += 1);
assert_eq!(counter.get(), 1);

counter.update(|x| *x *= 2);
assert_eq!(counter.get(), 2);
Source

pub fn clone(&self) -> Self

Clones for use in another thread

This method creates a new SimpleShare<T> instance that shares the same underlying data. Each clone can be safely moved to different threads.

§Returns

A new SimpleShare<T> instance sharing the same data.

§Example
use thread_share::SimpleShare;
use std::thread;

let data = SimpleShare::new(0);
let clone1 = data.clone();
let clone2 = data.clone();

// Each clone can be used in different threads
thread::spawn(move || {
    clone1.set(100);
});

thread::spawn(move || {
    clone2.set(200);
});
Source

pub fn into_arc(self) -> Arc<Mutex<T>>

Gets Arc on data for transfer to thread without cloning

This method consumes the SimpleShare<T> and returns the underlying Arc<Mutex<T>>. This is useful when you need to work directly with the Arc<Mutex<T>> structure.

§Returns

The underlying Arc<Mutex<T>> containing the shared data.

§Example
use thread_share::SimpleShare;

let data = SimpleShare::new(vec![1, 2, 3]);
let arc_data = data.into_arc();

// Use the Arc<Mutex<T>> directly
let mut guard = arc_data.lock().unwrap();
guard.push(4);
drop(guard);
Source

pub fn as_arc(&self) -> Arc<Mutex<T>>

Gets Arc on data for transfer to thread without cloning (reference)

This method returns an Arc<Mutex<T>> that shares the same data as this SimpleShare<T>. This is useful when you need to work directly with the underlying Arc<Mutex<T>> structure.

§Returns

An Arc<Mutex<T>> sharing the same data.

§Example
use thread_share::SimpleShare;

let data = SimpleShare::new(vec![1, 2, 3]);
let arc_data = data.as_arc();

// Use the Arc<Mutex<T>> directly
let mut guard = arc_data.lock().unwrap();
guard.push(4);
drop(guard);

// Changes are visible in the original SimpleShare
assert_eq!(data.get(), vec![1, 2, 3, 4]);

Trait Implementations§

Source§

impl<T> Clone for SimpleShare<T>

Source§

fn clone(&self) -> Self

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> Send for SimpleShare<T>

Source§

impl<T> Sync for SimpleShare<T>

Auto Trait Implementations§

§

impl<T> Freeze for SimpleShare<T>

§

impl<T> RefUnwindSafe for SimpleShare<T>

§

impl<T> Unpin for SimpleShare<T>

§

impl<T> UnwindSafe for SimpleShare<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.