-
Notifications
You must be signed in to change notification settings - Fork 15.3k
/
repeated_field.cc
224 lines (213 loc) · 8.43 KB
/
repeated_field.cc
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#include <string>
#include "absl/strings/string_view.h"
#include "google/protobuf/compiler/cpp/helpers.h"
#include "google/protobuf/compiler/rust/accessors/accessor_case.h"
#include "google/protobuf/compiler/rust/accessors/accessor_generator.h"
#include "google/protobuf/compiler/rust/context.h"
#include "google/protobuf/compiler/rust/naming.h"
#include "google/protobuf/descriptor.h"
namespace google {
namespace protobuf {
namespace compiler {
namespace rust {
void RepeatedField::InMsgImpl(Context& ctx, const FieldDescriptor& field,
AccessorCase accessor_case) const {
std::string field_name = FieldNameWithCollisionAvoidance(field);
ctx.Emit(
{
{"field", RsSafeName(field_name)},
{"raw_field_name", field_name}, // Never r# prefixed
{"RsType", RsTypePath(ctx, field)},
{"view_lifetime", ViewLifetime(accessor_case)},
{"view_self", ViewReceiver(accessor_case)},
{"getter_thunk", ThunkName(ctx, field, "get")},
{"getter_mut_thunk", ThunkName(ctx, field, "get_mut")},
{"getter",
[&] {
if (ctx.is_upb()) {
ctx.Emit({}, R"rs(
pub fn $field$($view_self$) -> $pb$::RepeatedView<$view_lifetime$, $RsType$> {
unsafe {
$getter_thunk$(
self.raw_msg(),
/* optional size pointer */ std::ptr::null(),
) }
.map_or_else(
$pbr$::empty_array::<$RsType$>,
|raw| unsafe {
$pb$::RepeatedView::from_raw($pbi$::Private, raw)
}
)
}
)rs");
} else {
ctx.Emit({}, R"rs(
pub fn $field$($view_self$) -> $pb$::RepeatedView<$view_lifetime$, $RsType$> {
unsafe {
$pb$::RepeatedView::from_raw(
$pbi$::Private,
$getter_thunk$(self.raw_msg()),
)
}
}
)rs");
}
}},
{"clearer_thunk", ThunkName(ctx, field, "clear")},
{"getter_mut",
[&] {
if (accessor_case == AccessorCase::VIEW) {
return;
}
if (ctx.is_upb()) {
ctx.Emit({}, R"rs(
pub fn $field$_mut(&mut self) -> $pb$::RepeatedMut<'_, $RsType$> {
unsafe {
$pb$::RepeatedMut::from_inner(
$pbi$::Private,
$pbr$::InnerRepeatedMut::new(
$pbi$::Private,
$getter_mut_thunk$(
self.raw_msg(),
/* optional size pointer */ std::ptr::null(),
self.arena().raw(),
),
self.arena(),
),
)
}
}
)rs");
} else {
ctx.Emit({}, R"rs(
pub fn $field$_mut(&mut self) -> $pb$::RepeatedMut<'_, $RsType$> {
unsafe {
$pb$::RepeatedMut::from_inner(
$pbi$::Private,
$pbr$::InnerRepeatedMut::new(
$pbi$::Private,
$getter_mut_thunk$(self.raw_msg()),
),
)
}
}
)rs");
}
}},
{"setter",
[&] {
if (accessor_case == AccessorCase::VIEW) {
return;
}
ctx.Emit({}, R"rs(
pub fn set_$raw_field_name$(&mut self, src: $pb$::RepeatedView<'_, $RsType$>) {
// TODO: Implement IntoProxied and avoid copying.
self.$field$_mut().copy_from(src);
}
)rs");
}},
},
R"rs(
$getter$
$getter_mut$
$setter$
)rs");
}
void RepeatedField::InExternC(Context& ctx,
const FieldDescriptor& field) const {
ctx.Emit({{"getter_thunk", ThunkName(ctx, field, "get")},
{"getter_mut_thunk", ThunkName(ctx, field, "get_mut")},
{"getter",
[&] {
if (ctx.is_upb()) {
ctx.Emit(R"rs(
fn $getter_mut_thunk$(
raw_msg: $pbr$::RawMessage,
size: *const usize,
arena: $pbr$::RawArena,
) -> $pbr$::RawRepeatedField;
// Returns `None` when returned array pointer is NULL.
fn $getter_thunk$(
raw_msg: $pbr$::RawMessage,
size: *const usize,
) -> Option<$pbr$::RawRepeatedField>;
)rs");
} else {
ctx.Emit(R"rs(
fn $getter_mut_thunk$(raw_msg: $pbr$::RawMessage) -> $pbr$::RawRepeatedField;
fn $getter_thunk$(raw_msg: $pbr$::RawMessage) -> $pbr$::RawRepeatedField;
)rs");
}
}},
{"clearer_thunk", ThunkName(ctx, field, "clear")}},
R"rs(
fn $clearer_thunk$(raw_msg: $pbr$::RawMessage);
$getter$
)rs");
}
bool IsRepeatedPrimitive(const FieldDescriptor& field) {
return field.cpp_type() == FieldDescriptor::CPPTYPE_ENUM ||
field.cpp_type() == FieldDescriptor::CPPTYPE_BOOL ||
field.cpp_type() == FieldDescriptor::CPPTYPE_DOUBLE ||
field.cpp_type() == FieldDescriptor::CPPTYPE_FLOAT ||
field.cpp_type() == FieldDescriptor::CPPTYPE_INT32 ||
field.cpp_type() == FieldDescriptor::CPPTYPE_INT64 ||
field.cpp_type() == FieldDescriptor::CPPTYPE_UINT32 ||
field.cpp_type() == FieldDescriptor::CPPTYPE_UINT64;
}
bool IsRepeatedPtrPrimitive(const FieldDescriptor& field) {
return field.cpp_type() == FieldDescriptor::CPPTYPE_STRING;
}
std::string CppElementType(const FieldDescriptor& field) {
if (IsRepeatedPrimitive(field) || IsRepeatedPtrPrimitive(field)) {
return cpp::PrimitiveTypeName(field.cpp_type());
} else {
return cpp::QualifiedClassName(field.message_type());
}
}
const char* CppRepeatedContainerType(const FieldDescriptor& field) {
if (IsRepeatedPrimitive(field)) {
return "google::protobuf::RepeatedField";
} else {
return "google::protobuf::RepeatedPtrField";
}
}
void RepeatedField::InThunkCc(Context& ctx,
const FieldDescriptor& field) const {
ctx.Emit({{"field", cpp::FieldName(&field)},
{"ElementType", CppElementType(field)},
{"ContainerType", CppRepeatedContainerType(field)},
{"QualifiedMsg", cpp::QualifiedClassName(field.containing_type())},
{"clearer_thunk", ThunkName(ctx, field, "clear")},
{"getter_thunk", ThunkName(ctx, field, "get")},
{"getter_mut_thunk", ThunkName(ctx, field, "get_mut")},
{"repeated_copy_from_thunk",
ThunkName(ctx, field, "repeated_copy_from")},
{"impls",
[&] {
ctx.Emit(
R"cc(
void $clearer_thunk$($QualifiedMsg$* msg) {
msg->clear_$field$();
}
$ContainerType$<$ElementType$>* $getter_mut_thunk$($QualifiedMsg$* msg) {
return msg->mutable_$field$();
}
const $ContainerType$<$ElementType$>* $getter_thunk$(
const $QualifiedMsg$* msg) {
return &msg->$field$();
}
)cc");
}}},
"$impls$");
}
} // namespace rust
} // namespace compiler
} // namespace protobuf
} // namespace google