Struct Reader

Source
pub struct Reader<T> { /* private fields */ }
Expand description

Read side of the triple buffer.

Implementations§

Source§

impl<T> Reader<T>

Source

pub fn read_newest(&mut self) -> &T

Get a view to the newest state currently in the buffer.

The Writer is not blocked while the returned borrow is held, but any new written data will only be visible by calling this method again.

It is possible for multiple write updates to happen while a single read is in process.

§Example
let (mut writer, mut reader) = simple_triple_buffer::new_clone(0);

let guard = reader.read_newest();
assert_eq!(*guard, 0);

writer.write_new(|old, new| *new = *old + 1);
assert_eq!(*guard, 0);

let guard = reader.read_newest();
assert_eq!(*guard, 1);
Examples found in repository?
examples/counter.rs (line 25)
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 Reader<T>

§

impl<T> RefUnwindSafe for Reader<T>
where T: RefUnwindSafe,

§

impl<T> Send for Reader<T>
where T: Sync + Send,

§

impl<T> Sync for Reader<T>
where T: Sync + Send,

§

impl<T> Unpin for Reader<T>

§

impl<T> UnwindSafe for Reader<T>
where T: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.