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::Thread; fn main() { let numbers: Vec<_> = range(0, 100u32).map(|i| i as f32).collect(); let shared_numbers = Arc::new(numbers); for _ in range(0u, 10) { let child_numbers = shared_numbers.clone(); Thread::spawn(move || { let local_numbers = child_numbers.as_slice(); // Work with the local numbers }).detach(); } }
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(5i); }use std::sync::Arc; let five = Arc::new(5i);
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(5i); let weak_five = five.downgrade(); }use std::sync::Arc; let five = Arc::new(5i); let weak_five = five.downgrade();
impl<T: Clone> Arc<T>
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(5i); let mut_five = five.make_unique(); }use std::sync::Arc; let mut five = Arc::new(5i); let mut_five = five.make_unique();
Trait Implementations
impl<T> Send for Arc<T>
impl<T> Sync for Arc<T>
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(5i); five.clone(); }use std::sync::Arc; let five = Arc::new(5i); five.clone();
fn clone_from(&mut self, &Arc<T>)
impl<T> Deref<T> for Arc<T>
fn deref(&self) -> &T
impl<T> Drop for Arc<T>
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(5i); // stuff drop(five); // explict drop } { let five = Arc::new(5i); // stuff } // implicit drop }use std::sync::Arc; { let five = Arc::new(5i); // stuff drop(five); // explict drop } { let five = Arc::new(5i); // stuff } // implicit drop
impl<T: PartialEq<T>> PartialEq<Arc<T>> for Arc<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(5i); five == Arc::new(5i); }use std::sync::Arc; let five = Arc::new(5i); five == Arc::new(5i);
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(5i); five != Arc::new(5i); }use std::sync::Arc; let five = Arc::new(5i); five != Arc::new(5i);
fn ne(&self, &Arc<T>) -> bool
impl<T: PartialOrd<T>> PartialOrd<Arc<T>> for Arc<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(5i); five.partial_cmp(&Arc::new(5i)); }use std::sync::Arc; let five = Arc::new(5i); five.partial_cmp(&Arc::new(5i));
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(5i); five < Arc::new(5i); }use std::sync::Arc; let five = Arc::new(5i); five < Arc::new(5i);
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(5i); five <= Arc::new(5i); }use std::sync::Arc; let five = Arc::new(5i); five <= Arc::new(5i);
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(5i); five > Arc::new(5i); }use std::sync::Arc; let five = Arc::new(5i); five > Arc::new(5i);
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(5i); five >= Arc::new(5i); }use std::sync::Arc; let five = Arc::new(5i); five >= Arc::new(5i);