public static MemorySpan <byte> AsBytes <T>(this MemorySpan <T> buffer) where T : unmanaged { var sizeOfT = CilVerifiable.SizeOf <T>(); var count = checked ((int)(sizeOfT * buffer.Count)); return(new MemorySpan <byte>(buffer.Manager, buffer.ByteOffset, count)); }
public static unsafe Span <T> DangerousCreateSpan <T>(object?manager, IntPtr byteOffset, int count) { if (null != manager) { var handle = GCHandle.Alloc(manager, GCHandleType.Pinned); try { IntPtr address = handle.AddrOfPinnedObject(); return(MemoryMarshal.CreateSpan(ref Unsafe.AsRef <T>(CilVerifiable.AddUnchecked(byteOffset, address).ToPointer()), count)); } finally { handle.Free(); } } return(MemoryMarshal.CreateSpan(ref Unsafe.AsRef <T>(byteOffset.ToPointer()), count)); }