pub struct SimpleShare<T> { /* private fields */ }Expand description
Simplified version for simple types
Implementations§
Sourcepub fn new(data: T) -> Self
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]);Sourcepub fn get(&self) -> Twhere
T: Clone,
pub fn get(&self) -> Twhere
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);Sourcepub fn update<F>(&self, f: F)
pub fn update<F>(&self, f: F)
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);Sourcepub fn clone(&self) -> Self
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);
});Sourcepub fn into_arc(self) -> Arc<Mutex<T>>
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);Sourcepub fn as_arc(&self) -> Arc<Mutex<T>>
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]);