core/vec
skip to the library navigationShared: available on every enabled target.
A growable contiguous list with explicit allocation and rollback on failed growth.
| Call | What it does |
|---|---|
vec.new | Start an empty list; nothing is allocated. |
vec.reserve, vec.push | Grow the list, through the provider's handle. |
vec.get, vec.last, vec.pop | Copy an item out; what it refers to is what the list held, not a view of the list. |
vec.at | Lend a writable pointer to an item. |
vec.used | Lend the initialized slice. |
End views before relocation or release. Clearing or releasing a container never frees the resources that its items refer to. So an item copied out of a list is no view of it: a pointer read with vec.last goes back in with vec.push while nothing else views the list (D1369).
A list of bytes is the growable byte container:
append_bytescopies a slice in after the list's bytes. The slice may be the list's own bytes,vec.used(list)or a part of it: what is appended is what it held when the call began, though the list moved.spare_byteslends the spare capacity, which a read fills.extend_bytescounts what was written there.
They move a word at a time where mem.copy moves words. A list's items move the same way when the list grows into a new block.
Items
Types 1
Functions 15
Also on this page
public list: type (item: type) = list_value(item)
Growable contiguous list with explicit allocator arguments; no allocator is stored in the list. Its storage is the module's own, so nothing outside it can put another block in a list's place, or take one out, behind its length and capacity.
public append_bytes: (provider: type is mem.allocator,
inout value: list(u8), memory: provider, bytes: []u8)
-> none ! mem.out_of_memory
Append every byte of bytes, growing capacity as push does, and copying them a word at a time where mem.copy moves words. Reports mem.out_of_memory without appending anything when growth fails. bytes may be a view of the list's own bytes, vec.used(value) or a part of it: the bytes appended are the ones it held when the call began, read before a growth frees the block they were in (D1122). Successful growth invalidates views into the previous allocation once the call has returned, that one among them.
public at: (item: type, inout value: list(item), index: usize)
-> (slot: ptr mut item from value) ! mem.out_of_bounds
Lend a writable pointer to an indexed item for in-place update. Reports mem.out_of_bounds for a missing slot. While the result lives the list binding may not be passed inout or sink [0800]: bind it, update through it, and let it end before the next push, reserve or release.
public capacity: (item: type, value: list(item)) -> (count: usize)
Return the number of items that fit without further growth.
public clear: (item: type, inout value: list(item)) -> none
Discard all items and retain capacity. Does not release resources referenced by discarded values.
public extend_bytes: (inout value: list(u8), count: usize)
-> none ! mem.out_of_bounds
Count the first count bytes of the spare capacity, which the caller has written through spare_bytes, as appended. Reports mem.out_of_bounds, changing nothing, when fewer than count are spare.
public get: (item: type, value: list(item), index: usize)
-> (result: item from held value) ! mem.out_of_bounds
Copy the indexed item, preserving origins of references it contains. Reports mem.out_of_bounds outside the initialized prefix.
Copy the last item without removing it. Reports mem.empty for an empty list.
public length: (item: type, value: list(item)) -> (count: usize)
Return the number of initialized items.
public new: (item: type) -> (result: list(item))
Create an empty list without allocating. Supply the allocator when growing or releasing it.
Remove and return the last item. Reports mem.empty without mutation when empty; retains capacity.
public push: (item: type, provider: type is mem.allocator,
inout value: list(item), memory: provider,
escaping into value added: item) -> none ! mem.out_of_memory
Append a value, growing capacity when needed. Reports mem.out_of_memory without appending on allocation failure. Successful growth may invalidate views into the previous allocation.
public release: (item: type, provider: type is mem.allocator,
inout value: list(item), memory: provider) -> none
Discard items, free backing through the original provider and reset the list. End all views first; nested resources remain the caller's responsibility.
public reserve: (item: type, provider: type is mem.allocator,
inout value: list(item), memory: provider, want: usize)
-> none ! mem.out_of_memory
Ensure room for at least want items. May grow backing in place or copy to a replacement. Failure preserves the old length, capacity and values; successful relocation invalidates old views. Use the original provider for existing backing.
public spare_bytes: (provider: type is mem.allocator, inout value: list(u8),
memory: provider, extra: usize)
-> (room: []mut u8 from value) ! mem.out_of_memory
Lend the spare capacity past the list's bytes, at least extra bytes of it, growing as push does when there are fewer: the buffer a read fills, io.read(host, file, room), before extend_bytes counts what it wrote. Its bytes are whatever the storage held. End uses of the view before the next append, reserve or release.
public used: (item: type, value: list(item))
-> (items: []mut item from value)
Lend a mutable slice covering only the initialized items. The slice derives from the list.
End every use of the view before an operation that changes capacity, relocates the backing or releases the storage.
Example program
This complete program is one of the compiler's runtime tests. View source.
The program, 31 lines
import core/arena import core/mem import core/vec exercise: () -> (ok: bool) ! mem.out_of_memory | mem.empty | mem.out_of_bounds = ok = false mut backing: [256]u8 = zeroed mut space: arena.space = zeroed memory := arena.over_unchecked(space, addr backing[0], 256) mut values := vec.new(item: i32) try vec.push(values, memory, i32(41)) return when vec.length(values) <> 1 slot: ptr mut i32 = try vec.at(values, usize(0)) slot.val = 42 last: i32 = try vec.last(values) return when last <> 42 removed := try vec.pop(values) return when removed <> 42 _ = vec.pop(values) else (problem) ok = problem == mem.empty and vec.length(values) == 0 return end end exercise public main: () -> (code: i32) = code = 1 ok := exercise() else false if ok then code = 42 end if end main