Skip to main content

Backend

Trait Backend 

Source
pub trait Backend {
    // Required methods
    fn draw<'a, I>(&mut self, content: I)
       where I: Iterator<Item = (Pos, &'a Tile)>;
    fn flush(&mut self);
    fn size(&self) -> Size;
    fn clear(&mut self);
    fn poll_event(&mut self, timeout: Duration) -> Option<Event>;
    fn set_cursor_visible(&mut self, visible: bool);
    fn set_cursor_position(&mut self, position: Pos);

    // Provided methods
    fn draw_layers<'a, I>(&mut self, content: I)
       where I: Iterator<Item = (u8, Pos, &'a Tile)> { ... }
    fn needs_full_frame(&self) -> bool { ... }
    fn resize(&mut self, size: Size) { ... }
    fn is_connected(&self) -> bool { ... }
    fn push_event(&mut self, _event: Event) { ... }
}
Expand description

A rendering backend that presents grid content to a display and provides input events.

Required Methods§

Source

fn draw<'a, I>(&mut self, content: I)
where I: Iterator<Item = (Pos, &'a Tile)>,

Draw changed cells to the output surface.

Source

fn flush(&mut self)

Flush buffered output to the display.

Source

fn size(&self) -> Size

Return current display dimensions.

Source

fn clear(&mut self)

Clear the entire display.

Source

fn poll_event(&mut self, timeout: Duration) -> Option<Event>

Poll for an input event, waiting up to timeout.

Source

fn set_cursor_visible(&mut self, visible: bool)

Show or hide the cursor.

Source

fn set_cursor_position(&mut self, position: Pos)

Move the cursor to a position.

Provided Methods§

Source

fn draw_layers<'a, I>(&mut self, content: I)
where I: Iterator<Item = (u8, Pos, &'a Tile)>,

Draw changed cells across all layers.

The default implementation forwards layer-0 tiles to draw and ignores higher layers. Override this to support multi-layer compositing, sub-cell offsets, or transparency.

When needs_full_frame returns true, this receives all cells from every allocated layer, and the backend should clear its output surface before drawing.

Source

fn needs_full_frame(&self) -> bool

Returns true if the backend needs the entire frame (all cells on all layers) on every call to draw_layers, rather than just the changed cells.

Pixel-based backends (e.g. SoftwareRenderer) need this because sub-cell offsets can spill glyph pixels into adjacent cells — without a full redraw, orphaned pixels from the previous frame linger.

The default implementation returns false.

Source

fn resize(&mut self, size: Size)

Notify the backend of a terminal resize.

Called automatically by crate::Terminal::resize after both grids are resized. Backends that maintain internal state tied to terminal dimensions (such as Headless) should override this to update that state. The default implementation is a no-op.

Source

fn is_connected(&self) -> bool

Returns false if the backend has been disconnected from its output (e.g. the window was closed). The game loop should terminate when this returns false.

The default implementation always returns true. Override for backends that can detect disconnect.

Source

fn push_event(&mut self, _event: Event)

Push an event into the backend’s event buffer.

Backends that receive events externally (e.g., from a window event loop or a test harness) override this to queue events for poll_event. The default is a no-op.

  • Windowed backends: called by ApplicationHandler on each event.
  • Headless: called by tests to inject synthetic events.
  • Crossterm: reads from its own event stream; no-op here.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§