From b8b18f181b5c2c0e2d317f5e9321b311ccee47f8 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Sun, 1 Oct 2023 16:23:04 +0200 Subject: [PATCH 1/2] Executor - Added support for delegates - We now secretly convert everything to a delegate --- source/guillotine/executor.d | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/source/guillotine/executor.d b/source/guillotine/executor.d index 542b6a7..70005f0 100644 --- a/source/guillotine/executor.d +++ b/source/guillotine/executor.d @@ -7,7 +7,8 @@ import guillotine.future : Future; import guillotine.value; import guillotine.result : Result; import guillotine.provider; -import std.traits : isFunction, FunctionTypeOf, ReturnType, arity; +import std.traits : isFunction, isDelegate, FunctionTypeOf, ReturnType, arity; +import std.functional : toDelegate; // TODO: Document private bool isSupportedReturn(alias FuncSymbol)() @@ -44,7 +45,7 @@ private class FutureTask : Task /** * Function to call */ - private Value function() func; + private Value delegate() func; /** * Constructs a new `FutureTask` with the @@ -54,7 +55,7 @@ private class FutureTask : Task * future = the `Future` to associate with * func = the function to run */ - this(Future future, Value function() func) + this(Future future, Value delegate() func) { this.future = future; this.func = func; @@ -217,15 +218,15 @@ public class Executor * Returns: the task's `Future` */ public Future submitTask(alias Symbol)() - if (isFunction!(Symbol) && isSupportedFunction!(Symbol)) + if ((isFunction!(Symbol) || isDelegate!(Symbol)) && isSupportedFunction!(Symbol)) { FutureTask task; - Value function() ptr; + Value delegate() ptr; alias func = Symbol; - - ptr = &WorkerFunction!(func).workerFunc; + + ptr = toDelegate(&WorkerFunction!(func).workerFunc); version(unittest) { From 5e9a005457334f172dc974769601ca32ff3ca90d Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Sun, 1 Oct 2023 16:24:41 +0200 Subject: [PATCH 2/2] Executor - Only do conversion to a delegate if it is a function, otherwise assign directly (with no conversion) --- source/guillotine/executor.d | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/source/guillotine/executor.d b/source/guillotine/executor.d index 70005f0..0e5a761 100644 --- a/source/guillotine/executor.d +++ b/source/guillotine/executor.d @@ -225,8 +225,14 @@ public class Executor Value delegate() ptr; alias func = Symbol; - - ptr = toDelegate(&WorkerFunction!(func).workerFunc); + static if(isDelegate!(Symbol)) + { + ptr = &WorkerFunction!(func).workerFunc; + } + else + { + ptr = toDelegate(&WorkerFunction!(func).workerFunc); + } version(unittest) {