Module dependent_view::arc[][src]

Module defining DependentArc, a wrapper around the Arc type.

This module both defines the DependentArc struct, as well as the corresponding to_view_sync! macro, which can be used to obtain thread safe views from an instance of DependentArc.

Examples

let mut dancers : Vec<DependentArc<Dancer>> = Vec::new();
let mut prancers : Vec<DependentArc<Prancer>> = Vec::new();
 
let mut dance_refs : Vec<Weak<Dance>> = Vec::new();
let mut prance_refs : Vec<Weak<Prance>> = Vec::new();
 
for i in 0..10 {
    let mut dancer = DependentArc::new(Dancer { id: i });
    let mut prancer = DependentArc::new(Prancer { id: i+10 });
 
    dance_refs.push(to_view_sync!(dancer));
    prance_refs.push(to_view_sync!(prancer));
 
    dancers.push(dancer);
    prancers.push(prancer);
}
 
// owning thread
let h1 = thread::spawn(move|| {
        let mut count = dancers.len() + prancers.len();
        let mut seed = 13;
        while count > 0 {
            // drop a random reference
            count -= 1;
            println!("\nRemaining items {:?}", count);
        }
});
 
let h2 = thread::spawn(move || {
    loop {
 
        for (dancer_ref, prancer_ref) in dance_refs.iter().zip(prance_refs.iter()) {
            if let Some(dref) = dancer_ref.upgrade() {
                dref.dance();
            }
            if let Some(pref) = prancer_ref.upgrade() {
                pref.prance();
            }
        }
    }
});
 
h1.join();
h2.join();

Structs

DependentArc

DependentArc<T> is a simple wrapper around the Arc<T> type, imbuing it with the capability to provide thread safe "views" (Weak<Trait>) of non-owned structs to separate components of a system.