unsafe_iface.go 1.3 KB

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