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.