// SPDX-FileCopyrightText: 2025 Unity Technologies and the glTFast authors
// SPDX-License-Identifier: Apache-2.0
using System;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
namespace GLTFast
{
///
/// Wraps a and provides a for accessing it.
///
readonly unsafe struct ReadOnlyNativeArrayFromNativeArray where T : unmanaged
{
readonly ReadOnlyNativeArray m_Array;
public ReadOnlyNativeArray Array
{
get
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS && UNITY_2022_2_OR_NEWER
// Making sure the source was not disposed already.
// This indirectly triggers a check of the original's safety handle as in
// `AtomicSafetyHandle.CheckReadAndThrow(m_Source.m_Safety);`
m_Source.AsReadOnlySpan();
#endif
return m_Array;
}
}
#if ENABLE_UNITY_COLLECTIONS_CHECKS && UNITY_2022_2_OR_NEWER
readonly NativeArray.ReadOnly m_Source;
#endif
public ReadOnlyNativeArrayFromNativeArray(NativeArray.ReadOnly data)
{
var bufferAddress = data.GetUnsafeReadOnlyPtr();
#if ENABLE_UNITY_COLLECTIONS_CHECKS
#if UNITY_2022_2_OR_NEWER
m_Source = data;
#endif
var safety = AtomicSafetyHandle.Create();
m_Array = new ReadOnlyNativeArray(bufferAddress, data.Length, ref safety);
#else
m_Array = new ReadOnlyNativeArray(bufferAddress, data.Length);
#endif
}
}
}