Span
scoped struct Span<T>import Span from Wax;A mutable, scoped view into contiguous storage. It borrows its backing storage
and cannot be retained in a heap object, array, or static. Slices share storage;
ToArray makes a separate array. Writes through any overlapping mutable view
are visible through the others. A Span<T> can widen to ReadOnlySpan<T>.
This example returns 9: filling a slice updates the original array.
import Span from Wax;
api fn Main() : int32 {
int32[] values = new int32[] { 1, 2, 3 };
Span<int32> middle = values.ToSpan().Slice(1, 1);
middle.Fill(9);
return values[1];
}
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.
thisindexer-setter
public set this(int32 index, T value) : voidWrites the element at a zero-based index relative to this view, changing its backing storage
and all overlapping views. An index outside [0, 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() : Span<uint8> where T is blittableBorrows a mutable view of the same raw storage, with length measured in bytes. Writes through the result change the original elements. This exposes their storage representation, including padding; it is not a portable serialization format.
Slicemethod
public fn Slice(int32 sliceStart, int32 len) : Span<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.
Fillmethod
public fn Fill(T value) : voidAssigns value to every element of the backing window. An empty view does nothing.
Reference elements all receive the same reference.
FillRangemethod
public fn FillRange(T value, int32 start, int32 count) : voidFills [start, start + count) with value. Both endpoints must lie in [0, length];
invalid endpoints panic before any write. An empty or reversed range whose endpoints are
valid performs no writes.
Reversemethod
public fn Reverse() : voidReverses element order in the backing window. Empty and one-element views are unchanged. Overlapping views observe the reordered elements.
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.