lib+test: bufio rename bstream → stream
Validates the #15 same-leaf-name cross-module type fix (9d85aa4):
`bufio.stream` and `io.stream` now coexist on a `use bufio; use io;`
surface — bufiotest.ww references both in the same scope (e.g.
`let m: io.stream; let b: bufio.stream;`) and compiles clean.
Lifts the bufio-side workaround that bstream existed to dodge.
@test fns rename in lockstep (bstreamsmallwrite → streamsmallwrite,
etc.). Also tidies the stale "until #21 lands" parenthetical in
test/wcc/696_modtype_leaf_collision.c, since #21 is this commit.
make test: 54/54; bootstrap fixed-point 990–997 holds.
This commit is contained in:
@@ -10,21 +10,13 @@
|
||||
// ([]u8 | io.eof | io.closed | overflow)
|
||||
// bufio.scanline (s: *scanner) (str | io.eof | io.closed | overflow)
|
||||
//
|
||||
// bufio.init (b: *bstream, src: *io.stream, rbuf: []u8, wbuf: []u8) void
|
||||
// bufio.flush (b: *bstream) (void | io.closed)
|
||||
// bufio.unread (b: *bstream, buf: []u8) void
|
||||
// bufio.init (b: *stream, src: *io.stream, rbuf: []u8, wbuf: []u8) void
|
||||
// bufio.flush (b: *stream) (void | io.closed)
|
||||
// bufio.unread (b: *stream, buf: []u8) void
|
||||
// bufio.isbuffered (s: *io.stream) bool
|
||||
//
|
||||
// Divergence from Hare:
|
||||
//
|
||||
// • Hare's `bufio::stream` (ref/hare/bufio/stream.ha:27) becomes
|
||||
// `bufio.bstream` here. Two modules can't both `export type
|
||||
// stream` under ww's flat-scope module concat — `io.stream`
|
||||
// is already taken. Filed as a compiler followup
|
||||
// (cross-module type collision, sibling of the `mod.mod`
|
||||
// use_alias fix); graduate to `bufio.stream` once that lands.
|
||||
// Same precedent as `memio.state` (not `stream`).
|
||||
//
|
||||
// • Hare returns the scanner / stream by value; ww cgen can't
|
||||
// return structs wider than 16B by value yet, so `newscanner`
|
||||
// and `init` are out-parameter shaped. Same workaround
|
||||
@@ -33,21 +25,21 @@
|
||||
// • The scanner's read buffer is held flat as `(ptr, cap)` rather
|
||||
// than a `[]u8` field; the scanner predates struct-held slice
|
||||
// support and is wired through scanbyte/scantok/scanline. The
|
||||
// new bstream writer half uses slice-typed `rbuf` / `wbuf`
|
||||
// new stream writer half uses slice-typed `rbuf` / `wbuf`
|
||||
// fields directly — chained dot through a struct slice field
|
||||
// works after task #6.
|
||||
//
|
||||
// • Hare's `bufio::stream` discriminates r-only / w-only / r+w
|
||||
// via three different vtable singletons (vtable_r, vtable_w,
|
||||
// vtable_rw); ww's [[io.stream]] vtable always carries all
|
||||
// three callbacks, so bstream always installs `bread` /
|
||||
// three callbacks, so stream always installs `bread` /
|
||||
// `bwrite` / `bclose`. A zero-length rbuf or wbuf makes the
|
||||
// matching callback degenerate (rbuf=[]: bread short-circuits
|
||||
// to io.eof; wbuf=[]: bwrite passes through to src).
|
||||
//
|
||||
// • Hare's `init` also takes a `flag` argument carrying
|
||||
// MANAGED_HANDLE / MANAGED_RDBUF / MANAGED_WRBUF ownership
|
||||
// bits; bstream never owns its buffers or src, so the
|
||||
// bits; stream never owns its buffers or src, so the
|
||||
// `flags` field and the init-time argument are dropped
|
||||
// wholesale. The Hare `flush: []u8` byte-set is preserved —
|
||||
// [[init]] seeds it to `['\n']` (line buffering, matching
|
||||
@@ -64,11 +56,11 @@
|
||||
// on the call that would have read them. Hare's EOF_GREEDY mode
|
||||
// isn't shipped (no caller needs it yet).
|
||||
//
|
||||
// Owning model: caller owns the scanner state, the bstream state,
|
||||
// Owning model: caller owns the scanner state, the stream state,
|
||||
// the byte buffers, and the source stream. `finish` doesn't free
|
||||
// the buffer and doesn't close src; it's a no-op today but stays
|
||||
// on the surface so callers don't churn when bufio grows internal
|
||||
// allocations. The bstream vtable `close` callback flushes pending
|
||||
// allocations. The stream vtable `close` callback flushes pending
|
||||
// writes and forwards close to src; it does not free rbuf / wbuf.
|
||||
//
|
||||
// let mem: memio.state;
|
||||
@@ -80,7 +72,7 @@
|
||||
// match (bufio.scanline(&sc)) { ... };
|
||||
// bufio.finish(&sc);
|
||||
//
|
||||
// let b: bufio.bstream;
|
||||
// let b: bufio.stream;
|
||||
// let rbuf: [256]u8;
|
||||
// let wbuf: [128]u8;
|
||||
// bufio.init(&b, src, rbuf[0:256], wbuf[0:128]);
|
||||
@@ -243,19 +235,19 @@ export fn scanline(s: *scanner) (str | io.eof | io.closed | overflow) = {
|
||||
};
|
||||
};
|
||||
|
||||
// bstream — buffered read+write over an underlying *io.stream.
|
||||
// stream — buffered read+write over an underlying *io.stream.
|
||||
//
|
||||
// `vtable` is the first field so a `*bstream` is castable to an
|
||||
// `vtable` is the first field so a `*stream` is castable to an
|
||||
// `*io.stream` via address-of-field (`&b.vtable`). Higher layers
|
||||
// (fmt.fprint, scanner, …) only see the io.stream view; bufio
|
||||
// recovers the outer bstream by casting the dispatch arg back to
|
||||
// `*bstream` inside the bread/bwrite/bclose callbacks.
|
||||
// recovers the outer stream by casting the dispatch arg back to
|
||||
// `*stream` inside the bread/bwrite/bclose callbacks.
|
||||
//
|
||||
// rbuf[rstart..rend] is pending read data; wbuf[0..wend] is pending
|
||||
// write data. rbuf or wbuf may be zero-length: read-empty makes
|
||||
// bread return io.eof immediately, write-empty makes bwrite a
|
||||
// pass-through to src (uses src.write directly, no buffering).
|
||||
export type bstream = struct {
|
||||
export type stream = struct {
|
||||
vtable: io.stream,
|
||||
src: *io.stream,
|
||||
rbuf: []u8,
|
||||
@@ -268,9 +260,9 @@ export type bstream = struct {
|
||||
|
||||
// init — wire `b` over `src` with caller-supplied buffers. Both
|
||||
// rbuf and wbuf may be empty slices; the corresponding direction
|
||||
// degenerates (see [[bstream]]). The flush byte-set defaults to
|
||||
// degenerates (see [[stream]]). The flush byte-set defaults to
|
||||
// "\n" (line-buffered writes); [[setflush]] swaps it.
|
||||
export fn init(b: *bstream, src: *io.stream, rbuf: []u8, wbuf: []u8) void = {
|
||||
export fn init(b: *stream, src: *io.stream, rbuf: []u8, wbuf: []u8) void = {
|
||||
b.vtable.ctx = b: *void;
|
||||
b.vtable.read = bread;
|
||||
b.vtable.write = bwrite;
|
||||
@@ -290,14 +282,14 @@ export fn init(b: *bstream, src: *io.stream, rbuf: []u8, wbuf: []u8) void = {
|
||||
// appearing in a write payload triggers an automatic flush after
|
||||
// the write copies into wbuf. Mirrors Hare's `setflush`
|
||||
// (ref/hare/bufio/stream.ha:128).
|
||||
export fn setflush(b: *bstream, bs: []u8) void = {
|
||||
export fn setflush(b: *stream, bs: []u8) void = {
|
||||
b.flush = bs;
|
||||
};
|
||||
|
||||
// flush — drain any pending wbuf data to src. Hare returns
|
||||
// `(void | io::error)`; ww's io.stream write-side error channel
|
||||
// is `io.closed` alone, so the return shape narrows.
|
||||
export fn flush(b: *bstream) (void | io.closed) = {
|
||||
export fn flush(b: *stream) (void | io.closed) = {
|
||||
if (b.wend == 0) { return; };
|
||||
let off: i32 = 0;
|
||||
for (off < b.wend) {
|
||||
@@ -324,7 +316,7 @@ export fn flush(b: *bstream) (void | io.closed) = {
|
||||
// region (rstart >= buf.len); Hare aborts on overflow, ww does the
|
||||
// same via rt_abort. Mirrors Hare's `stream_unread`
|
||||
// (ref/hare/bufio/stream.ha:164).
|
||||
export fn unread(b: *bstream, buf: []u8) void = {
|
||||
export fn unread(b: *stream, buf: []u8) void = {
|
||||
if (b.rstart < buf.len) {
|
||||
rtabort("bufio.unread: more data than rbuf has room for");
|
||||
};
|
||||
@@ -336,7 +328,7 @@ export fn unread(b: *bstream, buf: []u8) void = {
|
||||
b.rstart -= buf.len;
|
||||
};
|
||||
|
||||
// isbuffered — true when `s` is a [[bufio.bstream]]'s embedded
|
||||
// isbuffered — true when `s` is a [[bufio.stream]]'s embedded
|
||||
// vtable. Hare's discriminator is callback identity
|
||||
// (ref/hare/bufio/stream.ha:179); we match the shape directly. The
|
||||
// read or write callback being bread / bwrite is sufficient (close
|
||||
@@ -350,7 +342,7 @@ export fn isbuffered(s: *io.stream) bool = {
|
||||
// ---- vtable callbacks ------------------------------------------------
|
||||
|
||||
fn bread(s: *io.stream, buf: []u8) (i32 | io.eof | io.closed) = {
|
||||
let b: *bstream = s.ctx: *bstream;
|
||||
let b: *stream = s.ctx: *stream;
|
||||
// No read buffer configured: surface eof immediately rather
|
||||
// than punch through to src (callers asked for a write-only
|
||||
// stream, reads against it are a misuse).
|
||||
@@ -380,7 +372,7 @@ fn bread(s: *io.stream, buf: []u8) (i32 | io.eof | io.closed) = {
|
||||
};
|
||||
|
||||
fn bwrite(s: *io.stream, buf: []u8) (i32 | io.closed) = {
|
||||
let b: *bstream = s.ctx: *bstream;
|
||||
let b: *stream = s.ctx: *stream;
|
||||
// No write buffer configured: pass through to src directly.
|
||||
if (b.wbuf.len == 0) { return io.write(b.src, buf); };
|
||||
// Scan the write payload for any byte present in b.flush — a
|
||||
@@ -435,7 +427,7 @@ fn bwrite(s: *io.stream, buf: []u8) (i32 | io.closed) = {
|
||||
};
|
||||
|
||||
fn bclose(s: *io.stream) (void | io.closed) = {
|
||||
let b: *bstream = s.ctx: *bstream;
|
||||
let b: *stream = s.ctx: *stream;
|
||||
let r: (void | io.closed) = flush(b);
|
||||
match (r) {
|
||||
case void => { };
|
||||
|
||||
Reference in New Issue
Block a user