refactor: use contextual empty slice literals

This commit is contained in:
2026-10-25 01:21:02 +02:00
parent 678bb2583b
commit 2761c77182
7 changed files with 57 additions and 54 deletions
+2 -2
View File
@@ -13,7 +13,7 @@ ArrayList proc($T type) type {
#! retain `allocator` for later growth and cleanup; this call does not allocate.
init proc($T type, allocator mem.Allocator) ArrayList(T) {
return ArrayList(T) {
items = mem.empty(T),
items = &[],
capacity = 0,
allocator = allocator,
}
@@ -23,7 +23,7 @@ init proc($T type, allocator mem.Allocator) ArrayList(T) {
deinit proc($T type, list @mut ArrayList(T)) void {
allocation :: list.items.ptr[..list.capacity]
mem.free(list.allocator, allocation)
list.items = mem.empty(T)
list.items = &[]
list.capacity = 0
}
+1 -1
View File
@@ -46,7 +46,7 @@ init proc(
allocator mem.Allocator,
) HashMap(K, V, hash_key, keys_eql) {
return HashMap(K, V, hash_key, keys_eql){
entries = mem.empty(Entry(K, V)),
entries = &[],
count = 0,
allocator = allocator,
}
+4 -11
View File
@@ -62,7 +62,7 @@ set proc($T type, destination []mut T, value T) void {
#! allocate memory for a slice of type `T` with `count` elements.
#! returned storage belongs to the caller; release it with free using the same allocator.
alloc proc($T type, allocator Allocator, count usize) []mut T ! AllocError {
if (count == 0) return empty_slice(T, 0)
if (count == 0) return &[]
element_size :: sizeof!(T)
if (element_size == 0) return empty_slice(T, count)
@@ -95,7 +95,7 @@ realloc proc(
if new_count == 0 {
free(allocator, memory)
return empty_slice(T, 0)
return &[]
}
element_size :: sizeof!(T)
@@ -137,21 +137,14 @@ free proc($T type, allocator Allocator, memory []T) void {
)
}
#! create a slice backed by `empty_storage` without allocating.
#! precondition: `count` must be 0 or `sizeof!(T)` must be 0; otherwise the slice
#! exceeds its backing storage.
#! create a logically nonempty slice of zero-sized elements without allocating.
#! precondition: sizeof!(T) must be 0; no element storage is accessed.
@hide:file
empty_slice proc($T type, count usize) []mut T {
pointer *mut T :: ptrcast!((&empty_storage).ptr)
return pointer[..count]
}
#! get an empty slice of type `T` with 0 elements.
#! the view borrows shared empty storage and requires no cleanup.
empty proc($T type) []mut T {
return empty_slice(T, 0)
}
@hide:file
empty_storage [1]mut u64 := [0]