github.com/batchcorp/thrift-iterator@v0.0.0-20220918180557-4c4a158fc6e9/binding/reflection/unsafe.go (about) 1 package reflection 2 3 import ( 4 "unsafe" 5 "github.com/batchcorp/thrift-iterator/spi" 6 "github.com/batchcorp/thrift-iterator/protocol" 7 ) 8 9 type internalDecoder interface { 10 decode(ptr unsafe.Pointer, iter spi.Iterator) 11 } 12 13 type valDecoderAdapter struct { 14 decoder internalDecoder 15 } 16 17 func (decoder *valDecoderAdapter) Decode(val interface{}, iter spi.Iterator) { 18 ptr := (*emptyInterface)(unsafe.Pointer(&val)).word 19 decoder.decoder.decode(ptr, iter) 20 } 21 22 type internalDecoderAdapter struct { 23 decoder spi.ValDecoder 24 valEmptyInterface emptyInterface 25 } 26 27 func (decoder *internalDecoderAdapter) decode(ptr unsafe.Pointer, iter spi.Iterator) { 28 valEmptyInterface := decoder.valEmptyInterface 29 valEmptyInterface.word = ptr 30 valObj := *(*interface{})((unsafe.Pointer(&valEmptyInterface))) 31 decoder.decoder.Decode(valObj, iter) 32 } 33 34 type internalEncoder interface { 35 encode(ptr unsafe.Pointer, stream spi.Stream) 36 thriftType() protocol.TType 37 } 38 39 type valEncoderAdapter struct { 40 encoder internalEncoder 41 } 42 43 func (encoder *valEncoderAdapter) Encode(val interface{}, stream spi.Stream) { 44 ptr := (*emptyInterface)(unsafe.Pointer(&val)).word 45 encoder.encoder.encode(ptr, stream) 46 } 47 48 func (encoder *valEncoderAdapter) ThriftType() protocol.TType { 49 return encoder.encoder.thriftType() 50 } 51 52 type ptrEncoderAdapter struct { 53 encoder internalEncoder 54 } 55 56 func (encoder *ptrEncoderAdapter) Encode(val interface{}, stream spi.Stream) { 57 ptr := (*emptyInterface)(unsafe.Pointer(&val)).word 58 encoder.encoder.encode(unsafe.Pointer(&ptr), stream) 59 } 60 61 func (encoder *ptrEncoderAdapter) ThriftType() protocol.TType { 62 return encoder.encoder.thriftType() 63 } 64 65 type internalEncoderAdapter struct { 66 encoder spi.ValEncoder 67 valEmptyInterface emptyInterface 68 } 69 70 func (encoder *internalEncoderAdapter) encode(ptr unsafe.Pointer, stream spi.Stream) { 71 valEmptyInterface := encoder.valEmptyInterface 72 valEmptyInterface.word = ptr 73 valObj := *(*interface{})((unsafe.Pointer(&valEmptyInterface))) 74 encoder.encoder.Encode(valObj, stream) 75 } 76 77 func (encoder *internalEncoderAdapter) thriftType() protocol.TType { 78 return encoder.encoder.ThriftType() 79 } 80 81 // emptyInterface is the header for an interface{} value. 82 type emptyInterface struct { 83 typ unsafe.Pointer 84 word unsafe.Pointer 85 } 86 87 // sliceHeader is a safe version of SliceHeader used within this package. 88 type sliceHeader struct { 89 Data unsafe.Pointer 90 Len int 91 Cap int 92 }