ReadOnlySpan
scoped struct ReadOnlySpan<T>import ReadOnlySpan from Wax;A read-only, scoped view into contiguous storage. It borrows storage and cannot
be retained in a heap object, array, or static. Read-only prevents writes through
this view; it does not freeze objects referenced by elements or storage shared
with a mutable view. A Span<T> widens to this type, but the reverse conversion
is not allowed. Use ToArray when you need a separate, retainable array.
This example returns 8: the view sees a later array write, while the copy
keeps the old element.
import ReadOnlySpan from Wax;
api fn Main() : int32 {
int32[] values = new int32[] { 2, 4 };
ReadOnlySpan<int32> view = values.ToSpan();
int32[] copy = view.ToArray();
values[0] = 6;
return view[0] + copy[0];
}
Members
lengthfield
public native int32 lengthThe number of elements in this view, not its byte size.
sizegetter
public get size() : int32The number of elements in this view, equal to length.
thisindexer-getter
public get this(int32 index) : TReads the element at a zero-based index relative to this view. A negative index or an index
at or beyond length panics.
SequenceEqualsmethod
public fn SequenceEquals(ReadOnlySpan<T> other) : bool where T is blittableReturns true when both views have equal element counts and identical backing bytes. Compares
representations, without calling Equals; padding and floating-point bit patterns can
affect the result. Two empty views compare equal.
SequenceCompareTomethod
public fn SequenceCompareTo(ReadOnlySpan<T> other) : int32 where T is blittableCompares backing bytes lexicographically, returning -1, 0, or 1; a matching prefix
sorts before a longer view. Does not call CompareTo. For multibyte elements this is
representation ordering, not numeric ordering.
IndexOfSequencemethod
public fn IndexOfSequence(ReadOnlySpan<T> needle) : int32 where T is blittableReturns the first element index whose backing bytes match the needle, or -1 when absent.
Tests element-aligned positions without calling Equals. An empty needle returns 0.
AsBytesmethod
public fn AsBytes() : ReadOnlySpan<uint8> where T is blittableBorrows a read-only view of the same raw storage, with length measured in bytes. It includes the elements' storage representation and padding; it is not a portable serialization format.
Slicemethod
public fn Slice(int32 sliceStart, int32 len) : ReadOnlySpan<T>Borrows [sliceStart, sliceStart + len) from the same storage without copying. Both
arguments must be nonnegative and the entire range must fit, otherwise it panics.
Slice(length, 0) is valid. Indices in the result start at zero.
CopyTomethod
public fn CopyTo(Span<T> dest) : voidCopies the first min(length, dest.length) elements into dest, safely handling
overlapping views. A shorter destination truncates the copy without a panic; any remaining
destination elements are unchanged. An empty source or destination does nothing. Reference
elements copy their references, not the referenced objects.
Containsmethod
public fn Contains(T item) : bool where T is EquatableTests whether any element matches using Equals. Returns false for an empty view.
IndexOfmethod
public fn IndexOf(T item) : int32 where T is EquatableReturns the first matching element index using Equals, or -1 when absent, including an
empty view.
LastIndexOfmethod
public fn LastIndexOf(T item) : int32 where T is EquatableReturns the last matching element index using Equals, or -1 when absent, including an
empty view.
BinarySearchmethod
public fn BinarySearch(T item) : int32 where T is ComparableSearches a view already sorted ascending under CompareTo. Returns a matching element
index; when duplicates exist, the chosen match is unspecified. If absent, returns
~insertionIndex; apply ~ again to recover the insertion position. An empty view returns
-1 (~0).
ToArraymethod
public fn ToArray() : T[]Copies the elements into a new dense array that can outlive this scoped view. Later writes to either storage do not change the other. Reference elements still refer to the same objects; this is not a deep copy.
ToStringmethod
public fn ToString() : stringProvided by the compiler.
ToStringBuffermethod
public fn ToStringBuffer(StringBuilder builder) : int32Provided by the compiler.