예제 #1
0
        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));
        }
예제 #2
0
        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);
        }
예제 #3
0
        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));
        }