Struct DoubleBuffer

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

Implementations§

Source§

impl<T> DoubleBuffer<T>

An implementation of the Double Buffer pattern from ‘Game Programming Patterns’ by Robert Nystrom.

In a simulation, you often have to do a lot of processing to prepare the next “frame”, but if you’re iterating through the current-state data while mutating it, things can slip. The Double Buffer design pattern solves this by keeping two copies of the simulation state (or any variable): the “current” (or previous) state which is immutable, and the “next” (or future) state which is being prepared. When a turn of the simulation is completed, you simply switch the buffers.

Unlike other implementations on crates.io, this one wraps both buffers in std::cell::RefCell so that it is possible to borrow one buffer as mutable at the same time the other is borrowed as immutable – a typical use case is to iterate over objects in the current state and write updated versions of them to the next state.

Source

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

Source

pub fn current(&self) -> Ref<'_, T>

Get an immutable reference to the current-state buffer.

Source

pub fn next(&self) -> RefMut<'_, T>

Get a mutable reference to the next-state buffer.

Source

pub fn next_immut(&self) -> Ref<'_, T>

Get an immutable reference to the next-state buffer.

Source

pub fn switch(&mut self)

Switch the “current” and “next” buffers.

Auto Trait Implementations§

§

impl<T> !Freeze for DoubleBuffer<T>

§

impl<T> !RefUnwindSafe for DoubleBuffer<T>

§

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

§

impl<T> !Sync for DoubleBuffer<T>

§

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