Skip to content

Commit

Permalink
zeroalloc: don't use unsafe.Pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
serprex committed Dec 13, 2024
1 parent c6114ee commit 4a8c838
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions utils/zeroalloc.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
package utils

import "unsafe"
import (
"math"
"unsafe"
)

func StringToByteSlice(s string) []byte {
return *(*[]byte)(unsafe.Pointer(&s))
return unsafe.Slice(unsafe.StringData(s), len(s))
}

func ByteSliceToString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
return unsafe.String(unsafe.SliceData(b), len(b))
}

func Uint64ToInt64(val uint64) int64 {
return *(*int64)(unsafe.Pointer(&val))
return int64(val)
}

func Uint64ToFloat64(val uint64) float64 {
return *(*float64)(unsafe.Pointer(&val))
return math.Float64frombits(val)
}

func Int64ToUint64(val int64) uint64 {
return *(*uint64)(unsafe.Pointer(&val))
return uint64(val)
}

func Float64ToUint64(val float64) uint64 {
return *(*uint64)(unsafe.Pointer(&val))
return math.Float64bits(val)
}

0 comments on commit 4a8c838

Please sign in to comment.