Skip to content

Commit

Permalink
Add delegate support for task submission (#2)
Browse files Browse the repository at this point in the history
* Executor

- Added support for delegates
- We now secretly convert everything to a delegate

* Executor

- Only do conversion to a delegate if it is a function, otherwise assign directly (with no conversion)
  • Loading branch information
deavmi authored Oct 1, 2023
1 parent 93fded7 commit da0fdfc
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions source/guillotine/executor.d
Original file line number Diff line number Diff line change
Expand Up @@ -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)()
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -217,15 +218,21 @@ 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;
static if(isDelegate!(Symbol))
{
ptr = &WorkerFunction!(func).workerFunc;
}
else
{
ptr = toDelegate(&WorkerFunction!(func).workerFunc);
}

version(unittest)
{
Expand Down

0 comments on commit da0fdfc

Please # to comment.