-
Notifications
You must be signed in to change notification settings - Fork 0
/
instance.go
56 lines (50 loc) · 1.53 KB
/
instance.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package call
import (
"fmt"
"reflect"
)
// Instance summarizes a type and its methods.
//
// Instance and each Method in Instance.Methods are bound to the same receiver; see
// the Rebind example for demonstration of rebinding the receiver.
type Instance struct {
Methods Methods
receiver interface{}
receiverType reflect.Type
receiverValue reflect.Value
}
// Copy creates a copy of the Instance object.
//
// Copy() followed by Rebind() will create a new *Instance that has a different receiver
// than the original.
//
// Further each method in Methods will have its *Func shallow copied to a new *Func instance.
// Mutating a Method's *Func in the copy does not affect the original.
func (m *Instance) Copy() *Instance {
cp := &Instance{
Methods: append([]Method(nil), m.Methods...),
receiver: m.receiver,
receiverType: m.receiverType,
receiverValue: m.receiverValue,
}
for k := range cp.Methods {
cp.Methods[k].instance = cp
//
// Each method gets a copy of the embedded *Func
f, fnew := cp.Methods[k].Func, &Func{}
*fnew = *f
cp.Methods[k].Func = fnew
}
return cp
}
// Rebind sets the receiver to the new value.
//
// If the incoming value does not have the same type as the original receiver then a panic will occur.
func (m *Instance) Rebind(in interface{}) {
v, t := reflect.ValueOf(in), reflect.TypeOf(in)
if t != m.receiverType {
panic(fmt.Sprintf("%T.Rebind expects same underlying type: original %T not compatible with incoming %T", m, m.receiver, in))
}
m.receiver = in
m.receiverValue = v
}