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 take_pending_permission_response(&mut self) -> Option<PermissionResponse> { ... }
fn on_cancel_requested(&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 take_pending_permission_response(&mut self) -> Option<PermissionResponse>
fn take_pending_permission_response(&mut self) -> Option<PermissionResponse>
Take the permission answer staged by apply for
TuiEngine::respond_permission, if any (#3422).
Why: same shape and same reason as Self::take_pending_submit — a
synchronous reducer cannot call an async engine method, and the
answer must reach the backend because only the backend can release the
suspended tool call.
What: default None — a model with no permission surface needs no
override.
Sourcefn on_cancel_requested(&mut self)
fn on_cancel_requested(&mut self)
Called by run’s dispatch step immediately after a drained cancel
signal, before the cancel RPC is even dispatched.
Why: the RPC is async and runs on a spawned task (so a slow or hung
backend never freezes the render loop), so the model needs a hook that
runs at REQUEST time — but #8207 is the proof that this hook must not
announce an outcome. An earlier revision cleared the busy state and
printed “cancelled” here; the daemon’s cancel is cooperative, so the run
was often still executing, and the prompt the reopened input accepted
came back -32003 already has a task running. DOC-50 §5 Slice 5’s
“blocks user input until cancel completes” is now literal in the strict
sense: input stays blocked until
crate::event::ReplEvent::CancelSettled reports a confirmed stop.
[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 cancelling state to enter 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".