github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/convert/array-slice-unsafe.go (about)

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  package convert
     4  
     5  import (
     6  	"reflect"
     7  	"unsafe"
     8  )
     9  
    10  // Unsafe16ByteArraySliceToByteSlice returns ...
    11  func Unsafe16ByteArraySliceToByteSlice(req [][16]byte) (res []byte) {
    12  	var reqStruct = (*reflect.SliceHeader)(unsafe.Pointer(&req))
    13  	var resStruct = (*reflect.SliceHeader)(unsafe.Pointer(&res))
    14  	resStruct.Data = reqStruct.Data
    15  	resStruct.Len = reqStruct.Len * 16
    16  	resStruct.Cap = reqStruct.Cap * 16
    17  	return
    18  }
    19  
    20  // UnsafeByteSliceTo16ByteArraySlice returns ...
    21  func UnsafeByteSliceTo16ByteArraySlice(req []byte) (res [][16]byte) {
    22  	var reqStruct = (*reflect.SliceHeader)(unsafe.Pointer(&req))
    23  	var resStruct = (*reflect.SliceHeader)(unsafe.Pointer(&res))
    24  	resStruct.Data = reqStruct.Data
    25  	resStruct.Len = reqStruct.Len / 16
    26  	resStruct.Cap = reqStruct.Cap / 16
    27  	return
    28  }
    29  
    30  // Unsafe32ByteArraySliceToByteSlice returns ...
    31  func Unsafe32ByteArraySliceToByteSlice(req [][32]byte) (res []byte) {
    32  	var reqStruct = (*reflect.SliceHeader)(unsafe.Pointer(&req))
    33  	var resStruct = (*reflect.SliceHeader)(unsafe.Pointer(&res))
    34  	resStruct.Data = reqStruct.Data
    35  	resStruct.Len = reqStruct.Len * 32
    36  	resStruct.Cap = reqStruct.Cap * 32
    37  	return
    38  }
    39  
    40  // UnsafeByteSliceTo32ByteArraySlice returns ...
    41  func UnsafeByteSliceTo32ByteArraySlice(req []byte) (res [][32]byte) {
    42  	var reqStruct = (*reflect.SliceHeader)(unsafe.Pointer(&req))
    43  	var resStruct = (*reflect.SliceHeader)(unsafe.Pointer(&res))
    44  	resStruct.Data = reqStruct.Data
    45  	resStruct.Len = reqStruct.Len / 32
    46  	resStruct.Cap = reqStruct.Cap / 32
    47  	return
    48  }