implicit Converting NativeList<T> to NativeArray<T> may not be a good idea

/// <summary>
/// This list as a [NativeArray](https://docs.unity3d.com/ScriptReference/Unity.Collections.NativeArray_1.html).
/// </summary>
/// <remarks>The array is not a copy; it references the same memory as the original list.</remarks>
/// <param name="nativeList">A NativeList instance.</param>
/// <returns>A NativeArray containing all the items in the list.</returns>
public static implicit operator NativeArray<T>(NativeList<T> nativeList)
{
    return nativeList.AsArray();
}

As NativeList is ferquntly used across jobs implicit conversion is very likely to be wrong.
Like in this example

[BurstCompile]
internal unsafe struct SomeDeferJob : IJobParallelForDefer
{
    [ReadOnly] public NativeArray<int> mData;//User could be using Array here
    //by accident or on purpose
    public void Execute(int index)
    {
        //some Job using Array...
    }
}
public class SomeSystem : SystemBase
{
    protected override void OnUpdate()
    {
        var list = new NativeList<int>(Allocator.TempJob);
        Dependency = Job.WithName("PopSomeElements").WithCode(() =>
        {
            list.Add(1);
            list.Add(2);
            list.Add(3);
        }).Schedule(Dependency);

        Dependency = new SomeDeferJob()
        {
            mData= list,//Here list is implicitly converted to Array
            //which lead to safe error as previous Job could be woking on the list
        }.Schedule(list, 32, Dependency);//Here list is implicitly converted to Array too
        //the fact is that user may just want to use the list override of Schedule
        //which also lead to safe error as previous Job could be woking on the list

        Dependency = new SomeDeferJob()
        {
            mData= list,//But this is still not right
            //it lead to safe error as previous Job could be woking on the list
        }.Schedule<SomeDeferJob, int>(list, 32, Dependency);//Generice parameter fixed this one

        list.Dispose(Dependency);
    }
}

These errors are very likely to be made when user is writing code in IDE. but intelligence will not compliant about them. They only show themself when the Job is Scheduled.
It is all because implicit. maybe just make it explicit?

Never faced with any problems with this implicit conversion.
This part just can’t be implicitly coverted to array, because there is no any NativeArray overloads for this Schedule

5709490--597751--upload_2020-4-14_12-22-45.png

Try IJobParallelForDefer

This is exactly IJobParallelForDefer, we’re using it in many places.
5709517--597754--upload_2020-4-14_12-34-21.png

Sorry, my bad…
I “stealed” this from Unity.Physics…

        unsafe public static JobHandle Schedule<T>(this T jobData, NativeArray<int> forEachCount, int innerloopBatchCount, JobHandle dependsOn = new JobHandle())
            where T : struct, IJobParallelForDefer
        { return IJobParallelForDeferExtensions.Schedule(jobData, (int*)NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(forEachCount), innerloopBatchCount, dependsOn); }

Well this is why I don’t see that :slight_smile:

Adding ref to the array solved this problem…

unsafe public static JobHandle Schedule<T>(this T jobData,ref NativeArray<int> forEachCount, int innerloopBatchCount, JobHandle dependsOn = new JobHandle())
            where T : struct, IJobParallelForDefer
        { return IJobParallelForDeferExtensions.Schedule(jobData, (int*)NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(forEachCount), innerloopBatchCount, dependsOn); }