From 4c6f26bd00dfdd9ee8759ac12f4e8a6818aac4f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Sat, 18 Jun 2016 10:00:02 +0200 Subject: [PATCH] Add a minimalistic duck typing based stream representation. --- source/vibe/core/net.d | 4 +++- source/vibe/core/stream.d | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 source/vibe/core/stream.d diff --git a/source/vibe/core/net.d b/source/vibe/core/net.d index 29c306a..2473674 100644 --- a/source/vibe/core/net.d +++ b/source/vibe/core/net.d @@ -13,6 +13,7 @@ import std.format : format; import std.functional : toDelegate; import std.socket : AddressFamily, UnknownAddress; import vibe.core.log; +import vibe.core.stream; import vibe.internal.async; import core.time : Duration; @@ -441,9 +442,10 @@ mixin(tracer); mixin(tracer); } void finalize() {} - void write(InputStream)(InputStream stream, ulong nbytes = 0) { writeDefault(stream, nbytes); } + void write(InputStream)(InputStream stream, ulong nbytes = 0) if (isInputStream!InputStream) { writeDefault(stream, nbytes); } private void writeDefault(InputStream)(InputStream stream, ulong nbytes = 0) + if (isInputStream!InputStream) { import std.algorithm.comparison : min; diff --git a/source/vibe/core/stream.d b/source/vibe/core/stream.d new file mode 100644 index 0000000..86fc122 --- /dev/null +++ b/source/vibe/core/stream.d @@ -0,0 +1,16 @@ +module vibe.core.stream; + +enum isInputStream(T) = __traits(compiles, { + T s; + ubyte[] buf; + if (!s.empty) + s.read(buf); + if (s.leastSize > 0) + s.read(buf); +}); + +enum isOutputStream(T) = __traits(compiles, { + T s; + const(ubyte)[] buf; + s.write(buf); +});