← Standard library

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

ReadOnlySpan.wax:32

Members

lengthfield

public native int32 length

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

ReadOnlySpan.wax:36

sizegetter

public get size() : int32

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

ReadOnlySpan.wax:39

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.

ReadOnlySpan.wax:45

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.

ReadOnlySpan.wax:55

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.

ReadOnlySpan.wax:61

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.

ReadOnlySpan.wax:66

AsBytesmethod

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

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

ReadOnlySpan.wax:73

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.

ReadOnlySpan.wax:82

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.

ReadOnlySpan.wax:100

Containsmethod

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

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

ReadOnlySpan.wax:106

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.

ReadOnlySpan.wax:120

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.

ReadOnlySpan.wax:134

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

ReadOnlySpan.wax:153

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.

ReadOnlySpan.wax:182

ToStringmethod

public fn ToString() : string

Provided by the compiler.

ToStringBuffermethod

public fn ToStringBuffer(StringBuilder builder) : int32

Provided by the compiler.