blob: b60195533ccf5f8bdb8087ac0a009b9be3b456a0 [file] [log] [blame]
Zack Williamse940c7a2019-08-21 14:25:39 -07001package reflect2
2
3import (
4 "reflect"
5 "unsafe"
6)
7
8type iface struct {
9 itab *itab
10 data unsafe.Pointer
11}
12
13type itab struct {
14 ignore unsafe.Pointer
15 rtype unsafe.Pointer
16}
17
18func 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
26type UnsafeIFaceType struct {
27 unsafeType
28}
29
30func newUnsafeIFaceType(cfg *frozenConfig, type1 reflect.Type) *UnsafeIFaceType {
31 return &UnsafeIFaceType{
32 unsafeType: *newUnsafeType(cfg, type1),
33 }
34}
35
36func (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
42func (type2 *UnsafeIFaceType) UnsafeIndirect(ptr unsafe.Pointer) interface{} {
43 return IFaceToEFace(ptr)
44}
45
46func (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
55func (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}