unsafe_eface.go 1.3 KB

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