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:
- It makes the update step in an algorithm clean and explicit.
- It allocates all memory used at initialization so the algorithm doesn’t have to allocate memory in each step.
- It plays well with the rust borrow checker.
Implementations§
Source§impl<T: Sized + Clone> UpdateBuffer<T>
impl<T: Sized + Clone> UpdateBuffer<T>
Sourcepub fn from(initial: T) -> UpdateBuffer<T>
pub fn from(initial: T) -> UpdateBuffer<T>
Creates a new update buffer with the given data written to the old and new buffers.
Sourcepub fn swap_buffers(&mut self)
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).