Design notes

Well formed streams

The design enforces well-formed streams: sequences of non-empty slices terminated by a single Bytesrw.Bytes.Slice.eod empty slice.

We want to ensure that the read functions of streams readers do not return unexpected empty slices and that they are not called again once they returned Bytesrw.Bytes.Slice.eod. Likewise we want to ensure that the write function of writers is not called with unexpected empty slices or that they are called again once they were called with Bytesrw.Bytes.Slice.eod.

For example the Bytesrw.Bytes.Slice creation function like Bytesrw.Bytes.Slice.make raise Invalid_argument when they unexpectedly produce an empty slice and those that can produce them have dedicated names that include _eod (e.g. Bytesrw.Bytes.Slice.make_or_eod). This makes programmers well-aware of the points in the code that may terminate streams.

Besides, readers and writers internally overwrite their read and write function as soon as Bytesrw.Bytes.Slice.eod is seen so that no closure is kept longer than it should be.

Error strategy

The error strategy may look involved but it caters for:

while retaining the ability to pattern match on the error of specific streams and making sure informative human error messages can always be produced.

For users it means that when they use streams:

To enforce good behaviour by stream reader and writers the following type structure is used:

Effectively this system can be seen as an OCaml-like exception system inside the Bytesrw.Bytes.Stream.Error exception.

Signature of read/write primitives

Names

Resolved without strong rationale

The following points were resolved for the first release without a strong rationale.

Upstreaming

If we determine that the API works well we should consider proposing to upstream the extension of Bytesrw.Bytes.

In particular it would be an alternative to the modular IO proposal. The core code is mostly trivial it's "just" three data structures. But added code is more code to maintain.

However here are a few things to consider: