Skip to main content

Module cx

Module cx 

Source
Expand description

Capability context (Cx) for cooperative cancellation and deadline propagation.

Cx is a lightweight handle threaded through every async operation, I/O call, and timer in the FrankenTUI stack. It enables:

  • Cooperative cancellation: Any holder can check cx.is_cancelled() and bail out early.
  • Deadline propagation: A parent context’s deadline flows to children. cx.deadline() returns the tightest deadline in the chain.
  • Deterministic testing via Lab: In Lab mode, time is controlled externally, enabling fully reproducible test runs.

§Design

Cx is cheaply cloneable (Arc inside) and immutable from the outside. To cancel or advance Lab time, hold the companion CxController.

§Tracing

When the tracing feature is active, cancellation emits a WARN-level event and deadline checks emit TRACE-level spans with cx_id and deadline_remaining_us fields.

§Example

use ftui_core::cx::{Cx, CxController};
use web_time::Duration;

// Create a root context with a 500ms deadline.
let (cx, ctrl) = Cx::with_deadline(Duration::from_millis(500));
assert!(!cx.is_cancelled());
assert!(cx.deadline().is_some());

// Cancel it.
ctrl.cancel();
assert!(cx.is_cancelled());

Structs§

Cx
Capability context handle.
CxController
Control handle for a Cx.
LabClock
A manually-advanceable clock for deterministic tests.

Enums§

CxError
Error returned when an operation is cancelled or times out via Cx.

Functions§

cx_cancellations_total
Read the total cancellation count (for diagnostics/telemetry).