additions

This commit is contained in:
Judah Caruso 2026-01-09 16:54:10 -07:00
parent 150a982f78
commit 4722e6a61f
4 changed files with 44 additions and 12 deletions

View file

@ -63,7 +63,6 @@ func (x *Xar[T]) AppendMany(values ...T) *T {
} }
first := x.Append(values[0]) first := x.Append(values[0])
if len(values) > 1 { if len(values) > 1 {
for _, v := range values[1:] { for _, v := range values[1:] {
x.Append(v) x.Append(v)

View file

@ -40,6 +40,13 @@ func BitCast[TOut any, TIn any](value *TIn) TOut {
return *((*TOut)(unsafe.Pointer(value))) return *((*TOut)(unsafe.Pointer(value)))
} }
func BitCastValue[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 size number of bytes from src into dst. // Copy copies size number of bytes from src into dst.
// //
// Returns dst. // Returns dst.

View file

@ -14,7 +14,9 @@
// } // }
package osthread package osthread
import "runtime" import (
"runtime"
)
// Start allows arbitrary functions to be run on the main operating system thread. // Start allows arbitrary functions to be run on the main operating system thread.
// //
@ -24,14 +26,14 @@ func Start(entrypoint func()) {
done := make(chan any) done := make(chan any)
// Run entrypoint in a separate goroutine. // Immediately queue entrypoint
go func() { Go(func() {
defer func() { defer func() {
done <- nil done <- nil
}() }()
entrypoint() entrypoint()
}() })
// Call functions in our queue until entrypoint returns. // Call functions in our queue until entrypoint returns.
// These functions are called on the main operating system thread. // These functions are called on the main operating system thread.

38
xx.go
View file

@ -1,18 +1,13 @@
package xx package xx
import ( import (
"runtime"
"strings"
"unsafe" "unsafe"
"git.brut.systems/judah/xx/mem" "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. // Copy copies src number of bytes into dst.
// Returns dst. // Returns dst.
// //
@ -34,3 +29,32 @@ func Clone[T any](value *T) *T {
func BoolUint(b bool) uint { func BoolUint(b bool) uint {
return uint(*(*uint8)(unsafe.Pointer(&b))) return uint(*(*uint8)(unsafe.Pointer(&b)))
} }
// CallerLocation returns the source location of the function CallerLocation is called in.
func CallerLocation() (file string, line int) {
_, file, line, _ = runtime.Caller(2)
// @todo: I'm sure there's a better way to do this
// Special-case when CallerLocation is called from main
if strings.Contains(file, "runtime") && strings.Contains(file, "proc.go") {
_, file, line, _ = runtime.Caller(1)
}
return
}
// HashLocation returns a hash of file and line, most likely returned from [CallerLocation].
func HashLocation(file string, line int) uint64 {
const (
FNV64_PRIME uint64 = 0x100000001B3
FNV64_BIAS uint64 = 0xCBF29CE484222325
)
h := FNV64_BIAS
for _, c := range file {
h = (h ^ uint64(c)) * FNV64_PRIME
}
h = (h ^ uint64(line)) * FNV64_PRIME
return h
}