/// <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?

