public static unsafe bool HasFlag<T>(this T @this, T another) where T : unmanaged, Enum
{
switch (sizeof(T))
{
case 1:
case 2:
case 4:
int i = Unsafe.As<T, int>(ref other);
return (Unsafe.As<T, int>(ref @this) & i) == i;
case 8:
long l = Unsafe.As<T, long>(ref other);
return (Unsafe.As<T, long>(ref @this) & l) == l;
default:
throw new ArgumentException(
"The parameter should be one of the values 1, 2, 4 or 8.",
nameof(@this)
);
}
}
public static unsafe bool HasFlag<T>(this T @this, T another) where T : unmanaged, Enum =>
sizeof(T) switch
{
1 or 2 or 4 when Unsafe.As<T, int>(ref other) is var i => (Unsafe.As<T, int>(ref @this) & i) == i,
8 when Unsafe.As<T, long>(ref other) is var l => (Unsafe.As<T, long>(ref @this) & l) == l,
_ => throw new ArgumentException(
"The parameter should be one of the values 1, 2, 4 or 8.",
nameof(@this)
)
};