feat: add typed mem.set
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user