new_with

Function new_with 

Source
pub fn new_with<T>(
    init: T,
    make_buf: impl FnMut(&T) -> T + 'static + Send,
) -> (Writer<T>, Reader<T>)
Expand description

Create a new buffer pair that creates additional buffer instances with a custom clone function.

The number of copies of T will reach a steady state around 2-4.

Examples found in repository?
examples/counter.rs (lines 11-14)
10fn main() {
11    let (mut w, mut r) = new_with(State { v: 0 }, |s| {
12        println!("Cloned state!");
13        s.clone()
14    });
15
16    let tw = std::thread::spawn(move || loop {
17        w.write_new(|last, new| {
18            new.v = last.v + 1;
19        });
20    });
21
22    let tr = std::thread::spawn(move || {
23        let mut last = 0;
24        loop {
25            let state = r.read_newest();
26            println!("Value: {} (+{})", state.v, state.v - last);
27            last = state.v;
28            std::thread::sleep(Duration::from_millis(20));
29        }
30    });
31
32    tw.join().unwrap();
33    tr.join().unwrap();
34}