MemoryMarshalEx.cs 740 B

123456789101112131415161718192021222324
  1. using System.Runtime.CompilerServices;
  2. using System.Runtime.InteropServices;
  3. namespace Lua.Internal;
  4. internal static class MemoryMarshalEx
  5. {
  6. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  7. public static ref T UnsafeElementAt<T>(T[] array, int index)
  8. {
  9. #if NET6_0_OR_GREATER
  10. return ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(array), index);
  11. #else
  12. ref var reference = ref MemoryMarshal.GetReference(array.AsSpan());
  13. return ref Unsafe.Add(ref reference, index);
  14. #endif
  15. }
  16. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  17. public static ref T UnsafeElementAt<T>(Span<T> array, int index)
  18. {
  19. return ref Unsafe.Add(ref MemoryMarshal.GetReference(array), index);
  20. }
  21. }