...
1
2
3
4
5
6
7
8 package protoreflect
9
10 import (
11 "unsafe"
12
13 "google.golang.org/protobuf/internal/pragma"
14 )
15
16 type (
17 ifaceHeader struct {
18 _ [0]interface{}
19 Type unsafe.Pointer
20 Data unsafe.Pointer
21 }
22 )
23
24 var (
25 nilType = typeOf(nil)
26 boolType = typeOf(*new(bool))
27 int32Type = typeOf(*new(int32))
28 int64Type = typeOf(*new(int64))
29 uint32Type = typeOf(*new(uint32))
30 uint64Type = typeOf(*new(uint64))
31 float32Type = typeOf(*new(float32))
32 float64Type = typeOf(*new(float64))
33 stringType = typeOf(*new(string))
34 bytesType = typeOf(*new([]byte))
35 enumType = typeOf(*new(EnumNumber))
36 )
37
38
39
40 func typeOf(t interface{}) unsafe.Pointer {
41 return (*ifaceHeader)(unsafe.Pointer(&t)).Type
42 }
43
44
45
46
47
48
49
50 type value struct {
51 pragma.DoNotCompare
52
53
54 typ unsafe.Pointer
55
56
57 ptr unsafe.Pointer
58
59
60
61
62
63
64 num uint64
65 }
66
67 func valueOfString(v string) Value {
68 return Value{typ: stringType, ptr: unsafe.Pointer(unsafe.StringData(v)), num: uint64(len(v))}
69 }
70 func valueOfBytes(v []byte) Value {
71 return Value{typ: bytesType, ptr: unsafe.Pointer(unsafe.SliceData(v)), num: uint64(len(v))}
72 }
73 func valueOfIface(v interface{}) Value {
74 p := (*ifaceHeader)(unsafe.Pointer(&v))
75 return Value{typ: p.Type, ptr: p.Data}
76 }
77
78 func (v Value) getString() string {
79 return unsafe.String((*byte)(v.ptr), v.num)
80 }
81 func (v Value) getBytes() []byte {
82 return unsafe.Slice((*byte)(v.ptr), v.num)
83 }
84 func (v Value) getIface() (x interface{}) {
85 *(*ifaceHeader)(unsafe.Pointer(&x)) = ifaceHeader{Type: v.typ, Data: v.ptr}
86 return x
87 }
88
View as plain text