github.com/jxskiss/gopkg@v0.17.3/reflectx/rtype.go (about)

     1  package reflectx
     2  
     3  import (
     4  	"github.com/jxskiss/gopkg/internal"
     5  	"github.com/jxskiss/gopkg/internal/linkname"
     6  	"reflect"
     7  	"unsafe"
     8  )
     9  
    10  // ---- reflect.Type ---- //
    11  
    12  // RType representing reflect.rtype for noescape trick.
    13  // It maps the exported methods of reflect.Type.
    14  type RType struct{}
    15  
    16  func (t *RType) Align() int {
    17  	return linkname.Reflect_rtype_Align(unsafe.Pointer(t))
    18  }
    19  
    20  func (t *RType) FieldAlign() int {
    21  	return linkname.Reflect_rtype_FieldAlign(unsafe.Pointer(t))
    22  }
    23  
    24  func (t *RType) Method(a0 int) reflect.Method {
    25  	return linkname.Reflect_rtype_Method(unsafe.Pointer(t), a0)
    26  }
    27  
    28  func (t *RType) MethodByName(a0 string) (reflect.Method, bool) {
    29  	return linkname.Reflect_rtype_MethodByName(unsafe.Pointer(t), a0)
    30  }
    31  
    32  func (t *RType) NumMethod() int {
    33  	return linkname.Reflect_rtype_NumMethod(unsafe.Pointer(t))
    34  }
    35  
    36  func (t *RType) Name() string {
    37  	return linkname.Reflect_rtype_Name(unsafe.Pointer(t))
    38  }
    39  
    40  func (t *RType) PkgPath() string {
    41  	return linkname.Reflect_rtype_PkgPath(unsafe.Pointer(t))
    42  }
    43  
    44  func (t *RType) Size() uintptr {
    45  	return linkname.Reflect_rtype_Size(unsafe.Pointer(t))
    46  }
    47  
    48  func (t *RType) String() string {
    49  	return linkname.Reflect_rtype_String(unsafe.Pointer(t))
    50  }
    51  
    52  func (t *RType) Kind() reflect.Kind {
    53  	return linkname.Reflect_rtype_Kind(unsafe.Pointer(t))
    54  }
    55  
    56  func (t *RType) Implements(u reflect.Type) bool {
    57  	return linkname.Reflect_rtype_Implements(unsafe.Pointer(t), u)
    58  }
    59  
    60  func (t *RType) AssignableTo(u reflect.Type) bool {
    61  	return linkname.Reflect_rtype_AssignableTo(unsafe.Pointer(t), u)
    62  }
    63  
    64  func (t *RType) ConvertibleTo(u reflect.Type) bool {
    65  	return linkname.Reflect_rtype_ConvertibleTo(unsafe.Pointer(t), u)
    66  }
    67  
    68  func (t *RType) Comparable() bool {
    69  	return linkname.Reflect_rtype_Comparable(unsafe.Pointer(t))
    70  }
    71  
    72  func (t *RType) Bits() int {
    73  	return linkname.Reflect_rtype_Bits(unsafe.Pointer(t))
    74  }
    75  
    76  func (t *RType) ChanDir() reflect.ChanDir {
    77  	return linkname.Reflect_rtype_ChanDir(unsafe.Pointer(t))
    78  }
    79  
    80  func (t *RType) IsVariadic() bool {
    81  	return linkname.Reflect_rtype_IsVariadic(unsafe.Pointer(t))
    82  }
    83  
    84  func (t *RType) Elem() *RType {
    85  	return ToRType(linkname.Reflect_rtype_Elem(unsafe.Pointer(t)))
    86  }
    87  
    88  func (t *RType) Field(i int) reflect.StructField {
    89  	return linkname.Reflect_rtype_Field(unsafe.Pointer(t), i)
    90  }
    91  
    92  func (t *RType) FieldByIndex(index []int) reflect.StructField {
    93  	return linkname.Reflect_rtype_FieldByIndex(unsafe.Pointer(t), index)
    94  }
    95  
    96  func (t *RType) FieldByName(name string) (reflect.StructField, bool) {
    97  	return linkname.Reflect_rtype_FieldByName(unsafe.Pointer(t), name)
    98  }
    99  
   100  func (t *RType) FieldByNameFunc(match func(string) bool) (reflect.StructField, bool) {
   101  	return linkname.Reflect_rtype_FieldByNameFunc(unsafe.Pointer(t), match)
   102  }
   103  
   104  func (t *RType) In(i int) reflect.Type {
   105  	return linkname.Reflect_rtype_In(unsafe.Pointer(t), i)
   106  }
   107  
   108  func (t *RType) Key() *RType {
   109  	return ToRType(linkname.Reflect_rtype_Key(unsafe.Pointer(t)))
   110  }
   111  
   112  func (t *RType) Len() int {
   113  	return linkname.Reflect_rtype_Len(unsafe.Pointer(t))
   114  }
   115  
   116  func (t *RType) NumField() int {
   117  	return linkname.Reflect_rtype_NumField(unsafe.Pointer(t))
   118  }
   119  
   120  func (t *RType) NumIn() int {
   121  	return linkname.Reflect_rtype_NumIn(unsafe.Pointer(t))
   122  }
   123  
   124  func (t *RType) NumOut() int {
   125  	return linkname.Reflect_rtype_NumOut(unsafe.Pointer(t))
   126  }
   127  
   128  func (t *RType) Out(i int) reflect.Type {
   129  	return linkname.Reflect_rtype_Out(unsafe.Pointer(t), i)
   130  }
   131  
   132  // ---- extended methods not in reflect package ---- //
   133  
   134  func (t *RType) IfaceIndir() bool {
   135  	return linkname.Reflect_ifaceIndir(unsafe.Pointer(t))
   136  }
   137  
   138  func (t *RType) PackInterface(word unsafe.Pointer) interface{} {
   139  	return *(*interface{})(unsafe.Pointer(&internal.EmptyInterface{
   140  		RType: unsafe.Pointer(t),
   141  		Word:  word,
   142  	}))
   143  }
   144  
   145  func (t *RType) ToType() reflect.Type {
   146  	return linkname.Reflect_toType(unsafe.Pointer(t))
   147  }
   148  
   149  func (t *RType) Pointer() unsafe.Pointer {
   150  	return unsafe.Pointer(t)
   151  }
   152  
   153  // ---- exported public functions ---- //
   154  
   155  // PtrTo returns the pointer type with element t.
   156  // For example, if t represents type Foo, PtrTo(t) represents *Foo.
   157  func PtrTo(t *RType) *RType {
   158  	return (*RType)(linkname.Reflect_rtype_ptrTo(unsafe.Pointer(t)))
   159  }
   160  
   161  // SliceOf returns the slice type with element type t.
   162  // For example, if t represents int, SliceOf(t) represents []int.
   163  func SliceOf(t *RType) *RType {
   164  	return ToRType(reflect.SliceOf(t.ToType()))
   165  }
   166  
   167  // MapOf returns the map type with the given key and element types.
   168  // For example, if k represents int and e represents string,
   169  // MapOf(k, e) represents map[int]string.
   170  //
   171  // If the key type is not a valid map key type (that is, if it does
   172  // not implement Go's == operator), MapOf panics.
   173  func MapOf(key, elem *RType) *RType {
   174  	return ToRType(reflect.MapOf(key.ToType(), elem.ToType()))
   175  }
   176  
   177  // ToRType converts a reflect.Type value to *Type.
   178  func ToRType(t reflect.Type) *RType {
   179  	return (*RType)((*iface)(unsafe.Pointer(&t)).data)
   180  }
   181  
   182  // RTypeOf returns the underlying rtype pointer of the given interface{} value.
   183  func RTypeOf(v interface{}) *RType {
   184  	switch x := v.(type) {
   185  	case *RType:
   186  		return x
   187  	case reflect.Type:
   188  		return ToRType(x)
   189  	case reflect.Value:
   190  		return ToRType(x.Type())
   191  	default:
   192  		eface := internal.EFaceOf(&x)
   193  		return (*RType)(eface.RType)
   194  	}
   195  }
   196  
   197  // ---- private things ---- //
   198  
   199  // iface is a copy type of runtime.iface.
   200  type iface struct {
   201  	tab  unsafe.Pointer // *itab
   202  	data unsafe.Pointer
   203  }