Fix waitForDataAsync compilation error for callbacks that have scoped destruction.

This commit is contained in:
Sönke Ludwig 2018-09-03 12:35:05 +02:00
parent d8546b843b
commit 0b638634c9

View file

@ -619,7 +619,7 @@ mixin(tracer);
if (is(typeof(() @safe { read_ready_callback(true); } ()))) if (is(typeof(() @safe { read_ready_callback(true); } ())))
{ {
mixin(tracer); mixin(tracer);
import vibe.core.core : setTimer; import vibe.core.core : Timer, setTimer;
if (!m_context) if (!m_context)
return WaitForDataAsyncStatus.noMoreData; return WaitForDataAsyncStatus.noMoreData;
@ -632,17 +632,39 @@ mixin(tracer);
return rs ? WaitForDataAsyncStatus.dataAvailable : WaitForDataAsyncStatus.noMoreData; return rs ? WaitForDataAsyncStatus.dataAvailable : WaitForDataAsyncStatus.noMoreData;
} }
auto tm = setTimer(timeout, { static final class WaitContext {
eventDriver.sockets.cancelRead(m_socket); CALLABLE callback;
read_ready_callback(false); TCPConnection connection;
}); Timer timer;
eventDriver.sockets.read(m_socket, m_context.readBuffer.peekDst(), IOMode.once,
(sock, st, nb) { this(CALLABLE callback, TCPConnection connection, Duration timeout)
tm.stop(); {
assert(m_context.readBuffer.length == 0); this.callback = callback;
m_context.readBuffer.putN(nb); this.connection = connection;
read_ready_callback(m_context.readBuffer.length > 0); if (timeout < Duration.max)
}); this.timer = setTimer(timeout, &onTimeout);
}
void onTimeout()
{
eventDriver.sockets.cancelRead(connection.m_socket);
callback(false);
}
void onData(StreamSocketFD, IOStatus st, size_t nb)
{
if (timer) timer.stop();
assert(connection.m_context.readBuffer.length == 0);
connection.m_context.readBuffer.putN(nb);
callback(connection.m_context.readBuffer.length > 0);
}
}
// FIXME: make this work without a heap allocation!
auto context = new WaitContext(read_ready_callback, this, timeout);
eventDriver.sockets.read(m_socket, m_context.readBuffer.peekDst(),
IOMode.once, &context.onData);
return WaitForDataAsyncStatus.waiting; return WaitForDataAsyncStatus.waiting;
} }
@ -758,6 +780,20 @@ enum WaitForDataAsyncStatus {
waiting, waiting,
} }
unittest { // test compilation of callback with scoped destruction
static struct CB {
~this() {}
this(this) {}
void opCall(bool) {}
}
void test() {
TCPConnection c;
CB cb;
c.waitForDataAsync(cb);
}
}
mixin validateConnectionStream!TCPConnection; mixin validateConnectionStream!TCPConnection;