Skip to main content

Input

Trait Input 

Source
pub trait Input {
    // Required method
    fn poll_event(&mut self, timeout: Duration) -> Option<Event>;

    // Provided method
    fn push_event(&mut self, _event: Event) { ... }
}
Expand description

Polls for and accepts input events.

Backends that never receive events from outside their own poll_event implementation (e.g. Crossterm, which reads its own event stream) can use the default no-op push_event via an empty impl Input for X {}.

§Examples

use core::time::Duration;
use retroglyph_core::backend::Input;
use retroglyph_core::event::Event;
use std::collections::VecDeque;

struct QueuedInput(VecDeque<Event>);

impl Input for QueuedInput {
    fn poll_event(&mut self, _timeout: Duration) -> Option<Event> {
        self.0.pop_front()
    }

    fn push_event(&mut self, event: Event) {
        self.0.push_back(event);
    }
}

Required Methods§

Source

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

Poll for an input event, waiting up to timeout.

Provided Methods§

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: also queues events pushed here (a test harness injecting synthetic input, or a driver handing back an event it already read off the real terminal); its own poll_event still reads the real terminal’s stream separately.

Coalescing a burst of consecutive Event::Mouse(MouseEventKind::Moved) pushes down to the latest one is not required of every implementation, only of backends whose events can arrive at device-polling rate from an external source (a windowed backend’s motion events, for example); see retroglyph_core::testing::conformance::assert_input_contract for the harness that pins it, and that module’s doc for which backends it binds.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§