-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcopy.go
More file actions
46 lines (40 loc) · 793 Bytes
/
copy.go
File metadata and controls
46 lines (40 loc) · 793 Bytes
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
package deecpy
import (
"reflect"
"unsafe"
"v8.run/go/deecpy/unsafeops"
)
// Copy deeply copies the given object.
func Copy[T any](dst, src *T) error {
sAny := any(src)
typID := unsafeops.TypeID(&sAny)
// Lookup the type in the cache
inst, ok := getOps(typID)
if !ok {
var err error
inst, err = build(reflect.TypeOf(src).Elem())
if err != nil {
return err
}
setOps(typID, inst)
}
var pmapArr [64]ptrmap
pmap := pmapArr[:0]
exec(
unsafe.Pointer(dst),
unsafe.Pointer(src),
inst,
false,
unsafeops.NoEscape(&pmap),
)
return nil
}
// Duplicate returns a copy of the given object.
func Duplicate[T any](src T) (T, error) {
var dst T
err := Copy(unsafeops.NoEscape(&dst), unsafeops.NoEscape(&src))
if err != nil {
return dst, err
}
return dst, nil
}