protocol for using InputRanges

Marco Leise Marco.Leise at gmx.de
Sat Mar 29 17:21:16 PDT 2014


Am Fri, 28 Mar 2014 19:23:29 -0700
schrieb Walter Bright <newshound2 at digitalmars.com>:

> On 3/28/2014 3:42 AM, Steven Schveighoffer wrote:
> >> I'm also curious what a generic read() should do when the stream is empty.
> >
> > Returns 0 elements read.

I guess we all have a clear concept of streams in our mind
from all kinds of programming languages. They typically
operate on bytes, have an EOF flag and offer read/write
methods that you pass a byte pointer and a length into. The
result is the count of bytes read or written.
Optionally they have an "available" property and handle any
combination of the following:
o all basic data types of the programming language
o POD structs
o formatted strings
o bitwise operations
o seeking after calculating offsets

They are used for I/O on heterogeneous data like binary
file formats or network protocols.

Ranges on the other hand work on sequences of items of the
same type, which is a small subset of what streams are
supposed to support. While there should be a connection
between both worlds, one cannot replace the other. There will
always be a separate raw stream type in Phobos.

> Meaning it must also write through a pointer if it did read something.
> 
> How is that faster than a 1 element buffer?

You can write your stream in such a way that you "map" a
memory area. E.g. you call "stream.waitForSoManyBytes(123);"
and then "ubyte[] arr = stream.mapMemory(123);" where arr is
then a slice into the stream's internal buffer.
(This also requires a mechanism to release the
 buffer after use, so the stream can reuse it:
 stream.doneWithSoManyBytes(123);)

-- 
Marco



More information about the Digitalmars-d mailing list