public static bool TryReadMachineEndian <T>(ReadOnlySpan <byte> buffer, out T value) where T : struct { #if netstandard if (SpanHelpers.IsReferenceOrContainsReferences <T>()) { ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); } #else if (RuntimeHelpers.IsReferenceOrContainsReferences <T>()) { ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); } #endif if (Unsafe.SizeOf <T>() > (uint)buffer.Length) { #if __MonoCS__ value = default(T); #else value = default; #endif return(false); } value = Unsafe.ReadUnaligned <T>(ref MemoryMarshal.GetReference(buffer)); return(true); }
public static ReadOnlySpan <byte> AsBytes <T>(ReadOnlySpan <T> span) where T : struct { if (SpanHelpers.IsReferenceOrContainsReferences <T>()) { ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); } int newLength = checked (span.Length * Unsafe.SizeOf <T>()); return(new ReadOnlySpan <byte>(Unsafe.As <Pinnable <byte> >(span.Pinnable), span.ByteOffset, newLength)); }
/// <summary> /// Casts a ReadOnlySpan of one primitive type <typeparamref name="TFrom"/> to another primitive type <typeparamref name="TTo"/>. /// These types may not contain pointers or references. This is checked at runtime in order to preserve type safety. /// </summary> /// <remarks> /// Supported only for platforms that support misaligned memory access. /// </remarks> /// <param name="span">The source slice, of type <typeparamref name="TFrom"/>.</param> /// <exception cref="System.ArgumentException"> /// Thrown when <typeparamref name="TFrom"/> or <typeparamref name="TTo"/> contains pointers. /// </exception> public static ReadOnlySpan <TTo> Cast <TFrom, TTo>(ReadOnlySpan <TFrom> span) where TFrom : struct where TTo : struct { if (SpanHelpers.IsReferenceOrContainsReferences <TFrom>()) { ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(TFrom)); } if (SpanHelpers.IsReferenceOrContainsReferences <TTo>()) { ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(TTo)); } int newLength = checked ((int)((long)span.Length * Unsafe.SizeOf <TFrom>() / Unsafe.SizeOf <TTo>())); return(new ReadOnlySpan <TTo>(Unsafe.As <Pinnable <TTo> >(span.Pinnable), span.ByteOffset, newLength)); }
public static T ReadMachineEndian <T>(ReadOnlySpan <byte> source) where T : struct { #if netstandard if (SpanHelpers.IsReferenceOrContainsReferences <T>()) { ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); } #else if (RuntimeHelpers.IsReferenceOrContainsReferences <T>()) { ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); } #endif if (Unsafe.SizeOf <T>() > source.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.length); } return(Unsafe.ReadUnaligned <T>(ref MemoryMarshal.GetReference(source))); }
public static void WriteMachineEndian <T>(Span <byte> buffer, ref T value) where T : struct { #if netstandard if (SpanHelpers.IsReferenceOrContainsReferences <T>()) { ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); } #else if (RuntimeHelpers.IsReferenceOrContainsReferences <T>()) { ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); } #endif if ((uint)Unsafe.SizeOf <T>() > (uint)buffer.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.length); } Unsafe.WriteUnaligned <T>(ref buffer.DangerousGetPinnableReference(), value); }
public static T ReadMachineEndian <T>(ReadOnlySpan <byte> buffer) where T : struct { #if netstandard if (SpanHelpers.IsReferenceOrContainsReferences <T>()) { ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); } #else if (RuntimeHelpers.IsReferenceOrContainsReferences <T>()) { throw new ArgumentException(SR.Format(SR.Argument_InvalidTypeWithPointersNotSupported, typeof(T))); } #endif if (Unsafe.SizeOf <T>() > buffer.Length) { throw new ArgumentOutOfRangeException(); } return(Unsafe.ReadUnaligned <T>(ref buffer.DangerousGetPinnableReference())); }
public static bool TryWriteMachineEndian <T>(Span <byte> buffer, ref T value) where T : struct { #if netstandard if (SpanHelpers.IsReferenceOrContainsReferences <T>()) { ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); } #else if (RuntimeHelpers.IsReferenceOrContainsReferences <T>()) { throw new ArgumentException(SR.Format(SR.Argument_InvalidTypeWithPointersNotSupported, typeof(T))); } #endif if (Unsafe.SizeOf <T>() > (uint)buffer.Length) { return(false); } Unsafe.WriteUnaligned <T>(ref buffer.DangerousGetPinnableReference(), value); return(true); }
public static bool TryWriteMachineEndian <T>(Span <byte> buffer, ref T value) where T : struct { #if netstandard if (SpanHelpers.IsReferenceOrContainsReferences <T>()) { ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); } #else if (RuntimeHelpers.IsReferenceOrContainsReferences <T>()) { ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); } #endif if (Unsafe.SizeOf <T>() > (uint)buffer.Length) { return(false); } Unsafe.WriteUnaligned <T>(ref MemoryMarshal.GetReference(buffer), value); return(true); }
public static bool TryReadMachineEndian <T>(ReadOnlySpan <byte> buffer, out T value) where T : struct { #if IsPartialFacade if (RuntimeHelpers.IsReferenceOrContainsReferences <T>()) { throw new ArgumentException(SR.Format(SR.Argument_InvalidTypeWithPointersNotSupported, typeof(T))); } #else if (SpanHelpers.IsReferenceOrContainsReferences <T>()) { ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); } #endif if (Unsafe.SizeOf <T>() > (uint)buffer.Length) { value = default; return(false); } value = Unsafe.ReadUnaligned <T>(ref buffer.DangerousGetPinnableReference()); return(true); }