xx/xx.go

35 lines
750 B
Go

package xx
import (
"unsafe"
"git.brut.systems/judah/xx/mem"
)
// New returns a newly allocated value with an initial value.
func New[T any](expr T) *T {
p := new(T)
*p = expr
return p
}
// Copy copies src number of bytes into dst.
// Returns dst.
//
// Copy panics if src is smaller than dst.
func Copy[TDst any, TSrc any](dst *TDst, src *TSrc) *TDst {
if mem.SizeOf[TSrc]() < mem.SizeOf[TDst]() {
panic("copy: size of src must be >= dst")
}
mem.Copy(unsafe.Pointer(dst), unsafe.Pointer(src), mem.SizeOf[TDst]())
return dst
}
// Clone returns a newly allocated shallow copy of the given value.
func Clone[T any](value *T) *T {
return Copy(new(T), value)
}
func BoolUint(b bool) uint {
return uint(*(*uint8)(unsafe.Pointer(&b)))
}