github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/zreflect/type.go (about)

     1  package zreflect
     2  
     3  import (
     4  	"reflect"
     5  	"unsafe"
     6  	_ "unsafe"
     7  )
     8  
     9  func TypeOf(v interface{}) reflect.Type {
    10  	return toRType(NewType(v))
    11  }
    12  
    13  //go:linkname toRType reflect.toType
    14  //go:noescape
    15  func toRType(Type) reflect.Type
    16  
    17  func NewType(v interface{}) Type {
    18  	switch t := v.(type) {
    19  	case Type:
    20  		return t
    21  	case Value:
    22  		return t.typ
    23  	case reflect.Type:
    24  		return rtypeToType(t)
    25  	case reflect.Value:
    26  		return (*Value)(unsafe.Pointer(&t)).typ
    27  	default:
    28  		return (*Value)(unsafe.Pointer(&v)).typ
    29  	}
    30  
    31  }
    32  
    33  func (t *rtype) Native() reflect.Type {
    34  	return toRType(t)
    35  }
    36  
    37  func rtypeToType(t reflect.Type) Type {
    38  	return (Type)(((*Value)(unsafe.Pointer(&t))).ptr)
    39  }
    40  
    41  //go:linkname typeNumMethod reflect.(*rtype).NumMethod
    42  //go:noescape
    43  func typeNumMethod(Type) int
    44  
    45  // NumMethod returns the number of exported methods in the type's method set.
    46  func (t *rtype) NumMethod() int {
    47  	return typeNumMethod(t)
    48  }