using CommunityToolkit.HighPerformance; namespace Brutal.Extensions; public static class SpanExtensions { [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool Contains(this Span span, T value) where T : unmanaged, Enum { return ((ReadOnlySpan)span).Contains(value); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe bool Contains(this ReadOnlySpan span, T value) where T : unmanaged, Enum { return sizeof(T) switch { sizeof(byte) => span.Cast().Contains(Unsafe.BitCast(value)), sizeof(short) => span.Cast().Contains(Unsafe.BitCast(value)), sizeof(int) => span.Cast().Contains(Unsafe.BitCast(value)), sizeof(long) => span.Cast().Contains(Unsafe.BitCast(value)), _ => throw new NotSupportedException($"Enum of size {sizeof(T)} is not supported."), }; } /// public static bool TryCopyTo(this ReadOnlySpan value, Span destination, out int elementsWritten) { var result = value.TryCopyTo(destination); elementsWritten = result ? value.Length : 0; return result; } }