Module: nupp.span

Bounds-carrying borrowed pointer views.

ByteSpan keeps a runtime element count beside a rooted pointer. Indexing and slicing check that count before the only unsafe pointer operations in this module. Public code never has to dereference or offset a raw pointer.

Module contents

Types

TypeKindDescription
ByteSpanrecordA checked, shared view over contiguous elements.
ByteWriteSpanrecordAn affine checked write range.

Functions

FunctionKindDescription
ByteSpan:getmethodReturns one byte after checking the one-based index.
ByteSpan:slicemethodReturns a checked subspan, inclusive at both ends.
from_carrayfunctionCreates a checked shared span over a C array and an explicit logical count.
from_stringfunctionCreates a byte span over a Lua string and keeps that string rooted.
write_carrayfunctionCreates an affine write span.

Types#

ByteSpanrecord#

A checked, shared view over contiguous elements.

local record ByteSpan
    readonly anchor: any
    readonly pointer: uint8[?] borrows anchor
    readonly offset: integer
    readonly count: integer
end

Fields

NameTypeDescription
anchorany
pointeruint8[?] borrows anchor
offsetinteger
countinteger

ByteWriteSpanrecord#

An affine checked write range. Its live token keeps the source under an incompatible-borrow barrier until commit or scope exit consumes it.

local record ByteWriteSpan
    readonly anchor: any
    readonly pointer: uint8[?] borrows anchor
    readonly offset: integer
    readonly count: integer

    @drop
    function commit(takes self: ByteWriteSpan): nil
    end

    function set(self: ByteWriteSpan, index: integer, value: uint8): nil
    end
end

Methods

commit
commit: function commit(takes self: ByteWriteSpan): nil
Arguments
NameTypeDescription
selfByteWriteSpan
Returns
TypeDescription
nil
set
set: function set(self: ByteWriteSpan, index: integer, value: uint8): nil
Arguments
NameTypeDescription
selfByteWriteSpan
indexinteger
valueuint8
Returns
TypeDescription
nil

Fields

NameTypeDescription
anchorany
pointeruint8[?] borrows anchor
offsetinteger
countinteger

Functions#

ByteSpan:getmethod#

Returns one byte after checking the one-based index.

function ByteSpan:get(index: integer): uint8

Arguments

NameTypeDescription
indexinteger

Returns

TypeDescription
uint8

Raises

  • when index is outside 1 through count

ByteSpan:slicemethod#

Returns a checked subspan, inclusive at both ends.

function ByteSpan:slice(first: integer, last: integer?): ByteSpan borrows self

Arguments

NameTypeDescription
firstinteger
lastinteger?

Returns

TypeDescription
ByteSpan borrows self

Raises

  • when the requested range is outside this span

span.from_carrayfunction#

Creates a checked shared span over a C array and an explicit logical count.

function span.from_carray(borrows source: uint8[?], count: integer): ByteSpan borrows source

Arguments

NameTypeDescription
sourceuint8[?]
countinteger

Returns

TypeDescription
ByteSpan borrows source

Raises

  • when count is negative

span.from_stringfunction#

Creates a byte span over a Lua string and keeps that string rooted.

function span.from_string(borrows source: string): ByteSpan borrows source

Arguments

NameTypeDescription
sourcestring

Returns

TypeDescription
ByteSpan borrows source

span.write_carrayfunction#

Creates an affine write span. The source is exclusive during construction and the returned dependent owner prevents invalidating operations until it is committed.

function span.write_carray(exclusive source: uint8[?], count: integer): ByteWriteSpan borrows source

Arguments

NameTypeDescription
sourceuint8[?]
countinteger

Returns

TypeDescription
ByteWriteSpan borrows source

Raises

  • when count is negative