...
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 stringHeader struct {
18 Data unsafe.Pointer
19 Len int
20 }
21 sliceHeader struct {
22 Data unsafe.Pointer
23 Len int
24 Cap int
25 }
26 ifaceHeader struct {
27 Type unsafe.Pointer
28 Data unsafe.Pointer
29 }
30 )
31
32 var (
33 nilType = typeOf(nil)
34 boolType = typeOf(*new(bool))
35 int32Type = typeOf(*new(int32))
36 int64Type = typeOf(*new(int64))
37 uint32Type = typeOf(*new(uint32))
38 uint64Type = typeOf(*new(uint64))
39 float32Type = typeOf(*new(float32))
40 float64Type = typeOf(*new(float64))
41 stringType = typeOf(*new(string))
42 bytesType = typeOf(*new([]byte))
43 enumType = typeOf(*new(EnumNumber))
44 )
45
46
47
48 func typeOf(t interface{}) unsafe.Pointer {
49 return (*ifaceHeader)(unsafe.Pointer(&t)).Type
50 }
51
52
53
54
55
56
57
58 type value struct {
59 pragma.DoNotCompare
60
61
62 typ unsafe.Pointer
63
64
65 ptr unsafe.Pointer
66
67
68
69
70
71
72 num uint64
73 }
74
75 func valueOfString(v string) Value {
76 p := (*stringHeader)(unsafe.Pointer(&v))
77 return Value{typ: stringType, ptr: p.Data, num: uint64(len(v))}
78 }
79 func valueOfBytes(v []byte) Value {
80 p := (*sliceHeader)(unsafe.Pointer(&v))
81 return Value{typ: bytesType, ptr: p.Data, num: uint64(len(v))}
82 }
83 func valueOfIface(v interface{}) Value {
84 p := (*ifaceHeader)(unsafe.Pointer(&v))
85 return Value{typ: p.Type, ptr: p.Data}
86 }
87
88 func (v Value) getString() (x string) {
89 *(*stringHeader)(unsafe.Pointer(&x)) = stringHeader{Data: v.ptr, Len: int(v.num)}
90 return x
91 }
92 func (v Value) getBytes() (x []byte) {
93 *(*sliceHeader)(unsafe.Pointer(&x)) = sliceHeader{Data: v.ptr, Len: int(v.num), Cap: int(v.num)}
94 return x
95 }
96 func (v Value) getIface() (x interface{}) {
97 *(*ifaceHeader)(unsafe.Pointer(&x)) = ifaceHeader{Type: v.typ, Data: v.ptr}
98 return x
99 }
100
View as plain text