github.com/segmentio/parquet-go@v0.0.0-20230712180008-5d42db8f0d47/internal/unsafecast/unsafecast_go18.go (about) 1 //go:build go1.18 2 3 // Package unsafecast exposes functions to bypass the Go type system and perform 4 // conversions between types that would otherwise not be possible. 5 // 6 // The functions of this package are mostly useful as optimizations to avoid 7 // memory copies when converting between compatible memory layouts; for example, 8 // casting a [][16]byte to a []byte in order to use functions of the standard 9 // bytes package on the slices. 10 // 11 // With great power comes great responsibility. 12 package unsafecast 13 14 import ( 15 "reflect" 16 "unsafe" 17 ) 18 19 // AddressOf returns the address to the first element in data, even if the slice 20 // has length zero. 21 func AddressOf[T any](data []T) *T { 22 return *(**T)(unsafe.Pointer(&data)) 23 } 24 25 // AddressOfBytes returns the address of the first byte in data. 26 func AddressOfBytes(data []byte) *byte { 27 return *(**byte)(unsafe.Pointer(&data)) 28 } 29 30 // AddressOfString returns the address of the first byte in data. 31 func AddressOfString(data string) *byte { 32 return *(**byte)(unsafe.Pointer(&data)) 33 } 34 35 // PointerOf is like AddressOf but returns an unsafe.Pointer, losing type 36 // information about the underlying data. 37 func PointerOf[T any](data []T) unsafe.Pointer { 38 return unsafe.Pointer(AddressOf(data)) 39 } 40 41 // PointerOfString is like AddressOfString but returns an unsafe.Pointer, losing 42 // type information about the underlying data. 43 func PointerOfString(data string) unsafe.Pointer { 44 return unsafe.Pointer(AddressOfString(data)) 45 } 46 47 // PointerOfValue returns the address of the object packed in the given value. 48 // 49 // This function is like value.UnsafePointer but works for any underlying type, 50 // bypassing the safety checks done by the reflect package. 51 func PointerOfValue(value reflect.Value) unsafe.Pointer { 52 return (*[2]unsafe.Pointer)(unsafe.Pointer(&value))[1] 53 } 54 55 // The slice type represents the memory layout of slices in Go. It is similar to 56 // reflect.SliceHeader but uses a unsafe.Pointer instead of uintptr to for the 57 // backing array to allow the garbage collector to track track the reference. 58 type slice struct { 59 ptr unsafe.Pointer 60 len int 61 cap int 62 } 63 64 // Slice converts the data slice of type []From to a slice of type []To sharing 65 // the same backing array. The length and capacity of the returned slice are 66 // scaled according to the size difference between the source and destination 67 // types. 68 // 69 // Note that the function does not perform any checks to ensure that the memory 70 // layouts of the types are compatible, it is possible to cause memory 71 // corruption if the layouts mismatch (e.g. the pointers in the From are different 72 // than the pointers in To). 73 func Slice[To, From any](data []From) []To { 74 // This function could use unsafe.Slice but it would drop the capacity 75 // information, so instead we implement the type conversion. 76 var zf From 77 var zt To 78 var s = (*slice)(unsafe.Pointer(&data)) 79 s.len = int((uintptr(s.len) * unsafe.Sizeof(zf)) / unsafe.Sizeof(zt)) 80 s.cap = int((uintptr(s.cap) * unsafe.Sizeof(zf)) / unsafe.Sizeof(zt)) 81 return *(*[]To)(unsafe.Pointer(s)) 82 } 83 84 // Bytes constructs a byte slice. The pointer to the first element of the slice 85 // is set to data, the length and capacity are set to size. 86 func Bytes(data *byte, size int) []byte { 87 return *(*[]byte)(unsafe.Pointer(&slice{ 88 ptr: unsafe.Pointer(data), 89 len: size, 90 cap: size, 91 })) 92 } 93 94 // BytesToString converts a byte slice to a string value. The returned string 95 // shares the backing array of the byte slice. 96 // 97 // Programs using this function are responsible for ensuring that the data slice 98 // is not modified while the returned string is in use, otherwise the guarantee 99 // of immutability of Go string values will be violated, resulting in undefined 100 // behavior. 101 func BytesToString(data []byte) string { 102 return *(*string)(unsafe.Pointer(&data)) 103 } 104 105 // StringToBytes applies the inverse conversion of BytesToString. 106 func StringToBytes(data string) []byte { 107 return *(*[]byte)(unsafe.Pointer(&slice{ 108 ptr: PointerOfString(data), 109 len: len(data), 110 cap: len(data), 111 })) 112 } 113 114 // ----------------------------------------------------------------------------- 115 // TODO: the functions below are used for backward compatibility with Go 1.17 116 // where generics weren't available. We should remove them and inline calls to 117 // unsafecast.Slice when we change our minimum supported Go version to 1.18. 118 // ----------------------------------------------------------------------------- 119 120 func BoolToBytes(data []bool) []byte { return Slice[byte](data) } 121 122 func Int8ToBytes(data []int8) []byte { return Slice[byte](data) } 123 124 func Int16ToBytes(data []int16) []byte { return Slice[byte](data) } 125 126 func Int32ToBytes(data []int32) []byte { return Slice[byte](data) } 127 128 func Int64ToBytes(data []int64) []byte { return Slice[byte](data) } 129 130 func Float32ToBytes(data []float32) []byte { return Slice[byte](data) } 131 132 func Float64ToBytes(data []float64) []byte { return Slice[byte](data) } 133 134 func Uint32ToBytes(data []uint32) []byte { return Slice[byte](data) } 135 136 func Uint64ToBytes(data []uint64) []byte { return Slice[byte](data) } 137 138 func Uint128ToBytes(data [][16]byte) []byte { return Slice[byte](data) } 139 140 func Int16ToUint16(data []int16) []uint16 { return Slice[uint16](data) } 141 142 func Int32ToUint32(data []int32) []uint32 { return Slice[uint32](data) } 143 144 func Int64ToUint64(data []int64) []uint64 { return Slice[uint64](data) } 145 146 func Float32ToUint32(data []float32) []uint32 { return Slice[uint32](data) } 147 148 func Float64ToUint64(data []float64) []uint64 { return Slice[uint64](data) } 149 150 func Uint32ToInt32(data []uint32) []int32 { return Slice[int32](data) } 151 152 func Uint32ToInt64(data []uint32) []int64 { return Slice[int64](data) } 153 154 func Uint64ToInt64(data []uint64) []int64 { return Slice[int64](data) } 155 156 func BytesToBool(data []byte) []bool { return Slice[bool](data) } 157 158 func BytesToInt8(data []byte) []int8 { return Slice[int8](data) } 159 160 func BytesToInt16(data []byte) []int16 { return Slice[int16](data) } 161 162 func BytesToInt32(data []byte) []int32 { return Slice[int32](data) } 163 164 func BytesToInt64(data []byte) []int64 { return Slice[int64](data) } 165 166 func BytesToUint32(data []byte) []uint32 { return Slice[uint32](data) } 167 168 func BytesToUint64(data []byte) []uint64 { return Slice[uint64](data) } 169 170 func BytesToUint128(data []byte) [][16]byte { return Slice[[16]byte](data) } 171 172 func BytesToFloat32(data []byte) []float32 { return Slice[float32](data) } 173 174 func BytesToFloat64(data []byte) []float64 { return Slice[float64](data) }