github.com/Schaudge/grailbase@v0.0.0-20240223061707-44c758a471c0/gtl/unsafe.go.tpl (about) 1 package PACKAGE 2 3 import ( 4 "reflect" 5 "unsafe" 6 ) 7 8 // ZZELEMsToBytes casts []ELEM to []byte without reallocating. 9 func ZZELEMsToBytes(src []ELEM) (d []byte) { // nolint: deadcode 10 if len(src) == 0 { 11 return nil 12 } 13 sh := (*reflect.SliceHeader)(unsafe.Pointer(&src)) 14 dh := (*reflect.SliceHeader)(unsafe.Pointer(&d)) 15 const elemSize = int(unsafe.Sizeof(src[0])) 16 dh.Data = sh.Data 17 dh.Len = sh.Len * elemSize 18 dh.Cap = sh.Cap * elemSize 19 return d 20 } 21 22 // ZZBytesToELEMs casts []byte to []ELEM without reallocating. 23 func ZZBytesToELEMs(src []byte) (d []ELEM) { // nolint: deadcode 24 if len(src) == 0 { 25 return nil 26 } 27 sh := (*reflect.SliceHeader)(unsafe.Pointer(&src)) 28 dh := (*reflect.SliceHeader)(unsafe.Pointer(&d)) 29 const elemSize = int(unsafe.Sizeof(d[0])) 30 dh.Data = sh.Data 31 dh.Len = sh.Len / elemSize 32 dh.Cap = sh.Cap / elemSize 33 return d 34 }