Struct DoubleBuffer

Source
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.

§Trait implementations

If trait use an immutable reference (AsRef<T>, Deref, Borrow<T>…) give access to the current value and mutable references (AsMut<T>, DerefMut, BorrowMut<T>…) give access to the next value.

§Swapping

There are three ways to swap:

  1. DoubleBuffer::swap() - when swapping, the next value will have the previous current value.
  2. DoubleBuffer::swap_with_clone() - when swapping, the next value will keep same and will be cloned to the current value.
  3. DoubleBuffer::swap_with_default() - like DoubleBuffer::swap() but the next value will be set to the default value of the type.

Note that for the third way, the type must implement Default.

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

§Swapping performance

If it’s not important to keep the pointer address of the current value unchanged, DoubleBuffer::swap() is the best option.

Or DoubleBuffer::swap_with_default() if the type implements Default and starts with the default value is important.

Only use DoubleBuffer::swap_with_clone() if it’s important to keep the pointer address of the current value unchanged.

§Examples

The following example shows how the buffer is swapped with the three ways:

let mut buffer: DoubleBuffer<[u8; 32]> = DoubleBuffer::default();
print!("{:?}", buffer); // DoubleBuffer { current: [0, ...], next: [0, ...] }

buffer[0] = 1;
print!("{:?}", buffer); // DoubleBuffer { current: [0, ...], next: [1, ...] }

buffer.swap();
print!("{:?}", buffer); // DoubleBuffer { current: [1, ...], next: [0, ...] }

buffer[0] = 2;
print!("{:?}", buffer); // DoubleBuffer { current: [1, ...], next: [2, ...] }

buffer.swap_with_clone();
print!("{:?}", buffer); // DoubleBuffer { current: [2, ...], next: [2, ...] }

buffer[0] = 3;
print!("{:?}", buffer); // DoubleBuffer { current: [2, ...], next: [3, ...] }

buffer.swap_with_default();
print!("{:?}", buffer); // DoubleBuffer { current: [3, ...], next: [0, ...] }

Implementations§

Source§

impl<T> DoubleBuffer<T>

Source

pub const 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.

This changes the pointer address of the current value.

§Examples
let mut buffer: DoubleBuffer<[u8; 8192]> = DoubleBuffer::new([0; 8192], [0; 8192]);
let first_address = format!("{:p}", buffer);
buffer.swap();
let second_address = format!("{:p}", buffer);
// The addresses are different.
assert_ne!(first_address, second_address);
Source§

impl<T: Clone> DoubleBuffer<T>

Source

pub fn swap_with_clone(&mut self)

Clone the next value to the current value, then writes will continue over the same next value.

This let the pointer address of the current value unchanged.

§Examples
let mut buffer: DoubleBuffer<[u8; 8192]> = DoubleBuffer::new([0; 8192], [0; 8192]);
let first_address = format!("{:p}", buffer);
buffer.swap_with_clone();
let second_address = format!("{:p}", buffer);
// The addresses are different.
assert_eq!(first_address, second_address);
Source§

impl<T: Default> DoubleBuffer<T>

Source

pub fn swap_with_default(&mut self)

Swaps buffers like DoubleBuffer::swap() and sets the next value to the default value of the type, then writes will be over the default 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>

Source§

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: Ord> Ord for DoubleBuffer<T>

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

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

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

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

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

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

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

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

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

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

Source§

fn partial_cmp(&self, other: &Self) -> 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

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

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

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T> Pointer for DoubleBuffer<T>

Source§

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

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

impl<T: Eq> Eq for DoubleBuffer<T>

Auto Trait Implementations§

§

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

§

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 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.