Last active
December 21, 2015 17:19
-
-
Save DmitryOlshansky/6339932 to your computer and use it in GitHub Desktop.
Output range adapters proof of concept
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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