using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using Unity.Collections; namespace Newtonsoft.Json.UnityConverters { /// /// Array of values used to pass around through the converters via the /// PartialConverter type . /// /// Type of the values in this array. public struct ValuesArray : IReadOnlyList, IDisposable where TInner : struct { private NativeArray _array; public int Length => _array.Length; int IReadOnlyCollection.Count => _array.Length; /// /// Gets or sets the value at a given index. If array contains a nullable /// type, this indexer may return null. /// /// The index of which value get or set. /// Value in the given index. [MaybeNull] public TInner this[int index] { get => _array[index]; set => _array[index] = value; } /// /// Creates a new array as a shallow copy from an existing array. /// /// Array to copy values from. public ValuesArray(TInner[] array, Allocator allocator) { _array = new NativeArray(array, allocator); } /// /// Creates a new blank array with all values set to default() /// /// Size of the array. public ValuesArray(int capacity, Allocator allocator, NativeArrayOptions options = NativeArrayOptions.ClearMemory) { _array = new NativeArray(capacity, allocator, options); } /// /// Useful for nullable value types (T?). Will try to return the value /// at specified index casted to the generic type . /// If it's an invalid cast, or the value is null, then will instead /// return the default value for the type default(). /// /// Wanted return type. If value at index cannot be casted to this type then method will return default() /// Index of where to look in the array. /// The value at specified index, or default value for type given by generic type paramter . /// Index is outside the bounds of the inner array. [return: MaybeNull] public T GetAsTypeOrDefault(int index) where T : struct { return _array[index] as T? ?? default; } /// /// Useful for nullable value types (T?). Will try to return the value /// at specified index casted to the generic type . /// If it's an invalid cast, or the value is null, then will instead /// return the value given by the fallback parameter . /// /// Wanted return type. If value at index cannot be casted to this type then method will return value of fallback parameter . /// Index of where to look in the array. /// Fallback value that will be returned if value cannot be casted or is null. /// The value at specified index, or value of fallback parameter . /// Index is outside the bounds of the inner array. [return: NotNullIfNotNull("fallback")] public T GetAsTypeOrDefault(int index, [AllowNull] T fallback) where T : struct { return _array[index] as T? ?? fallback; } public IEnumerator GetEnumerator() { return _array.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } public void Dispose() { _array.Dispose(); } } }