-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert.go
50 lines (41 loc) · 1.13 KB
/
convert.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
package unsafeheader
import (
"reflect"
"unsafe"
)
func StringToBytes(s string) []byte {
sh := (*StringHeader)(unsafe.Pointer(&s))
bh := &SliceHeader{
Data: sh.Data,
Len: sh.Len,
Cap: sh.Len,
}
return *(*[]byte)(unsafe.Pointer(bh))
}
func BytesToString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
// ToEface casts an empty interface{} value to an Eface value.
func ToEface(ep *any) Eface {
return *(*Eface)(unsafe.Pointer(ep))
}
// ToIface casts a [reflect.Type] to an Iface value.
func ToIface(t reflect.Type) Iface {
return *(*Iface)(unsafe.Pointer(&t))
}
// ToRType gets the underlying [*reflect.rtype] from a [reflect.Type].
func ToRType(t reflect.Type) unsafe.Pointer {
return (*Iface)(unsafe.Pointer(&t)).Data
}
// ToReflectType convert an [*reflect.rtype] pointer to a [reflect.Type] value.
// It is the reverse operation of ToRType.
func ToReflectType(rtype unsafe.Pointer) reflect.Type {
t := reflectTypeTmpl
t.Data = rtype
return *(*reflect.Type)(unsafe.Pointer(&t))
}
var reflectTypeTmpl Iface
func init() {
sampleTyp := reflect.TypeOf(0)
reflectTypeTmpl = *(*Iface)(unsafe.Pointer(&sampleTyp))
}