Struct std::sync::ArcStable
[-] [+]
[src]
pub struct Arc<T> { // some fields omitted }
An atomically reference counted wrapper for shared state.
Example
In this example, a large vector of floats is shared between several tasks.
With simple pipes, without Arc
, a copy would have to be made for each
task.
use std::sync::Arc; use std::thread; fn main() { let numbers: Vec<_> = (0..100u32).map(|i| i as f32).collect(); let shared_numbers = Arc::new(numbers); for _ in 0..10 { let child_numbers = shared_numbers.clone(); thread::spawn(move || { let local_numbers = child_numbers.as_slice(); // Work with the local numbers }); } }
Methods
impl<T> Arc<T>
fn new(data: T) -> Arc<T>
Constructs a new Arc<T>
.
Examples
fn main() { use std::sync::Arc; let five = Arc::new(5); }use std::sync::Arc; let five = Arc::new(5);
fn downgrade(&self) -> Weak<T>
Downgrades the Arc<T>
to a Weak<T>
reference.
Examples
fn main() { use std::sync::Arc; let five = Arc::new(5); let weak_five = five.downgrade(); }use std::sync::Arc; let five = Arc::new(5); let weak_five = five.downgrade();
impl<T> Arc<T> where T: Send, T: Sync, T: Clone
fn make_unique(&mut self) -> &mut T
Make a mutable reference from the given Arc<T>
.
This is also referred to as a copy-on-write operation because the inner data is cloned if the reference count is greater than one.
Examples
fn main() { use std::sync::Arc; let mut five = Arc::new(5); let mut_five = five.make_unique(); }use std::sync::Arc; let mut five = Arc::new(5); let mut_five = five.make_unique();
Trait Implementations
impl<T> Send for Arc<T> where T: Send, T: Sync
impl<T> Sync for Arc<T> where T: Send, T: Sync
impl<T> Clone for Arc<T>
fn clone(&self) -> Arc<T>
Makes a clone of the Arc<T>
.
This increases the strong reference count.
Examples
fn main() { use std::sync::Arc; let five = Arc::new(5); five.clone(); }use std::sync::Arc; let five = Arc::new(5); five.clone();
fn clone_from(&mut self, &Arc<T>)
impl<T> Deref for Arc<T>
type Target = T
fn deref(&self) -> &T
impl<T> Drop for Arc<T> where T: Send, T: Sync
fn drop(&mut self)
Drops the Arc<T>
.
This will decrement the strong reference count. If the strong reference count becomes zero
and the only other references are Weak<T>
ones, drop
s the inner value.
Examples
fn main() { use std::sync::Arc; { let five = Arc::new(5); // stuff drop(five); // explicit drop } { let five = Arc::new(5); // stuff } // implicit drop }use std::sync::Arc; { let five = Arc::new(5); // stuff drop(five); // explicit drop } { let five = Arc::new(5); // stuff } // implicit drop
impl<T> PartialEq<Arc<T>> for Arc<T> where T: PartialEq<T>
fn eq(&self, other: &Arc<T>) -> bool
Equality for two Arc<T>
s.
Two Arc<T>
s are equal if their inner value are equal.
Examples
fn main() { use std::sync::Arc; let five = Arc::new(5); five == Arc::new(5); }use std::sync::Arc; let five = Arc::new(5); five == Arc::new(5);
fn ne(&self, other: &Arc<T>) -> bool
Inequality for two Arc<T>
s.
Two Arc<T>
s are unequal if their inner value are unequal.
Examples
fn main() { use std::sync::Arc; let five = Arc::new(5); five != Arc::new(5); }use std::sync::Arc; let five = Arc::new(5); five != Arc::new(5);
fn ne(&self, &Arc<T>) -> bool
impl<T> PartialOrd<Arc<T>> for Arc<T> where T: PartialOrd<T>
fn partial_cmp(&self, other: &Arc<T>) -> Option<Ordering>
Partial comparison for two Arc<T>
s.
The two are compared by calling partial_cmp()
on their inner values.
Examples
fn main() { use std::sync::Arc; let five = Arc::new(5); five.partial_cmp(&Arc::new(5)); }use std::sync::Arc; let five = Arc::new(5); five.partial_cmp(&Arc::new(5));
fn lt(&self, other: &Arc<T>) -> bool
Less-than comparison for two Arc<T>
s.
The two are compared by calling <
on their inner values.
Examples
fn main() { use std::sync::Arc; let five = Arc::new(5); five < Arc::new(5); }use std::sync::Arc; let five = Arc::new(5); five < Arc::new(5);
fn le(&self, other: &Arc<T>) -> bool
'Less-than or equal to' comparison for two Arc<T>
s.
The two are compared by calling <=
on their inner values.
Examples
fn main() { use std::sync::Arc; let five = Arc::new(5); five <= Arc::new(5); }use std::sync::Arc; let five = Arc::new(5); five <= Arc::new(5);
fn gt(&self, other: &Arc<T>) -> bool
Greater-than comparison for two Arc<T>
s.
The two are compared by calling >
on their inner values.
Examples
fn main() { use std::sync::Arc; let five = Arc::new(5); five > Arc::new(5); }use std::sync::Arc; let five = Arc::new(5); five > Arc::new(5);
fn ge(&self, other: &Arc<T>) -> bool
'Greater-than or equal to' comparison for two Arc<T>
s.
The two are compared by calling >=
on their inner values.
Examples
fn main() { use std::sync::Arc; let five = Arc::new(5); five >= Arc::new(5); }use std::sync::Arc; let five = Arc::new(5); five >= Arc::new(5);