From 508669b78157c89285140894f4c326d89d0c2d46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Sun, 10 Apr 2016 14:22:03 +0200 Subject: [PATCH] Fix compile error when calling async() an unshared delegate. (cherry picked from commit a8faddf64170e5d1347b67e1d48b7796bbe08fd9) --- source/vibe/core/concurrency.d | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/source/vibe/core/concurrency.d b/source/vibe/core/concurrency.d index 09c153e..9cb0696 100644 --- a/source/vibe/core/concurrency.d +++ b/source/vibe/core/concurrency.d @@ -1127,6 +1127,8 @@ Future!(ReturnType!CALLABLE) async(CALLABLE, ARGS...)(CALLABLE callable, ARGS ar if (is(typeof(callable(args)) == ReturnType!CALLABLE)) { import vibe.core.core; + import std.functional : toDelegate; + alias RET = ReturnType!CALLABLE; Future!RET ret; ret.init(); @@ -1136,7 +1138,7 @@ Future!(ReturnType!CALLABLE) async(CALLABLE, ARGS...)(CALLABLE callable, ARGS ar static if (isWeaklyIsolated!CALLABLE && isWeaklyIsolated!ARGS) { ret.m_task = runWorkerTaskH(&compute, ret.m_result, callable, args); } else { - ret.m_task = runTask(&compute, ret.m_result, callable, args); + ret.m_task = runTask(toDelegate(&compute), ret.m_result, callable, args); } return ret; } @@ -1164,6 +1166,28 @@ unittest { } } +/// +unittest { + int sum(int a, int b) + { + return a + b; + } + + static int sum2(int a, int b) + { + return a + b; + } + + void test() + { + // Using a delegate will use runTask internally + assert(async(&sum, 2, 3).getResult() == 5); + + // Using a static function will use runTaskWorker internally, + // if all arguments are weakly isolated + assert(async(&sum2, 2, 3).getResult() == 5); + } +} /******************************************************************************/ /* std.concurrency compatible interface for message passing */