Struct alloc::arc::Arc [] [src]

pub struct Arc<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.

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

use std::sync::Arc;

let five = Arc::new(5);

impl<T: ?Sized> Arc<T>

fn downgrade(&self) -> Weak<T>

Unstable (arc_weak #27718)

: Weak pointers may not belong in this module.

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

Examples

#![feature(arc_weak)]

use std::sync::Arc;

let five = Arc::new(5);

let weak_five = five.downgrade();

fn weak_count(this: &Arc<T>) -> usize

Unstable (arc_counts #27718)

Get the number of weak references to this value.

fn strong_count(this: &Arc<T>) -> usize

Unstable (arc_counts #27718)

Get the number of strong references to this value.

impl<T: Clone> Arc<T>

fn make_unique(this: &mut Arc<T>) -> &mut T

Unstable (arc_unique #27718)

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 (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)]

use std::sync::Arc;

let mut five = Arc::new(5);

let mut_five = Arc::make_unique(&mut five);

impl<T: ?Sized> Arc<T>

fn get_mut(this: &mut Arc<T>) -> Option<&mut T>

Unstable (arc_unique #27718)

Returns a mutable reference to the contained value if the Arc<T> is unique.

Returns None if the Arc<T> is not unique.

Examples

#![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());

Trait Implementations

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

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

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

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

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

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

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

type Target = T

fn deref(&self) -> &T

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

fn drop(&mut self)

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

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

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

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

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: ?Sized + Ord> Ord for Arc<T>

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

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

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

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

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

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

impl<T> Pointer for Arc<T>

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

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

fn default() -> Arc<T>

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

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

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