Struct std::sync::Arc
[−]
[src]
pub struct Arc<T> where T: ?Sized {
// some fields omitted
}
An atomically reference counted wrapper for shared state.
Examples
In this example, a large vector of floats is shared between several threads.
With simple pipes, without Arc
`Arc`, a copy would have to be made for each
thread.
When you clone an Arc<T>
`Arc
use std::sync::Arc; use std::thread; fn main() { let numbers: Vec<_> = (0..100u32).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[..]; // Work with the local numbers }); } }
Methods
impl<T> Arc<T>
fn new(data: T) -> Arc<T>
Constructs a new Arc<T>
`Arc
Examples
fn main() { use std::sync::Arc; let five = Arc::new(5); }use std::sync::Arc; let five = Arc::new(5);
impl<T> Arc<T> where T: ?Sized
fn downgrade(&self) -> Weak<T>
: Weak pointers may not belong in this module.
Downgrades the Arc<T>
`Arcto a
` to a Weak<T>
`Weak
Examples
#![feature(arc_weak)] fn main() { use std::sync::Arc; let five = Arc::new(5); let weak_five = five.downgrade(); }#![feature(arc_weak)] use std::sync::Arc; let five = Arc::new(5); let weak_five = five.downgrade();
fn weak_count(this: &Arc<T>) -> usize
Get the number of weak references to this value.
fn strong_count(this: &Arc<T>) -> usize
Get the number of strong references to this value.
impl<T> Arc<T> where T: Clone
fn make_unique(this: &mut Arc<T>) -> &mut T
Make a mutable reference from the given Arc<T>
`Arc
This is also referred to as a copy-on-write operation because the inner data is cloned if the (strong) reference count is greater than one. If we hold the only strong reference, any existing weak references will no longer be upgradeable.
Examples
#![feature(arc_unique)] fn main() { use std::sync::Arc; let mut five = Arc::new(5); let mut_five = Arc::make_unique(&mut five); }#![feature(arc_unique)] use std::sync::Arc; let mut five = Arc::new(5); let mut_five = Arc::make_unique(&mut five);
impl<T> Arc<T> where T: ?Sized
fn get_mut(this: &mut Arc<T>) -> Option<&mut T>
Returns a mutable reference to the contained value if the Arc<T>
`Arc
Returns None
`Noneif the
` if the Arc<T>
`Arc
Examples
#![feature(arc_unique, alloc)] extern crate alloc; fn main() { use alloc::arc::Arc; let mut x = Arc::new(3); *Arc::get_mut(&mut x).unwrap() = 4; assert_eq!(*x, 4); let _y = x.clone(); assert!(Arc::get_mut(&mut x).is_none()); }#![feature(arc_unique, alloc)] extern crate alloc; use alloc::arc::Arc; let mut x = Arc::new(3); *Arc::get_mut(&mut x).unwrap() = 4; assert_eq!(*x, 4); let _y = x.clone(); assert!(Arc::get_mut(&mut x).is_none());