sysl

Arrays and slices

Two sequence types — storage and a view of it — and the rules for writing one down, indexing it, slicing it, and knowing what it keeps alive.

sysl has two sequence types where many languages have one:

typewhat it isshape
[N]Ta fixed array: N elements of T, a value, no headerthe elements, and nothing else
[]Ta slice: a view of elements someone owns{ owner, ptr, len } — three words
[]const Ta slice that may be read and not writtenthe same three words, with a bit

As in Go, one type is storage and one is a view, so a function that only reads takes the view and need not care where the elements live. Both carry a length, so every index is checked, which makes them the safe choice in place of a *T.

An array is its elements. A slice names elements that live somewhere else. The rest of this page follows from that distinction, and a function call shows it most plainly:

scribble(a: [4]int) -> int
    a[0] = 99
    a[0]

set_first(xs: []int)
    xs[0] = 99

var a = [1, 2, 3, 4]

print(scribble(a), a[0])

set_first(a)

print(a[0])
99 1
99

scribble receives four integers and writes to its own copy of them. set_first receives three words naming the caller’s four integers, and writes to those. Neither function says anything about copying; the parameter types decide it.

A string is the second row of that table with one thing added and one taken away — its bytes are guaranteed well-formed UTF-8, and nothing may write through it. Everything here about indexing, slicing, length and ownership is therefore true of a string as well, and strings is where the two differences are.

A third shape is deliberately not in that table: <N>T, a vector, holds the same values an [N]T holds but is not storage at all. It lives in a register and its operators work on every lane at once, so a + b on two <4>f32 is one instruction doing four additions. Everything on this page — run-time index checks, slicing, ownership — concerns values with an address, and a vector has none.

Arrays and slices are how a vector reaches memory, though, and the two methods that move a run of elements between them belong to the array and the slice: xs.load(i) reads the run starting at element i into a vector, and xs.store(i, v) writes one back. Both are bounds-checked like a subscript, and checked as a whole run — see vectors.

Writing one down

A literal lists its elements, and the count becomes part of the type:

var primes = [2, 3, 5, 7]
var row: [3]f64 = [1, 0, 0]

print(primes.len, primes[2], row[0])
4 5 1

The elements share one type, and a bare literal takes it from an element that has one — the rule the two sides of n + 1 follow. One suffix, or one typed name, anywhere in the list is enough, and a nested literal settles row by row:

const k: usize = 5

var sizes = [1, 2usize, 7]
var mixed = [1, k, 3]
var table = [[1usize, 2], [3, 4]]

var total = sizes[0] + mixed[1] + table[1][1]

print(total)
10

The bare literals are read at that type, not as an int first. An integer literal beside an integer element is read at its type whether it fits or not, so [1u8, 300] is refused at the 300, as a literal beside a typed operand is (§ Which conversions are implicit); a float literal that does not fit takes its default, real, and the typed elements widen to that where every value of theirs is one of its. Two elements that each have a type meet at the wider where one holds every value of the other, as two operands do — [b, n] over a byte and an int is a [2]int — and otherwise have to agree, so [u, n] over a u32 and an int is refused. An integer literal beside a real is not made a real.

var bytes = [1u8, 300]
the literal 300 does not fit byte

Tuple elements meet part by part, each part by the same rule, however deeply they nest: a bare literal part takes the type of the part at its place in another element, so the 0s below are longs and the list is a [3](long, int). A tuple written out of bare literals alone takes the type of a tuple beside it whole, in an array literal, as an operand, and as an argument for a generic parameter.

val spots = [(0, 0), (1i64 << 40, 2), (-1, 7)]
val wide: [3](long, int) = spots

print(wide[1].0, wide[2].0 + wide[2].1)
1099511627776 6

Two typed parts that do not meet are refused as two typed elements are, and the message names both tuples — [(u, 0), (n, 0)] over a u32 and an int is told it got (uint, int) and (int, int).

A declaration with no initializer starts at the type’s zero value. A repeat [value; count] fills every element with one value, and is the form for an element type whose zero is not the wanted starting point — or which has no zero at all:

const n = 6

var counters: [4]int
var ones = [1; 5]
var grid = [[0; 3]; 3]
var window = [0; n]

grid[1][2] = 5

print(counters[0], ones[4], grid[1][2], grid[0][0], window.len)
0 1 5 0 6

Three rules govern the repeat.

The count is a compile-time constant — a literal, a const naming one, or an expression over those — for the same reason an array bound is one: it is the bound, and the type is not known without it.

var k = 4
var buf: [4]u8 = [0; k]
an array's repeat count must be a constant, since it is the array's bound

The diagnostic goes on to name the alternative, described in a section below: a count computed while running makes storage rather than an array, and storage is written where a []T is expected.

The element type comes from the value, or from the context where the value is a bare literal. And the value is evaluated exactly once, then copied into every element:

struct Counter
    n: int

bump(c: *Counter) -> int
    c.n = c.n + 1
    7

var c = Counter(0)
var three = [bump(&c); 3]

print(three[0], three[1], three[2], c.n)
7 7 7 1

One call fills three elements, so the form is a construction rather than shorthand for writing the value out count times. Where the element type holds a reference, each copy is a share of its own.

An empty literal has no element type of its own and takes one from the context, exactly as a bare None does:

var empty: [0]int = []

print(empty.len)
0

The zero-value form is not about arrays

var counters: [4]int is a var with a declared type and no =, and it means “the zero value of this type” for any type that has one. What matters here is which types have none: a type containing a &T has no zero value, because a reference always points at something live.

struct Node
    next: &Node

var n: Node
Node has no zero value, so 'n' needs an initial value

That keeps the non-null guarantee of memory true with no special case for arrays, and it is why an array of enum values is written as a repeat such as [Empty; 16].

A table written at the top of a file

The same three forms written outside a function make a val, the natural form for a fixed table such as a lookup order defined by a file format:

val order = [16, 17, 18, 0, 8]

total(xs: []const int) -> int
    var t = 0

    for x in xs do t += x

    t

print(order[2], total(order[1..<3]), order.len)
18 35 5

The difference from a const is an address. A constant is folded into every use and has none, so it can size an array but cannot be one; a val is storage, so it may be indexed at a position only known while running, sliced, and iterated. It may not be written, so slicing one yields a []const T.

A val may hold a value the program has to build. Storage that exists for the whole run is never released, so the share it holds is never given back — which is what a static is, and why this is allowed:

squares(k: usize) -> []int
    var xs: []int = [0; k]

    for i in 0..<k do xs[i] = int(i) * int(i)

    xs

static val table = squares(4)

print(table[0], table[3], table.len)
0 9 4

What the value is still decides where the storage is filled. A string literal’s bytes are a constant in the object file and its owner word is null, so a table of them is constant data throughout and needs no code to run first. That lets a module with no allocator hold one, as it can hold order above:

val names = ["alpha", "beta", "gamma"]

var i = 2

print(names[i], names[0].len)
gamma 5

A const would not serve here: a constant is folded into its uses and has no address, so there is nothing to index at i. A raw pointer may be held for the same reason a literal may — it counts nothing, so there is no release to write.

A byte array from a string literal

On-disk formats are full of fixed-width text: an OEM name in eight bytes, a volume label in eleven, a magic number in four. A string literal is accepted wherever a [N]u8 is expected, when its UTF-8 length is exactly N — a binding’s annotation, a field, an argument, an element of an outer array and a function’s result:

struct Boot
    oem: [8]u8
    label: [11]u8

magic() -> [4]u8 = "FAT1"

sum(b: [3]u8) -> int = int(b[0]) + int(b[1]) + int(b[2])

val boot = Boot("MSWIN4.1", "NO NAME    ")
val kinds: [2][3]u8 = ["FAT", "EXT"]

print(boot.oem[0], boot.label[10], magic()[3], kinds[1][0], sum("abc"))
77 32 49 69 294

The literal is its bytes and nothing else: no NUL is added, and an escape counts as the bytes it encodes to, so a non-ASCII character takes its full UTF-8 width:

val e: [2]u8 = "é"
val nl: [2]u8 = "\n\0"

print(e[0], e[1], nl[0], nl[1])
195 169 10 0

It is an array literal of those bytes, so a module val or var written this way is constant data: on a bare-metal target it is laid into .rodata or .data with nothing run before the first statement, exactly as [77, 83, …] would be.

A field whose bytes are not text — a magic number with a byte above 0x7F — is written as a byte string, which fills a [N]u8 by the same rule:

val magic: [4]u8 = b"\x89PNG"

print(magic[0], magic[1])
137 80

The length has to match exactly, either way. A shorter literal is not padded, because what the rest should hold — spaces, NULs — is the format’s to say, not the language’s:

val oem: [8]u8 = "MSWIN"
this string literal is 5 bytes and [8]byte holds 8 — a string literal fills a byte array only when its UTF-8 length is exactly the array's

Only u8 (and a name for it) takes one. A [N]i8 or a [N]char is not a byte array the literal spells — a char is a scalar value, not a byte — so there the literal stays a string, and the position refuses it as it refuses any other mismatch:

val a: [2]char = "ab"
cannot initialize 'a': declared [2]char but the value is string

A byte above 0x7F that is not part of a UTF-8 character — a boot sector’s 0xEB jump — cannot be written in a string literal at all, since a string is valid UTF-8; that is an array literal’s job.

A file’s bytes, embedded

Fixed data often comes as a file — an image, a font, a program a kernel carries before it has a disk. embed("path") is that file’s bytes, read while compiling, as a []const u8 over a constant the object file carries, as include_bytes! is in Rust and #embed in C. This program is main.sysl, and embeds itself:

// hello, from the file itself
import sysl.raw.str_cast

val me = embed("main.sysl")

print(str_cast(me[3..<8]), me[0] == u8('/'))
hello true
  • A relative path is read from the directory of the file that writes it, never from wherever the build was started, so a source file and the data beside it move together and the tree builds from any directory. An absolute path is taken as written.
  • The bytes start on a 16-byte boundary, as aligned as any scalar a target has, so a header read in place from the start of an image is aligned even on a machine that faults on an unaligned load. embed("disk.img", align = 4096) asks for more; the boundary is a constant power of two.
  • It is constant data, so a val holding one is laid down rather than filled by code — a module with no allocator and no entry point, a kernel’s, may hold one — and a file embedded in several places is laid down once.
  • The build depends on the file: its bytes are in the key of every cache that replays a build, so an edit to it is a different program, exactly as an edit to the source is.
  • embed is a form only where nothing else claims the name: a function or a local called embed is what a call reaches.

A file that is not there is refused while compiling, naming the path that was tried:

print(embed("logo.bin").len)
cannot embed 'logo.bin': there is no file at '

The path is a string literal and nothing else. A build that is replayed has to know which files it read without being compiled again, and a literal is what can be found in the source text; a const or a computed string is refused:

val name = "logo.bin"

print(embed(name).len)
'embed' reads its file while compiling, and a build that is replayed has to know which files it read without compiling again — so the path is written as a string literal, not computed

A directory’s files, embedded

A firmware image with a few hundred C sources, a site with its templates, a game with its levels: listing each file in an embed line means editing the list whenever a file is added or moved. embed_dir("dir") is every file under a directory, read while compiling, as a []const Embedded — each element the file’s path below the directory and its bytes, laid down as embed lays one file down. glob = "pattern" keeps the files it matches. This program is main.sysl, alone in its directory, and lists the sources beside it:

// the line every embedded copy starts with
import sysl.raw.str_cast

for e in embed_dir(".", glob = "*.sysl")
    print(e.path, str_cast(e.bytes[3..<6]))
main.sysl the

Embedded is the standard module’s own struct, named without an import:

struct Embedded
    path: string
    bytes: []const u8
  • The order is the paths’ bytes, with / between their parts on every host — B.c before a.c, sub-1.c before sub/x.c — so the list is the same on every machine, whatever order a filesystem hands its entries out in.
  • files.find(path) is the bytes of the entry whose path is exactly path, as an Option[[]const u8]: None for a path no entry has. The order above is what makes it a binary search, with no allocation, so it runs on a bare target, and it runs while compiling: a module val holding its answer is constant data, as the list is. A prefix (sub for sub/x.c) and a path of another case are not matches. A list built some other way and not sorted by the path’s bytes gets an unspecified answer, which is the caller’s own lookout.
// lookup by path
import sysl.raw.str_cast

val files = embed_dir(".", glob = "*.sysl")

files.find("main.sysl") match
    Some(b) -> print(str_cast(b[3..<9]))
    None -> print("missing")

print(files.find("main").is_none())
lookup
true
  • The pattern is sysl.path.matches‘s, read against that path: * stays in one directory and ** crosses any number, so glob = "*.c" is the top level and glob = "**/*.c" is every depth. With no glob, every file at any depth is listed.
  • A hidden file or directory — one whose name begins with . — is left out unless the pattern names its dot: glob = ".*", glob = ".config/*". A .git beside the sources never ends up in the program.
  • A link is followed. A link to a file is embedded under the link’s own name, and a link to a directory is walked as the directory. One that leads back to a directory holding it is refused, since the walk would never end.
  • It is constant data: the list, its paths and every file’s bytes are laid down in the object file, with no allocation and no startup code, in a module val or inside a function, on a bare target as on a host. So is a module val worked out from it — its len, an entry’s path or bytes, a find, a loop over it in a function it calls — and one that is the whole of a file’s bytes names the embedded file itself, on its boundary. align = n sets every file’s boundary, as it does for embed.
  • The build depends on the listing as well as the bytes: a file added, removed, renamed or edited is a different program to every cache that replays a build.
  • The path and the pattern are string literals for the reason embed‘s path is, and embed_dir is a form only where nothing else claims the name.

A match of nothing is refused, since a mistyped pattern would otherwise embed nothing without a word:

print(embed_dir(".", glob = "*.cc").len)
holds no file that 'glob = "*.cc"' matches — hidden files are left out unless the pattern names them, and a match of nothing is refused, since a mistyped pattern would otherwise embed nothing

Storage sized while running

Every form so far fixes its length in the type, which a program reading a file cannot do: the size is in the header, and the header is read by the code that needs the buffer.

A sequence whose length is not in its type is exactly what a []T is, so no new spelling is needed. The expected type decides which reading a literal or a repeat gets:

decode(header: []const u8) -> []u8
    var out: []u8 = [0; header[0]]

    for i in 0..<out.len do out[i] = header[1] + u8(i)

    out

var frame: [2]u8 = [3, 10]
var body = decode(frame)

print(body.len, body[0], body[2])
3 10 12

Under a [N]T a non-constant count is the error above; under a []T a constant count is also accepted. The three spellings side by side:

var buf: [64]u8 = [0; 64]     // an array — the count is in the type, so it is constant
var raw: []u8   = [0; n]      // a view of fresh storage — n is any expression
var xs:  []int  = [1, 2, 3]   // likewise, with the elements written out

The storage is the view’s own. The owner word is an ordinary counted reference to the elements, so everything under ownership is already true of it: a sub-slice retains it, the last view to go releases it, and the elements are destroyed before the bytes go back. Nothing about indexing, slicing, .len or iterating tells such a view apart from one over another owner’s elements, so a separate type would add nothing.

What it adds is leaving the frame, as the program above does: a decoder that learns its size from what it is decoding returns a result, instead of asking its caller to size a buffer whose size is in a header the caller has not read.

Three things are checked, because a computed length is where arithmetic goes wrong. The count is widened to the address width and read unsigned, so a negative one arrives as a very large one; the byte size is computed with an overflow-checked multiply and add, so a count that would wrap cannot allocate a small buffer that is then written past; and a failed allocation traps rather than handing back a null the elements are then stored through.

Indexing

a[i] reads the element, and it is a place — so a[i] = v, a[i] += 1, a[i]++ and &a[i] all follow from the general rules for assignment and address-of, with nothing specific to arrays:

var a = [10, 20, 30, 40]

a[0] += 5
a[1]++

var p = &a[2]

*p = 99

print(a[0], a[1], a[2])
15 21 99

The index may be any integer type. Requiring usize would make for i in 0..<10 do a[i] … need a conversion for no benefit, since the check has to happen anyway:

var a = [10, 20, 30, 40]
val narrow: i8 = 1
val unsigned: u8 = 2
val sized: usize = 3

print(a[narrow], a[unsigned], a[sized])
20 30 40

The index is widened to the address width and compared unsigned against the length. That is one comparison rather than two, and it rejects a negative index as a very large one, the standard technique for a bounds check. (It is the address width rather than a fixed 64 bits because it is compared against a length, and a length is a usize.)

An index already wider than an address — a u128 anywhere, or an ordinary int on a 16-bit machine — is asked whether it fits and then narrowed, in that order. Nothing holds more than usize elements, so a value that does not fit names no element and traps like any other index out of range; testing before narrowing is what stops 2^64 + 5 arriving as 5 and passing on a six-element array. So for i in 0..<n do a[i] … needs no conversion on any machine.

A failed check traps

var a = [1, 2, 3]
var i = -1

print("before")
print(a[i])

The process stops at the index. This is the same runtime-safety category as the partial char(u) conversion and a mid-character string slice, and it gets the same treatment: a trap instruction, no message, and no unwinding. Output already written but not yet flushed is lost with it, so before does not appear; a truncated log is therefore not reliable evidence of how far a program got.

A check a guard already paid for is folded

Plain + on an unsigned integer wraps, so a guard written as n + len > out.len proves nothing about out[n + k]: a huge n wraps the guard’s sum to a small value, the guard passes, and only the subscript’s own check stops the program. Written so that nothing in it can wrap, the guard does settle the subscripts after it:

fill(out: []u8, n: usize, len: usize, v: u8) -> bool
    if n > out.len || len > out.len - n then return false

    for k in 0..<len
        out[n + k] = v

    true

var buf = [u8(0); 8]

print(fill(buf, 2, 3, 7), fill(buf, 6, 3, 7), fill(buf, 18446744073709551615, 3, 7))
print(buf)
true false false
[0, 0, 7, 7, 7, 0, 0, 0]

Past that guard n <= out.len and len <= out.len - n, so the compiler lays n + len and n + k (for k running below len, in a loop entered after the guard) down as additions that cannot wrap — LLVM’s add nuw — and repeats the guard’s conclusion as an llvm.assume where the fall-through begins. The optimizer can then see that every out[n + k] is inside, and the check goes. It costs nothing at run time: the flag and the assumption emit no code.

The fact lasts until the first statement that writes n, len or out — a loop whose body writes one of them is outside it from the start — and a local whose address is taken anywhere in the function never takes part. The same flag goes on any unsigned add both of whose operands are bounded by half the word: a view’s .len (no object reaches past half the address space), a literal no larger than isize::MAX, a narrower unsigned value widened, a & with one of these, a % by one, a right shift or a division by a literal, or a local never written after its declaration, or a loop variable, bounded by one of them.

A loop’s subscripts are checked once

A check inside a loop is a branch out of it on every turn, and that branch costs more than its compare: it stops the optimizer carrying values from one turn to the next, so a loop that appends seq[i - 1 - j] to a Buf reads the buffer’s count and capacity again for every element. So where a for loop’s subscripts can be checked for the whole loop before it starts, the compiler lowers the loop twice: once with those checks left out, and once as written, with a test in front that picks the first only where every index the loop will reach is in range. A loop that fails the test runs as written, and stops at the same index, with the same message, as it always did.

import sysl.buf.{Buf, buf}

// `seq` back to front, `width` bytes to a line.
reversed(seq: []const u8, width: usize)
    val out: Buf[u8] = buf()
    var i = seq.len

    while i > 0
        val n = if i > width then width else i

        for j in 0..<n do out.push(seq[i - 1 - j])

        out.push(u8('/'))
        i -= n

    out.push(u8('\n'))
    putbytes(out.view())

reversed("abcdefg".bytes, 3)
gfe/dcb/a/

The test is made each time the loop is entered — here once per line, against the i of that line. What it can prove is a subscript of an array, or of a view held in a local the loop leaves alone, whose index is built with + and - from the loop variable (once at most), literals, the .len of such a view, and locals the loop neither writes nor reaches by address. Such an index moves by one each time the variable does, so the test works out its value at the first turn and at the last, in arithmetic wide enough to be exact, and asks that both are in range and fit the index’s type; every turn between is then in range too. A loop that runs no turn passes it.

Anything else keeps its check on every turn, in both copies: an index with a *, one reading the variable twice, one through a call. A loop is not copied at all where its body writes its variable or reaches it by address, holds a loop of its own, returns, uses ? or defer, is in an async function, or is long; nor where the only subscripts are xs[j] with j running below xs.len, whose check is the loop’s own condition and is folded anyway, and ys[j] past an if xs.len != ys.len then return … that made the two one length, which folds the same way. An index the body moves is checked where it is read:

show(xs: []const int)
    var k: usize = 0

    for j in 0..<3
        print(xs[k + j])
        k += 1

show([1, 2, 3, 4])
1
3

A raw pointer is indexed anyway

A *T is the one receiver with nothing to check against, and it can be subscripted anyway: that is C’s subscript, unchecked address arithmetic. Slicing one needs the end written, because nothing in the type can supply it:

total(xs: []const int) -> int
    var t = 0

    for x in xs do t += x

    t

var back = [7, 8, 9]
var p = &back[0]

print(p[1], total(p[0..<2]))
8 15

The resulting view owns nothing, since a *T region has nothing to keep alive. The check is a property of the type, not of the syntax: a *[N]T, whose length is in its type, keeps every check an array has. A slice keeps a program safe; a pointer is how it talks to hardware and to C, and the language supplies both.

A pointer to something with a subscript of its own is indexed as that thing. The *[N]T above is one; a pointer to a container indexed through a trait (the next section) is the other. p[i] on a *Buf[int] is the ith element, checked by the Buf — exactly what p.len() and (*p)[i] reach — and a write through it lands in the element, never the ith Buf along:

import sysl.buf.{Buf, buf}

first(b: *Buf[int]) -> int = b[0]

bump(b: *Buf[int])
    b[0] += 1

var b: Buf[int] = buf()

b.push(41)
bump(&b)

print(first(&b), b.len())
42 1

The shorthand reaches one level, as selection does, so a **Buf[int] is subscripted as the pointer it is. A generic decides by the bound, not by the instantiation: in nth[T](p: *T, i: int) -> T = p[i] the subscript is the ith T whatever T turns out to be, an array or a Buf included, while a [C: Index[usize, int]](p: *C) reads p[i] as the container’s element at every instantiation.

Unchecked access

Where a program has proved an index is inside and the optimizer cannot see the proof, the check can be left out by name. xs.get_unchecked(i) reads the ith element and xs.set_unchecked(i, v) writes it, on an array or a slice, with no test of i at all; everything else about the call — the element type, a val that may not be written, a read-only view — is checked as a subscript is.

val xs = [10, 20, 30]
var ys = [0; 3]

ys.set_unchecked(2, xs.get_unchecked(1))
print(ys)
[0, 0, 20]

For a stretch of code rather than one subscript, @unchecked on a line of its own marks the block indented under it: every subscript written inside it is read with no bounds check.

dot(a: []int, b: []int) -> int
    var sum = 0
    val n = if a.len < b.len then a.len else b.len

    @unchecked
        for i in 0..<n
            sum += a[i] * b[i]

    sum

print(dot([1, 2, 3], [4, 5, 6, 7]))
32

A vector load or store is a subscript of several elements, and the block covers it. Inside @unchecked, xs.load(i) and xs.store(i, v) on an array or a slice (see vectors) leave out the test that the whole run fits, exactly as xs[i] leaves out its test; outside the block the run is still checked. Like the scalar subscript, a run that does not fit is not a trap: it reads or writes whatever the memory holds.

var xs: [8]f32 = [1, 2, 3, 4, 5, 6, 7, 8]
var ys: [8]f32 = [0; 8]

@unchecked
    val v: <4>f32 = xs.load(4)
    ys.store(0, v * 2)

print(ys[0], ys[3])
10 16

The block is lexical. A closure or a nested function written inside it is inside it; one written outside and called from inside keeps its checks, and so does every subscript after the block ends. A Buf, and any container whose subscript is its own, takes part through its IndexPlace implementation: inside the block b[i] is b.index_place_unchecked(i), whose default is the checked index_place, so a container that has not said how to skip its test keeps it. Buf says how, and has get_unchecked and set_unchecked of its own.

An index out of range in either form is not a trap: the read or write lands wherever the address falls, as it does through a raw pointer. What the compiler evaluates while compiling still checks, so a constant that reads past the end is refused, block or not. @unchecked marks a block and nothing else:

val xs = [1, 2, 3]

@unchecked print(xs[0])
'@unchecked' marks a block: write it on a line of its own and indent the statements it covers under it

There is no unsafe block to stand them in: like from_utf8_unchecked and a raw pointer’s *, the spelling is the marker a reader searches for.

A type with no elements of its own is indexed through a trait

Everything above is about the built-in subscript rather than about [] as a token. A user type — the library’s Buf[T], a lookup table, anything a program writes — implements IndexPlace or Index and is read with the same syntax:

import sysl.buf.{Buf, buf}

var b: &Buf[int] = buf()

b.push(1)
b.push(2)

b[0] = 9

print(b[0], b.len())
9 2

A type that implements IndexPlace has elements that are places, as an array’s are. Its one method answers the element’s address, checked against the bounds, and b[i] means *b.index_place(i) — so b[i].f reads one field where it lives, b[i] += 5 writes the element, and ref e = b[i] names it. Buf is one:

import sysl.buf.{Buf, buf}

var b: &Buf[int] = buf()

b.push(1)

b[0] += 5

print(b[0])
6

A program’s own container takes the same trait. It is declared *self, so a container whose elements are its own bytes answers an address in the container and not in a copy of it:

struct Ring
    cells: [4]int
    head: usize

impl IndexPlace[usize, int] for Ring
    index_place(*self, i: usize) -> *int = &self.cells[(self.head + i) % 4]

var r = Ring([1, 2, 3, 4], 2)

r[0] *= 10

ref last = r[1]

last = 0

print(r.cells)
[1, 2, 30, 0]

Several indices in one subscript

A container indexed by a tuple or an array takes its index as a list: g[i, j] means g[(i, j)] where the trait is over (usize, usize), and g[[i, j]] where it is over [2]usize. It is the same subscript in every other way — a place through IndexPlace, a call through Index and IndexSet — so it is read, assigned, compounded, borrowed with & and named with ref exactly as b[i] is:

struct Grid
    cells: [6]int

impl IndexPlace[(usize, usize), int] for Grid
    index_place(*self, at: (usize, usize)) -> *int = &self.cells[at.0 * 3 + at.1]

var g = Grid([1, 2, 3, 4, 5, 6])
val row = 1

g[row, 2] += 10
g[0, 0] = g[row, 0]

print(g[0, 0], g[1, 2], g.cells)
4 16 [4, 2, 3, 4, 5, 16]

Each part is a subscript’s index where it lands. A part whose field is a usize takes any integer, as b[i] does, and traps on a negative one; a part of any other type — a key, a Range — is an ordinary argument, so a container can take a range in one position and an index in another:

struct Sheet
    cells: [6]int

impl Index[(Range[usize], usize), int] for Sheet
    index(self, at: (Range[usize], usize)) -> int
        var sum = 0

        for r in at.0
            sum += self.cells[r * 3 + at.1]

        sum

impl Index[[2]usize, int] for Sheet
    index(self, at: [2]usize) -> int = self.cells[at[0] * 3 + at[1]]

val s = Sheet([1, 2, 3, 4, 5, 6])

print(s[0..<2, 1], s[1, 2])
7 6

A type may be indexed both ways, by one index and by several, and s[i] and s[i, j] each find theirs. The count is the tuple’s or the array’s, and a different one is refused:

struct Cube
    cells: [8]int

impl Index[(usize, usize, usize), int] for Cube
    index(self, at: (usize, usize, usize)) -> int = self.cells[at.0 * 4 + at.1 * 2 + at.2]

val c = Cube([0, 1, 2, 3, 4, 5, 6, 7])

print(c[1, 0])
2 indices were written, and this subscript takes 3 — its index is (usize, usize, usize)

The list is a subscript only on a value indexed through one of the three traits. After a generic’s name the same brackets are its type arguments (reference/generics.md), and a built-in array keeps one index: an array of arrays is indexed a level at a time, m[i][j].

var m = [[1, 2], [3, 4]]

print(m[1, 0])
a subscript takes one index, and 2 were written

Writing through a subscript

A write through a subscript goes through index_place_mut; a read goes through index_place. A write is the target of an assignment or a compound assignment, the operand of &, ++ or --, the place a ref names, a destructuring target, and the receiver of a *self method called on the element. The subscripts it writes through are every one on the path from the place down to the first step that keeps its elements somewhere else: o.inner[1].n = 7 writes through o.inner[1], while ps[0].n = 1 with *P elements writes through the pointer and only reads ps[0]. A subscript read inside a write — c[c[0].n].n = 3 — is still a read.

The member’s default is index_place, so a container that leaves it out is indexed the same way at a write as at a read. A container that owes something before an element is written — making shared storage its own, counting the write — overrides it. Inside an @unchecked block a write still goes through index_place_mut, since a container that wrote one has something to do before the write:

import sysl.buf.{Buf, buf}

static var reads = 0
static var writes = 0

struct Logged
    cells: Buf[int]

impl IndexPlace[usize, int] for Logged
    index_place(self, i: usize) -> *int
        reads += 1
        self.cells.index_place(i)

    override index_place_mut(*self, i: usize) -> *int
        writes += 1
        self.cells.index_place(i)

var l = Logged(buf())

l.cells.push(1)
l.cells.push(2)

l[0] += 10
l[1] = l[0] + 1

ref r = l[1]

r += 1

print(l[0], l[1], reads, writes)
11 13 3 3

The write member takes *self, so a write through a val is refused even where index_place takes self and the val may still be read:

import sysl.buf.{Buf, buf}

struct Logged
    cells: Buf[int]

impl IndexPlace[usize, int] for Logged
    index_place(self, i: usize) -> *int = self.cells.index_place(i)
    override index_place_mut(*self, i: usize) -> *int = self.cells.index_place(i)

val l = Logged(buf())

l[0] = 1
'index_place_mut' takes '*self', so it writes through what it is called on, and a 'val' is written once — write 'var l' if it is meant to change

A generic body over an IndexPlace[I, E] bound is read through index_place where it is defined; each instantiation is read again at its concrete type, so it writes through that type’s own member.

A view of part of an element keeps the storage the element lives in. Where the element is an array, c[i][..], c[i].cells[1..] and c[i][j][..] are taken through the trait’s index_view rather than index_place: c[i][..] is c.index_view(i)[0][..], the element read as the one element of a view, so the view taken carries that view’s owner and stays good after the container grows or is dropped. A container whose elements live in a Buf or a view says so by answering a part of it — Buf does, which is all that makes b[i][..] safe.

A container that is viewed into writes its index_view; it is never written for it. Only the container knows what keeps its elements alive, so its block answers the view from that storage — a field’s own view, a part of a view field, a part of its own array:

import sysl.buf.{Buf, buf}

struct Rows
    items: Buf[[4]int]

impl IndexPlace[usize, [4]int] for Rows
    index_place(self, i: usize) -> *[4]int = self.items.index_place(i)
    override index_view(self, i: usize) -> [][4]int = self.items.index_view(i)

var r = Rows(buf())

r.items.push([1, 2, 3, 4])

val v = r[0][1..]

for k in 0..<100 do r.items.push([k, k, k, k])

r = Rows(buf())

print(v)
[2, 3, 4]

A container whose elements are its own bytes answers a part of its array (self.cells[i..<i + 1]), which owns nothing and lasts as long as the container: a view of part of such an element leaves a frame only where the container lives outside it — in module storage, or reached through a *T — and is refused where the container is the frame’s own (Memory).

The trait’s default index_view answers a view whose owner is nothing, and a view of part of an element taken through it is refused, wherever the elements live; so is a call of the default itself. The diagnostic gives the one line that answers it:

import sysl.buf.{Buf, buf}

struct Hidden
    items: Buf[[4]int]

impl IndexPlace[usize, [4]int] for Hidden
    index_place(self, i: usize) -> *[4]int = self.items.index_place(i)

var h = Hidden(buf())

h.items.push([1, 2, 3, 4])

val v = h[0][1..]

print(v)
a view of part of an element of 'Hidden' is taken through the default 'index_view', which owns nothing of the storage the element lives in — write 'index_view' in the container's 'impl IndexPlace' block, answering the element as a view of that storage: 'override index_view(*self, i: usize) -> [][4]int = <storage>.index_view(i)'

override index_view(self, i: usize) -> [][4]int = self.items.index_view(i) in the block is the answer here. index_view_unchecked is the member a view inside an @unchecked block is taken through, and is written the same way where the container is viewed into there.

Where the method takes a usize, the subscript takes any integer, exactly as an array’s does: the compiler brings the index to a usize before the call, so b[i] with i an int, an i8 or a u64 needs no conversion written, and the index fixes no literal binding’s type. This holds for Index, IndexPlace and IndexSet alike, and for a type parameter bounded by one of them:

import sysl.buf.{Buf, buf}

var b: &Buf[int] = buf()

b.push(10)
b.push(20)

val i = 1
val narrow: i8 = 0

b[narrow] += 5

print(b[i], b[narrow])
20 15

A negative index stops the program at the subscript, as an array’s does, since no usize holds it; the method is never called. A negative literal is refused while compiling:

import sysl.buf.{Buf, buf}

at(b: &Buf[int], i: int) -> int = b[i]

var b: &Buf[int] = buf()

b.push(1)
print("reading")
print(at(b, 0 - 1))
reading
import sysl.buf.{Buf, buf}

var b: &Buf[int] = buf()

print(b[-1])
the literal -1 does not fit usize

What happens to an index past the end is the method’s to decide: an array’s check traps, and the standard containers’ — Buf, Frozen, Deque, Ring — trap the same way, reporting the index and the count through sysl_bounds_fail as an array’s check does, without the caller’s location (Buf). A method that takes some other type — an int, a string key — is handed the index as it was written, and an argument of the wrong type is refused as it would be in a call.

A type that implements only Index is read by a call, which yields a value rather than a place. The index is whatever the implementation takes, and need not be an integer; b[i] = v reaches a second trait’s method, IndexSet, and the compound forms are refused, since expanding them would evaluate the receiver and the index twice (core).

Note also that b.len() has parentheses where a.len does not: the built-in length is a property of a type the compiler knows, and Buf‘s is an ordinary method. Nothing a program writes competes with the built-in subscript — an array, a slice and a string are indexed by the compiler.

Slicing

A slice expression is an index whose subscript is a range, and the two range operators keep the meanings they have everywhere else: .. includes its high end, ..< excludes it.

var d = [1, 2, 3, 4, 5]

print(d[2..4].len, d[2..<4].len, d[2..].len, d[..<2].len, d[..].len)
3 2 3 2 5

The inclusive .. differs from C-family habit, but the range operators mean the same thing everywhere: for i in 0..<n and a[0..<n] cover the same indexes. So “the first n“ is a[..<n], matching the loop that walks it.

Both ends are optional: an omitted low end is 0, an omitted high end is the last element. Because “through the last” is not a question of including or excluding anything, only one spelling of the open-ended form exists:

var a = [1, 2, 3, 4]
var v = a[1..<]
an open-ended slice is written 'a[lo..]'

The check is on the half-open interval the slice ends up naming. With s the first index and e one past the last, s <= e and e <= len must both hold, and the inclusive form additionally requires that its named high element exist. An empty slice is legal, including at the very end:

var d = [1, 2, 3, 4, 5]

print(d[d.len..].len, d[2..2].len)
0 1

What can be sliced: a fixed array, a slice, or an array reached through a &[N]T or a *[N]T — one-level auto-deref applies to a subscript as it does to field selection, so the expression reads the same whether the name is the array or a reference to it:

total(xs: []const int) -> int
    var t = 0

    for x in xs do t += x

    t

var boxed: &[4]int = [1, 2, 3, 4]

print(total(boxed), boxed[2], boxed.len)
10 3 4

The call passes boxed itself rather than boxed[..]; the next section explains why that works.

A type read by a subscript through Index or IndexPlace is sliced through its view. Where it has a view answering its elements as a slice, c[lo..<hi] means c.view()[lo..<hi], with that slice’s type, bounds trap and lifetime. A range is never the index the trait is asked for. A Buf is sliced this way, and so is a Frozen. A type of your own gets the same by writing the member:

import sysl.buf.{Buf, buf}

struct Scores
    all: Buf[int]

impl Index[usize, int] for Scores
    index(self, i: usize) -> int = self.all[i]

impl Scores
    view(self) -> []const int = self.all.view()

var all: Buf[int] = buf()

for x in [70, 85, 92] do all.push(x)

val s = Scores(all)

print(s[1], s[1..], s[..<1].len)
85 [85, 92] 1

Without a view, the slice is refused, and the refusal names the member that is missing:

struct Pair
    a: int
    b: int

impl Index[usize, int] for Pair
    index(self, i: usize) -> int = if i == 0 then self.a else self.b

val p = Pair(1, 2)

print(p[0..<2])
cannot slice Pair: a type read by a subscript through 'Index' is sliced through its 'view()', which answers its elements as a slice, and it has no 'view'

An array where a view is asked for

A [N]T standing where a []T is wanted is a[..], written by the position instead of by hand. An array already knows both halves a view is made of — where the elements are, and how many — so nothing is taken on trust and no bound is guessed.

This matters most for an API built on caller-supplied storage, the usual style of freestanding code: the caller lends, the callee fills, and nothing allocates.

fill(s: []int, v: int)
    for i in 0..<s.len do s[i] = v

var a = [0; 4]

fill(a, 7)

print(a[0], a[3], a.len)
7 7 4

It happens at every position that asks for a view rather than merely requiring one — an argument, a val or var with the view’s type written on it, a return, a parameter’s default — and a &[N]T converts on the same terms, with the reference as what the view is taken over.

The conversion does not change the storage’s own terms. A view of a val array is a []const T, exactly as a[..] would give, so a val array passed where a []T is wanted is refused, and the diagnostic says why:

fill(s: []int, v: int)
    for i in 0..<s.len do s[i] = v

val a = [1, 2, 3]

fill(a, 7)
is a licence to write them — so the one does not become the other

A &sync [N]T converts too, into a view that is only lent (§ Views of storage inside a ‘&sync’ box, below). A *[N]T is excluded deliberately: the raw-pointer tier is written out where it is used, and a view of pointed-at storage taken silently is the one case where nothing in the type says the elements are really there.

An empty literal is a view of nothing, and needs no allocator

[] where a view is wanted is the empty view, which is the zero value of what a view is made of — no elements, no length, and no owner. So it makes no storage and reaches no allocator, and a module without a heap may write one.

@no_alloc

val none: []const int = []
var also: []int = []

print(none.len, also.len)
0 0

It takes its element type from the position it stands at, having none of its own — an [] with nothing asking for a view is refused for exactly that reason.

A literal with elements in it is different, and the difference is where the storage comes from: at a []T such a literal builds a buffer of its own, which needs an allocator. Where the elements are laid out in the caller’s frame instead, what is written is an array and a view of it — [1, 2, 3][..] — and that is the form a variadic call packs its arguments into; see declarations.

A sliced array literal takes its element type from the view that was asked for, which is the one receiver with no type of its own:

show(xs: []const &Display) -> unit
    for x in xs
        print(x)

show([1, "hi", true][..])
1
hi
true

[]const T — a view that may not be written

The const sits after the brackets, where sync sits after the &, and for the same reason: it is a property of the view, not of the element type. It is the signature a function that only reads should have.

scale(xs: []const int)
    xs[0] = 99

var data = [1, 2, 3]

scale(data)
which views elements it may not write, so there is nothing to assign through

The same diagnostic answers xs[i] += 1 and xs[i]++, and ends by naming the alternative: to write the elements, copy them into a []T of your own first.

Every other road to an element is the same write. An assignment to a part of one (xs[0].n = 1, xs[0].arr[1] = 3) is refused as the element is, and so is a *self method called on an element or a part of one, since *self is a licence to write handed over without an & being written:

struct Counter
    n: int

impl Counter
    bump(*self) = self.n += 1

tally(xs: []const Counter)
    xs[0].bump()

var cs = [Counter(1)]

tally(cs)
'bump' takes '*self', so it writes through what it is called on, and this element belongs to a '[]const Counter', which views elements it may not write

A ref bound to an element is a second name for it, so it may be read through and not written:

scale(xs: []const int)
    ref first = xs[0]
    print(first)
    first = 4

var data = [1, 2, 3]

scale(data)
this element belongs to a '[]const int', which views elements it may not write

A (self) method is a copy and reads freely, and so does a ref that is only read. What the view leads to by address is not its own: an element that is a &T box, or a []T field of an element, is written exactly as its owner allows.

It is one type with a bit, not two types. Both forms are the same three words, reach through the same instructions, and keep the same thing alive; what the bit changes is only what may be done with the view. So a []T is accepted wherever a []const T is wanted — and never the other way round:

writes(xs: []int) -> usize
    xs[0] = 9
    xs.len

hand_over(xs: []const int) -> usize
    writes(xs)
is a licence to write them — so the one does not become the other

A caller can always give up the ability to write; gaining it is what the type exists to prevent.

Where two of them have to agree, they meet at the read-only view. A pair of operands, the branches of an if, the arms of a match, a loop’s break values — each is a place where two types have to become one. Apart from the lossless numeric conversions (§ Which conversions are implicit), the two views are the only pair of concrete types with a common type to meet at; any other two must agree or the form is refused, since there is no subtyping to widen towards.

var a = [1, 2, 3]
var b = [1, 2, 3]

val c: []const int = b[..]
val k = if a.len > 0 then a[..] else c

print(a[..] == c, c == a[..], k.len)
true true 3

Without this rule the order would decide: the first of the two would settle the type and the second would be read at it, so a form and its mirror image could disagree about the same two values.

What they meet at is the read-only view, so this is not a way of inventing a licence to write:

var a = [1, 2, 3]
val c: []const int = a[..]

var k = if a.len > 0 then a[..] else c

k[0] = 9
views elements it may not write, so there is nothing to assign through

Two writable views are untouched — nothing is read-only, so there is nothing to meet — and a form with only one branch keeps that branch’s view whichever it is.

What produces one. Slicing a val array, since read-only storage gives a read-only view — and so is a val array standing where a view is asked for, which is the same rule reached without the brackets. An array inside a val struct value is the binding’s storage too. s.bytes, whose elements are a string’s own and may be a literal’s. Re-slicing one, because a bit a second subscript dropped would make xs[..] the way around xs. And a buffer literal written where one is wanted, since storage an expression makes has no other holder to disagree with it.

readonly(xs: []const int) -> usize
    var again = xs[1..]
    again.len

first(xs: []const int) -> int
    val p = &xs[0]
    *p

var a = [1, 2, 3, 4]

print(readonly(a), first(a))
3 1

A val holding an address is not read-only storage of elements. val is a promise about the name’s own bytes, and a *T, a &[N]T or a []T is an address: the elements it leads to belong to whoever laid them down, so a view taken through one is exactly as writable as that owner allows.

fill(s: []int, v: int)
    for i in 0..<s.len do s[i] = v

var store = [1, 2, 3, 4]

val p: *int = &store[0]
val whole: []int = store[..]
val heap: &[2]int = [5, 6]

fill(p[0..<1], 7)
fill(whole[2..], 8)
fill(heap, 9)

print(store[0], store[3], heap[1])
7 8 9

A read-only view still allows &. &xs[0] is a *T the moment it is written, the tier where the guarantees stop, and it is how a view reaches a C function taking a pointer and a length. The library’s own find_byte is memchr over exactly this, and a read-only view that could not yield an address could not be passed to C at all. This is not the rule for a val itself, where &k[0] is refused: a val is storage whose promise is kept where it was made, and a view is a value whose promise is about writing through it.

[N]const T is not a type. An array is storage rather than a view of one, and storage written once is what val declares:

frozen() -> int
    var table: [4]const int
    table[0]
'const' says a view refuses writes, and an array is storage rather than a view of one

What a view does not record is whose elements these are, whether they outlive the program, and whether their owner’s count is atomic. Those are properties of the owner, which a view cannot describe; the rule for views of storage inside a &sync box, below, follows from that.

Length

a.len is the number of elements, as a usize. On a [N]T it is the constant N and costs nothing; on a []T it is the third word.

It is a property — a member read without parentheses — because a length is a projection of what is already there rather than a computation over it. On the built-in array and slice types it is compiler-provided, so a.len reads the same whether a is a fixed array whose length is a constant or a slice whose length is a word it carries.

Iterating

var xs = [1, 2, 3]

for x in xs
    x = x * 10

for i in 0..<xs.len
    xs[i] *= 10

print(xs[0], xs[1], xs[2])
10 20 30

for x in seq over an array or a slice binds a copy of each element, as value semantics require — the first loop above multiplies three copies and discards them. Changing the sequence goes through the index form. The loop evaluates its sequence once, so a slice written directly in the header lives for the whole loop rather than being rebuilt each step.

Storage is walked by index, so a container is not an iterator. A for also accepts a cursor — a value implementing Iterate — but no built-in sequence implements it, and Buf deliberately does not: its elements are already in memory, and reading them costs an index where a cursor would cost a call per element. The protocol is for sequences whose elements have to be computed, which a container’s never are. What Buf implements instead is Walk, whose walk() answers the view, so for x in b asks for it once and then walks the slice by index.

Asking one a question

An array is a Sequence at every length, as a slice and a Buf are, so any, all, find, position, count_where, fold, each, map, filter and flat_map are called on it directly:

val RUNS = ["warmup", "sprint", "cooldown"]

print(RUNS.any(r -> r == "sprint"))
print(RUNS.count_where(r -> r.len > 6))
print(RUNS.map(r -> r.len)[2])
print(RUNS.find(r -> r.starts_with("c")).unwrap())
true
1
8
cooldown

Asking costs no copy. An array too large to travel in registers crosses as the address of the storage it is in, and these members only read it, so RUNS.any(p) walks the module’s own RUNS. The answers that are sequences — map, filter, flat_map — are new buffers, never views of the array. A generic S: Sequence[T] takes an array as it takes a slice.

What a view keeps alive

Taking a slice retains the owner; dropping one releases it. That is what guarantees a slice never dangles, and it holds across a reallocation:

import sysl.buf.{Buf, buf}

var b: &Buf[int] = buf()

b.push(1)
b.push(2)

var v = b.view()

for i in 0..<100 do b.push(i)

print(v.len, v[0], b.len())
2 1 102

The pushes moved the elements to fresh storage. The view made before the move keeps the old storage alive and goes on showing what it was made from; it does not become invalid. It does not grow with the buffer either: it is a view of some elements and keeps the length it was made with. Take it again to see more.

Two consequences for how this is implemented:

  • The owner word is null whenever there is nothing to keep alive — a view of a string literal, of static storage, or of a *T region — so retain and release on a slice must tolerate null. The &T path does not pay for that check: a reference is non-null by construction, so only slices need to handle null, and they have their own pair of runtime helpers.
  • Release cannot be per payload type. A []u8 may view a 64-byte buffer at one moment and a 4096-byte one at the next, so the static type of the slice does not name the type of the object its owner points at. Giving back a count is therefore type-erased, through the deallocation hook every ARC object carries, so releasing a slice’s owner is the same instruction sequence as releasing any other reference.

A view that would outlive the array it was made from does not fail: the array is promoted to a buffer instead, so a program that means to return one writes the ordinary var buf: [64]u8 and says nothing. Memory is where that is settled, along with what is still refused — storage the body did not declare.

The one view with nothing to keep alive and something that can die is a view of memory a C library owns, made over a *T by an object that frees it in its destructor. Its owner word is null, so the view cannot hold the object; instead the member that hands it out is marked @views(self), and the compiler refuses every use of the view that could come after the object it was called on is freed:

extern "free" c_free(p: *u8)

struct Rows
    p: *u8
    n: usize

    @views(self)
    view(&self) -> []const u8 = self.p[0..<self.n]

impl Drop for Rows
    drop(self) = c_free(self.p)

newest() -> []const u8
    val r: &Rows = Rows(null, 0)

    r.view()
this returns a view borrowed from 'r', and nothing keeps 'r' alive once this function returns

The view is still a plain []const u8 at no run-time cost. Memory has the four places it is refused.

Reading a view as another element

xs.view_as[T]() is the same storage read as Ts: the same owner, the same first byte, and the length counted again — eight bytes are two u32s, and two u32s are eight bytes. It is called on a view or on an array, which is read whole. Nothing is copied, so a write through either side is seen through the other, and the view keeps the storage alive exactly as the view it was made from does:

@align(4)
var packet: [8]u8 = [1, 0, 0, 0, 2, 0, 0, 0]

val words = packet.view_as[u32]()

print(words.len, words[0], words[1])

words[1] = 0x0403_0201

print(packet[4], packet[7])
2 1 2
1 4

Both element types must be plain data: no address and no count in them, and no bit pattern of their bytes that is not a value. The integers whose bits fill their bytes, the floats, fixed point, a new type that checks nothing, and arrays, vectors and structs of those qualify; so does a union whose every field is its full size. A bool, a char, an enum, a ranged subtype, a pointer, a reference, a view, a type with a destructor and a struct with padding do not, on either side — a write through the view would otherwise put into the source a value its type does not have:

var flags: [4]bool = [true, false, true, false]
val raw = flags.view_as[u8]()
'view_as' reads the storage of a '[]bool', and bool is not plain data — a 'bool' is valid only as 0 or 1

What only the run can know, the run checks: that the first element sits on T‘s boundary, and that the bytes are a whole number of Ts. Each check is made only where the two layouts leave it open — a []u32 read as []u8 checks nothing — and an empty view passes both, whatever address it carries. A failure traps, as a subscript out of range does; @align(n) on the storage is how an array of bytes is put on a wider element’s boundary:

@align(4)
var packet: [8]u8 = [1, 0, 0, 0, 2, 0, 0, 0]

print(packet[0..<4].view_as[u32]().len)
print(packet[0..<6].view_as[u32]().len)
1

A read-only view is read as a read-only one, and a view of a val array is read-only, so the licence to write is never gained by changing the element. A zero-sized element is refused on either side, since no count of them names any bytes. The bytes are the machine’s, in its own order: a program that reads a format of a stated order reads its fields through @byte_order or sysl.math.ByteOrder.

Growing one

Buf[T] is the growable array, and it is ordinary sysl in the library rather than a type the compiler knows: a []T field for the storage, a count of how much of it is live, and the methods over them — with the one fact below, which only the compiler can pass on to the optimizer. It can be written in ordinary sysl because of storage sized while running: a container needs no destructor of its own when its storage is a value that already has one, as the []T field does.

It lives in sysl.buf, which is where the surface is documented. Nothing in the language reaches it: an array literal makes an array or a slice, and a for walks whatever implements Iterate.

Whether a push is seen through another name for the same buffer needs no special rule; it follows from how the buffer is held:

var p: &Buf[int] = buf()
var q = p                   // one buffer, two names: q sees every push through p
var c = *p                  // a copy, because copying a struct is what that means

In Go two slices can agree until one of them grows, because one representation has to serve both roles. Held by reference, a Buf behaves as any &T does; held by value it is a value. Neither is a rule specific to growable arrays.

A buffer that is done growing can be frozen. b.freeze() takes its elements and answers a &Frozen[T] — one count on the whole buffer, indexed, walked and viewed as a []const T, and never written; b is left empty. An index past its length traps, as a Buf‘s does. Where the position expects a &sync Frozen[T], the count is atomic and every thread may read it at once without a lock; what that one may hold, and why, is in A frozen buffer crosses:

import sysl.buf.{Buf, buf}

var b: Buf[int] = buf()

b.push(3)
b.push(4)

val f = b.freeze()
val v = f.view()

print(f[1], f.len(), v, b.len())
4 2 [3, 4] 0

Its elements are never its header

A Buf‘s header — the handle in its box, and the record behind it holding the storage view and the count — and its elements are two different allocations: the storage is made by the buffer for itself, and a box is never made out of a buffer’s storage. So a write to an element cannot change the count or move the storage, and a write to the header cannot change an element. The compiler tells the optimizer exactly that, and nothing more: every access the buffer’s own members make to its header is marked as one side, every access they make to an element as the other — and so is the read or write of b[i] itself, and of a field of it, through the address the subscript checks — and the optimizer may keep the header in registers across a loop that writes elements. A row operation over a matrix held in a &Buf[real] reads the length and the storage pointer once, not once per cell. An append is an element write too, together with the live-slot count kept in the storage’s own allocation, so a loop that pushes keeps the count and the capacity in registers, and reads them again only on the turn whose push grows the storage.

A handle kept anywhere else, such as a field of a struct, is marked only as not being that record, since the record holds no handle: a vm.stack.pop() writes the count and the next one need not read the handle again. An element write says nothing about such a handle, because elements can hold handles themselves, so a handle is read again after one.

It is a fact about where things are, not a rule a program has to keep. Every other access is unmarked: a view handed out by view() or elems, a reinterpreting view over the elements, a ptr_cast of an element’s address, and C given the data pointer all read and write the elements with no mark at all, which the optimizer must assume can touch anything. A member that grows the buffer writes the header, so an element read after it reads through the new storage:

import sysl.buf.{Buf, buf}

var b: &Buf[int] = buf()

b.push(1)

for i in 0..<20 do b.push(b[i] + 1)

print(b.len(), b[0], b[20], b.cap())
21 1 21 32

Views of storage inside a ‘&sync’ box

A view of a &sync array, or of an array inside a &sync box, is lent. A view’s count of its owner would be the ordinary one while another domain changes the same count atomically, so such a view counts nothing of the box: it may be handed to a call that borrows it or held in a local of the body — a field, a field of a field, the array converted where a view is wanted — and the binding the box was reached through keeps it alive meanwhile:

struct Ring
    samples: [8]i16

total(xs: []const i16) -> int
    var t = 0
    for x in xs
        t += x
    t

val r: &sync Ring = Ring([1, 2, 3, 4, 5, 6, 7, 8])
val recent = r.samples[2..<5]
val boxed: &sync [4]i16 = [1, 2, 3, 4]

print(recent.len, total(r.samples), total(boxed[1..]))
3 36 9

A view that would get out is refused — returned, stored anywhere the frame does not own, captured by a closure that is kept, or handed to a callee that holds on to it — and so is one whose box is reached through something that may let go of it while the view lives: a field of another box, a module var, or a local the body stores into. A val holding the box is the answer to the second:

struct Ring
    samples: [8]i16

recent(r: &sync Ring) -> []const i16 = r.samples[2..<5]

val r: &sync Ring = Ring([1, 2, 3, 4, 5, 6, 7, 8])
print(recent(r).len)
a view of storage inside a '&sync Ring' is returned, but such a view takes no count of the box — a count update through it would not be atomic — so it may only be lent: handed to a call that borrows it, or held in a local of this body. Copy the elements into an array of their own to keep them

An index and a walk take no view at all, so both work through the same box anywhere. Such a view is a []const T, and an element is not written through the box, since a field reached through a &sync is read-only:

struct Ring
    samples: [8]i16

val r: &sync Ring = Ring([1, 2, 3, 4, 5, 6, 7, 8])

var sum = 0
for x in r.samples
    sum += x

print(r.samples[3], sum)
4 36

A bit like the read-only one could not make such a view counted, because the two properties are not symmetric. A []const T can be made out of a writable view by giving something up, which is why widening is safe and why the bit costs nothing at run time. An atomic count cannot be made out of a non-atomic one at all: it is fixed when the object is allocated, and a view claiming it would be reporting on another object’s storage rather than describing itself. Rust divides the same properties the same way — &[T] carries writability in the reference, while Rc<[T]> against Arc<[T]> carries count discipline in the owner’s type.

What is still refused

There is no rectangular multi-dimensional type. [3][3]f64 is an array of arrays and works; there is no distinct built-in type with one bounds check for two indexes. A program’s own matrix is one, indexed m[i, j] through a trait over a tuple (several indices in one subscript).


Next: strings — the same three words, with a guarantee added and an operation taken away.

Search

Esc
↑↓ to navigate ↵ to open Esc to close