init
This commit is contained in:
commit
d7f9236b38
4 changed files with 118 additions and 0 deletions
4
doc.go
Normal file
4
doc.go
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
// Package xx contains various experiments.
|
||||
//
|
||||
// Everything in here should be expected to be unstable.
|
||||
package xx
|
||||
3
go.mod
Normal file
3
go.mod
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
module git.brut.systems/judah/xx
|
||||
|
||||
go 1.25.0
|
||||
49
utils.go
Normal file
49
utils.go
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
package xx
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// New returns a newly allocated value with an initial value.
|
||||
func New[T any](expr T) *T {
|
||||
p := new(T)
|
||||
*p = expr
|
||||
return p
|
||||
}
|
||||
|
||||
// Bitcast performs a bit conversion between two types of the same size.
|
||||
//
|
||||
// Bitcast panics if the sizes of the types differ.
|
||||
func Bitcast[TOut any, TIn any](value TIn) TOut {
|
||||
if SizeOf[TOut]() != SizeOf[TIn]() {
|
||||
panic("bitcast: sizes of types must match")
|
||||
}
|
||||
return *((*TOut)(unsafe.Pointer(&value)))
|
||||
}
|
||||
|
||||
// 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 SizeOf[TSrc]() < SizeOf[TDst]() {
|
||||
panic("copy: size of src must be >= dst")
|
||||
}
|
||||
MemCopy(unsafe.Pointer(dst), unsafe.Pointer(src), SizeOf[TDst]())
|
||||
return dst
|
||||
}
|
||||
|
||||
// MemCopy copies size number of bytes from src into dst.
|
||||
// Returns dst.
|
||||
func MemCopy(dst, src unsafe.Pointer, size uintptr) unsafe.Pointer {
|
||||
copy(unsafe.Slice((*byte)(dst), size), unsafe.Slice((*byte)(src), size))
|
||||
return dst
|
||||
}
|
||||
|
||||
// SizeOf returns the size in bytes of the given type.
|
||||
//
|
||||
// Not to be confused with [unsafe.Sizeof] which returns the size of an expression.
|
||||
func SizeOf[T any]() uintptr {
|
||||
var zero T
|
||||
return unsafe.Sizeof(zero)
|
||||
}
|
||||
62
utils_test.go
Normal file
62
utils_test.go
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
package xx_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"unsafe"
|
||||
|
||||
"git.brut.systems/judah/xx"
|
||||
)
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
a := xx.New(uint32(1024))
|
||||
if *a != 1024 {
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
if unsafe.Sizeof(*a) != xx.SizeOf[uint32]() {
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
b := xx.New(struct{ x, y, z float32 }{10, 20, 30})
|
||||
if b.x != 10 {
|
||||
t.Fail()
|
||||
}
|
||||
if b.y != 20 {
|
||||
t.Fail()
|
||||
}
|
||||
if b.z != 30 {
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
c := xx.New(b)
|
||||
if c == &b {
|
||||
t.Fail()
|
||||
}
|
||||
if (*c).x != 10 {
|
||||
t.Fail()
|
||||
}
|
||||
if (*c).y != 20 {
|
||||
t.Fail()
|
||||
}
|
||||
if (*c).z != 30 {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestBitcast(t *testing.T) {
|
||||
a := uint32(0xFFFF_FFFF)
|
||||
b := xx.Bitcast[float32](a)
|
||||
c := xx.Bitcast[uint32](b)
|
||||
if a != c {
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
d := xx.Bitcast[int8](uint8(0xFF))
|
||||
if d != -1 {
|
||||
t.Fail()
|
||||
}
|
||||
e := xx.Bitcast[uint8](d)
|
||||
if e != 255 {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue