Struct UnsafeSpan

Public

Replacement for System.Span which is compatible with earlier versions of C#.

Warning

These spans do not in any way guarantee that the memory they refer to is valid. It is up to the user to make sure the memory is not deallocated before usage. It should never be used to refer to managed heap memory without pinning it, since unpinned managed memory can be moved by some runtimes.

This has several benefits over e.g. UnsafeList:

  • It is faster to index into a span than into an UnsafeList, especially from C#. In fact, indexing into an UnsafeSpan is as fast as indexing into a native C# array.

    • As a comparison, indexing into a NativeArray can easily be 10x slower, and indexing into an UnsafeList is at least a few times slower.

  • You can create a UnsafeSpan from a C# array by pinning it.

  • It can be sliced efficiently.

  • It supports ref returns for the indexing operations.

Public Methods

Clone (allocator)

Creates a new copy of the span allocated using the given allocator.

CopyTo (...)

...

Free (allocator)

Frees the underlaying memory.

Move (startIndex, toIndex, count)

Copy the range [startIndex,startIndex+count) to [toIndex,toIndex+count)

MoveToNativeArray (allocator)

Moves this data to a new NativeArray.

Reallocate (allocator, newSize)

Returns a new span with a different size, copies the current data over to it, and frees this span.

Reinterpret ([...])

Returns a copy of this span, but with a different data-type.

Slice (start, [length])

Creates a new span which is a slice of this span.

ToArray ()

Converts the span to a managed array.

UnsafeSpan (...)

Public Variables

Length

Number of elements in this span.

Public
this[int index]
Public
this[uint index]
Public

Private/Protected Members

length
Internal Readonly
ptr
Internal Readonly