pub trait TuiModel {
// Required method
fn should_quit(&self) -> bool;
// Provided methods
fn take_pending_submit(&mut self) -> Option<String> { ... }
fn take_pending_cancel(&mut self) -> bool { ... }
fn on_cancelled(&mut self) { ... }
fn set_current_generation(&mut self, generation: u64) { ... }
}Expand description
A render/reducer model the shared event_loop/run can drive
without knowing its concrete shape.
Why: event_loop needs exactly one piece of information it cannot get
from ReplEvent alone — whether the model just decided to quit (e.g. the
user typed /quit, hit Ctrl-D, or the engine returned Ok(false) from
handle_input). run (Slice 5, DOC-50 §5) needs two more: whether
apply just staged a line to submit or a cancel signal to relay, so it
can dispatch both to TuiEngine without event_loop itself needing to
know TuiEngine exists (keeping event_loop terminal-generic and
testable against TestBackend with no engine at all — see that
function’s doc comment). Everything else about M stays opaque to this
crate; Slice 4 defines the real model (crate::app::ReplApp).
What: three methods, all defaulted so a minimal TuiModel (like this
module’s own test-only CountingModel) compiles unchanged — only
should_quit is required to mean anything for event_loop alone to
work; the pending-take methods only matter to callers that go through
run’s engine-dispatch wrapper.
Test: crate::app::ReplApp’s implementation is exercised via
crate::app::reduce::tests; this module’s own dispatch wiring is
exercised in tests::dispatch_pending below.
Required Methods§
Sourcefn should_quit(&self) -> bool
fn should_quit(&self) -> bool
Whether the render loop should exit after the current frame.
Provided Methods§
Sourcefn take_pending_submit(&mut self) -> Option<String>
fn take_pending_submit(&mut self) -> Option<String>
Take the line staged by apply for TuiEngine::handle_input, if any.
Why: apply (a synchronous FnMut(&mut M, ReplEvent)) cannot itself
call an async fn on the engine, so it stages the line on M
instead (mirrors crate::app::ReplApp::pending_submit); run
drains it right after apply returns and dispatches it.
What: a drain-on-read Option::take-shaped accessor. Default None
— a model with nothing to submit (or one not wired through run
at all) needs no override.
Sourcefn take_pending_cancel(&mut self) -> bool
fn take_pending_cancel(&mut self) -> bool
Take-and-clear the cancel signal staged by apply (Ctrl-C, or Up-
arrow while busy), if any.
Why: same shape as Self::take_pending_submit, for
TuiEngine::cancel_session instead of handle_input.
What: default false — a model with no cancellation concept needs no
override.
Sourcefn on_cancelled(&mut self)
fn on_cancelled(&mut self)
Called by run’s dispatch step immediately after a drained cancel
signal, before the cancel_session call is even dispatched.
Why: the actual TuiEngine::cancel_session RPC is async and runs on
a spawned task (so a slow/hung backend never freezes the render
loop), but the visible “busy” state should clear the moment the user
asked to cancel, not whenever the RPC eventually resolves — direct
parity with tagent’s real cancel path
(crates/trusty-agents/src/repl/tui/events.rs::process_event), which
resets thinking/busy_since synchronously, before h.abort() even
runs. DOC-50 §5 Slice 5’s “blocks user input until cancel completes”
is implemented literally, not reasoned away: crate::app::ReplApp’s
submit_line refuses a second turn while busy is true
(crate::app::ReplApp::submit_line’s doc comment), so this method
clearing busy is exactly the moment new input becomes acceptable
again — before that, Enter/Submit is a genuine no-op, not merely
cosmetically blocked. [dispatch_pending] separately bumps the
generation counter in the SAME cancel branch this method is called
from, so any output still in flight from the just-cancelled turn is
dropped rather than rendered once it eventually arrives (see that
function’s doc comment for the generation mechanism).
What: default no-op — a model with no busy/streaming state to reset
needs no override.
Sourcefn set_current_generation(&mut self, generation: u64)
fn set_current_generation(&mut self, generation: u64)
Record the generation number [dispatch_pending] just assigned to a
new turn (submit) or bumped past (cancel), so a later
ReplEvent::TurnFinished { generation } can compare against it.
Why: a prior revision had dispatch_pending’s spawned completion
task load the live AtomicU64 generation counter and compare it to
its own turn’s number before deciding whether to send
TurnFinished — a genuine TOCTOU race under multi-threaded tokio
(a cancel and a new submit could both run in the gap between that
load and the send, so a stale terminal signal from turn N could
clear busy/streaming_idx for a genuinely in-flight turn N+2).
Fixed by construction instead of narrowing the window: this method
is called from dispatch_pending ONLY on the single serial
event-loop task (the same task apply/the reducer run on, and the
same task that owns the AtomicU64 bump) — so a model tracking its
own copy of “the current generation” lets the REDUCER do the
comparison with no cross-thread race possible at all, rather than a
spawned task racing a shared counter.
What: default no-op — a model with no generation concept (or one not
wired through run) needs no override.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".