xunsafe

package module
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2023 License: Apache-2.0 Imports: 5 Imported by: 44

README

xunsafe (faster golang reflection)

GoReportCard GoDoc

This library is compatible with Go 1.17+

Please refer to CHANGELOG.md if you encounter breaking changes.

Motivation

In order to solve a problem that can work with any golang type, one has no choice but to use reflection. Native golang reflection comes with hefty performance price, on benchmarking simple getter/setter case to manipulate struct dynamically I've seen around 100 time worst performance comparing to statically typed code. I believe that, native reflect package could be easily implemented way better to provide optimized performance. This library comes with reflection implementation that greatly improved performance, that is between 25 to 50x time faster than native golang reflection. What that means that extra overhead of using reflection is only around 1.5 to four times comparing to statically typed code.

Introduction

In order to achieve better performance, this library uses unsafe.Pointer along with StructField.Offset to effectively access/modify struct fields. On top of that most of implemented methods, inline giving substantial performance boost which is x40 times as opposed to the same not inlined version.

Usage

Accessor/Mutator
func Example_FastReflection() {
    
    type Foo struct {
        ID int
        Name string
    }
    fooType := reflect.TypeOf(Foo{})

    fooID := xunsafe.FieldByName(fooType, "ID")
    fooName := xunsafe.FieldByName(fooType, "Name")

    var foos = make([]Foo, 100)
    for i := range foos {
        fooAddr := unsafe.Pointer(&foos[i])
        fooID.SetInt(fooAddr, i)
        fooName.SetString(fooAddr, fmt.Sprintf("name %d", i))
    }

    for i := range foos {
        fooAddr := unsafe.Pointer(&foos[i])
        fmt.Printf("[%v] ID: %v, Name: %v\n", i, fooID.Int(fooAddr), fooName.String(fooAddr))
    }
}
Field Address

Field Addr returns an interface{} wrapping actual field pointer

func ExampleAddr() {
	type Foo struct {
		ID int
		Name string
	}
	fooType := reflect.TypeOf(Foo{})
	fooID := xunsafe.FieldByName(fooType, "ID")
	foo := &Foo{ID: 101, Name: "name 101"}

	fooPtr := unsafe.Pointer(foo)
	*(fooID.Addr(fooPtr).(*int)) = 201
	fmt.Printf("foo.ID: %v\n", foo.ID)//prints 201
}
Field Value

Field Interface returns an interface{} wrapping actual field value

func ExampleAddr() {
	type Foo struct {
		ID int
		Name string
	}
	fooType := reflect.TypeOf(Foo{})
	fooID := xunsafe.FieldByName(fooType, "ID")
	foo := &Foo{ID: 101, Name: "name 101"}

    fooAddr := unsafe.Pointer(foo)
	fmt.Printf("foo.ID: %v\n", fooID.Interface(fooAddr))//prints 101
}

For base golang type Field Addr and Value got optimized with casting unsafe address to actual corresponding type. For example for filed with int type, the casting come in form (*int)(unsafe.Pointer(structAddr + field.Offset))

Slice range
        type T {ID int}
        aSlice := NewSlice(reflect.TypeOf([]T))
		var items = []T{
			{ID:1}, {ID:2}, {ID:3},
        }   
		aSlice.Range(unsafe.Pointer(&items), func(index int, item interface{}) bool {
			actual := item.(*T)
			fmt.Printf("%+v\n", item)
			return true //to continue
		})
Slice index
        type T {ID int}
        aSlice := NewSlice(reflect.TypeOf([]T))
		var items = []T{
			{ID:1}, {ID:2}, {ID:3},
        }   
		slicePtr := unsafe.Pointer(&items)
		for i :=0;i<len(items);i++ {
            item := aSlice.Index(slicePtr, i).(T)
            fmt.Printf("%+v\n", item)
        }
	
Slice appender
        type T {ID int}
        aSlice := NewSlice(reflect.TypeOf([]T))
		var items []T
		
		appender := aSlice.Appender(unsafe.Pointer(&items))
        appender.Append(T{ID:1},{ID:2})
        appender.Append(T{ID:3},T{ID:4})
        fmt.Printf("%v\n", items)
		
Arbitrary type Ref/Deref/Pointer

Defined Type implements Pointer and Deref arbitrary type.

        type T 
    	aType := xunsafe.NewType(reflect.TypeOf(T))
		var t T
		ref := aType.Ref(t) //return *T
		deref := aType.Deref(ref) //return T
		ptr := aType.Pointer(t) //return unsafe.Pointer

Bugs

This package operates on unsafe.Pointer and also uses some redefined private reflect package types like rtype, emptyInterface. While directed type should work well, some generic method like Field.Interface, Field.Set, may not support all data types. User of the package should ensure the code is fully tested and run test with -race and -gcflags=all=-d=checkptr flags

Benchmark

Accessor/Mutator benchmark

goos: darwin
goarch: amd64
pkg: github.com/viant/xunsafe
cpu: Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
BenchmarkField_Accessor_Native-16                       886540330                1.209 ns/op           0 B/op          0 allocs/op
BenchmarkField_Accessor_Direct_Xunsafe-16               606187651                1.967 ns/op           0 B/op          0 allocs/op
BenchmarkField_Accessor_Interface_Xunsafe-16            256453082                4.520 ns/op           0 B/op          0 allocs/op
BenchmarkField_Accessor_Interface_Reflect-16            10056830               118.1 ns/op            56 B/op          4 allocs/op
BenchmarkField_Accessor_Addr_Xunsafe-16                 168350235                7.225 ns/op           0 B/op          0 allocs/op
BenchmarkField_Accessor_Addr_Reflect-16                 20753077                49.50 ns/op            0 B/op          0 allocs/op
BenchmarkField_Mutator_Native-16                        1000000000               0.9133 ns/op          0 B/op          0 allocs/op
Benchmark_Mutator_Direct_Xunsafe-16                     773207817                1.513 ns/op           0 B/op          0 allocs/op
Benchmark_Mutator_Set_Xunsafe-16                        458994487                2.750 ns/op           0 B/op          0 allocs/op
Benchmark_Mutator_Xunsafe_Ptr-16                        183253660                6.550 ns/op           0 B/op          0 allocs/op
BenchmarkField_Mutator_Reflect-16                       10741909                94.74 ns/op           32 B/op          3 allocs/op
BenchmarkField_Mutator_Addr_Reflect-16                  31762166                36.48 ns/op            0 B/op          0 allocs/op
BenchmarkSlice_Index_Native-16                          309795711                3.766 ns/op           0 B/op          0 allocs/op
BenchmarkSlice_Index_Xunsafe-16                         100000000               10.53 ns/op            0 B/op          0 allocs/op
BenchmarkSlice_Index_Reflect-16                          5836300               206.1 ns/op            80 B/op         10 allocs/op
BenchmarkAppender_Append_Xunsafe-16                       933180              1142 ns/op            2000 B/op         11 allocs/op
BenchmarkAppender_Append_Reflect-16                       130723              8838 ns/op            4464 B/op        109 allocs/op
BenchmarkAppender_Append_Native-16                       2436530               475.1 ns/op          2040 B/op          8 allocs/op
  • 'Native' suffix represent statically typed code
  • 'Xunsafe' suffix represent reflection implemented by this library
  • 'Reflect' suffix represent implementation with golang native reflect package

Contributing to xunsafe

XUnsafe is an open source project and contributors are welcome!

It would be nice if at some point native golang library exposes ability to create ref/deref/pointer for an actual value behind the interface without that can be inlined to remove dependency/exposure of the private reflect pacakge types from this library.

    i := 101
    var v interface = i
	vPtr := xxxx.Pointer(v)
	vPtrInterface := xxxx.Ref(v)
	cloned := xxxx.Deref(vPtrInterface)

License

The source code is made available under the terms of the Apache License, Version 2, as stated in the file LICENSE.

Individual files may be made available under their own specific license, all compatible with Apache License, Version 2. Please see individual files for details.

Credits and Acknowledgements

Library Author: Adrian Witas

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AsBool added in v0.4.0

func AsBool(pointer unsafe.Pointer) bool

AsBool casts pointer to bool

func AsBoolAddrPtr added in v0.5.0

func AsBoolAddrPtr(pointer unsafe.Pointer) **bool

AsBoolAddrPtr casts pointer to **bool

func AsBoolPtr added in v0.4.0

func AsBoolPtr(pointer unsafe.Pointer) *bool

AsBoolPtr casts pointer to *bool

func AsBools added in v0.4.0

func AsBools(pointer unsafe.Pointer) []bool

AsBools casts pointer to bool slice

func AsBytesAddrPtr added in v0.5.0

func AsBytesAddrPtr(pointer unsafe.Pointer) **[]byte

AsBytesAddrPtr casts pointer to []byte pointer

func AsBytesPtr added in v0.5.0

func AsBytesPtr(pointer unsafe.Pointer) *[]byte

AsBytesPtr casts pointer to []byte pointer

func AsError added in v0.8.0

func AsError(pointer unsafe.Pointer) error

AsError casts pointer to error

func AsErrorAddrPtr added in v0.8.0

func AsErrorAddrPtr(pointer unsafe.Pointer) **error

AsErrorAddrPtr casts pointer to **error

func AsErrorPtr added in v0.8.0

func AsErrorPtr(pointer unsafe.Pointer) *error

AsErrorPtr casts pointer to *error

func AsFloat32 added in v0.4.0

func AsFloat32(pointer unsafe.Pointer) float32

AsFloat32 casts pointer to float32

func AsFloat32AddrPtr added in v0.5.0

func AsFloat32AddrPtr(pointer unsafe.Pointer) **float32

AsFloat32AddrPtr casts pointer to **float32

func AsFloat32Ptr added in v0.4.0

func AsFloat32Ptr(pointer unsafe.Pointer) *float32

AsFloat32Ptr casts pointer to *float32

func AsFloat32s added in v0.4.0

func AsFloat32s(pointer unsafe.Pointer) []float32

AsFloat32s casts pointer to float32 slice

func AsFloat32sPtr added in v0.5.0

func AsFloat32sPtr(pointer unsafe.Pointer) *[]float32

AsFloat32sPtr casts pointer to float32 slice

func AsFloat64 added in v0.4.0

func AsFloat64(pointer unsafe.Pointer) float64

AsFloat64 casts pointer to float64

func AsFloat64AddrPtr added in v0.5.0

func AsFloat64AddrPtr(pointer unsafe.Pointer) **float64

AsFloat64AddrPtr casts pointer to **float64

func AsFloat64Ptr added in v0.4.0

func AsFloat64Ptr(pointer unsafe.Pointer) *float64

AsFloat64Ptr casts pointer to *float64

func AsFloat64s added in v0.4.0

func AsFloat64s(pointer unsafe.Pointer) []float64

AsFloat64s casts pointer to float64 slice

func AsFloat64sPtr added in v0.5.0

func AsFloat64sPtr(pointer unsafe.Pointer) *[]float64

AsFloat64sPtr casts pointer to float64 slice pointer

func AsInt added in v0.4.0

func AsInt(pointer unsafe.Pointer) int

AsInt casts pointer to int

func AsInt16 added in v0.4.0

func AsInt16(pointer unsafe.Pointer) int16

AsInt16 casts pointer to int16

func AsInt16AddrPtr added in v0.5.0

func AsInt16AddrPtr(pointer unsafe.Pointer) **int16

AsInt16AddrPtr casts pointer to *int16

func AsInt16Ptr added in v0.4.0

func AsInt16Ptr(pointer unsafe.Pointer) *int16

AsInt16Ptr casts pointer to *int16

func AsInt16s added in v0.4.0

func AsInt16s(pointer unsafe.Pointer) []int16

AsInt16s casts pointer to int16 slice

func AsInt32 added in v0.4.0

func AsInt32(pointer unsafe.Pointer) int32

AsInt32 casts pointer to int32

func AsInt32AddrPtr added in v0.5.0

func AsInt32AddrPtr(pointer unsafe.Pointer) **int32

AsInt32AddrPtr casts pointer to **int32

func AsInt32Ptr added in v0.4.0

func AsInt32Ptr(pointer unsafe.Pointer) *int32

AsInt32Ptr casts pointer to *int32

func AsInt32s added in v0.4.0

func AsInt32s(pointer unsafe.Pointer) []int32

AsInt32s casts pointer to int32 slice

func AsInt64 added in v0.4.0

func AsInt64(pointer unsafe.Pointer) int64

AsInt64 casts pointer to int64

func AsInt64AddrPtr added in v0.5.0

func AsInt64AddrPtr(pointer unsafe.Pointer) **int64

AsInt64AddrPtr casts pointer to **int64

func AsInt64Ptr added in v0.4.0

func AsInt64Ptr(pointer unsafe.Pointer) *int64

AsInt64Ptr casts pointer to *int64

func AsInt64s added in v0.4.0

func AsInt64s(pointer unsafe.Pointer) []int64

AsInt64s casts pointer to int64 slice

func AsInt8 added in v0.4.0

func AsInt8(pointer unsafe.Pointer) int8

AsInt8 casts pointer to int8

func AsInt8AddrPtr added in v0.5.0

func AsInt8AddrPtr(pointer unsafe.Pointer) **int8

AsInt8AddrPtr casts pointer to *int8

func AsInt8Ptr added in v0.4.0

func AsInt8Ptr(pointer unsafe.Pointer) *int8

AsInt8Ptr casts pointer to *int8

func AsInt8s added in v0.4.0

func AsInt8s(pointer unsafe.Pointer) []int8

AsInt8s casts pointer to int8 slice

func AsIntAddrPtr added in v0.5.0

func AsIntAddrPtr(pointer unsafe.Pointer) **int

AsIntAddrPtr casts pointer to **int

func AsIntPtr added in v0.4.0

func AsIntPtr(pointer unsafe.Pointer) *int

AsIntPtr casts pointer to *int

func AsInterface added in v0.4.0

func AsInterface(pointer unsafe.Pointer) interface{}

AsInterface casts pointer to interface

func AsInterfaces added in v0.4.0

func AsInterfaces(pointer unsafe.Pointer) []interface{}

AsInterfaces casts pointer to interface skuce

func AsInts added in v0.4.0

func AsInts(pointer unsafe.Pointer) []int

AsInts casts pointer to []int

func AsIntsPtr added in v0.5.0

func AsIntsPtr(pointer unsafe.Pointer) *[]int

AsIntsPtr casts pointer to int slice pointer

func AsMap added in v0.4.0

func AsMap(pointer unsafe.Pointer) map[interface{}]interface{}

AsMap casts pointer to map[string]interface

func AsPointer added in v0.5.0

func AsPointer(v interface{}) unsafe.Pointer

AsPointer returns a pointer for an empty interface

func AsString added in v0.5.0

func AsString(pointer unsafe.Pointer) string

AsString casts pointer to string

func AsStringAddrPtr added in v0.5.0

func AsStringAddrPtr(pointer unsafe.Pointer) **string

AsStringAddrPtr casts pointer to **string

func AsStringMap added in v0.4.0

func AsStringMap(pointer unsafe.Pointer) map[string]interface{}

AsStringMap casts pointer to map[string]interface

func AsStringPtr added in v0.5.0

func AsStringPtr(pointer unsafe.Pointer) *string

AsStringPtr casts pointer to *string

func AsStrings added in v0.5.0

func AsStrings(pointer unsafe.Pointer) []string

AsStrings casts pointer to string slice

func AsStringsPtr added in v0.5.0

func AsStringsPtr(pointer unsafe.Pointer) *[]string

AsStringsPtr casts pointer to string slice pointer

func AsTime added in v0.4.0

func AsTime(pointer unsafe.Pointer) time.Time

AsTime cast pointer to time.Time

func AsTimeAddrPtr added in v0.5.0

func AsTimeAddrPtr(pointer unsafe.Pointer) **time.Time

AsTimeAddrPtr cast pointer to **time.Time

func AsTimePtr added in v0.4.0

func AsTimePtr(pointer unsafe.Pointer) *time.Time

AsTimePtr cast pointer to *time.Time

func AsUint added in v0.5.0

func AsUint(pointer unsafe.Pointer) uint

AsUint casts pointer to uint

func AsUint16 added in v0.5.0

func AsUint16(pointer unsafe.Pointer) uint16

AsUint16 casts pointer to uint16

func AsUint16AddrPtr added in v0.5.0

func AsUint16AddrPtr(pointer unsafe.Pointer) **uint16

AsUint16AddrPtr casts pointer to **uint

func AsUint16Ptr added in v0.5.0

func AsUint16Ptr(pointer unsafe.Pointer) *uint16

AsUint16Ptr casts pointer to *uint

func AsUint16s added in v0.5.0

func AsUint16s(pointer unsafe.Pointer) []uint16

AsUint16s casts pointer to uint16 slice

func AsUint32 added in v0.5.0

func AsUint32(pointer unsafe.Pointer) uint32

AsUint32 casts pointer to uint32

func AsUint32AddrPtr added in v0.5.0

func AsUint32AddrPtr(pointer unsafe.Pointer) **uint32

AsUint32AddrPtr casts pointer to *uint

func AsUint32Ptr added in v0.5.0

func AsUint32Ptr(pointer unsafe.Pointer) *uint32

AsUint32Ptr casts pointer to *uint

func AsUint32s added in v0.5.0

func AsUint32s(pointer unsafe.Pointer) []uint32

AsUint32s casts pointer to uint32 slice

func AsUint64 added in v0.5.0

func AsUint64(pointer unsafe.Pointer) uint64

AsUint64 casts pointer to uint64

func AsUint64AddrPtr added in v0.5.0

func AsUint64AddrPtr(pointer unsafe.Pointer) **uint64

AsUint64AddrPtr casts pointer to *uint

func AsUint64Ptr added in v0.5.0

func AsUint64Ptr(pointer unsafe.Pointer) *uint64

AsUint64Ptr casts pointer to *uint

func AsUint64s added in v0.5.0

func AsUint64s(pointer unsafe.Pointer) []uint64

AsUint64s casts pointer to uint64 slice

func AsUint8 added in v0.5.0

func AsUint8(pointer unsafe.Pointer) uint8

AsUint8 casts pointer to uint8

func AsUint8AddrPtr added in v0.5.0

func AsUint8AddrPtr(pointer unsafe.Pointer) **uint8

AsUint8AddrPtr casts pointer to **uint

func AsUint8Ptr added in v0.5.0

func AsUint8Ptr(pointer unsafe.Pointer) *uint8

AsUint8Ptr casts pointer to *uint

func AsUint8Ptrs added in v0.5.0

func AsUint8Ptrs(pointer unsafe.Pointer) *[]uint8

AsUint8Ptrs casts pointer to uint slice pointer

func AsUint8s added in v0.5.0

func AsUint8s(pointer unsafe.Pointer) []uint8

AsUint8s casts pointer to uint8 slice

func AsUintAddrPtr added in v0.5.0

func AsUintAddrPtr(pointer unsafe.Pointer) **uint

AsUintAddrPtr casts pointer to **uint

func AsUintPtr added in v0.5.0

func AsUintPtr(pointer unsafe.Pointer) *uint

AsUintPtr casts pointer to *uint

func AsUintptr added in v0.5.0

func AsUintptr(pointer unsafe.Pointer) uintptr

AsUintptr casts pointer to uint16

func AsUints added in v0.5.0

func AsUints(pointer unsafe.Pointer) []uint

AsUints casts pointer to uint slice

func Copy added in v0.8.0

func Copy(dest, src unsafe.Pointer, k int)

Copy k bytes from src to dest

func DerefPointer added in v0.5.0

func DerefPointer(pointer unsafe.Pointer) unsafe.Pointer

DerefPointer returns deref pointer (**T -> *T)

func EnsureAddressPointer added in v0.6.0

func EnsureAddressPointer(addrPtr unsafe.Pointer, target reflect.Type) *unsafe.Pointer

EnsureAddressPointer ensure that address pointer is not nil, ptr has to be address pointer

func EnsurePointer added in v0.5.0

func EnsurePointer(src interface{}) unsafe.Pointer

EnsurePointer returns unsafe pointer for src value or value pointer if you guarantee src is already pointer AsPointer is much faster option

func RefPointer added in v0.8.0

func RefPointer(pointer unsafe.Pointer) unsafe.Pointer

RefPointer returns reference to the pointer (*T -> **T)

func SafeDerefPointer added in v0.8.1

func SafeDerefPointer(pointer unsafe.Pointer, pType reflect.Type) unsafe.Pointer

SafeDerefPointer returns deref pointer (**T -> *T), pType has to be *T

func ValuePointer added in v0.4.0

func ValuePointer(v *reflect.Value) unsafe.Pointer

ValuePointer returns value pointer

Types

type Appender added in v0.4.0

type Appender struct {
	// contains filtered or unexported fields
}

Appender represents a slice appender

func (*Appender) Add added in v0.4.0

func (a *Appender) Add() interface{}

Add grows slice by 1 and returns item pointer (see UseItemAddrOpt)

func (*Appender) Append added in v0.4.0

func (a *Appender) Append(items ...interface{})

Append appends items to a slice

func (*Appender) Trunc added in v0.8.1

func (a *Appender) Trunc(size int) error

Trunc truncates a slice to provided size, if size grater than len return error

type Field

type Field struct {
	Name string
	reflect.Type
	Tag       reflect.StructTag
	Anonymous bool
	Offset    uintptr
	Index     uint16
	// contains filtered or unexported fields
}

Field represents a field

func FieldByIndex

func FieldByIndex(structType reflect.Type, index int) *Field

FieldByIndex creates a field for supplied struct type and field indexAddr

func FieldByName

func FieldByName(structType reflect.Type, name string) *Field

FieldByName creates a field for supplied struct type and field name

func NewField

func NewField(field reflect.StructField) *Field

NewField creates a new filed

func (*Field) Addr

func (f *Field) Addr(structPtr unsafe.Pointer) interface{}

Addr returns field addr

func (*Field) Bool

func (f *Field) Bool(structPtr unsafe.Pointer) bool

Bool cast field pointer to bool

func (*Field) BoolAddr

func (f *Field) BoolAddr(structPtr unsafe.Pointer) *bool

BoolAddr cast field pointer to **bool

func (*Field) BoolPtr

func (f *Field) BoolPtr(structPtr unsafe.Pointer) *bool

BoolPtr cast field pointer to *bool

func (*Field) Bytes

func (f *Field) Bytes(structPtr unsafe.Pointer) []byte

Bytes cast field pointer to []byte

func (*Field) BytesPtr

func (f *Field) BytesPtr(structPtr unsafe.Pointer) *[]byte

BytesPtr cast field pointer to *[]byte

func (*Field) EnsurePointer added in v0.5.0

func (f *Field) EnsurePointer(structPtr unsafe.Pointer) unsafe.Pointer

EnsurePointer initialises field type pointer if needed, and return filed type value pointer rather field pointer. for example if field is of T type this method returns *T, in case field is of *T, this method also return *T, if you need always field pointer use Field.Pointer method

func (*Field) Error added in v0.8.0

func (f *Field) Error(structPtr unsafe.Pointer) error

Error cast field pointer to error

func (*Field) ErrorPtr added in v0.8.0

func (f *Field) ErrorPtr(structPtr unsafe.Pointer) *error

ErrorPtr cast field pointer to *error

func (*Field) Float32

func (f *Field) Float32(structPtr unsafe.Pointer) float32

Float32 cast field pointer to float32

func (*Field) Float32Addr

func (f *Field) Float32Addr(structPtr unsafe.Pointer) *float32

Float32Addr cast field pointer to *float32

func (*Field) Float32Ptr

func (f *Field) Float32Ptr(structPtr unsafe.Pointer) *float32

Float32Ptr cast field pointer to *float32

func (*Field) Float64

func (f *Field) Float64(structPtr unsafe.Pointer) float64

Float64 cast field pointer to float64

func (*Field) Float64Addr

func (f *Field) Float64Addr(structPtr unsafe.Pointer) *float64

Float64Addr cast field pointer to *float64

func (*Field) Float64Ptr

func (f *Field) Float64Ptr(structPtr unsafe.Pointer) *float64

Float64Ptr cast field pointer to *float64

func (*Field) Int

func (f *Field) Int(structPtr unsafe.Pointer) int

Int cast field pointer to int

func (*Field) Int16

func (f *Field) Int16(structPtr unsafe.Pointer) int16

Int16 cast field pointer to int16

func (*Field) Int16Addr

func (f *Field) Int16Addr(structPtr unsafe.Pointer) *int16

Int16Addr returns *int16

func (*Field) Int16Ptr

func (f *Field) Int16Ptr(structPtr unsafe.Pointer) *int16

Int16Ptr cast field pointer to *int16

func (*Field) Int32

func (f *Field) Int32(structPtr unsafe.Pointer) int32

Int32 cast field pointer to int32

func (*Field) Int32Addr

func (f *Field) Int32Addr(structPtr unsafe.Pointer) *int32

Int32Addr cast field pointer to *int32

func (*Field) Int32Ptr

func (f *Field) Int32Ptr(structPtr unsafe.Pointer) *int32

Int32Ptr cast field pointer to *int32

func (*Field) Int64

func (f *Field) Int64(structPtr unsafe.Pointer) int64

Int64 cast field pointer to int64

func (*Field) Int64Addr

func (f *Field) Int64Addr(structPtr unsafe.Pointer) *int64

Int64Addr cast field pointer to *int64

func (*Field) Int64Ptr

func (f *Field) Int64Ptr(structPtr unsafe.Pointer) *int64

Int64Ptr cast field pointer to *int

func (*Field) Int8

func (f *Field) Int8(structPtr unsafe.Pointer) int8

Int8 cast field pointer to int8

func (*Field) Int8Addr

func (f *Field) Int8Addr(structPtr unsafe.Pointer) *int8

Int8Addr cast field pointer to *int8

func (*Field) Int8Ptr

func (f *Field) Int8Ptr(structPtr unsafe.Pointer) *int8

Int8Ptr cast field pointer to *int8

func (*Field) IntAddr

func (f *Field) IntAddr(structPtr unsafe.Pointer) *int

IntAddr cast field pointer to **int

func (*Field) IntPtr

func (f *Field) IntPtr(structPtr unsafe.Pointer) *int

IntPtr cast field pointer to *int

func (*Field) Interface

func (f *Field) Interface(structPtr unsafe.Pointer) interface{}

Interface cast field pointer to value

func (*Field) IsNil added in v0.8.1

func (f *Field) IsNil(structPtr unsafe.Pointer) bool

IsNil returns nil if structPtr is nil or field value is nil

func (*Field) MustBeAssignable added in v0.8.4

func (f *Field) MustBeAssignable(y interface{})

func (*Field) Pointer added in v0.5.0

func (f *Field) Pointer(structPtr unsafe.Pointer) unsafe.Pointer

Pointer return field pointer (structPtr + field.Offset)

func (*Field) SafePointer added in v0.5.0

func (f *Field) SafePointer(structPtr unsafe.Pointer, target reflect.Type) unsafe.Pointer

SafePointer returns field pointer, if field pointer is a pointer this method initialises that pointer

func (*Field) Set added in v0.4.0

func (f *Field) Set(structPtr unsafe.Pointer, source interface{})

Set sets only non pointer value, the reason for this limited functionality method is speed, its 20x faster than SetValue

func (*Field) SetBool

func (f *Field) SetBool(structPtr unsafe.Pointer, val bool)

SetBool sets field bool

func (*Field) SetBoolAt added in v0.8.4

func (f *Field) SetBoolAt(ptr unsafe.Pointer, source interface{})

func (*Field) SetBoolPtr

func (f *Field) SetBoolPtr(structPtr unsafe.Pointer, val *bool)

SetBoolPtr sets field *bool

func (*Field) SetBytes

func (f *Field) SetBytes(structPtr unsafe.Pointer, val []byte)

SetBytes sets field []byte

func (*Field) SetBytesPtr

func (f *Field) SetBytesPtr(structPtr unsafe.Pointer, val *[]byte)

SetBytesPtr sets field *[]byte

func (*Field) SetError added in v0.8.0

func (f *Field) SetError(structPtr unsafe.Pointer, val error)

SetError sets field error

func (*Field) SetFloat32

func (f *Field) SetFloat32(structPtr unsafe.Pointer, val float32)

SetFloat32 sets field float32

func (*Field) SetFloat32At added in v0.8.4

func (f *Field) SetFloat32At(ptr unsafe.Pointer, source interface{})

func (*Field) SetFloat32Ptr

func (f *Field) SetFloat32Ptr(structPtr unsafe.Pointer, val *float32)

SetFloat32Ptr sets field *float32

func (*Field) SetFloat64

func (f *Field) SetFloat64(structPtr unsafe.Pointer, val float64)

SetFloat64 sets field float64

func (*Field) SetFloat64At added in v0.8.4

func (f *Field) SetFloat64At(ptr unsafe.Pointer, source interface{})

func (*Field) SetFloat64Ptr

func (f *Field) SetFloat64Ptr(structPtr unsafe.Pointer, val *float64)

SetFloat64Ptr sets field *float64

func (*Field) SetFunc added in v0.8.4

func (f *Field) SetFunc(structPtr unsafe.Pointer, source interface{})

func (*Field) SetFuncAt added in v0.8.4

func (f *Field) SetFuncAt(ptr unsafe.Pointer, source interface{})

func (*Field) SetIface added in v0.8.4

func (f *Field) SetIface(structPtr unsafe.Pointer, source interface{})

func (*Field) SetInt

func (f *Field) SetInt(structPtr unsafe.Pointer, val int)

SetInt sets field int

func (*Field) SetInt16

func (f *Field) SetInt16(structPtr unsafe.Pointer, val int16)

SetInt16 sets field int

func (*Field) SetInt16Ptr

func (f *Field) SetInt16Ptr(structPtr unsafe.Pointer, val *int16)

SetInt16Ptr sets field *int

func (*Field) SetInt32

func (f *Field) SetInt32(structPtr unsafe.Pointer, val int32)

SetInt32 sets field int

func (*Field) SetInt32Ptr

func (f *Field) SetInt32Ptr(structPtr unsafe.Pointer, val *int32)

SetInt32Ptr sets field *int

func (*Field) SetInt64

func (f *Field) SetInt64(structPtr unsafe.Pointer, val int64)

SetInt64 sets field int

func (*Field) SetInt64Ptr

func (f *Field) SetInt64Ptr(structPtr unsafe.Pointer, val *int64)

SetInt64Ptr sets field *int

func (*Field) SetInt8

func (f *Field) SetInt8(structPtr unsafe.Pointer, val int8)

SetInt8 sets field int

func (*Field) SetInt8Ptr

func (f *Field) SetInt8Ptr(structPtr unsafe.Pointer, val *int8)

SetInt8Ptr sets field *int

func (*Field) SetIntAt added in v0.8.4

func (f *Field) SetIntAt(ptr unsafe.Pointer, source interface{})

func (*Field) SetIntPtr

func (f *Field) SetIntPtr(structPtr unsafe.Pointer, val *int)

SetIntPtr sets field *int

func (*Field) SetInterface

func (f *Field) SetInterface(structPtr unsafe.Pointer, val interface{})

SetInterface set field interface{}, only support empty interfaces

func (*Field) SetInterfaceAt added in v0.8.4

func (f *Field) SetInterfaceAt(ptr unsafe.Pointer, source interface{})

func (*Field) SetMap added in v0.8.4

func (f *Field) SetMap(structPtr unsafe.Pointer, aMap interface{})

func (*Field) SetMapAt added in v0.8.4

func (f *Field) SetMapAt(ptr unsafe.Pointer, source interface{})

func (*Field) SetSlice added in v0.8.4

func (f *Field) SetSlice(structPtr unsafe.Pointer, source interface{})

func (*Field) SetSliceAt added in v0.8.4

func (f *Field) SetSliceAt(ptr unsafe.Pointer, source interface{})

func (*Field) SetString

func (f *Field) SetString(structPtr unsafe.Pointer, val string)

SetString sets field string

func (*Field) SetStringAt added in v0.8.4

func (f *Field) SetStringAt(ptr unsafe.Pointer, source interface{})

func (*Field) SetStringPtr

func (f *Field) SetStringPtr(structPtr unsafe.Pointer, val *string)

SetStringPtr sets field *string

func (*Field) SetStruct added in v0.8.4

func (f *Field) SetStruct(structPtr unsafe.Pointer, source interface{})

func (*Field) SetStructAt added in v0.8.4

func (f *Field) SetStructAt(ptr unsafe.Pointer, source interface{})

func (*Field) SetTime

func (f *Field) SetTime(structPtr unsafe.Pointer, val time.Time)

SetTime sets field time.Time

func (*Field) SetTimePtr

func (f *Field) SetTimePtr(structPtr unsafe.Pointer, val *time.Time)

SetTimePtr sets field *time.Time

func (*Field) SetUint

func (f *Field) SetUint(structPtr unsafe.Pointer, val uint)

SetUint sets field uint

func (*Field) SetUint16

func (f *Field) SetUint16(structPtr unsafe.Pointer, val uint16)

SetUint16 sets field uint

func (*Field) SetUint16Ptr

func (f *Field) SetUint16Ptr(structPtr unsafe.Pointer, val *uint16)

SetUint16Ptr sets field *uint

func (*Field) SetUint32

func (f *Field) SetUint32(structPtr unsafe.Pointer, val uint32)

SetUint32 sets field uint

func (*Field) SetUint32Ptr

func (f *Field) SetUint32Ptr(structPtr unsafe.Pointer, val *uint32)

SetUint32Ptr sets field *uint

func (*Field) SetUint64

func (f *Field) SetUint64(structPtr unsafe.Pointer, val uint64)

SetUint64 sets field uint

func (*Field) SetUint64Ptr

func (f *Field) SetUint64Ptr(structPtr unsafe.Pointer, val *uint64)

SetUint64Ptr sets field *uint

func (*Field) SetUint8

func (f *Field) SetUint8(structPtr unsafe.Pointer, val uint8)

SetUint8 sets field uint

func (*Field) SetUint8Ptr

func (f *Field) SetUint8Ptr(structPtr unsafe.Pointer, val *uint8)

SetUint8Ptr sets field *uint

func (*Field) SetUintPtr

func (f *Field) SetUintPtr(structPtr unsafe.Pointer, val *uint)

SetUintPtr sets field *uint

func (*Field) SetValue

func (f *Field) SetValue(structPtr unsafe.Pointer, source interface{})

SetValue sets value

func (*Field) String

func (f *Field) String(structPtr unsafe.Pointer) string

String cast field pointer to string

func (*Field) StringAddr

func (f *Field) StringAddr(structPtr unsafe.Pointer) *string

StringAddr field pointer to *string

func (*Field) StringPtr

func (f *Field) StringPtr(structPtr unsafe.Pointer) *string

StringPtr cast field pointer to *string

func (*Field) Time

func (f *Field) Time(structPtr unsafe.Pointer) time.Time

Time cast field pointer to time.Time

func (*Field) TimePtr

func (f *Field) TimePtr(structPtr unsafe.Pointer) *time.Time

TimePtr cast field pointer to *time.Time

func (*Field) Uint

func (f *Field) Uint(structPtr unsafe.Pointer) uint

Uint cast field pointer to uint

func (*Field) Uint16

func (f *Field) Uint16(structPtr unsafe.Pointer) uint16

Uint16 cast field pointer to uint16

func (*Field) Uint16Addr

func (f *Field) Uint16Addr(structPtr unsafe.Pointer) *uint16

Uint16Addr cast field pointer to *uint16

func (*Field) Uint16Ptr

func (f *Field) Uint16Ptr(structPtr unsafe.Pointer) *uint16

Uint16Ptr cast field pointer to *uint16

func (*Field) Uint32

func (f *Field) Uint32(structPtr unsafe.Pointer) uint32

Uint32 cast field pointer to uint32

func (*Field) Uint32Addr

func (f *Field) Uint32Addr(structPtr unsafe.Pointer) *uint32

Uint32Addr cast field pointer to *uint32

func (*Field) Uint32Ptr

func (f *Field) Uint32Ptr(structPtr unsafe.Pointer) *uint32

Uint32Ptr cast field pointer to *uint32

func (*Field) Uint64

func (f *Field) Uint64(structPtr unsafe.Pointer) uint64

Uint64 cast field pointer to uint64

func (*Field) Uint64Addr

func (f *Field) Uint64Addr(structPtr unsafe.Pointer) *uint64

Uint64Addr cast field pointer to *uint64

func (*Field) Uint64Ptr

func (f *Field) Uint64Ptr(structPtr unsafe.Pointer) *uint64

Uint64Ptr cast field pointer to *uint64

func (*Field) Uint8

func (f *Field) Uint8(structPtr unsafe.Pointer) uint8

Uint8 cast field pointer to uint8

func (*Field) Uint8Addr

func (f *Field) Uint8Addr(structPtr unsafe.Pointer) *uint8

Uint8Addr cast field pointer to *uint8

func (*Field) Uint8Ptr

func (f *Field) Uint8Ptr(structPtr unsafe.Pointer) *uint8

Uint8Ptr cast field pointer to *uint8

func (*Field) UintAddr

func (f *Field) UintAddr(structPtr unsafe.Pointer) *uint

UintAddr cast field pointer to **uint

func (*Field) UintPtr

func (f *Field) UintPtr(structPtr unsafe.Pointer) *uint

UintPtr cast field pointer to *uint

func (*Field) Value

func (f *Field) Value(structPtr unsafe.Pointer) interface{}

Value returns field value

func (*Field) ValuePointer added in v0.8.0

func (f *Field) ValuePointer(structPtr unsafe.Pointer) unsafe.Pointer

ValuePointer return pointer to T, if value is as *T, this method would dereference it

type Getter

type Getter func(structAdd unsafe.Pointer) interface{}

Getter represents a func returning field value pointer, it takes holder address

type Matcher added in v0.5.0

type Matcher struct {
	// contains filtered or unexported fields
}

Matcher represents a field matcher

func (*Matcher) Match added in v0.5.0

func (s *Matcher) Match(name string) *Field

Match matches field with name

type Slice added in v0.2.0

type Slice struct {
	reflect.Type
	// contains filtered or unexported fields
}

Slice represents a slice

func NewSlice added in v0.2.0

func NewSlice(sliceType reflect.Type, options ...interface{}) *Slice

NewSlice creates slice

func (*Slice) AddrAt added in v0.7.0

func (s *Slice) AddrAt(slicePtr unsafe.Pointer, index int) interface{}

AddrAt return slice item addr for supplied index

func (*Slice) Appender added in v0.4.0

func (s *Slice) Appender(slicePointer unsafe.Pointer) *Appender

Appender returns a slice appender

func (*Slice) Cap added in v0.7.0

func (s *Slice) Cap(slicePtr unsafe.Pointer) int

Cap return slice capacity

func (*Slice) Len added in v0.7.0

func (s *Slice) Len(slicePtr unsafe.Pointer) int

Len return slice length

func (*Slice) PointerAt added in v0.7.0

func (s *Slice) PointerAt(sliceAddr unsafe.Pointer, index uintptr) unsafe.Pointer

PointerAt return slice item pointer for supplied index

func (*Slice) Range added in v0.2.0

func (s *Slice) Range(slicePtr unsafe.Pointer, visit func(index int, item interface{}) bool)

Range call visit callback for each slice element , to terminate visit should return false use useItemAddr would use item pointer as *T for a slice defined as []T or []*T, otherwise for slice defined as []*T, item would get **T pointer

func (*Slice) SetValueAt added in v0.9.0

func (s *Slice) SetValueAt(slicePtr unsafe.Pointer, index int, value interface{})

func (*Slice) ValueAt added in v0.7.0

func (s *Slice) ValueAt(slicePtr unsafe.Pointer, index int) interface{}

ValueAt return slice item for supplied index

func (*Slice) ValuePointerAt added in v0.7.0

func (s *Slice) ValuePointerAt(slicePtr unsafe.Pointer, index int) interface{}

ValuePointerAt return value pointer *T, for both []T and []*T slice definition

type Struct added in v0.5.0

type Struct struct {
	Fields []Field
}

Struct represents a struct

func NewStruct added in v0.5.0

func NewStruct(sType reflect.Type) *Struct

NewStruct creates a unsafe struct wrapper

func (*Struct) Matcher added in v0.5.0

func (s *Struct) Matcher(keyFn func(string) string) *Matcher

Matcher creates a filed matched for supplied key Fn

type Type added in v0.5.0

type Type struct {
	// contains filtered or unexported fields
}

Type represents a Type

func NewType added in v0.5.0

func NewType(t reflect.Type) *Type

NewType creates a type

func (*Type) Deref added in v0.5.0

func (t *Type) Deref(val interface{}) interface{}

Deref dereference pointer

func (*Type) Interface added in v0.5.1

func (t *Type) Interface(ptr unsafe.Pointer) interface{}

Interface returns an interface for the pointer

func (*Type) Kind added in v0.8.0

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

Kind returns reflect kind

func (*Type) Pointer added in v0.5.0

func (t *Type) Pointer(value interface{}) unsafe.Pointer

Pointer returns a pointer

func (*Type) Ref added in v0.5.0

func (t *Type) Ref(value interface{}) interface{}

Ref returns a reference to value

func (*Type) Type added in v0.5.0

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

Type returns reflect type

func (*Type) Value added in v0.8.1

func (t *Type) Value(ptr unsafe.Pointer) (v interface{})

Value returns value for the original type

type UseItemAddrOpt added in v0.4.0

type UseItemAddrOpt bool

UseItemAddrOpt option that instructs implementation to use item address as **T for []*T or *T for []T, otherwise *T would be used

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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