reflectx

package
v2.0.0-alpha.2 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2022 License: MIT Imports: 8 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 CastInt

func CastInt(v interface{}) int64

CastInt returns an integer v's value as int64. v must be an integer, else it panics.

func CastIntPointer

func CastIntPointer(kind reflect.Kind, ptr unsafe.Pointer) int64

CastIntPointer returns ptr's value as int64, the underlying value is cast to int64 using unsafe tricks according kind.

If ptr is not pointed to an integer or kind does not match ptr, the behavior is undefined, it may panic or return incorrect value.

func IsIdenticalThriftType

func IsIdenticalThriftType(a, b interface{}) (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 interface{}) (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) bool

IsIntType tells whether kind is an integer.

func IsNil

func IsNil(v interface{}) bool

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

func MapLen

func MapLen(m interface{}) 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 interface{}) 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 interface{}) 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 interface{}) (*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 *interface{}) *EmptyInterface

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

type RType

type RType = rtype.RType

RType representing reflect.rtype for noescape trick. It maps the exported methods of reflect.Type.

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 interface{}) *RType

RTypeOf returns the underlying rtype pointer of the given interface{} 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.

type SliceHeader

type SliceHeader = unsafeheader.Slice

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 interface{}, header *SliceHeader)

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

type StringHeader

type StringHeader = unsafeheader.String

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