feat: add typed mem.set

This commit is contained in:
2026-10-05 18:54:43 +02:00
parent 9b273f2471
commit d88513e005
4 changed files with 32 additions and 1 deletions
+1
View File
@@ -10,6 +10,7 @@ import "@source/sema"
ast_renderer :: import "@source/ast/renderer"
test import "@std/mem"
test import "@std/enummap"
test import "@std/arraylist"
test import "@std/hashmap"
+1 -1
View File
@@ -58,7 +58,7 @@ resolve proc(
) Resolution ! (mem.AllocError | diagnostics.CapacityError) {
bindings :: try mem.alloc(Binding, allocator, ast.node_count(tree))
errdefer mem.free(allocator, bindings)
for (0..bindings.len) |i| bindings[i] = Binding{}
mem.set(bindings, Binding{})
state := State{
tree = tree,
+6
View File
@@ -35,6 +35,12 @@ eql proc($T type, left, right []T) bool {
return true
}
#! assign `value` to each slice element, preserving its typed value rather than repeating a byte.
#! an empty destination is a no-op.
set proc($T type, destination []mut T, value T) void {
for (0..destination.len) |i| destination[i] = value
}
#! allocate memory for a slice of type `T` with `count` elements.
alloc proc($T type, allocator Allocator, count usize) []mut T ! AllocError {
if (count == 0) return empty_slice(T, 0)
+24
View File
@@ -0,0 +1,24 @@
import "@std/testing"
@hide:file
Record :: struct {
tag u8
id u32
}
fills_typed_values_within_slice_boundaries test {
values [4]mut u32 := [1, 2, 3, 4]
set(values[1..3], u32(305419896))
set(values[2..2], u32(0))
try testing.expect_equal(u32(1), values[0])
try testing.expect_equal(u32(305419896), values[1])
try testing.expect_equal(u32(305419896), values[2])
try testing.expect_equal(u32(4), values[3])
records [2]mut Record := [Record{ tag = 0, id = 0 }, Record{ tag = 0, id = 0 }]
set(records[..], Record{ tag = 3, id = maxval!(u32) })
for records |record| {
try testing.expect_equal(u8(3), record.tag)
try testing.expect_equal(maxval!(u32), record.id)
}
}