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, a copy would have to be made for each thread.

When you clone an Arc<T>, it will create another pointer to the data and increase the reference counter.

#![feature(alloc, core)] 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 }); } }
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>.

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>

Unstable

: Weak pointers may not belong in this module.

Downgrades the Arc<T> to a Weak<T> reference.

Examples

#![feature(alloc)] 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: Clone

fn make_unique(&mut self) -> &mut T

Unstable

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

#![feature(alloc)] 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 + Sync + ?Sized

impl<T> Sync for Arc<T> where T: Send + Sync + ?Sized

impl<T, U> CoerceUnsized<Arc<U>> for Arc<T> where U: ?Sized, T: Unsize<U> + ?Sized

impl<T> Clone for Arc<T> where T: ?Sized

fn clone(&self) -> Arc<T>

fn clone_from(&mut self, source: &Self)

impl<T> Deref for Arc<T> where T: ?Sized

type Target = T

fn deref(&self) -> &T

impl<T> Drop for Arc<T> where T: ?Sized

fn drop(&mut self)

impl<T> PartialEq<Arc<T>> for Arc<T> where T: PartialEq<T> + ?Sized

fn eq(&self, other: &Arc<T>) -> bool

fn ne(&self, other: &Arc<T>) -> bool

impl<T> PartialOrd<Arc<T>> for Arc<T> where T: PartialOrd<T> + ?Sized

fn partial_cmp(&self, other: &Arc<T>) -> Option<Ordering>

fn lt(&self, other: &Arc<T>) -> bool

fn le(&self, other: &Arc<T>) -> bool

fn gt(&self, other: &Arc<T>) -> bool

fn ge(&self, other: &Arc<T>) -> bool

impl<T> Ord for Arc<T> where T: Ord + ?Sized

fn cmp(&self, other: &Arc<T>) -> Ordering

impl<T> Eq for Arc<T> where T: Eq + ?Sized

impl<T> Display for Arc<T> where T: Display + ?Sized

fn fmt(&self, f: &mut Formatter) -> Result<(), Error>

impl<T> Debug for Arc<T> where T: Debug + ?Sized

fn fmt(&self, f: &mut Formatter) -> Result<(), Error>

impl<T> Pointer for Arc<T>

fn fmt(&self, f: &mut Formatter) -> Result<(), Error>

impl<T> Default for Arc<T> where T: Default

fn default() -> Arc<T>

impl<T> Hash for Arc<T> where T: Hash + ?Sized

fn hash<H>(&self, state: &mut H) where H: Hasher

fn hash_slice<H>(data: &[Self], state: &mut H) where H: Hasher

impl<T> Borrow<T> for Arc<T> where T: ?Sized

fn borrow(&self) -> &T