xunsafe

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2021 License: Apache-2.0 Imports: 7 Imported by: 58

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 50x time faster than native golang reflection. What that means that extra overhead of using reflection is only around twice 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.

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 := xunsafe.Addr(&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"}

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

Field Value 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.Value(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)) in other cases reflect.NewAt(field.Type, unsafe.Pointer(structAddr + field.Offset) is used.

Unsafe struct casting registry

Given that reflect.NewAt is quite slow, you can register custom unsafe type casting bypassing reflect.NewAt all together

    xunsafe.Register(reflect.TypeOf(time.Time{}), func(addr unsafe.Pointer) interface{} {
		return (*time.Time)(addr)
	})
    xunsafe.Register(reflect.TypeOf(&time.Time{}), func(addr unsafe.Pointer) interface{} {
		return (**time.Time)(addr)
	})

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, addr unsafe.Pointer) bool {
			item := (*T)(addr)
			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},
        }   
		index := aSlice.Index(unsafe.Pointer(&items))
		for i :=0;i<len(items);i++ {
            item := (*T)(index(i))
            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(unsafe.Pointer(&T{ID:1}),unsafe.Pointer(&T{ID:2}))
        appender.Append(unsafe.Pointer(&T{ID:3}),unsafe.Pointer(&T{ID:4}))
        fmt.Printf("%v\n", items)
		
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               15784005                69.36 ns/op            0 B/op          0 allocs/op
BenchmarkField_Accessor_Xunsafe-16               6739143               178.8 ns/op             0 B/op          0 allocs/op
BenchmarkField_Accessor_Value-16                 2807060               439.8 ns/op            56 B/op          3 allocs/op
BenchmarkField_Accessor_Reflect-16               1470356               826.7 ns/op            72 B/op          4 allocs/op
BenchmarkField_Accessor_PtrXunsafe-16            4269766               278.6 ns/op             0 B/op          0 allocs/op
BenchmarkField_Accessor_Reflect_Ptr-16           1902192               635.8 ns/op             0 B/op          0 allocs/op
BenchmarkField_Mutator_Native-16                25268916                46.56 ns/op            0 B/op          0 allocs/op
Benchmark_Mutator_Fast-16                        9487674               125.5 ns/op             0 B/op          0 allocs/op
Benchmark_Mutator_Fast_Ptr-16                    5344639               224.0 ns/op             0 B/op          0 allocs/op
BenchmarkField_Mutator_Reflect-16                2005026               604.2 ns/op            48 B/op          3 allocs/op
BenchmarkField_Mutator_Reflect_Ptr-16            2423137               483.6 ns/op             0 B/op          0 allocs/op
BenchmarkSlice_Index_Native-16                  14769567                76.22 ns/op
BenchmarkSlice_Index_Xunsafe-16                  4705651               243.9 ns/op
BenchmarkSlice_Index_Reflect-16                   643263              2001 ns/op
BenchmarkAppender_Append_Xunsafe-16                57892             19709 ns/op
BenchmarkAppender_Append_Relfect-16                18657             67098 ns/op
BenchmarkAppender_Append_Native-16                354895              3180 ns/op
PASS
ok      github.com/viant/xunsafe        26.395s
awitas@AWITAS-C02C42QCMD6R xunsafe % go teset -bench=.                              
go teset: unknown command
Run 'go help' for usage.
awitas@AWITAS-C02C42QCMD6R xunsafe % go test -bench=. 
goos: darwin
goarch: amd64
pkg: github.com/viant/xunsafe
cpu: Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
BenchmarkField_Accessor_Native-16               933782421                1.215 ns/op           0 B/op          0 allocs/op
BenchmarkField_Accessor_Xunsafe-16              594855732                1.934 ns/op           0 B/op          0 allocs/op
BenchmarkField_Accessor_Value-16                16291875                67.54 ns/op           44 B/op          3 allocs/op
BenchmarkField_Accessor_Reflect-16               9490488               115.7 ns/op            56 B/op          4 allocs/op
BenchmarkField_Accessor_PtrXunsafe-16           100000000               10.27 ns/op            0 B/op          0 allocs/op
BenchmarkField_Accessor_Reflect_Ptr-16          23711229                47.98 ns/op            0 B/op          0 allocs/op
BenchmarkField_Mutator_Native-16                1000000000               0.9083 ns/op          0 B/op          0 allocs/op
Benchmark_Mutator_Xunsafe-16                    778190858                1.529 ns/op           0 B/op          0 allocs/op
Benchmark_Mutator_Xunsafe_Ptr-16                156936856                7.637 ns/op           0 B/op          0 allocs/op
BenchmarkField_Mutator_Reflect-16               12456297                93.22 ns/op           32 B/op          3 allocs/op
BenchmarkField_Mutator_Reflect_Ptr-16           32652355                36.07 ns/op            0 B/op          0 allocs/op
BenchmarkSlice_Index_Native-16                  205128967                5.789 ns/op           0 B/op          0 allocs/op
BenchmarkSlice_Index_Xunsafe-16                 157107307                7.303 ns/op           0 B/op          0 allocs/op
BenchmarkSlice_Index_Reflect-16                  5529320               201.7 ns/op            80 B/op         10 allocs/op
BenchmarkAppender_Append_Xunsafe-16               857829              1331 ns/op            2128 B/op         13 allocs/op
BenchmarkAppender_Append_Relfect-16               140394              8425 ns/op            4464 B/op        109 allocs/op
BenchmarkAppender_Append_Native-16               2400387               480.7 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

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Addr

func Addr(src interface{}) unsafe.Pointer

Addr returns src unsafe addr

Example
package main

import (
	"fmt"
	"github.com/viant/xunsafe"
	"reflect"
)

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

	fooAddr := xunsafe.Addr(foo)
	*(fooID.Addr(fooAddr).(*int)) = 201
	fmt.Printf("foo.ID: %v\n", foo.ID) //prints 201
}
Output:

func AsBool added in v0.4.0

func AsBool(pointer unsafe.Pointer) bool

AsBool casts pointer to int

func AsBoolPtr added in v0.4.0

func AsBoolPtr(pointer unsafe.Pointer) *bool

AsBoolPtr casts pointer to *int

func AsBoolPtrs added in v0.4.0

func AsBoolPtrs(pointer unsafe.Pointer) []*bool

AsBoolPtrs casts pointer to *bool slice

func AsBools added in v0.4.0

func AsBools(pointer unsafe.Pointer) []bool

AsBools casts pointer to bool slice

func AsFloat32 added in v0.4.0

func AsFloat32(pointer unsafe.Pointer) float32

AsFloat32 casts pointer to int

func AsFloat32Ptr added in v0.4.0

func AsFloat32Ptr(pointer unsafe.Pointer) *float32

AsFloat32Ptr casts pointer to *int

func AsFloat32Ptrs added in v0.4.0

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

AsFloat32Ptrs casts pointer to *float32 slice

func AsFloat32s added in v0.4.0

func AsFloat32s(pointer unsafe.Pointer) []float32

AsFloat32s casts pointer to float32 slice

func AsFloat64 added in v0.4.0

func AsFloat64(pointer unsafe.Pointer) float64

AsFloat64 casts pointer to int

func AsFloat64Ptr added in v0.4.0

func AsFloat64Ptr(pointer unsafe.Pointer) *float64

AsFloat64Ptr casts pointer to *int

func AsFloat64Ptrs added in v0.4.0

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

AsFloat64Ptrs casts pointer to *float64 slice

func AsFloat64s added in v0.4.0

func AsFloat64s(pointer unsafe.Pointer) []float64

AsFloat64s casts pointer to float64 slice

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 AsInt16Ptr added in v0.4.0

func AsInt16Ptr(pointer unsafe.Pointer) *int16

AsInt16Ptr casts pointer to *int16

func AsInt16Ptrs added in v0.4.0

func AsInt16Ptrs(pointer unsafe.Pointer) []*int16

AsInt16Ptrs casts pointer to *int16 slice

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 AsInt32Ptr added in v0.4.0

func AsInt32Ptr(pointer unsafe.Pointer) *int32

AsInt32Ptr casts pointer to *int32

func AsInt32Ptrs added in v0.4.0

func AsInt32Ptrs(pointer unsafe.Pointer) []*int32

AsInt32Ptrs casts pointer to *int32 slice

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 AsInt64Ptr added in v0.4.0

func AsInt64Ptr(pointer unsafe.Pointer) *int64

AsInt64Ptr casts pointer to *int64

func AsInt64Ptrs added in v0.4.0

func AsInt64Ptrs(pointer unsafe.Pointer) []*int64

AsInt64Ptrs casts pointer to *int64 slice

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 AsInt8Ptr added in v0.4.0

func AsInt8Ptr(pointer unsafe.Pointer) *int8

AsInt8Ptr casts pointer to *int8

func AsInt8Ptrs added in v0.4.0

func AsInt8Ptrs(pointer unsafe.Pointer) []*int8

AsInt8Ptrs casts pointer to *int8 slice

func AsInt8s added in v0.4.0

func AsInt8s(pointer unsafe.Pointer) []int8

AsInt8s casts pointer to int8 slice

func AsIntPtr added in v0.4.0

func AsIntPtr(pointer unsafe.Pointer) *int

AsIntPtr casts pointer to *int

func AsIntPtrs added in v0.4.0

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

AsIntPtrs casts pointer to *int slice

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 AsMap added in v0.4.0

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

AsMap casts pointer to map[string]interface

func AsStringMap added in v0.4.0

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

AsStringMap casts pointer to map[string]interface

func AsTime added in v0.4.0

func AsTime(pointer unsafe.Pointer) time.Time

AsTime 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 AsTyped added in v0.4.0

func AsTyped(destType reflect.Type) func(pointer unsafe.Pointer) interface{}

AsTyped return function casting pointer to the specified type

func AsUInt added in v0.4.0

func AsUInt(pointer unsafe.Pointer) uint

AsUInt casts pointer to uint

func AsUInt16 added in v0.4.0

func AsUInt16(pointer unsafe.Pointer) uint16

AsUInt16 casts pointer to uint16

func AsUInt16Ptr added in v0.4.0

func AsUInt16Ptr(pointer unsafe.Pointer) *uint16

AsUInt16Ptr casts pointer to *uint

func AsUInt16Ptrs added in v0.4.0

func AsUInt16Ptrs(pointer unsafe.Pointer) []*uint16

AsUInt16Ptrs casts pointer to *uint slice

func AsUInt16s added in v0.4.0

func AsUInt16s(pointer unsafe.Pointer) []uint16

AsUInt16s casts pointer to uint16 slice

func AsUInt32 added in v0.4.0

func AsUInt32(pointer unsafe.Pointer) uint32

AsUInt32 casts pointer to uint32

func AsUInt32Ptr added in v0.4.0

func AsUInt32Ptr(pointer unsafe.Pointer) *uint32

AsUInt32Ptr casts pointer to *uint

func AsUInt32Ptrs added in v0.4.0

func AsUInt32Ptrs(pointer unsafe.Pointer) []*uint32

AsUInt32Ptrs casts pointer to *uint slice

func AsUInt32s added in v0.4.0

func AsUInt32s(pointer unsafe.Pointer) []uint32

AsUInt32s casts pointer to uint32 slice

func AsUInt64 added in v0.4.0

func AsUInt64(pointer unsafe.Pointer) uint64

AsUInt64 casts pointer to uint64

func AsUInt64Ptr added in v0.4.0

func AsUInt64Ptr(pointer unsafe.Pointer) *uint64

AsUInt64Ptr casts pointer to *uint

func AsUInt64Ptrs added in v0.4.0

func AsUInt64Ptrs(pointer unsafe.Pointer) []*uint64

AsUInt64Ptrs casts pointer to *uint slice

func AsUInt64s added in v0.4.0

func AsUInt64s(pointer unsafe.Pointer) []uint64

AsUInt64s casts pointer to uint64 slice

func AsUInt8 added in v0.4.0

func AsUInt8(pointer unsafe.Pointer) uint8

AsUInt8 casts pointer to uint8

func AsUInt8Ptr added in v0.4.0

func AsUInt8Ptr(pointer unsafe.Pointer) *uint8

AsUInt8Ptr casts pointer to *uint

func AsUInt8Ptrs added in v0.4.0

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

AsUInt8Ptrs casts pointer to *uint slice

func AsUInt8s added in v0.4.0

func AsUInt8s(pointer unsafe.Pointer) []uint8

AsUInt8s casts pointer to uint8 slice

func AsUIntPtr added in v0.4.0

func AsUIntPtr(pointer unsafe.Pointer) *uint

AsUIntPtr casts pointer to *uint

func AsUIntPtrs added in v0.4.0

func AsUIntPtrs(pointer unsafe.Pointer) []*uint

AsUIntPtrs casts pointer to *uint slice

func AsUInts added in v0.4.0

func AsUInts(pointer unsafe.Pointer) []uint

AsUInts casts pointer to uint slice

func DereferencePointer added in v0.4.0

func DereferencePointer(pointer unsafe.Pointer) unsafe.Pointer

DereferencePointer dereference pointer

func Register

func Register(aType reflect.Type, getter Getter)

Register register custom type with pointer getter

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() unsafe.Pointer

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

func (*Appender) Append added in v0.4.0

func (a *Appender) Append(items ...unsafe.Pointer)

Append appends items to a slice

type Field

type Field struct {
	Field *Field

	Value Getter

	Type reflect.Type
	// contains filtered or unexported fields
}

Field represent 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 FieldWithGetters

func FieldWithGetters(address, value Getter) *Field

FieldWithGetters creates a field supplied custom address, value getter

func NewField

func NewField(field reflect.StructField) *Field

NewField creates a new filed

func (*Field) Addr

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

Addr returns a field addr getter or error

func (*Field) AddrGetter

func (f *Field) AddrGetter() Getter

AddrGetter creates a Getter function returning filed pointer or error

func (*Field) Bool

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

Bool returns field bool

func (*Field) BoolAddr

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

BoolAddr returns field *bool addr

func (*Field) BoolPtr

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

BoolPtr returns field *bool

func (*Field) Bytes

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

Bytes returns field []byte

func (*Field) BytesAddr

func (f *Field) BytesAddr(structAddr unsafe.Pointer) *[]byte

BytesAddr returns field *[]byte addr

func (*Field) BytesPtr

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

BytesPtr returns field *[]byte

func (*Field) Float32

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

Float32 returns field float32

func (*Field) Float32Addr

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

Float32Addr returns field *float32 addr

func (*Field) Float32Ptr

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

Float32Ptr returns field *float32

func (*Field) Float64

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

Float64 returns field float64

func (*Field) Float64Addr

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

Float64Addr returns field *float64 addr

func (*Field) Float64Ptr

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

Float64Ptr returns field *float64

func (*Field) Int

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

Int returns field int

func (*Field) Int16

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

Int16 returns field int16

func (*Field) Int16Addr

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

Int16Addr returns field *int16 addr

func (*Field) Int16Ptr

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

Int16Ptr returns field *int16

func (*Field) Int32

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

Int32 returns field int32

func (*Field) Int32Addr

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

Int32Addr returns field *int32 addr

func (*Field) Int32Ptr

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

Int32Ptr returns field *int32

func (*Field) Int64

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

Int64 returns field int64

func (*Field) Int64Addr

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

Int64Addr returns field *int64 addr

func (*Field) Int64Ptr

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

Int64Ptr returns field *int64

func (*Field) Int8

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

Int8 returns field int8

func (*Field) Int8Addr

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

Int8Addr returns field *int8 addr

func (*Field) Int8Ptr

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

Int8Ptr returns field *int8

func (*Field) IntAddr

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

IntAddr returns field *int address

func (*Field) IntPtr

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

IntPtr returns field *int

func (*Field) Interface

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

Interface returns field address

func (*Field) InterfaceSlice added in v0.4.0

func (f *Field) InterfaceSlice(structAddr unsafe.Pointer) interface{}

InterfaceSlice returns interface slice

func (*Field) PtrSliceBool added in v0.4.0

func (f *Field) PtrSliceBool(structAddr unsafe.Pointer) *[]bool

PtrSliceBool returns bool slice pointer

func (*Field) PtrSliceFloat32 added in v0.4.0

func (f *Field) PtrSliceFloat32(structAddr unsafe.Pointer) *[]float32

PtrSliceFloat32 returns float32 slice pointer

func (*Field) PtrSliceFloat64 added in v0.4.0

func (f *Field) PtrSliceFloat64(structAddr unsafe.Pointer) *[]float64

PtrSliceFloat64 returns float64 slice pointer

func (*Field) PtrSliceInt16 added in v0.4.0

func (f *Field) PtrSliceInt16(structAddr unsafe.Pointer) *[]int16

PtrSliceInt16 returns int16 slice pointer

func (*Field) PtrSliceInt32 added in v0.4.0

func (f *Field) PtrSliceInt32(structAddr unsafe.Pointer) *[]int32

PtrSliceInt32 returns int32 slice pointer

func (*Field) PtrSliceInt64 added in v0.4.0

func (f *Field) PtrSliceInt64(structAddr unsafe.Pointer) *[]int64

PtrSliceInt64 returns int64 slice pointer

func (*Field) PtrSliceInt8 added in v0.4.0

func (f *Field) PtrSliceInt8(structAddr unsafe.Pointer) *[]int8

PtrSliceInt8 returns int8 slice pointer

func (*Field) PtrSliceString added in v0.4.0

func (f *Field) PtrSliceString(structAddr unsafe.Pointer) *[]string

PtrSliceString returns string slice pointer

func (*Field) PtrSliceUInt16 added in v0.4.0

func (f *Field) PtrSliceUInt16(structAddr unsafe.Pointer) *[]uint16

PtrSliceUInt16 returns uint16 slice pointer

func (*Field) PtrSliceUInt32 added in v0.4.0

func (f *Field) PtrSliceUInt32(structAddr unsafe.Pointer) *[]uint32

PtrSliceUInt32 returns uint32 slice pointer

func (*Field) PtrSliceUInt64 added in v0.4.0

func (f *Field) PtrSliceUInt64(structAddr unsafe.Pointer) *[]uint64

PtrSliceUInt64 returns uint64 slice pointer

func (*Field) PtrSliceUInt8 added in v0.4.0

func (f *Field) PtrSliceUInt8(structAddr unsafe.Pointer) *[]uint8

PtrSliceUInt8 returns uint8 slice pointer

func (*Field) PtrSliceUint added in v0.4.0

func (f *Field) PtrSliceUint(structAddr unsafe.Pointer) *[]uint

PtrSliceUint returns uint slice pointer

func (*Field) Set added in v0.4.0

func (f *Field) Set(structAddr unsafe.Pointer, v interface{})

Set sets field value

func (*Field) SetBool

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

SetBool sets field bool

func (*Field) SetBoolPtr

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

SetBoolPtr sets field *bool

func (*Field) SetBytes

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

SetBytes sets field []byte

func (*Field) SetBytesPtr

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

SetBytesPtr sets field *[]byte

func (*Field) SetFloat32

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

SetFloat32 sets field float32

func (*Field) SetFloat32Ptr

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

SetFloat32Ptr sets field *float32

func (*Field) SetFloat64

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

SetFloat64 sets field float64

func (*Field) SetFloat64Ptr

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

SetFloat64Ptr sets field *float64

func (*Field) SetInt

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

SetInt sets field int

func (*Field) SetInt16

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

SetInt16 sets field int

func (*Field) SetInt16Ptr

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

SetInt16Ptr sets field *int

func (*Field) SetInt32

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

SetInt32 sets field int

func (*Field) SetInt32Ptr

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

SetInt32Ptr sets field *int

func (*Field) SetInt64

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

SetInt64 sets field int

func (*Field) SetInt64Ptr

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

SetInt64Ptr sets field *int

func (*Field) SetInt8

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

SetInt8 sets field int

func (*Field) SetInt8Ptr

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

SetInt8Ptr sets field *int

func (*Field) SetIntPtr

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

SetIntPtr sets field *int

func (*Field) SetInterface

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

SetInterface set field interface{}

func (*Field) SetString

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

SetString sets field string

func (*Field) SetStringPtr

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

SetStringPtr sets field *string

func (*Field) SetTime

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

SetTime sets field time.Time

func (*Field) SetTimePtr

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

SetTimePtr sets field *time.Time

func (*Field) SetUint

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

SetUint sets field uint

func (*Field) SetUint16

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

SetUint16 sets field uint

func (*Field) SetUint16Ptr

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

SetUint16Ptr sets field *uint

func (*Field) SetUint32

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

SetUint32 sets field uint

func (*Field) SetUint32Ptr

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

SetUint32Ptr sets field *uint

func (*Field) SetUint64

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

SetUint64 sets field uint

func (*Field) SetUint64Ptr

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

SetUint64Ptr sets field *uint

func (*Field) SetUint8

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

SetUint8 sets field uint

func (*Field) SetUint8Ptr

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

SetUint8Ptr sets field *uint

func (*Field) SetUintPtr

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

SetUintPtr sets field *uint

func (*Field) SetValue

func (f *Field) SetValue(structAddr unsafe.Pointer, val interface{})

SetValue sets value

func (*Field) SliceBool added in v0.4.0

func (f *Field) SliceBool(structAddr unsafe.Pointer) []bool

SliceBool returns bool slice

func (*Field) SliceFloat32 added in v0.4.0

func (f *Field) SliceFloat32(structAddr unsafe.Pointer) []float32

SliceFloat32 returns float32 slice

func (*Field) SliceFloat64 added in v0.4.0

func (f *Field) SliceFloat64(structAddr unsafe.Pointer) []float64

SliceFloat64 returns float64 slice

func (*Field) SliceInt added in v0.4.0

func (f *Field) SliceInt(structAddr unsafe.Pointer) []int

SliceInt returns int slice

func (*Field) SliceInt16 added in v0.4.0

func (f *Field) SliceInt16(structAddr unsafe.Pointer) []int16

SliceInt16 returns int16 slice

func (*Field) SliceInt32 added in v0.4.0

func (f *Field) SliceInt32(structAddr unsafe.Pointer) []int32

SliceInt32 returns int32 slice

func (*Field) SliceInt64 added in v0.4.0

func (f *Field) SliceInt64(structAddr unsafe.Pointer) []int64

SliceInt64 returns int64 slice

func (*Field) SliceInt8 added in v0.4.0

func (f *Field) SliceInt8(structAddr unsafe.Pointer) []int8

SliceInt8 returns int8 slice

func (*Field) SliceString added in v0.4.0

func (f *Field) SliceString(structAddr unsafe.Pointer) []string

SliceString returns string slice

func (*Field) SliceUint added in v0.4.0

func (f *Field) SliceUint(structAddr unsafe.Pointer) []uint

SliceUint returns uint slice

func (*Field) SliceUint16 added in v0.4.0

func (f *Field) SliceUint16(structAddr unsafe.Pointer) []uint16

SliceUint16 returns uint16 slice

func (*Field) SliceUint32 added in v0.4.0

func (f *Field) SliceUint32(structAddr unsafe.Pointer) []uint32

SliceUint32 returns uint32 slice

func (*Field) SliceUint64 added in v0.4.0

func (f *Field) SliceUint64(structAddr unsafe.Pointer) []uint64

SliceUint64 returns uint64 slice

func (*Field) SliceUint8 added in v0.4.0

func (f *Field) SliceUint8(structAddr unsafe.Pointer) []uint8

SliceUint8 returns uint8 slice

func (*Field) String

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

String returns field string

func (*Field) StringAddr

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

StringAddr returns field *string addr

func (*Field) StringPtr

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

StringPtr returns field *string

func (*Field) Time

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

Time returns field time.Time

func (*Field) TimeAddr

func (f *Field) TimeAddr(structAddr unsafe.Pointer) *time.Time

TimeAddr returns field *time.Time addr

func (*Field) TimePtr

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

TimePtr returns field *time.Time

func (*Field) Uint

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

Uint returns field uint

func (*Field) Uint16

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

Uint16 returns field uint16

func (*Field) Uint16Addr

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

Uint16Addr returns field *uint16 addr

func (*Field) Uint16Ptr

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

Uint16Ptr returns field *uint16

func (*Field) Uint32

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

Uint32 returns field uint32

func (*Field) Uint32Addr

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

Uint32Addr returns field *uint32 addr

func (*Field) Uint32Ptr

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

Uint32Ptr returns field *uint32

func (*Field) Uint64

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

Uint64 returns field uint64

func (*Field) Uint64Addr

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

Uint64Addr returns field *uint64 addr

func (*Field) Uint64Ptr

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

Uint64Ptr returns field *uint64

func (*Field) Uint8

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

Uint8 returns field uint8

func (*Field) Uint8Addr

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

Uint8Addr returns field *uint8 addr

func (*Field) Uint8Ptr

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

Uint8Ptr returns field *uint8

func (*Field) UintAddr

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

UintAddr returns field *uint address

func (*Field) UintPtr

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

UintPtr returns field *uint

func (*Field) UnsafeAddr added in v0.2.0

func (f *Field) UnsafeAddr(structAddr unsafe.Pointer) unsafe.Pointer

UnsafeAddr returns unsafe address

type FieldPointer added in v0.4.0

type FieldPointer func(pointer unsafe.Pointer) unsafe.Pointer

FieldPointer represents a field pointer

type Getter

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

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

func FieldAccessor added in v0.4.0

func FieldAccessor(f *Field) Getter

type Index added in v0.4.0

type Index func(index uintptr) unsafe.Pointer

Index returns item pointer for supplied indexByAddr (see useItemAddr slice option)

type Selector added in v0.4.0

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

Selector represents nested abstraction selector

func NewSelector added in v0.4.0

func NewSelector(owner reflect.Type, expr string) (*Selector, error)

NewSelector creates a selector for supplied expression

func (*Selector) Bool added in v0.4.0

func (s *Selector) Bool(structAddr unsafe.Pointer) bool

Bool returns field bool value

func (*Selector) BoolAddr added in v0.4.0

func (s *Selector) BoolAddr(structAddr unsafe.Pointer) *bool

BoolAddr returns field *bool address

func (*Selector) Float64 added in v0.4.0

func (s *Selector) Float64(structAddr unsafe.Pointer) float64

Float64 returns field float64 value

func (*Selector) Float64Addr added in v0.4.0

func (s *Selector) Float64Addr(structAddr unsafe.Pointer) *float64

Float64Addr returns field *float64 address

func (*Selector) ISet added in v0.4.0

func (s *Selector) ISet(structAddr unsafe.Pointer, val interface{}, indexes []int)

ISet sets path value with optional slice indexes

func (*Selector) IValue added in v0.4.0

func (s *Selector) IValue(structAddr unsafe.Pointer, indexes []int) interface{}

IValue returns field value

func (*Selector) Int added in v0.4.0

func (s *Selector) Int(structAddr unsafe.Pointer) int

Int returns field int value

func (*Selector) IntAddr added in v0.4.0

func (s *Selector) IntAddr(structAddr unsafe.Pointer) *int

IntAddr returns field *int address

func (*Selector) Set added in v0.4.0

func (s *Selector) Set(structAddr unsafe.Pointer, val interface{})

Set sets path value

func (*Selector) SetBool added in v0.4.0

func (s *Selector) SetBool(structAddr unsafe.Pointer, val bool)

SetBool sets bool value

func (*Selector) SetFiled added in v0.4.0

func (s *Selector) SetFiled(field *Field)

SetFiled set selector field

func (*Selector) SetFloat64 added in v0.4.0

func (s *Selector) SetFloat64(structAddr unsafe.Pointer, val float64)

SetFloat64 sets int value

func (*Selector) SetInt added in v0.4.0

func (s *Selector) SetInt(structAddr unsafe.Pointer, val int)

SetInt sets int value

func (*Selector) SetString added in v0.4.0

func (s *Selector) SetString(structAddr unsafe.Pointer, val string)

SetString sets string value

func (*Selector) String added in v0.4.0

func (s *Selector) String(structAddr unsafe.Pointer) string

String returns field int value

func (*Selector) StringAddr added in v0.4.0

func (s *Selector) StringAddr(structAddr unsafe.Pointer) *string

StringAddr returns field *string addr

func (*Selector) Type added in v0.4.0

func (s *Selector) Type() reflect.Type

Type returns field type

func (*Selector) Value added in v0.4.0

func (s *Selector) Value(structAddr unsafe.Pointer) interface{}

Value returns field value

type Setter added in v0.4.0

type Setter func(structAddr unsafe.Pointer, val interface{})

Setter represents a func setting field value

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(aType reflect.Type, options ...interface{}) *Slice

NewSlice creates slice

func (*Slice) Appender added in v0.4.0

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

Appender returns a slice appender

func (*Slice) Index added in v0.2.0

func (s *Slice) Index(sliceAddr unsafe.Pointer) Index

Index return slice item

func (*Slice) IndexAddr added in v0.4.0

func (s *Slice) IndexAddr(sliceAddr unsafe.Pointer) Index

IndexAddr return slice item addr pointer

func (*Slice) Range added in v0.2.0

func (s *Slice) Range(sliceAddress unsafe.Pointer, visit func(index int, item unsafe.Pointer) 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

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

type ValuePointer added in v0.4.0

type ValuePointer func(interface{}) unsafe.Pointer

ValuePointer cast generic value ptr to unsafe pointer

func ValuePointerForType added in v0.4.0

func ValuePointerForType(t reflect.Type) ValuePointer

ValuePointerForType returns function casting interface to unsafe.Pointer for supplied type

Jump to

Keyboard shortcuts

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