Struct UpdateBuffer

Source
pub struct UpdateBuffer<T: Sized + Clone> { /* private fields */ }
Expand description

Data structure which uses a buffer to perform buffered updates to data.

Explicitly, this structure keeps two copies of some generic data type, which can be accesssed by the methods old() and new(). The idea is that during the course of an algorithm, you read from the old values and write to the new values. Then after the step is finished, you call swap_buffer such that old now points towards new.

Since you read from old and write to new, the methods old() and new() return &T and &mut T respectively. This prevents any borrowing conflicts.

This data structure provides three main benefits:

  1. It makes the update step in an algorithm clean and explicit.
  2. It allocates all memory used at initialization so the algorithm doesn’t have to allocate memory in each step.
  3. It plays well with the rust borrow checker.

Implementations§

Source§

impl<T: Sized + Clone> UpdateBuffer<T>

Source

pub fn from(initial: T) -> UpdateBuffer<T>

Creates a new update buffer with the given data written to the old and new buffers.

Source

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

Get mutable access to the new values in the buffer.

Source

pub fn old(&self) -> &T

Get immutable access to the old values in the buffer.

Source

pub fn swap_buffers(&mut self)

Swap the new and the old buffers (i.e. set the new updated data to be the old data).

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<T> UnwindSafe for UpdateBuffer<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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.