Status: Accepted
Date: November 28, 2026 (owner, #2834)
Answers: how a program reads a file, a pipe or a connection too large to
hold in memory as a stream of items that view the bytes they were parsed from,
with no copy and no failure at run time; how ADR-grammar-typed-read-v2 D7’s
“an item views the input” and “an item may be emitted” hold for such an input
(#2834)
Supersedes / Related: ADR-tether-v1 (a view that
escapes keeps its buffer alive, by frame or by count; this adds a buffer that
may not be kept without a bound), D15 there (a held view is checked against the
buffer the plan names); ADR-grammar-typed-read-v2
D7 (a @stream entry over a stream); ADR-loop-failure-v1
D4 (io::lines() yields views); ADR-149 (a channel is only
bounded, send pauses); ADR-async-v7 D2 (what a task takes);
ADR-037 D3 (a count per build); ADR-views-v4
D5 (a view is inferred, never written)
A program in a container reads a YAML or JSON stream of many gigabytes from standard input and handles it event by event. It should hold a fixed amount of memory, copy no text (an event’s scalar is a view of the bytes it was read from), and never fail or hang because of how it holds what it read. A view keeps its buffer alive (ADR-tether-v1); a stream cannot keep every buffer it read. Which views of a stream’s bytes may live how long, and who stops the reading while they do?
| Who | How | Cost |
|---|---|---|
Netty (Java), bytes::Bytes (Rust) |
pooled buffers with a reference count; a slice keeps its buffer | a count per slice; a held slice pins its buffer silently |
| quick-xml, serde | borrowed events only from a slice in memory; a stream reader hands out owned events | a copy per event on the stream path |
| SAX (XML) | text handed over in pieces; the handler copies what it keeps | no bound on what the handler keeps |
| LMAX Disruptor | a ring of slots and a cursor per consumer; the producer waits on the slowest cursor | one consumer order, no counts |
| Perceus (Koka, Lean 4; Reinking, Xie, de Moura, Leijen, PLDI 2021) | precise counts: borrow without a count, fuse a dup with a drop, drop at the last use, reuse a freed cell, atomic only where shared | the analysis of ownership the compiler already does |
A, copy on the stream path (#2735’s owned form): simple, but every item of the case that matters most is copied. B, keep every buffer a view points into (tethering as for text in memory): memory grows with the input; a slow consumer holds everything. C, a fixed pool with a count per buffer and a wait when it is full: no copy and bounded memory, but a program that holds views and waits for the next item while every buffer is held hangs. D, C with every place that holds a view counted while building: the pool is sized from the count, so it never runs out; the only wait is a full channel’s, which another task empties. Chosen: D.
A stream is input read in order and not held whole: standard input, an open
file read as a stream, a connection’s read side, the input of a @stream
grammar entry over one (ADR-grammar-typed-read-v2 D7), io::lines()
(ADR-loop-failure-v1 D4). It reads into a stream buffer taken from a pool
of buffers of one size. A view of one is a view like any other: nothing is
written, its state is in the ledger (ADR-views-v4 D5, ADR-tether-v1 D4).
| where the view lives | compiler |
|---|---|
| used in the loop’s body and dropped there (read, compared, cloned, ignored) | allowed: a plain borrow, no count |
a fixed number of names across turns (prev = ev) |
allowed: counts their number |
| a bounded channel to another task (ADR-149) | allowed: counts the channel’s capacity and what the receiver holds |
anything that grows across turns (list.push(ev), a map of views) |
refused, NK2308 |
NK2308’s message names the place that grows and the three ways out: copy what
is kept (.clone()), keep only what is needed and process the rest in the
body, or hand the items to a task over a bounded channel. A receiver of such a
channel follows the same rule where it holds what it receives; a channel whose
receiver is the task that sends on it is refused (it is a list in disguise),
and so is one whose receiver waits for the stream it is fed from.
The pool holds the buffers D2’s counts need at once, plus the one being read, plus the one an unfinished token is moved into (D8). The ledger names, per stream, the places counted and the pool’s size. No item waits for a buffer: the parse waits only for input.
A body that sends on a full channel pauses (Part II 12.5); the parse of a
stream whose body pauses runs in a thread of its own (ADR-grammar-typed-read-v2
D7), where waiting is not a pause inside the parse (NK2209 stands). A slow
receiver slows the reading; memory stays the pool.
A count is taken as Perceus takes one: no count for a borrow, a dup and its drop cancelled where both are seen, the drop at the last use, a freed buffer reused for the next read, atomic only where a view crosses a task (ADR-async-v7 D2). A buffer points to nothing, so no count can form a cycle.
Where an item’s text can be longer than a buffer, the grammar hands it over in
parts, each a view of one buffer (yaml: ScalarStart, ScalarPart,
ScalarEnd). A part is emitted only where the parse cannot go back past it
(ADR-grammar-typed-read-v2 D7). Keeping all the parts of one text is keeping a
growing list, D2’s last row. Comparing a text that may come in parts with a
literal, part by part with no copy, is std’s.
An item over a stream and an item over a String or Bytes are one type; only
the buffer behind the views differs, and nothing in the source says which
(ADR-views-v4 D5). An item handed to the Rust side carries a counted handle on
its buffer, so it is 'static without its text copied; the Rust reader of
ADR-grammar-typed-read-v2 D7 hands out views too.
The unfinished token’s bytes, at most the longest token the grammar can leave unfinished (yaml: an implicit key, at most 1024 characters, YAML 1.2 §7.4.2), move to the start of the next buffer before it is filled. It is the engine’s move, of no item and of no data the program sees. Rejected: tokens that span buffers (every rule pays for the seam).
A stream fails at run time only for its input: a read that fails (the loop
throws, ADR-loop-failure-v1 D1), a parse error, a limit of the format
(ADR-yaml-1 D8). The pool does not run out (D3), a wait is a full channel’s
(D4), and an item fits a buffer (D6).
Rejected: a copy on the stream path (#2735’s owned form: superseded); keeping every buffer a view points into (unbounded); waiting when the pool is full (a program that holds views and waits for the next item hangs); a pool that grows (unbounded, an out-of-memory end from outside); copying when the pool is full (a copy the program did not ask for, ADR-tether-v1 D3).
(supervisor, 2026-11-28, #2870) A free function whose ledger entry says
iterates = "stream" hands out views of its own pool: io::lines is
-> Seq[ref String] pauses throws. The item is checked as an item of
text.lines() is, with the stream’s pool as its source; assigned to a place
that holds a String, it is a counted keep (D2, D5). .collect() stays
NK2308, .cloned() is the copy. Outside such an entry, ref String with no
source keeps its meaning elsewhere: a ledger entry’s own result, a warning
in user code. No new syntax: this is D1 applied to the signature. Built for
a for over the call; a combinator over it (map, filter, take, …)
still walks owned items, a copy per line that D1 does not allow, to be
removed (#2881).
.clone().each an emitted item borrows its buffer for the turn, with no
count (D2, D5 step 1); only where the walk runs the parse in a thread of its
own (ADR-grammar-typed-read-v2 D7) does an item carry a counted handle,
atomic as D5 step 4 says. Neither needs unsafe (#2834’s defect).std surface of a byte stream (io::Stream from standard input, an open
file, a connection): proposed by its build issue, decided by the supervisor.