-
Notifications
You must be signed in to change notification settings - Fork 5
/
copier.go
199 lines (157 loc) · 5.04 KB
/
copier.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package copy
import (
"reflect"
"unsafe"
"github.com/gotidy/copy/funcs"
)
type TypeInfo struct {
Type Type
Name string
}
func NewTypeInfo(typ reflect.Type) TypeInfo {
p := reflect.New(typ)
return TypeInfo{Type: TypeOf(p.Interface()), Name: p.String()}
}
func (t TypeInfo) Check(typ Type) bool {
return t.Type != typ
}
type BaseCopier struct {
*Copiers
dst TypeInfo
src TypeInfo
}
func NewBaseCopier(c *Copiers) BaseCopier {
return BaseCopier{Copiers: c}
}
func (b *BaseCopier) init(dst, src reflect.Type) {
b.dst = NewTypeInfo(dst)
b.src = NewTypeInfo(src)
}
func (b *BaseCopier) getCopierFunc(dst, src reflect.Type, dstOffset, srcOffset uintptr) copierFunc {
copierFunc := funcs.Get(dst, src)
if copierFunc != nil {
return func(dstPtr, srcPtr unsafe.Pointer) {
copierFunc(unsafe.Pointer(uintptr(dstPtr)+dstOffset), unsafe.Pointer(uintptr(srcPtr)+srcOffset))
}
}
// same type -> same type
if src == dst {
size := int(src.Size())
return func(dstPtr, srcPtr unsafe.Pointer) {
// More safe and independent from internal structs
// src := reflect.NewAt(src, unsafe.Pointer(uintptr(srcPtr)+src.Offset)).Elem()
// dst := reflect.NewAt(dst, unsafe.Pointer(uintptr(dstPtr)+dst.Offset)).Elem()
// dst.Set(src)
memcopy(unsafe.Pointer(uintptr(dstPtr)+dstOffset), unsafe.Pointer(uintptr(srcPtr)+srcOffset), size)
}
}
copier, err := b.get(dst, src)
if err == nil {
return func(dstPtr, srcPtr unsafe.Pointer) {
copier.copy(unsafe.Pointer(uintptr(dstPtr)+dstOffset), unsafe.Pointer(uintptr(srcPtr)+srcOffset))
}
}
return nil
}
type ValueToPValueCopier struct {
BaseCopier
structCopier func(dst, src unsafe.Pointer)
size int
}
func NewValueToPValueCopier(c *Copiers) *ValueToPValueCopier {
copier := &ValueToPValueCopier{BaseCopier: NewBaseCopier(c)}
return copier
}
func (c *ValueToPValueCopier) init(dst, src reflect.Type) {
c.BaseCopier.init(dst, src)
dst = dst.Elem() // *struct -> struct
c.structCopier = checkGet(c.get(dst, src)).copy // Get struct copier for struct -> struct
c.size = int(dst.Size())
}
func (c *ValueToPValueCopier) Copy(dst, src interface{}) {
dstType, dstPtr := DataOf(dst)
srcType, srcPtr := DataOf(src)
if c.src.Check(srcType) {
panic("source expected type " + c.src.Name + ", but has " + reflect.TypeOf(src).String())
}
if c.dst.Check(dstType) {
panic("destination expected type " + c.dst.Name + ", but has " + reflect.TypeOf(dst).String())
}
c.copy(dstPtr, srcPtr)
}
func (c *ValueToPValueCopier) copy(dst, src unsafe.Pointer) {
dstFieldPtr := (**struct{})(dst)
if *dstFieldPtr == nil {
*dstFieldPtr = (*struct{})(alloc(c.size))
}
c.structCopier(unsafe.Pointer(*dstFieldPtr), src)
}
type PValueToValueCopier struct {
BaseCopier
structCopier func(dst, src unsafe.Pointer)
}
func NewPValueToValueCopier(c *Copiers) *PValueToValueCopier {
copier := &PValueToValueCopier{BaseCopier: NewBaseCopier(c)}
return copier
}
func (c *PValueToValueCopier) init(dst, src reflect.Type) {
c.BaseCopier.init(dst, src)
src = src.Elem() // *struct -> struct
c.structCopier = checkGet(c.get(dst, src)).copy // Get struct copier for struct -> struct
}
func (c *PValueToValueCopier) Copy(dst, src interface{}) {
dstType, dstPtr := DataOf(dst)
srcType, srcPtr := DataOf(src)
if c.src.Check(srcType) {
panic("source expected type " + c.src.Name + ", but has " + reflect.TypeOf(src).String())
}
if c.dst.Check(dstType) {
panic("destination expected type " + c.dst.Name + ", but has " + reflect.TypeOf(dst).String())
}
c.copy(dstPtr, srcPtr)
}
func (c *PValueToValueCopier) copy(dst, src unsafe.Pointer) {
srcFieldPtr := (**struct{})(src)
if *srcFieldPtr == nil {
return
}
c.structCopier(dst, unsafe.Pointer(*srcFieldPtr))
}
type PValueToPValueCopier struct {
BaseCopier
structCopier func(dst, src unsafe.Pointer)
size int
}
func NewPValueToPValueCopier(c *Copiers) *PValueToPValueCopier {
copier := &PValueToPValueCopier{BaseCopier: NewBaseCopier(c)}
return copier
}
func (c *PValueToPValueCopier) init(dst, src reflect.Type) {
c.BaseCopier.init(dst, src)
dst = dst.Elem() // *struct -> struct
src = src.Elem() // *struct -> struct
c.structCopier = checkGet(c.get(dst, src)).copy // Get struct copier for struct -> struct
c.size = int(dst.Size())
}
func (c *PValueToPValueCopier) Copy(dst, src interface{}) {
dstType, dstPtr := DataOf(dst)
srcType, srcPtr := DataOf(src)
if c.src.Check(srcType) {
panic("source expected type " + c.src.Name + ", but has " + reflect.TypeOf(src).String())
}
if c.dst.Check(dstType) {
panic("destination expected type " + c.dst.Name + ", but has " + reflect.TypeOf(dst).String())
}
c.copy(dstPtr, srcPtr)
}
func (c *PValueToPValueCopier) copy(dst, src unsafe.Pointer) {
srcFieldPtr := (**struct{})(src)
if *srcFieldPtr == nil {
return
}
dstFieldPtr := (**struct{})(dst)
if *dstFieldPtr == nil {
*dstFieldPtr = (*struct{})(alloc(c.size))
}
c.structCopier(unsafe.Pointer(*dstFieldPtr), unsafe.Pointer(*srcFieldPtr))
}