Skip to main content

DoubleBuffer

Struct DoubleBuffer 

Source
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

  1. Both buffers always have the same dimensions.
  2. swap() is O(1) — it only flips the index, never copies cells.
  3. After swap(), current_mut().clear() should be called to prepare the new frame buffer.
  4. resize() discards both buffers and returns true so callers know a full redraw is needed.

Implementations§

Source§

impl DoubleBuffer

Source

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.

Source

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.

Source

pub fn current(&self) -> &Buffer

Reference to the current (in-progress) frame buffer.

Source

pub fn current_mut(&mut self) -> &mut Buffer

Mutable reference to the current (in-progress) frame buffer.

Source

pub fn previous(&self) -> &Buffer

Reference to the previous (last-presented) frame buffer.

Source

pub fn previous_mut(&mut self) -> &mut Buffer

Mutable reference to the previous (last-presented) frame buffer.

Source

pub fn width(&self) -> u16

Width of both buffers.

Source

pub fn height(&self) -> u16

Height of both buffers.

Source

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.

Source

pub fn dimensions_match(&self, width: u16, height: u16) -> bool

Check whether both buffers have the given dimensions.

Trait Implementations§

Source§

impl Debug for DoubleBuffer

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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.