reflectx

package
v2.14.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 31, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ArrayAt

func ArrayAt(p unsafe.Pointer, i int, elemSize uintptr) unsafe.Pointer

ArrayAt returns the i-th element of p, an array whose elements are elemSize bytes wide. The array pointed at by p must have at least i+1 elements: it is invalid (but impossible to check here) to pass i >= len, because then the result will point outside the array.

func BytesToString

func BytesToString(b []byte) string

BytesToString converts a []byte to string without copying memory.

It uses unsafe tricks, it may panic your program or result unpredictable behavior.

func IsIdenticalThriftType

func IsIdenticalThriftType(a, b any) (equal bool, diff string)

IsIdenticalThriftType checks whether the given two object types have same struct fields and memory layout, in case that a field's name does not match, but the thrift tag's first two fields match, it also considers the field matches.

It is almost same with IsIdenticalType, but helps the situation that different Thrift generators which generate different field names.

If two types are identical, it is expected that unsafe pointer casting between the two types won't crash the program. If the given two types are not identical, the returned diff message contains the detail difference.

func IsIdenticalType

func IsIdenticalType(a, b any) (equal bool, diff string)

IsIdenticalType checks whether the given two object types have same struct fields and memory layout (same order, same name and same type). It's useful to check generated types are exactly same in different packages, e.g. Thrift, Protobuf, Msgpack, etc.

If two types are identical, it is expected that unsafe pointer casting between the two types won't crash the program. If the given two types are not identical, the returned diff message contains the detail difference.

func IsIntType

func IsIntType(kind reflect.Kind) (isInt, isSigned bool)

IsIntType tells whether kind is an integer.

func IsNil

func IsNil(v any) bool

IsNil tells whether v is nil or the underlying data is nil.

func MapLen

func MapLen(m any) int

MapLen returns the length of the given map interface{} value. The provided m must be a map, else it panics.

func ReflectInt

func ReflectInt(v reflect.Value) int64

ReflectInt returns v's underlying value as int64. It panics if v is not a integer value.

func SliceCap

func SliceCap(slice any) int

SliceCap returns the capacity of the given slice interface{} value. The provided slice must be a slice, else it panics.

func SliceLen

func SliceLen(slice any) int

SliceLen returns the length of the given slice interface{} value. The provided slice must be a slice, else it panics.

func StringToBytes

func StringToBytes(s string) []byte

StringToBytes converts a string to []byte without copying memory.

It uses unsafe tricks, it may panic your program or result unpredictable behavior.

func TypedMemMove

func TypedMemMove(rtype *RType, dst, src unsafe.Pointer)

TypedMemMove exports the typedmemmove function in reflect package.

func TypedSliceCopy

func TypedSliceCopy(elemRType *RType, dst, src SliceHeader) int

TypedSliceCopy exports the typedslicecopy function in reflect package.

func UnpackSlice

func UnpackSlice(slice any) (EmptyInterface, *SliceHeader)

UnpackSlice unpacks the given slice interface{} to the underlying EmptyInterface and SliceHeader. It panics if param slice is not a slice.

Types

type EmptyInterface

type EmptyInterface struct {
	RType *RType         // *rtype
	Word  unsafe.Pointer // data pointer
}

EmptyInterface is the header for an interface{} value. It's a copy type of runtime.eface.

func EfaceOf

func EfaceOf(ep *any) EmptyInterface

EfaceOf casts the empty interface{} pointer to an EmptyInterface pointer.

type RType

type RType struct{}

func MapOf

func MapOf(key, elem *RType) *RType

MapOf returns the map type with the given key and element types. For example, if k represents int and e represents string, MapOf(k, e) represents map[int]string.

If the key type is not a valid map key type (that is, if it does not implement Go's == operator), MapOf panics.

func PtrTo

func PtrTo(t *RType) *RType

PtrTo returns the pointer type with element t. For example, if t represents type Foo, PtrTo(t) represents *Foo.

func RTypeOf

func RTypeOf(v any) *RType

RTypeOf returns the underlying rtype pointer of the given any value.

func SliceOf

func SliceOf(t *RType) *RType

SliceOf returns the slice type with element type t. For example, if t represents int, SliceOf(t) represents []int.

func ToRType

func ToRType(t reflect.Type) *RType

ToRType converts a reflect.Type value to *RType.

func (*RType) Align

func (t *RType) Align() int

func (*RType) AssignableTo

func (t *RType) AssignableTo(u reflect.Type) bool

func (*RType) Bits

func (t *RType) Bits() int

func (*RType) ChanDir

func (t *RType) ChanDir() reflect.ChanDir

func (*RType) Comparable

func (t *RType) Comparable() bool

func (*RType) ConvertibleTo

func (t *RType) ConvertibleTo(u reflect.Type) bool

func (*RType) Elem

func (t *RType) Elem() *RType

func (*RType) Field

func (t *RType) Field(i int) reflect.StructField

func (*RType) FieldAlign

func (t *RType) FieldAlign() int

func (*RType) FieldByIndex

func (t *RType) FieldByIndex(index []int) reflect.StructField

func (*RType) FieldByName

func (t *RType) FieldByName(name string) (reflect.StructField, bool)

func (*RType) FieldByNameFunc

func (t *RType) FieldByNameFunc(match func(string) bool) (reflect.StructField, bool)

func (*RType) IfaceIndir

func (t *RType) IfaceIndir() bool

func (*RType) Implements

func (t *RType) Implements(u reflect.Type) bool

func (*RType) In

func (t *RType) In(i int) reflect.Type

func (*RType) IsVariadic

func (t *RType) IsVariadic() bool

func (*RType) Key

func (t *RType) Key() *RType

func (*RType) Kind

func (t *RType) Kind() reflect.Kind

func (*RType) Len

func (t *RType) Len() int

func (*RType) Method

func (t *RType) Method(a0 int) reflect.Method

func (*RType) MethodByName

func (t *RType) MethodByName(a0 string) (reflect.Method, bool)

func (*RType) Name

func (t *RType) Name() string

func (*RType) NumField

func (t *RType) NumField() int

func (*RType) NumIn

func (t *RType) NumIn() int

func (*RType) NumMethod

func (t *RType) NumMethod() int

func (*RType) NumOut

func (t *RType) NumOut() int

func (*RType) Out

func (t *RType) Out(i int) reflect.Type

func (*RType) PackInterface

func (t *RType) PackInterface(word unsafe.Pointer) any

func (*RType) PkgPath

func (t *RType) PkgPath() string

func (*RType) Pointer

func (t *RType) Pointer() unsafe.Pointer

func (*RType) Size

func (t *RType) Size() uintptr

func (*RType) String

func (t *RType) String() string

func (*RType) ToReflectType added in v2.12.0

func (t *RType) ToReflectType() reflect.Type

type SliceHeader

type SliceHeader = unsafeheader.SliceHeader

SliceHeader is the runtime representation of a slice.

Unlike reflect.SliceHeader, its Data field is sufficient to guarantee the data it references will not be garbage collected.

func MakeSlice

func MakeSlice(elemTyp reflect.Type, length, capacity int) (slice any, header *SliceHeader)

MakeSlice makes a new slice of the given reflect.Type and length, capacity.

type StringHeader

type StringHeader = unsafeheader.StringHeader

StringHeader is the runtime representation of a string.

Unlike reflect.StringHeader, its Data field is sufficient to guarantee the data it references will not be garbage collected.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL