Struct triple_buffer::Input

source ·
pub struct Input<T: Send> { /* private fields */ }
Expand description

Producer interface to the triple buffer

The producer of data can use this struct to submit updates to the triple buffer whenever he likes. These updates are nonblocking: a collision between the producer and the consumer will result in cache contention, but deadlocks and scheduling-induced slowdowns cannot happen.

Implementations§

source§

impl<T: Send> Input<T>

source

pub fn write(&mut self, value: T)

Write a new value into the triple buffer

source

pub fn consumed(&self) -> bool

Check if the consumer has fetched our last submission yet

This method is only intended for diagnostics purposes. Please do not let it inform your decision of sending or not sending a value, as that would effectively be building a very poor spinlock-based double buffer implementation. If what you truly need is a double buffer, build yourself a proper blocking one instead of wasting CPU time.

source

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

Access the input buffer directly

This advanced interface allows you to update the input buffer in place, so that you can avoid creating values of type T repeatedy just to push them into the triple buffer when doing so is expensive.

However, by using it, you force yourself to take into account some implementation subtleties that you could normally ignore.

First, the buffer does not contain the last value that you published (which is now available to the consumer thread). In fact, what you get may not match any value that you sent in the past, but rather be a new value that was written in there by the consumer thread. All you can safely assume is that the buffer contains a valid value of type T, which you may need to “clean up” before use using a type-specific process.

Second, we do not send updates automatically. You need to call publish() in order to propagate a buffer update to the consumer. Alternative designs based on Drop were considered, but considered too magical for the target audience of this interface.

source

pub fn publish(&mut self) -> bool

Publish the current input buffer, checking for overwrites

After updating the input buffer using input_buffer(), you can use this method to publish your updates to the consumer.

This will replace the current input buffer with another one, as you cannot continue using the old one while the consumer is accessing it.

It will also tell you whether you overwrote a value which was not read by the consumer thread.

Trait Implementations§

source§

impl<T: Debug + Send> Debug for Input<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for Input<T>

§

impl<T> Send for Input<T>

§

impl<T> Sync for Input<T>

§

impl<T> Unpin for Input<T>

§

impl<T> !UnwindSafe for Input<T>

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>,

§

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>,

§

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.