public static ReadOnlySpan <byte> AsBytes <T>(this ReadOnlySpan <T> source) where T : struct { if (!SpanHelpers.IsReferenceFree <T>()) { ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); } int newLength = checked (source.Length * Unsafe.SizeOf <T>()); return(new ReadOnlySpan <byte>(Unsafe.As <Pinnable <byte> >(source.Pinnable), source.ByteOffset, newLength)); }
public unsafe Span(void *pointer, int length) { if (!SpanHelpers.IsReferenceFree <T>()) { ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); } if (length < 0) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start); } _length = length; _pinnable = null; _byteOffset = new IntPtr(pointer); }
public static Span <TTo> NonPortableCast <TFrom, TTo>(this Span <TFrom> source) where TFrom : struct where TTo : struct { if (!SpanHelpers.IsReferenceFree <TFrom>()) { ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(TFrom)); } if (!SpanHelpers.IsReferenceFree <TTo>()) { ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(TTo)); } int newLength = checked ((int)((long)source.Length * Unsafe.SizeOf <TFrom>() / Unsafe.SizeOf <TTo>())); return(new Span <TTo>(Unsafe.As <Pinnable <TTo> >(source.Pinnable), source.ByteOffset, newLength)); }