Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 1 | package reflect2 |
| 2 | |
| 3 | import ( |
| 4 | "reflect" |
| 5 | "unsafe" |
| 6 | ) |
| 7 | |
| 8 | type iface struct { |
| 9 | itab *itab |
| 10 | data unsafe.Pointer |
| 11 | } |
| 12 | |
| 13 | type itab struct { |
| 14 | ignore unsafe.Pointer |
| 15 | rtype unsafe.Pointer |
| 16 | } |
| 17 | |
| 18 | func IFaceToEFace(ptr unsafe.Pointer) interface{} { |
| 19 | iface := (*iface)(ptr) |
| 20 | if iface.itab == nil { |
| 21 | return nil |
| 22 | } |
| 23 | return packEFace(iface.itab.rtype, iface.data) |
| 24 | } |
| 25 | |
| 26 | type UnsafeIFaceType struct { |
| 27 | unsafeType |
| 28 | } |
| 29 | |
| 30 | func newUnsafeIFaceType(cfg *frozenConfig, type1 reflect.Type) *UnsafeIFaceType { |
| 31 | return &UnsafeIFaceType{ |
| 32 | unsafeType: *newUnsafeType(cfg, type1), |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | func (type2 *UnsafeIFaceType) Indirect(obj interface{}) interface{} { |
| 37 | objEFace := unpackEFace(obj) |
| 38 | assertType("Type.Indirect argument 1", type2.ptrRType, objEFace.rtype) |
| 39 | return type2.UnsafeIndirect(objEFace.data) |
| 40 | } |
| 41 | |
| 42 | func (type2 *UnsafeIFaceType) UnsafeIndirect(ptr unsafe.Pointer) interface{} { |
| 43 | return IFaceToEFace(ptr) |
| 44 | } |
| 45 | |
| 46 | func (type2 *UnsafeIFaceType) IsNil(obj interface{}) bool { |
| 47 | if obj == nil { |
| 48 | return true |
| 49 | } |
| 50 | objEFace := unpackEFace(obj) |
| 51 | assertType("Type.IsNil argument 1", type2.ptrRType, objEFace.rtype) |
| 52 | return type2.UnsafeIsNil(objEFace.data) |
| 53 | } |
| 54 | |
| 55 | func (type2 *UnsafeIFaceType) UnsafeIsNil(ptr unsafe.Pointer) bool { |
| 56 | if ptr == nil { |
| 57 | return true |
| 58 | } |
| 59 | iface := (*iface)(ptr) |
| 60 | if iface.itab == nil { |
| 61 | return true |
| 62 | } |
| 63 | return false |
| 64 | } |