Struct alloc::arc::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

use std::sync::Arc;

let five = Arc::new(5);

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

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

Examples

use std::sync::Arc;

let five = Arc::new(5);

let weak_five = five.downgrade();

impl<T: Send + Sync + 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

use std::sync::Arc;

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

let mut_five = five.make_unique();

Trait Implementations

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

impl<T: Sync + Send> 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

use std::sync::Arc;

let five = Arc::new(5);

five.clone();

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

impl<T> Deref for Arc<T>

type Target = T

fn deref(&self) -> &T

impl<T: Sync + Send> 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, drops the inner value.

Examples

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> PartialEq 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

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

use std::sync::Arc;

let five = Arc::new(5);

five != Arc::new(5);

impl<T: PartialOrd> PartialOrd 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

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

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

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

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

use std::sync::Arc;

let five = Arc::new(5);

five >= Arc::new(5);

impl<T: Ord> Ord for Arc<T>

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

impl<T: Eq> Eq for Arc<T>

fn assert_receiver_is_total_eq(&self)

impl<T: Display> Display for Arc<T>

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

impl<T: Debug> Debug for Arc<T>

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

impl<T: Default + Sync + Send> Default for Arc<T>

fn default() -> Arc<T>

impl<T: 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