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)); }
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; }
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)); }
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)); }
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); }
/// <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)); }