Struct 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 latest 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

👎Deprecated: Please use input_buffer_mut() instead

Access the input buffer directly

This is, for now, a deprecated alias to input_buffer_mut(). Please use this method instead.

In a future major release of this crate, input_buffer() will undergo a breaking change to instead provide read-only access.

The aim of this process is to eventually migrate towards the standard accessor()/accessor_mut() method naming convention that most Rust libraries follow.

Source

pub fn input_buffer_mut(&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 otherwise 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 (like calling the clear() method of a Vec/String).

Second, we do not send updates automatically. You need to call publish() in order to propagate a buffer update to the consumer. If you would prefer this to be done automatically when the input buffer reference goes out of scope, consider using the input_buffer_publisher() RAII interface instead.

Source

pub fn publish(&mut self) -> bool

Publish the current input buffer, checking for overwrites

After updating the input buffer in-place using input_buffer_mut(), you can use this method to publish your updates to the consumer. Beware that this will replace the current input buffer with another one that has totally unrelated contents.

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

Source

pub fn input_buffer_publisher(&mut self) -> InputPublishGuard<'_, T>

Access the input buffer wrapped in the InputPublishGuard

This is an RAII alternative to the input_buffer_mut()/publish() workflow where the publish() transaction happens automatically when the input buffer handle goes out of scope.

Please check out the documentation of input_buffer_mut() and publish() to know more about the precautions that you need to take when using the lower-level in-place buffer access interface.

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> Freeze for Input<T>

§

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

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.