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

Encapsulates a piece of state that can be modified and we want all outside code to see the edit as a single atomic change.

There are two ways to swap:

  1. DoubleBuffer::swap() - when swapping, the next value will have the previous current value.
  2. DoubleBuffer::swap_cloning() - when swapping, the next value will keep same and will be cloned to the current value.

You can read about the two ways how the buffers are swapped in “Game Programming Patterns” by Robert Nystrom.

Examples

use double_buffer::DoubleBuffer;

let mut buffer: DoubleBuffer<u32> = DoubleBuffer::default();
*buffer = 1;

assert_eq!(buffer, 0);
buffer.swap();
assert_eq!(buffer, 1);

Implementations§

source§

impl<T> DoubleBuffer<T>

source

pub fn new(current: T, next: T) -> Self

source

pub fn swap(&mut self)

Swaps the current and next values, then writes will be over the previous current value.

source§

impl<T: Clone> DoubleBuffer<T>

source

pub fn swap_cloning(&mut self)

Swaps buffers cloning the next value to the current value, then writes will continue over the same next value.

Trait Implementations§

source§

impl<T> AsMut<T> for DoubleBuffer<T>

source§

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

Converts this type into a mutable reference of the (usually inferred) input type.
source§

impl<T> AsRef<T> for DoubleBuffer<T>

source§

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl<T> Borrow<T> for DoubleBuffer<T>

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for DoubleBuffer<T>

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T: Debug> Debug for DoubleBuffer<T>

source§

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

Formats the value using the given formatter. Read more
source§

impl<T: Default> Default for DoubleBuffer<T>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<T> Deref for DoubleBuffer<T>

§

type Target = T

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl<T> DerefMut for DoubleBuffer<T>

source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
source§

impl<T: PartialEq> PartialEq<T> for DoubleBuffer<T>

source§

fn eq(&self, other: &T) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T: PartialOrd> PartialOrd<T> for DoubleBuffer<T>

source§

fn partial_cmp(&self, other: &T) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Auto Trait Implementations§

§

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

§

impl<T> Send for DoubleBuffer<T>where T: Send,

§

impl<T> Sync for DoubleBuffer<T>where T: Sync,

§

impl<T> Unpin for DoubleBuffer<T>where T: Unpin,

§

impl<T> UnwindSafe for DoubleBuffer<T>where T: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere 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 Twhere 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.