예제 #1
0
        public static byte[] Reinterpret <TConvertType>(this TConvertType value, byte[] bytes, int start)
            where TConvertType : struct
        {
            if (!TypeIntrospector <TConvertType> .IsPrimitive)
            {
                ThrowHelpers.ThrowOnlyPrimitivesException <TConvertType>();
            }

            return(ReinterpretFromPrimitive(value, bytes, start));
        }
예제 #2
0
        private static void ReinterpretFromPrimitive <TConvertType>(TConvertType value, Span <byte> bytes, int start = 0)
            where TConvertType : unmanaged
        {
            if (bytes.Length - start < MarshalSizeOf <TConvertType> .SizeOf)
            {
                ThrowHelpers.ThrowBufferTooSmall <TConvertType>(bytes.Length - start);
            }

            Unsafe.As <byte, TConvertType>(ref bytes[start]) = value;
        }
예제 #3
0
        public static unsafe TConvertType Reinterpret <TConvertType>(this byte[] bytes, int start)
            where TConvertType : struct
        {
            if (!TypeIntrospector <TConvertType> .IsPrimitive)
            {
                ThrowHelpers.ThrowOnlyPrimitivesException <TConvertType>();
            }

            return(ReinterpretPrimitive <TConvertType>(bytes, start));
        }
        /// <summary>
        /// Reinterprets the provided <see cref="bytes"/> to the specified generic array of value types.
        /// </summary>
        /// <typeparam name="TConvertType">The element type to reinterpret to.</typeparam>
        /// <param name="bytes">The bytes chunk.</param>
        /// <returns>The array of converted values.</returns>
        public static TConvertType[] ReinterpretToArray <TConvertType>(this Span <byte> bytes)
            where TConvertType : unmanaged
        {
            //Don't check nullness for perf. Callers shouldn't give us null arrays
            if (bytes.Length == 0)
            {
                return(Array.Empty <TConvertType>());
            }

            if (bytes.Length % MarshalSizeOf <TConvertType> .SizeOf != 0)
            {
                ThrowHelpers.ThrowMismatchedArraySizeForElementType <char>();
            }

            return(ReinterpretPrimitiveArray <TConvertType>(bytes));
        }
예제 #5
0
        public static unsafe TConvertType Reinterpret <TConvertType>(this IntPtr bytes)
            where TConvertType : struct
        {
            //Originally we null and length checked the bytes. This caused performance issues on .NET Core for some reason
            //Removing them increased the speed by almost an order of magnitude.
            //We shouldn't really handhold the user trying to reinterpet things into other things
            //If they're using this library then they should KNOW they shouldn't mess around and anything could happen
            //We already sacrfice safety for performance. An order of magnitude performance increase is a no brainer here.

            if (!TypeIntrospector <TConvertType> .IsPrimitive)
            {
                ThrowHelpers.ThrowOnlyPrimitivesException <TConvertType>();
            }

            return(ReinterpretPrimitive <TConvertType>((byte *)bytes));
        }
예제 #6
0
        public unsafe static byte[] Reinterpret <TConvertType>(this TConvertType[] values)
            where TConvertType : struct
        {
            //Don't check if null. It's a lot faster not to
            if (values.Length == 0)
            {
                return(new byte[0]);
            }

            if (!TypeIntrospector <TConvertType> .IsPrimitive)
            {
                ThrowHelpers.ThrowOnlyPrimitivesException <TConvertType>();
            }

            //BlockCopy is slightly faster if we have to reallocate
            byte[] bytes = new byte[MarshalSizeOf <TConvertType> .SizeOf * values.Length];

            Buffer.BlockCopy(values, 0, bytes, 0, MarshalSizeOf <TConvertType> .SizeOf * values.Length);

            return(bytes);
        }
예제 #7
0
        /// <summary>
        /// Reinterprets the provided <see cref="bytes"/> to the specified generic array of value types.
        /// </summary>
        /// <typeparam name="TConvertType">The element type to reinterpret to.</typeparam>
        /// <param name="bytes">The bytes chunk.</param>
        /// <returns>The array of converted values.</returns>
        public static unsafe TConvertType[] ReinterpretToArray <TConvertType>(this byte[] bytes)
            where TConvertType : struct
        {
            //Don't check nullness for perf. Callers shouldn't give us null arrays
            if (bytes.Length == 0)
            {
                return(new TConvertType[0]);
            }

            if (!TypeIntrospector <TConvertType> .IsPrimitive)
            {
                ThrowHelpers.ThrowOnlyPrimitivesException <TConvertType>();
            }

            if (bytes.Length % MarshalSizeOf <TConvertType> .SizeOf != 0)
            {
                ThrowHelpers.ThrowMismatchedArraySizeForElementType <char>();
            }

            return(ReinterpretPrimitiveArray <TConvertType>(bytes));
        }