using System; using System.Runtime.CompilerServices; namespace LibHac.Tests.Common; public class Layout { public static int GetOffset(ref readonly TStruct structRef, ref readonly TField fieldRef) { ref TField structOffset = ref Unsafe.As(ref Unsafe.AsRef(in structRef)); ref TField fieldOffset = ref Unsafe.AsRef(in fieldRef); int offset = Unsafe.ByteOffset(ref structOffset, ref fieldOffset).ToInt32(); if (offset >= Unsafe.SizeOf()) throw new ArgumentException($"Error getting field offset. {nameof(structRef)} and {nameof(fieldRef)} must be from the same struct instance."); return offset; } public static int AlignOf() where T : unmanaged { return Unsafe.SizeOf>() - Unsafe.SizeOf(); } private readonly struct AlignOfHelper where T : unmanaged { #pragma warning disable CS0169 // Field is never used private readonly byte _padding; private readonly T _value; #pragma warning restore CS0169 } }