Skip to content

Instantly share code, notes, and snippets.

@DmitryOlshansky
Last active December 21, 2015 17:19
Show Gist options
  • Select an option

  • Save DmitryOlshansky/6339932 to your computer and use it in GitHub Desktop.

Select an option

Save DmitryOlshansky/6339932 to your computer and use it in GitHub Desktop.
Output range adapters proof of concept
import std.range, std.traits, std.stdio, std.format, std.digest.sha;
auto demux(Output...)(Output sinks)
{
static struct Demux
{
Output outs;
this(Output outs)
{
this.outs = outs;
}
void put(E)(E el)
{
foreach(sink; outs)
{
sink.put(el);
}
}
}
return Demux(sinks);
}
auto converter(alias conv, Output)(Output range)
{
static struct Adapter
{
Output sink;
this(Output sink)
{
this.sink = sink;
}
void put(E)(E el)
{
sink.put(conv(el));
}
}
return Adapter(range);
}
void main()
{
SHA1 hasher;
hasher.start();
auto conv = converter!(x => cast(ubyte[])x)(&hasher);
auto demuxed = demux( stdout.lockingTextWriter(), conv);
formattedWrite(demuxed, "Hello, world!\n");
writefln("SHA1: %(%02x%)", hasher.finish());
//verify that our nice pipeline is OK ;)
writefln("Verify SHA1: %(%02x%)", sha1Of("Hello, world!\n"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment