github.com/streamdal/segmentio-kafka-go@v0.4.47-streamdal/protocol/reflect_unsafe.go (about) 1 //go:build unsafe 2 // +build unsafe 3 4 package protocol 5 6 import ( 7 "reflect" 8 "unsafe" 9 ) 10 11 type iface struct { 12 typ unsafe.Pointer 13 ptr unsafe.Pointer 14 } 15 16 type slice struct { 17 ptr unsafe.Pointer 18 len int 19 cap int 20 } 21 22 type index uintptr 23 24 type _type struct { 25 ptr unsafe.Pointer 26 } 27 28 func typeOf(x interface{}) _type { 29 return _type{ptr: ((*iface)(unsafe.Pointer(&x))).typ} 30 } 31 32 func elemTypeOf(x interface{}) _type { 33 return makeType(reflect.TypeOf(x).Elem()) 34 } 35 36 func makeType(t reflect.Type) _type { 37 return _type{ptr: ((*iface)(unsafe.Pointer(&t))).ptr} 38 } 39 40 type value struct { 41 ptr unsafe.Pointer 42 } 43 44 func nonAddressableValueOf(x interface{}) value { 45 return valueOf(x) 46 } 47 48 func valueOf(x interface{}) value { 49 return value{ptr: ((*iface)(unsafe.Pointer(&x))).ptr} 50 } 51 52 func makeValue(t reflect.Type) value { 53 return value{ptr: unsafe.Pointer(reflect.New(t).Pointer())} 54 } 55 56 func (v value) bool() bool { return *(*bool)(v.ptr) } 57 58 func (v value) int8() int8 { return *(*int8)(v.ptr) } 59 60 func (v value) int16() int16 { return *(*int16)(v.ptr) } 61 62 func (v value) int32() int32 { return *(*int32)(v.ptr) } 63 64 func (v value) int64() int64 { return *(*int64)(v.ptr) } 65 66 func (v value) float64() float64 { return *(*float64)(v.ptr) } 67 68 func (v value) string() string { return *(*string)(v.ptr) } 69 70 func (v value) bytes() []byte { return *(*[]byte)(v.ptr) } 71 72 func (v value) iface(t reflect.Type) interface{} { 73 return *(*interface{})(unsafe.Pointer(&iface{ 74 typ: ((*iface)(unsafe.Pointer(&t))).ptr, 75 ptr: v.ptr, 76 })) 77 } 78 79 func (v value) array(t reflect.Type) array { 80 return array{ 81 size: uintptr(t.Size()), 82 elem: ((*slice)(v.ptr)).ptr, 83 len: ((*slice)(v.ptr)).len, 84 } 85 } 86 87 func (v value) setBool(b bool) { *(*bool)(v.ptr) = b } 88 89 func (v value) setInt8(i int8) { *(*int8)(v.ptr) = i } 90 91 func (v value) setInt16(i int16) { *(*int16)(v.ptr) = i } 92 93 func (v value) setInt32(i int32) { *(*int32)(v.ptr) = i } 94 95 func (v value) setInt64(i int64) { *(*int64)(v.ptr) = i } 96 97 func (v value) setFloat64(f float64) { *(*float64)(v.ptr) = f } 98 99 func (v value) setString(s string) { *(*string)(v.ptr) = s } 100 101 func (v value) setBytes(b []byte) { *(*[]byte)(v.ptr) = b } 102 103 func (v value) setArray(a array) { *(*slice)(v.ptr) = slice{ptr: a.elem, len: a.len, cap: a.len} } 104 105 func (v value) fieldByIndex(i index) value { 106 return value{ptr: unsafe.Pointer(uintptr(v.ptr) + uintptr(i))} 107 } 108 109 type array struct { 110 elem unsafe.Pointer 111 size uintptr 112 len int 113 } 114 115 var ( 116 emptyArray struct{} 117 ) 118 119 func makeArray(t reflect.Type, n int) array { 120 var elem unsafe.Pointer 121 var size = uintptr(t.Size()) 122 if n == 0 { 123 elem = unsafe.Pointer(&emptyArray) 124 } else { 125 elem = unsafe_NewArray(((*iface)(unsafe.Pointer(&t))).ptr, n) 126 } 127 return array{elem: elem, size: size, len: n} 128 } 129 130 func (a array) index(i int) value { 131 return value{ptr: unsafe.Pointer(uintptr(a.elem) + (uintptr(i) * a.size))} 132 } 133 134 func (a array) length() int { return a.len } 135 136 func (a array) isNil() bool { return a.elem == nil } 137 138 func indexOf(s reflect.StructField) index { return index(s.Offset) } 139 140 func bytesToString(b []byte) string { return *(*string)(unsafe.Pointer(&b)) } 141 142 //go:linkname unsafe_NewArray reflect.unsafe_NewArray 143 func unsafe_NewArray(rtype unsafe.Pointer, length int) unsafe.Pointer