Skip to content

Commit f742429

Browse files
committed
Add mem.buffer_from_slice_panicking proc
1 parent 3567c64 commit f742429

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

core/mem/mem.odin

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,35 @@ buffer_from_slice :: proc "contextless" (backing: $T/[]$E) -> [dynamic]E {
461461
}
462462
}
463463

464+
/*
465+
Create a dynamic array from slice.
466+
467+
This procedure creates a dynamic array, using slice `backing` as the backing
468+
buffer for the dynamic array. The resulting dynamic array will panic
469+
if attempted to grow beyond the size of the specified slice.
470+
*/
471+
@(require_results)
472+
buffer_from_slice_panicking :: proc "contextless" (backing: $T/[]$E) -> [dynamic]E {
473+
return transmute([dynamic]E)Raw_Dynamic_Array {
474+
data = raw_data(backing),
475+
len = 0,
476+
cap = len(backing),
477+
allocator = Allocator {
478+
procedure = proc(
479+
_ : rawptr,
480+
_ : Allocator_Mode,
481+
_, _: int,
482+
_ : rawptr,
483+
_ : int,
484+
loc := #caller_location,
485+
) -> ([]byte, Allocator_Error) {
486+
panic("atempted to grow slice-backed buffer", loc)
487+
},
488+
data = nil,
489+
},
490+
}
491+
}
492+
464493
/*
465494
Check whether a number is a power of two.
466495

0 commit comments

Comments
 (0)