aboutsummaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorJeroen van Rijn <Kelimion@users.noreply.github.com>2024-10-01 23:27:15 +0200
committerJeroen van Rijn <Kelimion@users.noreply.github.com>2024-10-01 23:27:15 +0200
commiteba7be34e7202e7ca2ad434ae8e81859f89aeee2 (patch)
treed67072175870c19e7de8a0cd2d5142e5e94239f5 /core
parentaf9ae4897ad9e526d74489ddd12cfae179639ff3 (diff)
Add slice.to_type
Diffstat (limited to 'core')
-rw-r--r--core/slice/slice.odin11
1 files changed, 11 insertions, 0 deletions
diff --git a/core/slice/slice.odin b/core/slice/slice.odin
index c917e6d9e..cd12a8c70 100644
--- a/core/slice/slice.odin
+++ b/core/slice/slice.odin
@@ -37,6 +37,17 @@ to_bytes :: proc "contextless" (s: []$T) -> []byte {
}
/*
+ Turns a byte slice into a type.
+*/
+@(require_results)
+to_type :: proc(buf: []u8, $T: typeid) -> (T, bool) #optional_ok {
+ if len(buf) < size_of(T) {
+ return {}, false
+ }
+ return intrinsics.unaligned_load((^T)(raw_data(buf))), true
+}
+
+/*
Turn a slice of one type, into a slice of another type.
Only converts the type and length of the slice itself.