Matteo Scandolo | a428586 | 2020-12-01 18:10:10 -0800 | [diff] [blame^] | 1 | package reflect2 |
| 2 | |
| 3 | import ( |
| 4 | "reflect" |
| 5 | "unsafe" |
| 6 | ) |
| 7 | |
| 8 | type eface struct { |
| 9 | rtype unsafe.Pointer |
| 10 | data unsafe.Pointer |
| 11 | } |
| 12 | |
| 13 | func unpackEFace(obj interface{}) *eface { |
| 14 | return (*eface)(unsafe.Pointer(&obj)) |
| 15 | } |
| 16 | |
| 17 | func packEFace(rtype unsafe.Pointer, data unsafe.Pointer) interface{} { |
| 18 | var i interface{} |
| 19 | e := (*eface)(unsafe.Pointer(&i)) |
| 20 | e.rtype = rtype |
| 21 | e.data = data |
| 22 | return i |
| 23 | } |
| 24 | |
| 25 | type UnsafeEFaceType struct { |
| 26 | unsafeType |
| 27 | } |
| 28 | |
| 29 | func newUnsafeEFaceType(cfg *frozenConfig, type1 reflect.Type) *UnsafeEFaceType { |
| 30 | return &UnsafeEFaceType{ |
| 31 | unsafeType: *newUnsafeType(cfg, type1), |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | func (type2 *UnsafeEFaceType) IsNil(obj interface{}) bool { |
| 36 | if obj == nil { |
| 37 | return true |
| 38 | } |
| 39 | objEFace := unpackEFace(obj) |
| 40 | assertType("Type.IsNil argument 1", type2.ptrRType, objEFace.rtype) |
| 41 | return type2.UnsafeIsNil(objEFace.data) |
| 42 | } |
| 43 | |
| 44 | func (type2 *UnsafeEFaceType) UnsafeIsNil(ptr unsafe.Pointer) bool { |
| 45 | if ptr == nil { |
| 46 | return true |
| 47 | } |
| 48 | return unpackEFace(*(*interface{})(ptr)).data == nil |
| 49 | } |
| 50 | |
| 51 | func (type2 *UnsafeEFaceType) Indirect(obj interface{}) interface{} { |
| 52 | objEFace := unpackEFace(obj) |
| 53 | assertType("Type.Indirect argument 1", type2.ptrRType, objEFace.rtype) |
| 54 | return type2.UnsafeIndirect(objEFace.data) |
| 55 | } |
| 56 | |
| 57 | func (type2 *UnsafeEFaceType) UnsafeIndirect(ptr unsafe.Pointer) interface{} { |
| 58 | return *(*interface{})(ptr) |
| 59 | } |