blob: 805010f3a0c553db57da2e791ebbe15672b798a5 [file] [log] [blame]
Scott Bakere7144bc2019-10-01 14:16:47 -07001package reflect2
2
3import (
4 "reflect"
5 "unsafe"
6)
7
8type eface struct {
9 rtype unsafe.Pointer
10 data unsafe.Pointer
11}
12
13func unpackEFace(obj interface{}) *eface {
14 return (*eface)(unsafe.Pointer(&obj))
15}
16
17func 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
25type UnsafeEFaceType struct {
26 unsafeType
27}
28
29func newUnsafeEFaceType(cfg *frozenConfig, type1 reflect.Type) *UnsafeEFaceType {
30 return &UnsafeEFaceType{
31 unsafeType: *newUnsafeType(cfg, type1),
32 }
33}
34
35func (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
44func (type2 *UnsafeEFaceType) UnsafeIsNil(ptr unsafe.Pointer) bool {
45 if ptr == nil {
46 return true
47 }
48 return unpackEFace(*(*interface{})(ptr)).data == nil
49}
50
51func (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
57func (type2 *UnsafeEFaceType) UnsafeIndirect(ptr unsafe.Pointer) interface{} {
58 return *(*interface{})(ptr)
59}