From 2dd74ce66084584c347fc0556b9e89f893ac6fab Mon Sep 17 00:00:00 2001 From: judah Date: Sun, 11 Jan 2026 23:10:09 -0700 Subject: [PATCH] mem: add BitCast, BitCastValue, UnsafeCast, UnsafeCastValue --- mem/mem.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/mem/mem.go b/mem/mem.go index 2ca7509..94c29fa 100644 --- a/mem/mem.go +++ b/mem/mem.go @@ -40,6 +40,9 @@ func BitCast[TOut any, TIn any](value *TIn) TOut { return *((*TOut)(unsafe.Pointer(value))) } +// BitCastValue performs a bit conversion between two types of the same size. +// +// BitCastValue panics if the sizes of the types differ. func BitCastValue[TOut any, TIn any](value TIn) TOut { if Sizeof[TOut]() != Sizeof[TIn]() { panic("bitcast: sizes of types must match") @@ -47,6 +50,16 @@ func BitCastValue[TOut any, TIn any](value TIn) TOut { return *((*TOut)(unsafe.Pointer(&value))) } +// UnsafeCast performs a bit conversion between two types without checking if their sizes match. +func UnsafeCast[TOut any, TIn any](value *TIn) TOut { + return *((*TOut)(unsafe.Pointer(value))) +} + +// UnsafeCastValue performs a bit conversion between two types without checking if their sizes match. +func UnsafeCastValue[TOut any, TIn any](value TIn) TOut { + return *((*TOut)(unsafe.Pointer(&value))) +} + // Copy copies size number of bytes from src into dst. // // Returns dst.