tiny.mprompt.SuspendedRun
Defined in zig_api.
Returns a struct type that holds a context pointer, storage for the body's result, and a done marker, for a caller that starts a body that may pause and hand control back, then continues it later from its own loop, as a scheduler does with workers or the Chic host does with an interpreter turn.
Source
Source: lib/mprompt/src/api.zig:184
zig
/// Returns a struct type that holds a context pointer, storage for the body's result, and a done/// marker, for a caller that starts a body that may pause and hand control back, then continues it/// later from its own loop, as a scheduler does with workers or the Chic host does with an/// interpreter turn. `Context` must be a pointer type, and any other type is a compile error./// `init(context)` builds the value, and `start(body)` runs `body(prompt, context)` on a new/// stacklet and returns a `PromptOutcome`. The body pauses with `suspendPrompt`, and the pause/// comes back to the caller as `.suspended`. The value must stay at one address from `start` until/// the body finishes or its last handle is dropped, because every handle points to its result/// storage and done marker. A caller may call `start` again after the previous run finished, as the/// scheduler test does with one value per worker.pub fn SuspendedRun( comptime ResumeValue: type, comptime Result: type, comptime Context: type,) type { requirePointer(Context, "SuspendedRun context"); return struct { /// The pointer passed to `init`, which `start` hands to the body. context: Context, /// Storage where the body's wrapper writes the body's result when the body finishes. A /// handle from this run reads the result from it. The default is empty storage, whose value /// stays undefined until the body returns. result: Slot(Result) = .{}, /// The done marker: a byte whose address the body's wrapper returns when the body finishes. /// `start` and each handle compare the runtime's result with this address to tell a /// finished body from a pause. Only its address is used, and its value stays at the /// default 0. done_marker: u8 = 0, const Self = @This(); /// Names the `PromptOutcome` type for this run's `ResumeValue` and `Result`, which `start` /// returns. pub const Outcome: type = PromptOutcome(ResumeValue, Result); /// Returns a run holding `context`, with empty result storage and the done marker at 0, for /// a caller that builds the run once before starting the body. The call allocates nothing /// and starts nothing. pub fn init(context: Context) Self { return .{ .context = context }; } /// Creates a prompt with a new stacklet and runs `body(prompt, context)` on it, for a /// caller that begins the body and learns whether it finished at once or paused. The call /// returns `.returned` with the body's result when the body finishes without pausing, or /// `.suspended` with a handle when it pauses through `suspendPrompt`. `self` must stay at /// the same address while any handle from this run is in use. pub fn start( self: *Self, comptime body: *const fn (*Prompt, Context) Result, ) Outcome { const Callback: type = SuspendedRunStartCallbackType(Self, Result, body); const raw_result = raw.prompt(Callback.run, self); return outcomeFromRaw( ResumeValue, Result, raw_result, &self.done_marker, &self.result, ); } };}Source: lib/mprompt/src/root.zig:61
zig
pub const SuspendedRun = zig_api.SuspendedRun;Audit
| Definitions | 1 |
|---|---|
| Public names | 2 |
| Members | 0 |
| Version | 26.7.0 |
| Revision | daab053ee433 |