-
Notifications
You must be signed in to change notification settings - Fork 73
/
unsafe_type.go
85 lines (70 loc) · 2.2 KB
/
unsafe_type.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
package reflect2
import (
"reflect"
"unsafe"
)
type unsafeType struct {
safeType
rtype unsafe.Pointer
ptrRType unsafe.Pointer
}
func newUnsafeType(cfg *frozenConfig, type1 reflect.Type) *unsafeType {
return &unsafeType{
safeType: safeType{
Type: type1,
cfg: cfg,
},
rtype: unpackEFace(type1).data,
ptrRType: unpackEFace(reflect.PtrTo(type1)).data,
}
}
func (type2 *unsafeType) Set(obj interface{}, val interface{}) {
objEFace := unpackEFace(obj)
assertType("Type.Set argument 1", type2.ptrRType, objEFace.rtype)
valEFace := unpackEFace(val)
assertType("Type.Set argument 2", type2.ptrRType, valEFace.rtype)
type2.UnsafeSet(objEFace.data, valEFace.data)
}
func (type2 *unsafeType) UnsafeSet(ptr unsafe.Pointer, val unsafe.Pointer) {
typedmemmove(type2.rtype, ptr, val)
}
func (type2 *unsafeType) IsNil(obj interface{}) bool {
objEFace := unpackEFace(obj)
assertType("Type.IsNil argument 1", type2.ptrRType, objEFace.rtype)
return type2.UnsafeIsNil(objEFace.data)
}
func (type2 *unsafeType) UnsafeIsNil(ptr unsafe.Pointer) bool {
return ptr == nil
}
func (type2 *unsafeType) UnsafeNew() unsafe.Pointer {
return unsafe_New(type2.rtype)
}
func (type2 *unsafeType) New() interface{} {
return packEFace(type2.ptrRType, type2.UnsafeNew())
}
func (type2 *unsafeType) PackEFace(ptr unsafe.Pointer) interface{} {
return packEFace(type2.ptrRType, ptr)
}
func (type2 *unsafeType) RType() uintptr {
return uintptr(type2.rtype)
}
func (type2 *unsafeType) Indirect(obj interface{}) interface{} {
objEFace := unpackEFace(obj)
assertType("Type.Indirect argument 1", type2.ptrRType, objEFace.rtype)
return type2.UnsafeIndirect(objEFace.data)
}
func (type2 *unsafeType) UnsafeIndirect(obj unsafe.Pointer) interface{} {
return packEFace(type2.rtype, obj)
}
func (type2 *unsafeType) LikePtr() bool {
return false
}
func assertType(where string, expectRType unsafe.Pointer, actualRType unsafe.Pointer) {
if expectRType != actualRType {
expectType := reflect.TypeOf(0)
(*iface)(unsafe.Pointer(&expectType)).data = expectRType
actualType := reflect.TypeOf(0)
(*iface)(unsafe.Pointer(&actualType)).data = actualRType
panic(where + ": expect " + expectType.String() + ", actual " + actualType.String())
}
}