Add a minimalistic duck typing based stream representation.
This commit is contained in:
parent
a9905ddcd9
commit
4c6f26bd00
|
@ -13,6 +13,7 @@ import std.format : format;
|
||||||
import std.functional : toDelegate;
|
import std.functional : toDelegate;
|
||||||
import std.socket : AddressFamily, UnknownAddress;
|
import std.socket : AddressFamily, UnknownAddress;
|
||||||
import vibe.core.log;
|
import vibe.core.log;
|
||||||
|
import vibe.core.stream;
|
||||||
import vibe.internal.async;
|
import vibe.internal.async;
|
||||||
import core.time : Duration;
|
import core.time : Duration;
|
||||||
|
|
||||||
|
@ -441,9 +442,10 @@ mixin(tracer);
|
||||||
mixin(tracer);
|
mixin(tracer);
|
||||||
}
|
}
|
||||||
void finalize() {}
|
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)
|
private void writeDefault(InputStream)(InputStream stream, ulong nbytes = 0)
|
||||||
|
if (isInputStream!InputStream)
|
||||||
{
|
{
|
||||||
import std.algorithm.comparison : min;
|
import std.algorithm.comparison : min;
|
||||||
|
|
||||||
|
|
16
source/vibe/core/stream.d
Normal file
16
source/vibe/core/stream.d
Normal file
|
@ -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);
|
||||||
|
});
|
Loading…
Reference in a new issue