github.com/Schaudge/grailbase@v0.0.0-20240223061707-44c758a471c0/gtl/tests/unsafe.go (about)

     1  // Code generated by "../generate.py --prefix= -DELEM=int32 --package=tests --output=unsafe.go ../unsafe.go.tpl". DO NOT EDIT.
     2  
     3  package tests
     4  
     5  import (
     6  	"reflect"
     7  	"unsafe"
     8  )
     9  
    10  // int32sToBytes casts []int32 to []byte without reallocating.
    11  func int32sToBytes(src []int32) (d []byte) { // nolint: deadcode
    12  	if len(src) == 0 {
    13  		return nil
    14  	}
    15  	sh := (*reflect.SliceHeader)(unsafe.Pointer(&src))
    16  	dh := (*reflect.SliceHeader)(unsafe.Pointer(&d))
    17  	const elemSize = int(unsafe.Sizeof(src[0]))
    18  	dh.Data = sh.Data
    19  	dh.Len = sh.Len * elemSize
    20  	dh.Cap = sh.Cap * elemSize
    21  	return d
    22  }
    23  
    24  // BytesToint32s casts []byte to []int32 without reallocating.
    25  func BytesToint32s(src []byte) (d []int32) { // nolint: deadcode
    26  	if len(src) == 0 {
    27  		return nil
    28  	}
    29  	sh := (*reflect.SliceHeader)(unsafe.Pointer(&src))
    30  	dh := (*reflect.SliceHeader)(unsafe.Pointer(&d))
    31  	const elemSize = int(unsafe.Sizeof(d[0]))
    32  	dh.Data = sh.Data
    33  	dh.Len = sh.Len / elemSize
    34  	dh.Cap = sh.Cap / elemSize
    35  	return d
    36  }