Job with interface constrained with generic to struct.

I wanted to use interface in my job, I know that only unmanaged types are allowed in jobs so I have tried something like this:

[BurstCompile]
public struct JobWithInterfaces<TGenericA,TGenericB>:IJobParallelFor
    where TGenericA:struct,IMyInterface
    where TGenericB:struct,IMyInterface
{
    [ReadOnly] public TGenericA A;
    public TGenericA A;
    public void Execute(int index)
    {
        //...
    }
}

But burst is complaining that A is a managed type of IMyInterface. Constraining it to struct is not taken into account. Is it possible to do something like that or I just have to get unmanaged elements from the interface manually and assigned them to fields of the job?

You’ll need to get more specific about the error and code, because this kind of thing is possible.

This is the pattern I use: Latios-Framework/PsyshockPhysics/Physics/Internal/Queries/FindPairsInternal.cs at v0.3.1 · Dreaming381/Latios-Framework · GitHub

Oddly I have done it again because I have changed my approach thinking that it is not possible and now I do not get this error. But I do get an error that I am writing to [ReadOnly] native array

InvalidOperationException: The Unity.Collections.NativeArray`1[System.Single] has been declared as [ReadOnly] in the job, but you are writing to it.

I get this native array using Property of en interface:

public struct AddMapAToMapBJobGeneric<T> : IJobParallelForBatch where T:struct, IMapIterface
    {
        public T MapA;

        public T MapB;

        public void Execute(int startIndex, int count)
        {
            var mapB = MapB.Data;
            for (int j = startIndex, end = startIndex + count; j < end; j++)
            {
                var d = MapA.Data[j];
                mapB[j] += d; // Exception is thrown here
            }
        }
    }

I have a new approach to my code so I do not need a Generic job, but it would be nice to know if what I am doing is OK for the future :slight_smile:

Without seeing the implementation for the concrete IMapInterface, I cannot help you.

 public struct TestMap : IMapInterface
    {
        public int Width { get; }
        public NativeArray<float> Data { get; }

        public TestMap(int width, Allocator allocator = Allocator.TempJob)
        {
            Width = width;
            Data = new NativeArray<float>(width*width, allocator);
        }

        public void Dispose()
        {
            Data.Dispose();
        }
    }

Here is my test implemetation

I don’t see any [ReadOnly] attribute anywhere in your code, so I am not sure what the error is referring to.