From 46b2ae3e9c631397b88c0428169b5ba580629f9f Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Thu, 19 Dec 2019 22:08:55 +1100 Subject: [PATCH] Update doc/note/io-input-output.md for io_bind --- doc/note/io-input-output.md | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/doc/note/io-input-output.md b/doc/note/io-input-output.md index e651686c..4bb711ce 100644 --- a/doc/note/io-input-output.md +++ b/doc/note/io-input-output.md @@ -210,5 +210,28 @@ methods. Wuffs code does not reference an `io_buffer` directly. ## Binding -TODO: discuss `io_bind`, which temporarily adapts a slice of bytes into an -`io_buffer`. +An `io_bind` block temporarily adapts a slice of bytes as an `io_reader` or +`io_writer`. This is typically done to call other functions that take an +`io_reader` or `io_writer` as an argument. + +``` +var r : base.io_reader +var s : slice base.u8 + +etc + +// Just before the io_bind, r's state is saved. + +io_bind (io: r, data: s) { + // At the top of the block, r's data slice is set to s, and r's metadata is + // set so that ri = 0, pos = 0 and closed = false. + // + // Because r is an io_reader, not an io_writer, the wi metadata field is + // set to the slice length, not 0. + // + // r must be a local variable, but s can be an expression. + etc +} + +// Just after the io_bind, r's state is restored. +```