github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/unsafe/reflectx/rtype.go (about)

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