github.com/inaneverb/ekacore/ekaunsafe/v4@v4.0.0/iface.go (about) 1 // Copyright © 2020. All rights reserved. 2 // Author: Ilya Stroy. 3 // Contacts: iyuryevich@pm.me, https://github.com/qioalice 4 // License: https://opensource.org/licenses/MIT 5 6 package ekaunsafe 7 8 import ( 9 "unsafe" 10 ) 11 12 // Interface represents what "any" is in internal Golang parts. 13 type Interface struct { 14 Type uintptr // pointer to the type definition struct 15 Word unsafe.Pointer // pointer to the value 16 } 17 18 // Pack does the reverse thing that UnpackInterface does. 19 // It returns Golang any object that current Interface describes. 20 func (i Interface) Pack() any { 21 22 if i.Type == 0 { 23 return nil 24 } 25 26 var ret any 27 var iRet = (*Interface)(unsafe.Pointer(&ret)) 28 29 iRet.Type = i.Type 30 iRet.Word = i.Word 31 32 return ret 33 } 34 35 // Tuple returns Interface arguments as a tuple. It's just for convenient usage 36 // while chaining or unpacking interface and passing its parts to anywhere else. 37 func (i Interface) Tuple() (uintptr, unsafe.Pointer) { 38 return i.Type, i.Word 39 } 40 41 // PackInterface is the same as Interface.Pack method, 42 // but it is just a small convenient alias. 43 func PackInterface(rtype uintptr, word unsafe.Pointer) any { 44 return Interface{rtype, word}.Pack() 45 } 46 47 // UnpackInterface exposes Golang any internal parts and returns it. 48 // If passed argument is absolutely nil, returns an empty Interface object. 49 func UnpackInterface(i any) Interface { 50 if i == nil { 51 return Interface{} 52 } 53 return *(*Interface)(unsafe.Pointer(&i)) 54 }