Merge pull request #4568 from giuliano-macedo/master

docs: improved `slice.reinterpret` docstring
This commit is contained in:
Laytan authored and GitHub committed 2024-12-14 16:46:17 +01:00
commit 8b1c9b0ff5
1 file changed
+26 -7
+26 -7
View File
@@ -53,17 +53,36 @@ to_type :: proc(buf: []u8, $T: typeid) -> (T, bool) #optional_ok {
Only converts the type and length of the slice itself. Only converts the type and length of the slice itself.
The length is rounded down to the nearest whole number of items. The length is rounded down to the nearest whole number of items.
``` Example:
import "core:fmt"
import "core:slice"
i64s_as_i32s :: proc() {
large_items := []i64{1, 2, 3, 4} large_items := []i64{1, 2, 3, 4}
small_items := slice.reinterpret([]i32, large_items) small_items := slice.reinterpret([]i32, large_items)
assert(len(small_items) == 8) assert(len(small_items) == 8)
``` fmt.println(large_items, "->", small_items)
``` }
small_items := []byte{1, 0, 0, 0, 0, 0, 0, 0,
2, 0, 0, 0} bytes_as_i64s :: proc() {
large_items := slice.reinterpret([]i64, small_items) small_items := [12]byte{}
small_items[0] = 1
small_items[8] = 2
large_items := slice.reinterpret([]i64, small_items[:])
assert(len(large_items) == 1) // only enough bytes to make 1 x i64; two would need at least 8 bytes. assert(len(large_items) == 1) // only enough bytes to make 1 x i64; two would need at least 8 bytes.
``` fmt.println(small_items, "->", large_items)
}
reinterpret_example :: proc() {
i64s_as_i32s()
bytes_as_i64s()
}
Output:
[1, 2, 3, 4] -> [1, 0, 2, 0, 3, 0, 4, 0]
[1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0] -> [1]
*/ */
@(require_results) @(require_results)
reinterpret :: proc "contextless" ($T: typeid/[]$U, s: []$V) -> []U { reinterpret :: proc "contextless" ($T: typeid/[]$U, s: []$V) -> []U {