github.com/webmafia/fast@v0.10.0/pointer.go (about) 1 package fast 2 3 import "unsafe" 4 5 //go:inline 6 func PointerToBytes[T any](val *T, length int) []byte { 7 header := sliceHeader{ 8 Data: unsafe.Pointer(val), 9 Len: length, 10 Cap: length, 11 } 12 13 return *(*[]byte)(unsafe.Pointer(&header)) 14 } 15 16 //go:inline 17 func PointerToBytesOffset[T any](val *T, length, offset int) []byte { 18 header := sliceHeader{ 19 Data: unsafe.Add(unsafe.Pointer(val), offset), 20 Len: length, 21 Cap: length, 22 } 23 24 return *(*[]byte)(unsafe.Pointer(&header)) 25 } 26 27 //go:inline 28 func BytesToPointer[T any](b []byte) *T { 29 header := *(*sliceHeader)(unsafe.Pointer(&b)) 30 return (*T)(header.Data) 31 }