← Standard library

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];
}

Span.wax:26

Members

lengthfield

public native int32 length

The number of elements in this view, not its byte size.

Span.wax:39

sizegetter

public get size() : int32

The number of elements in this view, equal to length.

Span.wax:42

thisindexer-getter

public get this(int32 index) : T

Reads the element at a zero-based index relative to this view. A negative index or an index at or beyond length panics.

Span.wax:48

thisindexer-setter

public set this(int32 index, T value) : void

Writes 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.

Span.wax:53

SequenceEqualsmethod

public fn SequenceEquals(ReadOnlySpan<T> other) : bool where T is blittable

Returns 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.

Span.wax:67

SequenceCompareTomethod

public fn SequenceCompareTo(ReadOnlySpan<T> other) : int32 where T is blittable

Compares 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.

Span.wax:73

IndexOfSequencemethod

public fn IndexOfSequence(ReadOnlySpan<T> needle) : int32 where T is blittable

Returns 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.

Span.wax:78

AsBytesmethod

public fn AsBytes() : Span<uint8> where T is blittable

Borrows 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.

Span.wax:87

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.

Span.wax:100

CopyTomethod

public fn CopyTo(Span<T> dest) : void

Copies 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.

Span.wax:120

Containsmethod

public fn Contains(T item) : bool where T is Equatable

Tests whether any element matches using Equals. Returns false for an empty view.

Span.wax:621

IndexOfmethod

public fn IndexOf(T item) : int32 where T is Equatable

Returns the first matching element index using Equals, or -1 when absent, including an empty view.

Span.wax:635

LastIndexOfmethod

public fn LastIndexOf(T item) : int32 where T is Equatable

Returns the last matching element index using Equals, or -1 when absent, including an empty view.

Span.wax:649

Fillmethod

public fn Fill(T value) : void

Assigns value to every element of the backing window. An empty view does nothing. Reference elements all receive the same reference.

Span.wax:664

FillRangemethod

public fn FillRange(T value, int32 start, int32 count) : void

Fills [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.

Span.wax:676

Reversemethod

public fn Reverse() : void

Reverses element order in the backing window. Empty and one-element views are unchanged. Overlapping views observe the reordered elements.

Span.wax:698

BinarySearchmethod

public fn BinarySearch(T item) : int32 where T is Comparable

Searches 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).

Span.wax:718

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.

Span.wax:746

ToStringmethod

public fn ToString() : string

Provided by the compiler.

ToStringBuffermethod

public fn ToStringBuffer(StringBuilder builder) : int32

Provided by the compiler.