Streams vs ranges

Jonathan M Davis jmdavisProg at gmx.com
Fri Jan 13 04:11:47 PST 2012


On Friday, January 13, 2012 12:17:06 Piotr Szturmaj wrote:
> Is there a plan to replace streams with byte ranges? Or should I just
> use streams?

At some point, std.stream will be replace with a range-based API. There has 
been some discussion on the design, but it hasn't been fully fleshed out, let 
alone implemented yet.

> I need to do some binary parsing and I found using ranges is not very
> comfortable. For example to read an uint I need to:
> 
> version (LittleEndian)
>      auto r = retro(takeExactly(range, 4));
> else
>      auto r = takeExactly(range, 4);
> 
> uint u;
> auto ar = (cast(ubyte*)&u)[0 .. 4];
> replaceInPlace(ar, 0, 4, r);
> 
> while with streams its easier:
> 
> uint u;
> stream.read(u);
> version (LittleEndian)
>      u = swapEndian(u);

Just because it's a range doesn't mean that there won't be a function allowing 
you to do something something more like

auto val = read!int(range);

That sort of thing will have to be discussed and sorted out when the stream 
API is overhauled. Unfortunately, it's one of those things that seems to be 
forever on the TODO list.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list