public static int WriteCompressed <T>(this BitWriter @this, T val, int bits, bool unsigned) where T : struct
        {
            var size = Marshal.SizeOf <T>();
            var buf  = new byte[size];
            var ptr  = Marshal.AllocHGlobal(size);

            Marshal.StructureToPtr(val, ptr, false);
            Marshal.Copy(ptr, buf, 0, size);
            Marshal.FreeHGlobal(ptr);

            return(@this.WriteCompressed(new ReadOnlySpan <byte>(buf), bits, unsigned));
        }
        public static int WriteCompressed(this BitWriter @this, byte[] buf, int index, int length, int bits,
                                          bool unsigned)
        {
            if (bits > length * 8)
            {
                throw new ArgumentOutOfRangeException(nameof(bits), "Bit count exceeds buffer length");
            }

            if (index > length)
            {
                throw new ArgumentOutOfRangeException(nameof(index), "Index exceeds buffer length");
            }

            return(@this.WriteCompressed(new ReadOnlySpan <byte>(buf, index, length), bits, unsigned));
        }
 public static int WriteCompressed <T>(this BitWriter @this, T val, bool unsigned) where T : struct
 {
     return(@this.WriteCompressed(val, Marshal.SizeOf <T>() * 8, unsigned));
 }
 public static int WriteCompressed(this BitWriter @this, Span <byte> buf, int bits, bool unsigned)
 {
     return(@this.WriteCompressed((ReadOnlySpan <byte>)buf, bits, unsigned));
 }