Deprecation: module std.stream is deprecated

BBaz via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Nov 8 00:29:29 PST 2015


On Sunday, 8 November 2015 at 08:21:38 UTC, BBaz wrote:
> On Saturday, 7 November 2015 at 13:52:29 UTC, Spacen Jasset 
> wrote:
>> [...]
>> I have a used a template, because I cannot directly use the 
>> InputRange(char) interface as a type, and auto won't work 
>> either, so is there another parameter type I can use, such 
>> that I can have the concept of an abstract stream of bytes.
>
> With the help of a template constraint you can abstract this. 
> It looks like your problem is more getting the whole range 
> since .byLine or .byChunk don't return the full stuff. Maybe 
> use std.alsorithm.joiner. As you noted you can carry the 
> iterator without knowing the type with auto, example:
>
> ---
> import std.stdio;
> import std.range, std.algorithm;
>
> void main(string[] args)
> {
>     auto inputRange = File(__FILE__).byChunk(1024).joiner;
>     Foo foo = Foo(inputRange);
> }
>
> struct Foo
> {
>     this(T)(T t)
>     if (isInputRange!T && is(ElementType!T == ubyte))
>     {
>         foreach(byte b; t)
>         {
>             writef("0x%.2X ", b);
>             if (b == 0xA) writeln;
>         }
>     }
> }
> ---

or even using an auto function:

---
import std.stdio;
import std.range, std.algorithm;

void main(string[] args)
{
     auto inputRange0 = inputByteRange(File(__FILE__));
     Foo foo0 = Foo(inputRange0);
     ubyte[] arr = [1,2,3,4,5];
     auto inputRange1 = inputByteRange(arr);
     Foo foo1 = Foo(inputRange1);
}

auto inputByteRange(T)(auto ref T t)
{
     static if (is(T == File))
         return t.byChunk(1024).joiner;
     else static if (is(T == ubyte[]))
         return t;
     else assert(0, "unsupported inputByteRange arg");
}

struct Foo
{
     this(T)(T t)
     if (isInputRange!T && is(ElementType!T == ubyte)){}
}
---


More information about the Digitalmars-d-learn mailing list