Documentation
¶
Index ¶
Constants ¶
View Source
const DispatchVersion = 4
Variables ¶
View Source
var DefaultRegistry = NewRegistry()
View Source
var GenericDispatch = &DispatchTable{ Version: DispatchVersion, ChanRecver: func(c interface{}) RecvFunc { ch := reflect.ValueOf(c) if (ch.Type().ChanDir() & reflect.RecvDir) == 0 { panic("invalid chan direction") } return func(cancel <-chan struct{}, block bool) (interface{}, bool) { if !block { if val, ok := ch.TryRecv(); ok { return val.Interface(), true } return nil, false } if cancel == nil { if val, ok := ch.Recv(); ok { return val.Interface(), true } return nil, false } idx, val, ok := reflect.Select([]reflect.SelectCase{ { Dir: reflect.SelectRecv, Chan: ch, }, { Dir: reflect.SelectRecv, Chan: reflect.ValueOf(cancel), }, }) if idx == 0 && ok { return val.Interface(), true } return nil, false } }, ChanSender: func(c interface{}) SendFunc { ch := reflect.ValueOf(c) if (ch.Type().ChanDir() & reflect.SendDir) == 0 { panic("invalid chan direction") } nilElem := reflect.Zero(ch.Type().Elem()) return func(v interface{}, cancel <-chan struct{}, block bool) bool { el := nilElem if v != nil { el = reflect.ValueOf(v) } if !block { return ch.TrySend(el) } if cancel == nil { ch.Send(el) return true } idx, _, _ := reflect.Select([]reflect.SelectCase{ { Dir: reflect.SelectSend, Chan: ch, Send: el, }, { Dir: reflect.SelectRecv, Chan: reflect.ValueOf(cancel), }, }) return idx == 0 } }, FuncCaller: func(f interface{}) CallFunc { fn := reflect.ValueOf(f) t := fn.Type() if t.Kind() != reflect.Func { panic("invalid function") } nilArgs := make([]reflect.Value, t.NumIn()) for i := range nilArgs { nilArgs[i] = reflect.Zero(t.In(i)) } return func(in, out []interface{}) []interface{} { var buf [4]reflect.Value args := buf[:0] for i, v := range in { if v == nil { args = append(args, nilArgs[i]) } else { args = append(args, reflect.ValueOf(v)) } } ret := fn.Call(args) if len(ret) == 0 { return nil } if cap(out)-len(out) < len(ret) { out = make([]interface{}, 0, len(ret)) } for _, r := range ret { out = append(out, r.Interface()) } return out } }, }
Functions ¶
func MustRegisterDispatch ¶
func MustRegisterDispatch(t reflect.Type, i *DispatchTable)
func RegisterDispatch ¶
func RegisterDispatch(t reflect.Type, i *DispatchTable) error
Types ¶
type AnyMessage ¶
type AnyMessage interface{}
AnyMessage for bypassing type convert restrictions, can be connected to and connected by any node.
type CallFunc ¶
type CallFunc func(arg, buf []interface{}) []interface{}
func FuncCaller ¶
func FuncCaller(fn interface{}) CallFunc
type DispatchTable ¶
type RecvFunc ¶
func ChanRecver ¶
func ChanRecver(ch interface{}) RecvFunc
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
func NewRegistry ¶
func NewRegistry() *Registry
func (*Registry) GetDispatchTable ¶
func (r *Registry) GetDispatchTable(t reflect.Type) *DispatchTable
func (*Registry) RegisterDispatch ¶
func (r *Registry) RegisterDispatch(t reflect.Type, i *DispatchTable) error
Click to show internal directories.
Click to hide internal directories.