pub struct Writer<T> { /* private fields */ }
Expand description
Write side of the triple buffer.
Implementations§
Source§impl<T> Writer<T>
impl<T> Writer<T>
Sourcepub fn write_new(&mut self, write_op: impl FnMut(&T, &mut T))
pub fn write_new(&mut self, write_op: impl FnMut(&T, &mut T))
Write the next state into the buffer.
The closure takes two arguments:
- The first is a reference to the previous state.
- The second is a mutable reference to some unspecified
T
value that should be overwritten with the new state.
The Reader
is not blocked while this function runs.
It is possible for multiple independent reads to happen
while a single write is in process.
§Example
let (mut writer, mut reader) = simple_triple_buffer::new_clone(0);
writer.write_new(|old, new| *new = *old + 1);
assert_eq!(*reader.read_newest(), 1);
Examples found in repository?
examples/counter.rs (lines 17-19)
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}
Auto Trait Implementations§
impl<T> Freeze for Writer<T>
impl<T> !RefUnwindSafe for Writer<T>
impl<T> Send for Writer<T>
impl<T> !Sync for Writer<T>
impl<T> Unpin for Writer<T>
impl<T> !UnwindSafe for Writer<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more