github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/convert/signed-integer-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  // UnsafeInt8SliceToByteSlice returns ...
    11  func UnsafeInt8SliceToByteSlice(req []int8) (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  	resStruct.Cap = reqStruct.Cap
    17  	return
    18  }
    19  
    20  // UnsafeByteSliceToInt8Slice returns ...
    21  func UnsafeByteSliceToInt8Slice(req []byte) (res []int8) {
    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
    26  	resStruct.Cap = reqStruct.Cap
    27  	return
    28  }
    29  
    30  // UnsafeInt16SliceToByteSlice returns ...
    31  func UnsafeInt16SliceToByteSlice(req []int16) (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 * 2
    36  	resStruct.Cap = reqStruct.Cap * 2
    37  	return
    38  }
    39  
    40  // UnsafeByteSliceToInt16Slice returns ...
    41  func UnsafeByteSliceToInt16Slice(req []byte) (res []int16) {
    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 / 2
    46  	resStruct.Cap = reqStruct.Cap / 2
    47  	return
    48  }
    49  
    50  // UnsafeInt32SliceToByteSlice returns ...
    51  func UnsafeInt32SliceToByteSlice(req []int32) (res []byte) {
    52  	var reqStruct = (*reflect.SliceHeader)(unsafe.Pointer(&req))
    53  	var resStruct = (*reflect.SliceHeader)(unsafe.Pointer(&res))
    54  	resStruct.Data = reqStruct.Data
    55  	resStruct.Len = reqStruct.Len * 4
    56  	resStruct.Cap = reqStruct.Cap * 4
    57  	return
    58  }
    59  
    60  // UnsafeByteSliceToInt32Slice returns ...
    61  func UnsafeByteSliceToInt32Slice(req []byte) (res []int32) {
    62  	var reqStruct = (*reflect.SliceHeader)(unsafe.Pointer(&req))
    63  	var resStruct = (*reflect.SliceHeader)(unsafe.Pointer(&res))
    64  	resStruct.Data = reqStruct.Data
    65  	resStruct.Len = reqStruct.Len / 4
    66  	resStruct.Cap = reqStruct.Cap / 4
    67  	return
    68  }
    69  
    70  // UnsafeInt64SliceToByteSlice returns ...
    71  func UnsafeInt64SliceToByteSlice(req []int64) (res []byte) {
    72  	var reqStruct = (*reflect.SliceHeader)(unsafe.Pointer(&req))
    73  	var resStruct = (*reflect.SliceHeader)(unsafe.Pointer(&res))
    74  	resStruct.Data = reqStruct.Data
    75  	resStruct.Len = reqStruct.Len * 8
    76  	resStruct.Cap = reqStruct.Cap * 8
    77  	return
    78  }
    79  
    80  // UnsafeByteSliceToInt64Slice returns ...
    81  func UnsafeByteSliceToInt64Slice(req []byte) (res []int64) {
    82  	var reqStruct = (*reflect.SliceHeader)(unsafe.Pointer(&req))
    83  	var resStruct = (*reflect.SliceHeader)(unsafe.Pointer(&res))
    84  	resStruct.Data = reqStruct.Data
    85  	resStruct.Len = reqStruct.Len / 8
    86  	resStruct.Cap = reqStruct.Cap / 8
    87  	return
    88  }