src/AnvilPacker/Util/Mem.cs
/// <summary> Reverse bytes of a primitive value. </summary>
/// <typeparam name="T">The type of the value to reverse the bytes. Must be a blittable type with size <c>1, 2, 4, or 8</c>. </typeparam>
Reverse bytes of a primitive value.
Reverse bytes of a primitive value .
[MethodImpl(Inline)]
        public static T BSwap<T>(T value) where T : unmanaged
        {
            static V I<V>(T v) => Unsafe.As<T, V>(ref v);
            static T O<V>(V v) => Unsafe.As<V, T>(ref v);
            if (sizeof(T) == 1) return O(BinaryPrimitives.ReverseEndianness(I<byte>(value)));
            if (sizeof(T) == 2) return O(BinaryPrimitives.ReverseEndianness(I<ushort>(value)));
            if (sizeof(T) == 4) return O(BinaryPrimitives.ReverseEndianness(I<uint>(value)));
            if (sizeof(T) == 8) return O(BinaryPrimitives.ReverseEndianness(I<ulong>(value)));
            throw new NotSupportedException();
        }
[ MethodImpl ( Inline ) ] public static T BSwap < T > ( T value ) where T : unmanaged { static V I < V > ( T v ) => Unsafe . As < T , V > ( ref v ) ; static T O < V > ( V v ) => Unsafe . As < V , T > ( ref v ) ; if ( sizeof ( T ) == 1 ) return O ( BinaryPrimitives . ReverseEndianness ( I < byte > ( value ) ) ) ; if ( sizeof ( T ) == 2 ) return O ( BinaryPrimitives . ReverseEndianness ( I < ushort > ( value ) ) ) ; if ( sizeof ( T ) == 4 ) return O ( BinaryPrimitives . ReverseEndianness ( I < uint > ( value ) ) ) ; if ( sizeof ( T ) == 8 ) return O ( BinaryPrimitives . ReverseEndianness ( I < ulong > ( value ) ) ) ; throw new NotSupportedException ( ) ; }
Reverse bytes of a primitive value.
Reverse bytes of a primitive value .
value
T
typeparam
The type of the value to reverse the bytes. Must be a blittable type with size 1, 2, 4, or 8
The type of the value to reverse the bytes . Must be a blittable type with size 1 2 4 or 8