Struct atomic_immut::AtomicImmut [] [src]

pub struct AtomicImmut<T> { /* fields omitted */ }

A thread-safe pointer for immutable value.

This is a thin container. Each AtomicImmut instance has an immutable value. After the AtomicImmut instance is created, it is not possible to modify a part of the contained value. But you can replace the value entirely with another value.

AtomicImmut is useful for sharing rarely updated and complex (e.g., hashmap) data structures between threads.

Examples

use std::collections::HashMap;
use std::sync::Arc;
use std::thread;
use atomic_immut::AtomicImmut;

let mut map = HashMap::new();
map.insert("foo", 0);

let v = Arc::new(AtomicImmut::new(map));
{
    let v = v.clone();
    thread::spawn(move || {
                      let mut new = (&*v.load()).clone();
                      new.insert("bar", 1);
                      v.store(new);
                  });
}
while v.load().len() == 1 {}
assert_eq!(v.load().get("foo"), Some(&0));
assert_eq!(v.load().get("bar"), Some(&1));

Methods

impl<T> AtomicImmut<T>
[src]

Makes a new AtomicImmut instance.

Loads the value from this pointer.

Examples

use atomic_immut::AtomicImmut;

let value = AtomicImmut::new(5);
assert_eq!(*value.load(), 5);

Stores a value into this pointer.

Examples

use atomic_immut::AtomicImmut;

let value = AtomicImmut::new(5);
assert_eq!(*value.load(), 5);

value.store(1);
assert_eq!(*value.load(), 1);

Stores a value into this pointer, returning the old value.

Examples

use atomic_immut::AtomicImmut;

let value = AtomicImmut::new(5);
assert_eq!(*value.load(), 5);

let old = value.swap(1);
assert_eq!(*value.load(), 1);
assert_eq!(*old, 5);

Trait Implementations

impl<T: Debug> Debug for AtomicImmut<T>
[src]

Formats the value using the given formatter.

impl<T: Send> Send for AtomicImmut<T>
[src]

impl<T: Send> Sync for AtomicImmut<T>
[src]

impl<T> Drop for AtomicImmut<T>
[src]

A method called when the value goes out of scope. Read more