pub struct DoubleBuffer { /* private fields */ }Expand description
Double-buffered render target with O(1) swap.
Maintains two pre-allocated buffers and swaps between them by flipping an index, avoiding the O(width × height) clone that a naive prev/current pattern requires.
§Invariants
- Both buffers always have the same dimensions.
swap()is O(1) — it only flips the index, never copies cells.- After
swap(),current_mut().clear()should be called to prepare the new frame buffer. resize()discards both buffers and returnstrueso callers know a full redraw is needed.
Implementations§
Source§impl DoubleBuffer
impl DoubleBuffer
Sourcepub fn new(width: u16, height: u16) -> Self
pub fn new(width: u16, height: u16) -> Self
Create a double buffer with the given dimensions.
Both buffers are initialized to default (empty) cells.
§Panics
Panics if width or height is 0.
Sourcepub fn swap(&mut self)
pub fn swap(&mut self)
O(1) swap: the current buffer becomes previous, and vice versa.
After swapping, call current_mut().clear() to prepare for the
next frame.
Sourcepub fn current_mut(&mut self) -> &mut Buffer
pub fn current_mut(&mut self) -> &mut Buffer
Mutable reference to the current (in-progress) frame buffer.
Sourcepub fn previous_mut(&mut self) -> &mut Buffer
pub fn previous_mut(&mut self) -> &mut Buffer
Mutable reference to the previous (last-presented) frame buffer.
Sourcepub fn resize(&mut self, width: u16, height: u16) -> bool
pub fn resize(&mut self, width: u16, height: u16) -> bool
Resize both buffers. Returns true if dimensions actually changed.
Both buffers are replaced with fresh allocations and the index is
reset. Callers should force a full redraw when this returns true.
Sourcepub fn dimensions_match(&self, width: u16, height: u16) -> bool
pub fn dimensions_match(&self, width: u16, height: u16) -> bool
Check whether both buffers have the given dimensions.