github.com/deanMdreon/kafka-go@v0.4.32/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) string() string { return *(*string)(v.ptr) } 67 68 func (v value) bytes() []byte { return *(*[]byte)(v.ptr) } 69 70 func (v value) iface(t reflect.Type) interface{} { 71 return *(*interface{})(unsafe.Pointer(&iface{ 72 typ: ((*iface)(unsafe.Pointer(&t))).ptr, 73 ptr: v.ptr, 74 })) 75 } 76 77 func (v value) array(t reflect.Type) array { 78 return array{ 79 size: uintptr(t.Size()), 80 elem: ((*slice)(v.ptr)).ptr, 81 len: ((*slice)(v.ptr)).len, 82 } 83 } 84 85 func (v value) setBool(b bool) { *(*bool)(v.ptr) = b } 86 87 func (v value) setInt8(i int8) { *(*int8)(v.ptr) = i } 88 89 func (v value) setInt16(i int16) { *(*int16)(v.ptr) = i } 90 91 func (v value) setInt32(i int32) { *(*int32)(v.ptr) = i } 92 93 func (v value) setInt64(i int64) { *(*int64)(v.ptr) = i } 94 95 func (v value) setString(s string) { *(*string)(v.ptr) = s } 96 97 func (v value) setBytes(b []byte) { *(*[]byte)(v.ptr) = b } 98 99 func (v value) setArray(a array) { *(*slice)(v.ptr) = slice{ptr: a.elem, len: a.len, cap: a.len} } 100 101 func (v value) fieldByIndex(i index) value { 102 return value{ptr: unsafe.Pointer(uintptr(v.ptr) + uintptr(i))} 103 } 104 105 type array struct { 106 elem unsafe.Pointer 107 size uintptr 108 len int 109 } 110 111 var ( 112 emptyArray struct{} 113 ) 114 115 func makeArray(t reflect.Type, n int) array { 116 var elem unsafe.Pointer 117 var size = uintptr(t.Size()) 118 if n == 0 { 119 elem = unsafe.Pointer(&emptyArray) 120 } else { 121 elem = unsafe_NewArray(((*iface)(unsafe.Pointer(&t))).ptr, n) 122 } 123 return array{elem: elem, size: size, len: n} 124 } 125 126 func (a array) index(i int) value { 127 return value{ptr: unsafe.Pointer(uintptr(a.elem) + (uintptr(i) * a.size))} 128 } 129 130 func (a array) length() int { return a.len } 131 132 func (a array) isNil() bool { return a.elem == nil } 133 134 func indexOf(s reflect.StructField) index { return index(s.Offset) } 135 136 func bytesToString(b []byte) string { return *(*string)(unsafe.Pointer(&b)) } 137 138 //go:linkname unsafe_NewArray reflect.unsafe_NewArray 139 func unsafe_NewArray(rtype unsafe.Pointer, length int) unsafe.Pointer