/proc/self/cwd/common/values/value_variant.cc
Line | Count | Source |
1 | | // Copyright 2025 Google LLC |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #include "common/values/value_variant.h" |
16 | | |
17 | | #include <cstddef> |
18 | | #include <cstring> |
19 | | #include <memory> |
20 | | #include <utility> |
21 | | |
22 | | #include "absl/base/optimization.h" |
23 | | #include "absl/log/absl_check.h" |
24 | | #include "common/values/bytes_value.h" |
25 | | #include "common/values/error_value.h" |
26 | | #include "common/values/string_value.h" |
27 | | #include "common/values/unknown_value.h" |
28 | | #include "common/values/values.h" |
29 | | |
30 | | namespace cel::common_internal { |
31 | | |
32 | 4.42M | void ValueVariant::SlowCopyConstruct(const ValueVariant& other) noexcept { |
33 | 4.42M | ABSL_DCHECK((flags_ & ValueFlags::kNonTrivial) == ValueFlags::kNonTrivial); |
34 | | |
35 | 4.42M | switch (index_) { |
36 | 107k | case ValueIndex::kBytes: |
37 | 107k | ::new (static_cast<void*>(&raw_[0])) BytesValue(*other.At<BytesValue>()); |
38 | 107k | break; |
39 | 20.0k | case ValueIndex::kString: |
40 | 20.0k | ::new (static_cast<void*>(&raw_[0])) |
41 | 20.0k | StringValue(*other.At<StringValue>()); |
42 | 20.0k | break; |
43 | 4.30M | case ValueIndex::kError: |
44 | 4.30M | ::new (static_cast<void*>(&raw_[0])) ErrorValue(*other.At<ErrorValue>()); |
45 | 4.30M | break; |
46 | 0 | case ValueIndex::kUnknown: |
47 | 0 | ::new (static_cast<void*>(&raw_[0])) |
48 | 0 | UnknownValue(*other.At<UnknownValue>()); |
49 | 0 | break; |
50 | 0 | default: |
51 | 0 | ABSL_UNREACHABLE(); |
52 | 4.42M | } |
53 | 4.42M | } |
54 | | |
55 | 11.0M | void ValueVariant::SlowMoveConstruct(ValueVariant& other) noexcept { |
56 | 11.0M | ABSL_DCHECK((flags_ & ValueFlags::kNonTrivial) == ValueFlags::kNonTrivial); |
57 | | |
58 | 11.0M | switch (index_) { |
59 | 333k | case ValueIndex::kBytes: |
60 | 333k | ::new (static_cast<void*>(&raw_[0])) |
61 | 333k | BytesValue(std::move(*other.At<BytesValue>())); |
62 | 333k | break; |
63 | 32.1k | case ValueIndex::kString: |
64 | 32.1k | ::new (static_cast<void*>(&raw_[0])) |
65 | 32.1k | StringValue(std::move(*other.At<StringValue>())); |
66 | 32.1k | break; |
67 | 10.6M | case ValueIndex::kError: |
68 | 10.6M | ::new (static_cast<void*>(&raw_[0])) |
69 | 10.6M | ErrorValue(std::move(*other.At<ErrorValue>())); |
70 | 10.6M | break; |
71 | 0 | case ValueIndex::kUnknown: |
72 | 0 | ::new (static_cast<void*>(&raw_[0])) |
73 | 0 | UnknownValue(std::move(*other.At<UnknownValue>())); |
74 | 0 | break; |
75 | 0 | default: |
76 | 0 | ABSL_UNREACHABLE(); |
77 | 11.0M | } |
78 | 11.0M | } |
79 | | |
80 | 20.7M | void ValueVariant::SlowDestruct() noexcept { |
81 | 20.7M | ABSL_DCHECK((flags_ & ValueFlags::kNonTrivial) == ValueFlags::kNonTrivial); |
82 | | |
83 | 20.7M | switch (index_) { |
84 | 515k | case ValueIndex::kBytes: |
85 | 515k | At<BytesValue>()->~BytesValue(); |
86 | 515k | break; |
87 | 61.2k | case ValueIndex::kString: |
88 | 61.2k | At<StringValue>()->~StringValue(); |
89 | 61.2k | break; |
90 | 20.1M | case ValueIndex::kError: |
91 | 20.1M | At<ErrorValue>()->~ErrorValue(); |
92 | 20.1M | break; |
93 | 0 | case ValueIndex::kUnknown: |
94 | 0 | At<UnknownValue>()->~UnknownValue(); |
95 | 0 | break; |
96 | 0 | default: |
97 | 0 | ABSL_UNREACHABLE(); |
98 | 20.7M | } |
99 | 20.7M | } |
100 | | |
101 | | void ValueVariant::SlowCopyAssign(const ValueVariant& other, bool trivial, |
102 | 855k | bool other_trivial) noexcept { |
103 | 855k | ABSL_DCHECK(!trivial || !other_trivial); |
104 | | |
105 | 855k | if (trivial) { |
106 | 855k | switch (other.index_) { |
107 | 10.5k | case ValueIndex::kBytes: |
108 | 10.5k | ::new (static_cast<void*>(&raw_[0])) |
109 | 10.5k | BytesValue(*other.At<BytesValue>()); |
110 | 10.5k | break; |
111 | 1.41k | case ValueIndex::kString: |
112 | 1.41k | ::new (static_cast<void*>(&raw_[0])) |
113 | 1.41k | StringValue(*other.At<StringValue>()); |
114 | 1.41k | break; |
115 | 843k | case ValueIndex::kError: |
116 | 843k | ::new (static_cast<void*>(&raw_[0])) |
117 | 843k | ErrorValue(*other.At<ErrorValue>()); |
118 | 843k | break; |
119 | 0 | case ValueIndex::kUnknown: |
120 | 0 | ::new (static_cast<void*>(&raw_[0])) |
121 | 0 | UnknownValue(*other.At<UnknownValue>()); |
122 | 0 | break; |
123 | 0 | default: |
124 | 0 | ABSL_UNREACHABLE(); |
125 | 855k | } |
126 | 855k | index_ = other.index_; |
127 | 855k | kind_ = other.kind_; |
128 | 855k | flags_ = other.flags_; |
129 | 855k | } else if (other_trivial) { |
130 | 0 | switch (index_) { |
131 | 0 | case ValueIndex::kBytes: |
132 | 0 | At<BytesValue>()->~BytesValue(); |
133 | 0 | break; |
134 | 0 | case ValueIndex::kString: |
135 | 0 | At<StringValue>()->~StringValue(); |
136 | 0 | break; |
137 | 0 | case ValueIndex::kError: |
138 | 0 | At<ErrorValue>()->~ErrorValue(); |
139 | 0 | break; |
140 | 0 | case ValueIndex::kUnknown: |
141 | 0 | At<UnknownValue>()->~UnknownValue(); |
142 | 0 | break; |
143 | 0 | default: |
144 | 0 | ABSL_UNREACHABLE(); |
145 | 0 | } |
146 | 0 | FastCopyAssign(other); |
147 | 0 | } else { |
148 | 0 | switch (index_) { |
149 | 0 | case ValueIndex::kBytes: |
150 | 0 | switch (other.index_) { |
151 | 0 | case ValueIndex::kBytes: |
152 | 0 | *At<BytesValue>() = *other.At<BytesValue>(); |
153 | 0 | break; |
154 | 0 | case ValueIndex::kString: |
155 | 0 | At<BytesValue>()->~BytesValue(); |
156 | 0 | ::new (static_cast<void*>(&raw_[0])) |
157 | 0 | StringValue(*other.At<StringValue>()); |
158 | 0 | index_ = other.index_; |
159 | 0 | kind_ = other.kind_; |
160 | 0 | break; |
161 | 0 | case ValueIndex::kError: |
162 | 0 | At<BytesValue>()->~BytesValue(); |
163 | 0 | ::new (static_cast<void*>(&raw_[0])) |
164 | 0 | ErrorValue(*other.At<ErrorValue>()); |
165 | 0 | index_ = other.index_; |
166 | 0 | kind_ = other.kind_; |
167 | 0 | break; |
168 | 0 | case ValueIndex::kUnknown: |
169 | 0 | At<BytesValue>()->~BytesValue(); |
170 | 0 | ::new (static_cast<void*>(&raw_[0])) |
171 | 0 | UnknownValue(*other.At<UnknownValue>()); |
172 | 0 | index_ = other.index_; |
173 | 0 | kind_ = other.kind_; |
174 | 0 | break; |
175 | 0 | default: |
176 | 0 | ABSL_UNREACHABLE(); |
177 | 0 | } |
178 | 0 | break; |
179 | 0 | case ValueIndex::kString: |
180 | 0 | switch (other.index_) { |
181 | 0 | case ValueIndex::kBytes: |
182 | 0 | At<StringValue>()->~StringValue(); |
183 | 0 | ::new (static_cast<void*>(&raw_[0])) |
184 | 0 | BytesValue(*other.At<BytesValue>()); |
185 | 0 | index_ = other.index_; |
186 | 0 | kind_ = other.kind_; |
187 | 0 | break; |
188 | 0 | case ValueIndex::kString: |
189 | 0 | *At<StringValue>() = *other.At<StringValue>(); |
190 | 0 | break; |
191 | 0 | case ValueIndex::kError: |
192 | 0 | At<StringValue>()->~StringValue(); |
193 | 0 | ::new (static_cast<void*>(&raw_[0])) |
194 | 0 | ErrorValue(*other.At<ErrorValue>()); |
195 | 0 | index_ = other.index_; |
196 | 0 | kind_ = other.kind_; |
197 | 0 | break; |
198 | 0 | case ValueIndex::kUnknown: |
199 | 0 | At<StringValue>()->~StringValue(); |
200 | 0 | ::new (static_cast<void*>(&raw_[0])) |
201 | 0 | UnknownValue(*other.At<UnknownValue>()); |
202 | 0 | index_ = other.index_; |
203 | 0 | kind_ = other.kind_; |
204 | 0 | break; |
205 | 0 | default: |
206 | 0 | ABSL_UNREACHABLE(); |
207 | 0 | } |
208 | 0 | break; |
209 | 0 | case ValueIndex::kError: |
210 | 0 | switch (other.index_) { |
211 | 0 | case ValueIndex::kBytes: |
212 | 0 | At<ErrorValue>()->~ErrorValue(); |
213 | 0 | ::new (static_cast<void*>(&raw_[0])) |
214 | 0 | BytesValue(*other.At<BytesValue>()); |
215 | 0 | index_ = other.index_; |
216 | 0 | kind_ = other.kind_; |
217 | 0 | break; |
218 | 0 | case ValueIndex::kString: |
219 | 0 | At<ErrorValue>()->~ErrorValue(); |
220 | 0 | ::new (static_cast<void*>(&raw_[0])) |
221 | 0 | StringValue(*other.At<StringValue>()); |
222 | 0 | index_ = other.index_; |
223 | 0 | kind_ = other.kind_; |
224 | 0 | break; |
225 | 0 | case ValueIndex::kError: |
226 | 0 | *At<ErrorValue>() = *other.At<ErrorValue>(); |
227 | 0 | break; |
228 | 0 | case ValueIndex::kUnknown: |
229 | 0 | At<ErrorValue>()->~ErrorValue(); |
230 | 0 | ::new (static_cast<void*>(&raw_[0])) |
231 | 0 | UnknownValue(*other.At<UnknownValue>()); |
232 | 0 | index_ = other.index_; |
233 | 0 | kind_ = other.kind_; |
234 | 0 | break; |
235 | 0 | default: |
236 | 0 | ABSL_UNREACHABLE(); |
237 | 0 | } |
238 | 0 | break; |
239 | 0 | case ValueIndex::kUnknown: |
240 | 0 | switch (other.index_) { |
241 | 0 | case ValueIndex::kBytes: |
242 | 0 | At<UnknownValue>()->~UnknownValue(); |
243 | 0 | ::new (static_cast<void*>(&raw_[0])) |
244 | 0 | BytesValue(*other.At<BytesValue>()); |
245 | 0 | index_ = other.index_; |
246 | 0 | kind_ = other.kind_; |
247 | 0 | break; |
248 | 0 | case ValueIndex::kString: |
249 | 0 | At<UnknownValue>()->~UnknownValue(); |
250 | 0 | ::new (static_cast<void*>(&raw_[0])) |
251 | 0 | StringValue(*other.At<StringValue>()); |
252 | 0 | index_ = other.index_; |
253 | 0 | kind_ = other.kind_; |
254 | 0 | break; |
255 | 0 | case ValueIndex::kError: |
256 | 0 | At<UnknownValue>()->~UnknownValue(); |
257 | 0 | ::new (static_cast<void*>(&raw_[0])) |
258 | 0 | ErrorValue(*other.At<ErrorValue>()); |
259 | 0 | index_ = other.index_; |
260 | 0 | kind_ = other.kind_; |
261 | 0 | break; |
262 | 0 | case ValueIndex::kUnknown: |
263 | 0 | At<UnknownValue>()->~UnknownValue(); |
264 | 0 | ::new (static_cast<void*>(&raw_[0])) |
265 | 0 | UnknownValue(*other.At<UnknownValue>()); |
266 | 0 | index_ = other.index_; |
267 | 0 | kind_ = other.kind_; |
268 | 0 | break; |
269 | 0 | default: |
270 | 0 | ABSL_UNREACHABLE(); |
271 | 0 | } |
272 | 0 | break; |
273 | 0 | default: |
274 | 0 | ABSL_UNREACHABLE(); |
275 | 0 | } |
276 | 0 | flags_ = other.flags_; |
277 | 0 | } |
278 | 855k | } |
279 | | |
280 | | void ValueVariant::SlowMoveAssign(ValueVariant& other, bool trivial, |
281 | 6.06M | bool other_trivial) noexcept { |
282 | 6.06M | ABSL_DCHECK(!trivial || !other_trivial); |
283 | | |
284 | 6.06M | if (trivial) { |
285 | 1.51M | switch (other.index_) { |
286 | 159 | case ValueIndex::kBytes: |
287 | 159 | ::new (static_cast<void*>(&raw_[0])) |
288 | 159 | BytesValue(std::move(*other.At<BytesValue>())); |
289 | 159 | break; |
290 | 2.35k | case ValueIndex::kString: |
291 | 2.35k | ::new (static_cast<void*>(&raw_[0])) |
292 | 2.35k | StringValue(std::move(*other.At<StringValue>())); |
293 | 2.35k | break; |
294 | 1.51M | case ValueIndex::kError: |
295 | 1.51M | ::new (static_cast<void*>(&raw_[0])) |
296 | 1.51M | ErrorValue(std::move(*other.At<ErrorValue>())); |
297 | 1.51M | break; |
298 | 0 | case ValueIndex::kUnknown: |
299 | 0 | ::new (static_cast<void*>(&raw_[0])) |
300 | 0 | UnknownValue(std::move(*other.At<UnknownValue>())); |
301 | 0 | break; |
302 | 0 | default: |
303 | 0 | ABSL_UNREACHABLE(); |
304 | 1.51M | } |
305 | 1.51M | index_ = other.index_; |
306 | 1.51M | kind_ = other.kind_; |
307 | 1.51M | flags_ = other.flags_; |
308 | 4.54M | } else if (other_trivial) { |
309 | 12.1k | switch (index_) { |
310 | 3.77k | case ValueIndex::kBytes: |
311 | 3.77k | At<BytesValue>()->~BytesValue(); |
312 | 3.77k | break; |
313 | 1.98k | case ValueIndex::kString: |
314 | 1.98k | At<StringValue>()->~StringValue(); |
315 | 1.98k | break; |
316 | 6.42k | case ValueIndex::kError: |
317 | 6.42k | At<ErrorValue>()->~ErrorValue(); |
318 | 6.42k | break; |
319 | 0 | case ValueIndex::kUnknown: |
320 | 0 | At<UnknownValue>()->~UnknownValue(); |
321 | 0 | break; |
322 | 0 | default: |
323 | 0 | ABSL_UNREACHABLE(); |
324 | 12.1k | } |
325 | 12.1k | FastMoveAssign(other); |
326 | 4.53M | } else { |
327 | 4.53M | switch (index_) { |
328 | 90.3k | case ValueIndex::kBytes: |
329 | 90.3k | switch (other.index_) { |
330 | 78.6k | case ValueIndex::kBytes: |
331 | 78.6k | *At<BytesValue>() = std::move(*other.At<BytesValue>()); |
332 | 78.6k | break; |
333 | 176 | case ValueIndex::kString: |
334 | 176 | At<BytesValue>()->~BytesValue(); |
335 | 176 | ::new (static_cast<void*>(&raw_[0])) |
336 | 176 | StringValue(std::move(*other.At<StringValue>())); |
337 | 176 | index_ = other.index_; |
338 | 176 | kind_ = other.kind_; |
339 | 176 | break; |
340 | 11.6k | case ValueIndex::kError: |
341 | 11.6k | At<BytesValue>()->~BytesValue(); |
342 | 11.6k | ::new (static_cast<void*>(&raw_[0])) |
343 | 11.6k | ErrorValue(std::move(*other.At<ErrorValue>())); |
344 | 11.6k | index_ = other.index_; |
345 | 11.6k | kind_ = other.kind_; |
346 | 11.6k | break; |
347 | 0 | case ValueIndex::kUnknown: |
348 | 0 | At<BytesValue>()->~BytesValue(); |
349 | 0 | ::new (static_cast<void*>(&raw_[0])) |
350 | 0 | UnknownValue(std::move(*other.At<UnknownValue>())); |
351 | 0 | index_ = other.index_; |
352 | 0 | kind_ = other.kind_; |
353 | 0 | break; |
354 | 0 | default: |
355 | 0 | ABSL_UNREACHABLE(); |
356 | 90.3k | } |
357 | 90.3k | break; |
358 | 90.3k | case ValueIndex::kString: |
359 | 13.0k | switch (other.index_) { |
360 | 0 | case ValueIndex::kBytes: |
361 | 0 | At<StringValue>()->~StringValue(); |
362 | 0 | ::new (static_cast<void*>(&raw_[0])) |
363 | 0 | BytesValue(std::move(*other.At<BytesValue>())); |
364 | 0 | index_ = other.index_; |
365 | 0 | kind_ = other.kind_; |
366 | 0 | break; |
367 | 418 | case ValueIndex::kString: |
368 | 418 | *At<StringValue>() = std::move(*other.At<StringValue>()); |
369 | 418 | break; |
370 | 12.6k | case ValueIndex::kError: |
371 | 12.6k | At<StringValue>()->~StringValue(); |
372 | 12.6k | ::new (static_cast<void*>(&raw_[0])) |
373 | 12.6k | ErrorValue(std::move(*other.At<ErrorValue>())); |
374 | 12.6k | index_ = other.index_; |
375 | 12.6k | kind_ = other.kind_; |
376 | 12.6k | break; |
377 | 0 | case ValueIndex::kUnknown: |
378 | 0 | At<StringValue>()->~StringValue(); |
379 | 0 | ::new (static_cast<void*>(&raw_[0])) |
380 | 0 | UnknownValue(std::move(*other.At<UnknownValue>())); |
381 | 0 | index_ = other.index_; |
382 | 0 | kind_ = other.kind_; |
383 | 0 | break; |
384 | 0 | default: |
385 | 0 | ABSL_UNREACHABLE(); |
386 | 13.0k | } |
387 | 13.0k | break; |
388 | 4.43M | case ValueIndex::kError: |
389 | 4.43M | switch (other.index_) { |
390 | 0 | case ValueIndex::kBytes: |
391 | 0 | At<ErrorValue>()->~ErrorValue(); |
392 | 0 | ::new (static_cast<void*>(&raw_[0])) |
393 | 0 | BytesValue(std::move(*other.At<BytesValue>())); |
394 | 0 | index_ = other.index_; |
395 | 0 | kind_ = other.kind_; |
396 | 0 | break; |
397 | 0 | case ValueIndex::kString: |
398 | 0 | At<ErrorValue>()->~ErrorValue(); |
399 | 0 | ::new (static_cast<void*>(&raw_[0])) |
400 | 0 | StringValue(std::move(*other.At<StringValue>())); |
401 | 0 | index_ = other.index_; |
402 | 0 | kind_ = other.kind_; |
403 | 0 | break; |
404 | 4.43M | case ValueIndex::kError: |
405 | 4.43M | *At<ErrorValue>() = std::move(*other.At<ErrorValue>()); |
406 | 4.43M | break; |
407 | 0 | case ValueIndex::kUnknown: |
408 | 0 | At<ErrorValue>()->~ErrorValue(); |
409 | 0 | ::new (static_cast<void*>(&raw_[0])) |
410 | 0 | UnknownValue(std::move(*other.At<UnknownValue>())); |
411 | 0 | index_ = other.index_; |
412 | 0 | kind_ = other.kind_; |
413 | 0 | break; |
414 | 0 | default: |
415 | 0 | ABSL_UNREACHABLE(); |
416 | 4.43M | } |
417 | 4.43M | break; |
418 | 4.43M | case ValueIndex::kUnknown: |
419 | 0 | switch (other.index_) { |
420 | 0 | case ValueIndex::kBytes: |
421 | 0 | At<UnknownValue>()->~UnknownValue(); |
422 | 0 | ::new (static_cast<void*>(&raw_[0])) |
423 | 0 | BytesValue(std::move(*other.At<BytesValue>())); |
424 | 0 | index_ = other.index_; |
425 | 0 | kind_ = other.kind_; |
426 | 0 | break; |
427 | 0 | case ValueIndex::kString: |
428 | 0 | At<UnknownValue>()->~UnknownValue(); |
429 | 0 | ::new (static_cast<void*>(&raw_[0])) |
430 | 0 | StringValue(std::move(*other.At<StringValue>())); |
431 | 0 | index_ = other.index_; |
432 | 0 | kind_ = other.kind_; |
433 | 0 | break; |
434 | 0 | case ValueIndex::kError: |
435 | 0 | At<UnknownValue>()->~UnknownValue(); |
436 | 0 | ::new (static_cast<void*>(&raw_[0])) |
437 | 0 | ErrorValue(std::move(*other.At<ErrorValue>())); |
438 | 0 | index_ = other.index_; |
439 | 0 | kind_ = other.kind_; |
440 | 0 | break; |
441 | 0 | case ValueIndex::kUnknown: |
442 | 0 | *At<UnknownValue>() = std::move(*other.At<UnknownValue>()); |
443 | 0 | break; |
444 | 0 | default: |
445 | 0 | ABSL_UNREACHABLE(); |
446 | 0 | } |
447 | 0 | break; |
448 | 0 | default: |
449 | 0 | ABSL_UNREACHABLE(); |
450 | 4.53M | } |
451 | 4.53M | flags_ = other.flags_; |
452 | 4.53M | } |
453 | 6.06M | } |
454 | | |
455 | | void ValueVariant::SlowSwap(ValueVariant& lhs, ValueVariant& rhs, |
456 | 51.9k | bool lhs_trivial, bool rhs_trivial) noexcept { |
457 | 51.9k | using std::swap; |
458 | 51.9k | ABSL_DCHECK(!lhs_trivial || !rhs_trivial); |
459 | | |
460 | 51.9k | if (lhs_trivial) { |
461 | 51.9k | alignas(ValueVariant) std::byte tmp[sizeof(ValueVariant)]; |
462 | | // This is acceptable. We know that both are trivially copyable at runtime. |
463 | | // NOLINTNEXTLINE(bugprone-undefined-memory-manipulation) |
464 | 51.9k | std::memcpy(tmp, std::addressof(lhs), sizeof(ValueVariant)); |
465 | 51.9k | switch (rhs.index_) { |
466 | 0 | case ValueIndex::kBytes: |
467 | 0 | ::new (static_cast<void*>(&lhs.raw_[0])) |
468 | 0 | BytesValue(*rhs.At<BytesValue>()); |
469 | 0 | rhs.At<BytesValue>()->~BytesValue(); |
470 | 0 | break; |
471 | 0 | case ValueIndex::kString: |
472 | 0 | ::new (static_cast<void*>(&lhs.raw_[0])) |
473 | 0 | StringValue(*rhs.At<StringValue>()); |
474 | 0 | rhs.At<StringValue>()->~StringValue(); |
475 | 0 | break; |
476 | 51.9k | case ValueIndex::kError: |
477 | 51.9k | ::new (static_cast<void*>(&lhs.raw_[0])) |
478 | 51.9k | ErrorValue(*rhs.At<ErrorValue>()); |
479 | 51.9k | rhs.At<ErrorValue>()->~ErrorValue(); |
480 | 51.9k | break; |
481 | 0 | case ValueIndex::kUnknown: |
482 | 0 | ::new (static_cast<void*>(&lhs.raw_[0])) |
483 | 0 | UnknownValue(*rhs.At<UnknownValue>()); |
484 | 0 | rhs.At<UnknownValue>()->~UnknownValue(); |
485 | 0 | break; |
486 | 0 | default: |
487 | 0 | ABSL_UNREACHABLE(); |
488 | 51.9k | } |
489 | 51.9k | lhs.index_ = rhs.index_; |
490 | 51.9k | lhs.kind_ = rhs.kind_; |
491 | 51.9k | lhs.flags_ = rhs.flags_; |
492 | | // This is acceptable. We know that both are trivially copyable at runtime. |
493 | | // NOLINTNEXTLINE(bugprone-undefined-memory-manipulation) |
494 | 51.9k | std::memcpy(std::addressof(rhs), tmp, sizeof(ValueVariant)); |
495 | 51.9k | } else if (rhs_trivial) { |
496 | 0 | alignas(ValueVariant) std::byte tmp[sizeof(ValueVariant)]; |
497 | | // This is acceptable. We know that both are trivially copyable at runtime. |
498 | | // NOLINTNEXTLINE(bugprone-undefined-memory-manipulation) |
499 | 0 | std::memcpy(tmp, std::addressof(rhs), sizeof(ValueVariant)); |
500 | 0 | switch (lhs.index_) { |
501 | 0 | case ValueIndex::kBytes: |
502 | 0 | ::new (static_cast<void*>(&rhs.raw_[0])) |
503 | 0 | BytesValue(*lhs.At<BytesValue>()); |
504 | 0 | lhs.At<BytesValue>()->~BytesValue(); |
505 | 0 | break; |
506 | 0 | case ValueIndex::kString: |
507 | 0 | ::new (static_cast<void*>(&rhs.raw_[0])) |
508 | 0 | StringValue(*lhs.At<StringValue>()); |
509 | 0 | lhs.At<StringValue>()->~StringValue(); |
510 | 0 | break; |
511 | 0 | case ValueIndex::kError: |
512 | 0 | ::new (static_cast<void*>(&rhs.raw_[0])) |
513 | 0 | ErrorValue(*lhs.At<ErrorValue>()); |
514 | 0 | lhs.At<ErrorValue>()->~ErrorValue(); |
515 | 0 | break; |
516 | 0 | case ValueIndex::kUnknown: |
517 | 0 | ::new (static_cast<void*>(&rhs.raw_[0])) |
518 | 0 | UnknownValue(*lhs.At<UnknownValue>()); |
519 | 0 | lhs.At<UnknownValue>()->~UnknownValue(); |
520 | 0 | break; |
521 | 0 | default: |
522 | 0 | ABSL_UNREACHABLE(); |
523 | 0 | } |
524 | 0 | rhs.index_ = lhs.index_; |
525 | 0 | rhs.kind_ = lhs.kind_; |
526 | 0 | rhs.flags_ = lhs.flags_; |
527 | | // This is acceptable. We know that both are trivially copyable at runtime. |
528 | | // NOLINTNEXTLINE(bugprone-undefined-memory-manipulation) |
529 | 0 | std::memcpy(std::addressof(lhs), tmp, sizeof(ValueVariant)); |
530 | 0 | } else { |
531 | 0 | ValueVariant tmp = std::move(lhs); |
532 | 0 | lhs = std::move(rhs); |
533 | 0 | rhs = std::move(tmp); |
534 | 0 | } |
535 | 51.9k | } |
536 | | |
537 | | } // namespace cel::common_internal |