github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/convert/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  // UnsafeUInt8SliceToByteSlice returns ...
    11  func UnsafeUInt8SliceToByteSlice(req []uint8) (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  // UnsafeByteSliceToUInt8Slice returns ...
    21  func UnsafeByteSliceToUInt8Slice(req []byte) (res []uint8) {
    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  // UnsafeUInt16SliceToByteSlice returns ...
    31  func UnsafeUInt16SliceToByteSlice(req []uint16) (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  // UnsafeByteSliceToUInt16Slice returns ...
    41  func UnsafeByteSliceToUInt16Slice(req []byte) (res []uint16) {
    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  // UnsafeUInt32SliceToByteSlice returns ...
    51  func UnsafeUInt32SliceToByteSlice(req []uint32) (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  // UnsafeByteSliceToUInt32Slice returns ...
    61  func UnsafeByteSliceToUInt32Slice(req []byte) (res []uint32) {
    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  // UnsafeUInt64SliceToByteSlice returns ...
    71  func UnsafeUInt64SliceToByteSlice(req []uint64) (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  // UnsafeByteSliceToUInt64Slice returns ...
    81  func UnsafeByteSliceToUInt64Slice(req []byte) (res []uint64) {
    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  }