MethodSet returns the method set of type T. It is thread-safe. If cache is nil, this function is equivalent to types.NewMethodSet(T). Utility functions can thus expose an optional *MethodSetCache parameter to clients that care about performance.
(T Type)
| 26 | // parameter to clients that care about performance. |
| 27 | // |
| 28 | func (cache *MethodSetCache) MethodSet(T Type) *MethodSet { |
| 29 | if cache == nil { |
| 30 | return NewMethodSet(T) |
| 31 | } |
| 32 | cache.mu.Lock() |
| 33 | defer cache.mu.Unlock() |
| 34 | |
| 35 | switch T := T.(type) { |
| 36 | case *Named: |
| 37 | return cache.lookupNamed(T).value |
| 38 | |
| 39 | case *Pointer: |
| 40 | if N, ok := T.Elem().(*Named); ok { |
| 41 | return cache.lookupNamed(N).pointer |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | // all other types |
| 46 | // (The map uses pointer equivalence, not type identity.) |
| 47 | mset := cache.others[T] |
| 48 | if mset == nil { |
| 49 | mset = NewMethodSet(T) |
| 50 | if cache.others == nil { |
| 51 | cache.others = make(map[Type]*MethodSet) |
| 52 | } |
| 53 | cache.others[T] = mset |
| 54 | } |
| 55 | return mset |
| 56 | } |
| 57 | |
| 58 | func (cache *MethodSetCache) lookupNamed(named *Named) struct{ value, pointer *MethodSet } { |
| 59 | if cache.named == nil { |
no test coverage detected