A set of Burst-compatible struct APIs to use instead of C# delegates.
Here are a couple examples showcasing how to write struct-based delegates
Delegate:
Func<int, int, bool> greaterThan = (int a, int b) => a > b;
Value Delegate:
public struct GreaterThanFunc : IFunc<int, int, bool>
{
public bool Invoke(int a, int b) => a > b;
}
var greaterThan = ValueFunc<int, int, bool>.New<GreaterThanFunc>();
Burst Delegate:
[BurstCompile]
public static bool GreaterThan(int a, int b) => a > b;
BurstFunc<int, int, bool> greaterThan = BurstFunc<int, int, bool>.Compile(GreaterThan);
System.Delegate:
public class MyClass
{
public static List<int> CompareNextVal(List<int> vals, Func<int, int, int> comparer)
{
// ...
}
}
Value Delegate:
public class MyClass
{
public static List<int> CompareNextVal<TComparer>(List<int> vals, ValueFunc<int, int, int>.Struct<TComparer> comparer)
{
// ...
}
}
Burst Delegate:
public class MyClass
{
public static List<int> CompareNextVal(List<int> vals, BurstFunc<int, int, int> comparer)
{
// ...
}
}
public struct AggregateJob<TAggregator> : IJob
where TAggregator : struct, IFunc<int, int, int>
{
public ValueFunc<int, int, int>.Struct<TAggregator> Aggregator;
public NativeArray<int> Input;
public NativeArray<int> Output;
public void Execute()
{
for (var i = 0; i < Input.Length; i++)
Output[0] = Aggregator.Invoke(Input[i], Output[0]);
}
}
[BurstCompile]
public class MyClass
{
[BurstCompile]
public static int Sum(int a, int b) => a + b;
public static readonly BurstFunc<int, int, int> SumFunc = BurstFunc<int, int, int>.Compile(Sum);
public static int SumSomeNumbers(int[] numbers)
{
using(var input = new NativeArray<int>(numbers, Allocator.Persistent))
using(var output = new NativeArray<int>(1, Allocator.Persistent))
{
var job = new AggregateJob<BurstFunc<int, int, int>>
{
Aggregator = SumFunc, // BurstFunc has an implicit operator to ValueFunc
Input = input,
Output = output
};
job.Run();
return output[0];
}
}
}