forked from protocolbuffers/protobuf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.rs
734 lines (620 loc) · 20.9 KB
/
map.rs
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
// 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
use crate::{
AsMut, AsView, IntoMut, IntoProxied, IntoView, MutProxied, MutProxy, Proxied, Proxy, View,
ViewProxy,
__internal::{Private, SealedInternal},
__runtime::{InnerMap, InnerMapMut, RawMap, RawMapIter},
};
use std::marker::PhantomData;
#[repr(transparent)]
pub struct MapView<'msg, K: ?Sized, V: ?Sized> {
raw: RawMap,
_phantom: PhantomData<(&'msg K, &'msg V)>,
}
impl<'msg, K: ?Sized, V: ?Sized> Copy for MapView<'msg, K, V> {}
impl<'msg, K: ?Sized, V: ?Sized> Clone for MapView<'msg, K, V> {
fn clone(&self) -> Self {
*self
}
}
unsafe impl<'msg, K: ?Sized, V: ?Sized> Sync for MapView<'msg, K, V> {}
unsafe impl<'msg, K: ?Sized, V: ?Sized> Send for MapView<'msg, K, V> {}
impl<'msg, K: ?Sized, V: ?Sized> std::fmt::Debug for MapView<'msg, K, V> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("MapView")
.field(&std::any::type_name::<K>())
.field(&std::any::type_name::<V>())
.finish()
}
}
pub struct MapMut<'msg, K: ?Sized, V: ?Sized> {
pub(crate) inner: InnerMapMut<'msg>,
_phantom: PhantomData<(&'msg mut K, &'msg mut V)>,
}
impl<'msg, K: ?Sized, V: ?Sized> MapMut<'msg, K, V> {
pub fn inner(&self, _private: Private) -> InnerMapMut {
self.inner
}
}
unsafe impl<'msg, K: ?Sized, V: ?Sized> Sync for MapMut<'msg, K, V> {}
impl<'msg, K: ?Sized, V: ?Sized> std::fmt::Debug for MapMut<'msg, K, V> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("MapMut")
.field(&std::any::type_name::<K>())
.field(&std::any::type_name::<V>())
.finish()
}
}
pub struct Map<K: Proxied, V: ProxiedInMapValue<K>> {
inner: InnerMap,
_phantom: PhantomData<(PhantomData<K>, PhantomData<V>)>,
}
// SAFETY: `Map` is Sync because it does not implement interior mutability.
unsafe impl<K: Proxied, V: ProxiedInMapValue<K>> Sync for Map<K, V> {}
// SAFETY: `Map` is Send because it's not bound to a specific thread e.g.
// it does not use thread-local data or similar.
unsafe impl<K: Proxied, V: ProxiedInMapValue<K>> Send for Map<K, V> {}
impl<K: Proxied, V: ProxiedInMapValue<K>> SealedInternal for Map<K, V> {}
impl<K: Proxied, V: ProxiedInMapValue<K>> Drop for Map<K, V> {
fn drop(&mut self) {
// SAFETY:
// - `drop` is only called once.
// - 'map_free` is only called here.
unsafe { V::map_free(Private, self) }
}
}
pub trait ProxiedInMapValue<K>: Proxied
where
K: Proxied,
{
fn map_new(_private: Private) -> Map<K, Self>;
/// # Safety
/// - After `map_free`, no other methods on the input are safe to call.
unsafe fn map_free(_private: Private, map: &mut Map<K, Self>);
fn map_clear(map: MapMut<K, Self>);
fn map_len(map: MapView<K, Self>) -> usize;
fn map_insert(map: MapMut<K, Self>, key: View<'_, K>, value: impl IntoProxied<Self>) -> bool;
fn map_get<'a>(map: MapView<'a, K, Self>, key: View<'_, K>) -> Option<View<'a, Self>>;
fn map_remove(map: MapMut<K, Self>, key: View<'_, K>) -> bool;
fn map_iter(map: MapView<K, Self>) -> MapIter<K, Self>;
fn map_iter_next<'a>(iter: &mut MapIter<'a, K, Self>) -> Option<(View<'a, K>, View<'a, Self>)>;
}
impl<K: Proxied, V: ProxiedInMapValue<K>> Proxied for Map<K, V> {
type View<'msg> = MapView<'msg, K, V> where K: 'msg, V: 'msg;
}
impl<K: Proxied, V: ProxiedInMapValue<K>> AsView for Map<K, V> {
type Proxied = Self;
fn as_view(&self) -> MapView<'_, K, V> {
self.as_view()
}
}
impl<K: Proxied, V: ProxiedInMapValue<K>> MutProxied for Map<K, V> {
type Mut<'msg> = MapMut<'msg, K, V> where K: 'msg, V: 'msg;
}
impl<K: Proxied, V: ProxiedInMapValue<K>> AsMut for Map<K, V> {
type MutProxied = Self;
fn as_mut(&mut self) -> MapMut<'_, K, V> {
self.as_mut()
}
}
impl<'msg, K: Proxied, V: ProxiedInMapValue<K>> SealedInternal for MapView<'msg, K, V> {}
impl<'msg, K: Proxied, V: ProxiedInMapValue<K>> Proxy<'msg> for MapView<'msg, K, V> {}
impl<'msg, K: Proxied, V: ProxiedInMapValue<K>> AsView for MapView<'msg, K, V> {
type Proxied = Map<K, V>;
fn as_view(&self) -> MapView<'_, K, V> {
*self
}
}
impl<'msg, K: Proxied, V: ProxiedInMapValue<K>> IntoView<'msg> for MapView<'msg, K, V> {
fn into_view<'shorter>(self) -> MapView<'shorter, K, V>
where
'msg: 'shorter,
{
MapView { raw: self.raw, _phantom: PhantomData }
}
}
impl<'msg, K: Proxied, V: ProxiedInMapValue<K>> ViewProxy<'msg> for MapView<'msg, K, V> {}
impl<'msg, K: Proxied, V: ProxiedInMapValue<K>> SealedInternal for MapMut<'msg, K, V> {}
impl<'msg, K: Proxied, V: ProxiedInMapValue<K>> Proxy<'msg> for MapMut<'msg, K, V> {}
impl<'msg, K: Proxied, V: ProxiedInMapValue<K>> AsView for MapMut<'msg, K, V> {
type Proxied = Map<K, V>;
fn as_view(&self) -> MapView<'_, K, V> {
MapView { raw: self.inner.raw, _phantom: PhantomData }
}
}
impl<'msg, K: Proxied, V: ProxiedInMapValue<K>> IntoView<'msg> for MapMut<'msg, K, V> {
fn into_view<'shorter>(self) -> MapView<'shorter, K, V>
where
'msg: 'shorter,
{
MapView { raw: self.inner.raw, _phantom: PhantomData }
}
}
impl<'msg, K: Proxied, V: ProxiedInMapValue<K>> AsMut for MapMut<'msg, K, V> {
type MutProxied = Map<K, V>;
fn as_mut(&mut self) -> MapMut<'_, K, V> {
MapMut { inner: self.inner, _phantom: PhantomData }
}
}
impl<'msg, K: Proxied, V: ProxiedInMapValue<K>> IntoMut<'msg> for MapMut<'msg, K, V> {
fn into_mut<'shorter>(self) -> MapMut<'shorter, K, V>
where
'msg: 'shorter,
{
MapMut { inner: self.inner, _phantom: PhantomData }
}
}
impl<'msg, K: Proxied, V: ProxiedInMapValue<K>> MutProxy<'msg> for MapMut<'msg, K, V> {}
impl<K, V> Map<K, V>
where
K: Proxied,
V: ProxiedInMapValue<K>,
{
pub fn new() -> Self {
V::map_new(Private)
}
pub fn as_mut(&mut self) -> MapMut<'_, K, V> {
MapMut { inner: self.inner.as_mut(), _phantom: PhantomData }
}
pub fn as_view(&self) -> MapView<'_, K, V> {
MapView { raw: self.inner.raw, _phantom: PhantomData }
}
#[doc(hidden)]
pub fn from_inner(_private: Private, inner: InnerMap) -> Self {
Self { inner, _phantom: PhantomData }
}
pub fn as_raw(&self, _private: Private) -> RawMap {
self.inner.raw
}
}
impl<K, V> Default for Map<K, V>
where
K: Proxied,
V: ProxiedInMapValue<K>,
{
fn default() -> Self {
Map::new()
}
}
#[doc(hidden)]
impl<'msg, K: ?Sized, V: ?Sized> MapView<'msg, K, V> {
#[doc(hidden)]
pub fn as_raw(&self, _private: Private) -> RawMap {
self.raw
}
/// # Safety
/// - `raw` must be valid to read from for `'msg`.
#[doc(hidden)]
pub unsafe fn from_raw(_private: Private, raw: RawMap) -> Self {
Self { raw, _phantom: PhantomData }
}
}
impl<'msg, K, V> MapView<'msg, K, V>
where
K: Proxied + 'msg,
V: ProxiedInMapValue<K> + 'msg,
{
pub fn get<'a>(self, key: impl Into<View<'a, K>>) -> Option<View<'msg, V>>
where
K: 'a,
{
V::map_get(self, key.into())
}
pub fn len(self) -> usize {
V::map_len(self)
}
pub fn is_empty(self) -> bool {
self.len() == 0
}
/// Returns an iterator visiting all key-value pairs in arbitrary order.
///
/// The iterator element type is `(View<K>, View<V>)`.
/// This is an alias for `<Self as IntoIterator>::into_iter`.
pub fn iter(self) -> MapIter<'msg, K, V> {
self.into_iter()
}
/// Returns an iterator visiting all keys in arbitrary order.
///
/// The iterator element type is `View<K>`.
pub fn keys(self) -> impl Iterator<Item = View<'msg, K>> + 'msg {
self.into_iter().map(|(k, _)| k)
}
/// Returns an iterator visiting all values in arbitrary order.
///
/// The iterator element type is `View<V>`.
pub fn values(self) -> impl Iterator<Item = View<'msg, V>> + 'msg {
self.into_iter().map(|(_, v)| v)
}
}
#[doc(hidden)]
impl<'msg, K: ?Sized, V: ?Sized> MapMut<'msg, K, V> {
/// # Safety
/// - `inner` must be valid to read and write from for `'msg`.
#[doc(hidden)]
pub unsafe fn from_inner(_private: Private, inner: InnerMapMut<'msg>) -> Self {
Self { inner, _phantom: PhantomData }
}
#[doc(hidden)]
pub fn as_raw(&mut self, _private: Private) -> RawMap {
self.inner.raw
}
}
impl<'msg, K, V> MapMut<'msg, K, V>
where
K: Proxied + 'msg,
V: ProxiedInMapValue<K> + 'msg,
{
pub fn len(&self) -> usize {
self.as_view().len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
/// Adds a key-value pair to the map.
///
/// Returns `true` if the entry was newly inserted.
pub fn insert<'a>(&mut self, key: impl Into<View<'a, K>>, value: impl IntoProxied<V>) -> bool
where
K: 'a,
{
V::map_insert(self.as_mut(), key.into(), value)
}
pub fn remove<'a>(&mut self, key: impl Into<View<'a, K>>) -> bool
where
K: 'a,
{
V::map_remove(self.as_mut(), key.into())
}
pub fn clear(&mut self) {
V::map_clear(self.as_mut())
}
pub fn get<'a>(&self, key: impl Into<View<'a, K>>) -> Option<View<V>>
where
K: 'a,
{
V::map_get(self.as_view(), key.into())
}
pub fn copy_from<'a>(
&mut self,
src: impl IntoIterator<Item = (impl Into<View<'a, K>>, impl IntoProxied<V>)>,
) where
K: 'a,
{
//TODO
self.clear();
for (k, v) in src.into_iter() {
self.insert(k, v);
}
}
/// Returns an iterator visiting all key-value pairs in arbitrary order.
///
/// The iterator element type is `(View<K>, View<V>)`.
pub fn iter(&self) -> MapIter<K, V> {
self.into_iter()
}
/// Returns an iterator visiting all keys in arbitrary order.
///
/// The iterator element type is `View<K>`.
pub fn keys(&self) -> impl Iterator<Item = View<'_, K>> + '_ {
self.as_view().keys()
}
/// Returns an iterator visiting all values in arbitrary order.
///
/// The iterator element type is `View<V>`.
pub fn values(&self) -> impl Iterator<Item = View<'_, V>> + '_ {
self.as_view().values()
}
}
impl<'msg, K, V> IntoProxied<Map<K, V>> for MapView<'msg, K, V>
where
K: Proxied,
V: ProxiedInMapValue<K>,
View<'msg, V>: IntoProxied<V>,
{
fn into_proxied(self, _private: Private) -> Map<K, V> {
let mut m = Map::<K, V>::new();
m.as_mut().copy_from(self);
m
}
}
impl<'msg, K, V> IntoProxied<Map<K, V>> for MapMut<'msg, K, V>
where
K: Proxied,
V: ProxiedInMapValue<K>,
View<'msg, V>: IntoProxied<V>,
{
fn into_proxied(self, _private: Private) -> Map<K, V> {
self.into_view().into_proxied(Private)
}
}
impl<'msg, 'k, 'v, K, KView, V, VView, I> IntoProxied<Map<K, V>> for I
where
I: Iterator<Item = (KView, VView)>,
K: Proxied + 'msg + 'k,
V: ProxiedInMapValue<K> + 'msg + 'v,
KView: Into<View<'k, K>>,
VView: IntoProxied<V>,
{
fn into_proxied(self, _private: Private) -> Map<K, V> {
let mut m = Map::<K, V>::new();
m.as_mut().extend(self);
m
}
}
/// An iterator visiting all key-value pairs in arbitrary order.
///
/// The iterator element type is `(View<Key>, View<Value>)`.
pub struct MapIter<'msg, K: ?Sized, V: ?Sized> {
raw: RawMapIter,
_phantom: PhantomData<(&'msg K, &'msg V)>,
}
impl<'msg, K: ?Sized, V: ?Sized> MapIter<'msg, K, V> {
/// # Safety
/// - `raw` must be a valid instance of the raw iterator for `'msg`.
/// - The untyped `raw` iterator must be for a map of `K,V`.
/// - The backing map must be live and unmodified for `'msg`.
#[doc(hidden)]
pub unsafe fn from_raw(_private: Private, raw: RawMapIter) -> Self {
Self { raw, _phantom: PhantomData }
}
#[doc(hidden)]
pub fn as_raw_mut(&mut self, _private: Private) -> &mut RawMapIter {
&mut self.raw
}
}
impl<'msg, K, V> Iterator for MapIter<'msg, K, V>
where
K: Proxied + 'msg,
V: ProxiedInMapValue<K> + 'msg,
{
type Item = (View<'msg, K>, View<'msg, V>);
fn next(&mut self) -> Option<Self::Item> {
V::map_iter_next(self)
}
}
impl<'msg, K, V> IntoIterator for MapView<'msg, K, V>
where
K: Proxied + 'msg,
V: ProxiedInMapValue<K> + 'msg,
{
type IntoIter = MapIter<'msg, K, V>;
type Item = (View<'msg, K>, View<'msg, V>);
fn into_iter(self) -> MapIter<'msg, K, V> {
V::map_iter(self)
}
}
impl<'msg, K, V> IntoIterator for &'msg Map<K, V>
where
K: Proxied + 'msg,
V: ProxiedInMapValue<K> + 'msg,
{
type IntoIter = MapIter<'msg, K, V>;
type Item = (View<'msg, K>, View<'msg, V>);
fn into_iter(self) -> MapIter<'msg, K, V> {
self.as_view().into_iter()
}
}
impl<'a, 'msg, K, V> IntoIterator for &'a MapView<'msg, K, V>
where
'msg: 'a,
K: Proxied + 'msg,
V: ProxiedInMapValue<K> + 'msg,
{
type IntoIter = MapIter<'msg, K, V>;
type Item = (View<'msg, K>, View<'msg, V>);
fn into_iter(self) -> MapIter<'msg, K, V> {
(*self).into_iter()
}
}
impl<'a, 'msg, K, V> IntoIterator for &'a MapMut<'msg, K, V>
where
'msg: 'a,
K: Proxied + 'msg,
V: ProxiedInMapValue<K> + 'msg,
{
type IntoIter = MapIter<'a, K, V>;
// The View's are valid for 'a instead of 'msg.
// This is because the mutator may mutate past 'a but before 'msg expires.
type Item = (View<'a, K>, View<'a, V>);
fn into_iter(self) -> MapIter<'a, K, V> {
self.as_view().into_iter()
}
}
impl<'msg, 'k, 'v, KView, VView, K, V> Extend<(KView, VView)> for MapMut<'msg, K, V>
where
K: Proxied + 'msg + 'k,
V: ProxiedInMapValue<K> + 'msg + 'v,
KView: Into<View<'k, K>>,
VView: IntoProxied<V>,
{
fn extend<T: IntoIterator<Item = (KView, VView)>>(&mut self, iter: T) {
for (k, v) in iter.into_iter() {
self.insert(k, v);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{ProtoBytes, ProtoStr, ProtoString};
use googletest::prelude::*;
#[googletest::test]
fn test_proxied_scalar() {
let mut map: Map<i32, i64> = Map::new();
let mut map_mut = map.as_mut();
map_mut.insert(1, 2);
assert_that!(map_mut.get(1), eq(Some(2)));
let map_view_1 = map_mut.as_view();
assert_that!(map_view_1.len(), eq(1));
assert_that!(map_view_1.get(1), eq(Some(2)));
map_mut.insert(3, 4);
let map_view_2 = map_mut.into_view();
assert_that!(map_view_2.len(), eq(2));
assert_that!(map_view_2.get(3), eq(Some(4)));
{
let map_view_3 = map_view_2.as_view();
assert_that!(map_view_3.is_empty(), eq(false));
}
let map_view_4 = map_view_2.into_view();
assert_that!(map_view_4.is_empty(), eq(false));
}
#[googletest::test]
fn test_proxied_str() {
let mut map: Map<ProtoString, ProtoString> = Map::new();
let mut map_mut = map.as_mut();
map_mut.insert("a", "b");
let map_view_1 = map_mut.as_view();
assert_that!(map_view_1.len(), eq(1));
assert_that!(map_view_1.get("a").unwrap(), eq("b"));
map_mut.insert("c", "d");
let map_view_2 = map_mut.into_view();
assert_that!(map_view_2.len(), eq(2));
assert_that!(map_view_2.get("c").unwrap(), eq("d"));
{
let map_view_3 = map_view_2.as_view();
assert_that!(map_view_3.is_empty(), eq(false));
}
let map_view_4 = map_view_2.into_view();
assert_that!(map_view_4.is_empty(), eq(false));
}
#[googletest::test]
fn test_proxied_iter() {
let mut map: Map<i32, ProtoString> = Map::new();
let mut map_mut = map.as_mut();
map_mut.insert(15, "fizzbuzz");
map_mut.insert(5, "buzz");
map_mut.insert(3, "fizz");
// ProtoStr::from_str is necessary below because
// https://doc.rust-lang.org/std/primitive.tuple.html#impl-PartialEq-for-(T,)
// only compares where the types are the same, even when the tuple types can
// compare with each other.
// googletest-rust matchers also do not currently implement Clone.
assert_that!(
map.as_view().iter().collect::<Vec<_>>(),
unordered_elements_are![
eq(&(3, ProtoStr::from_str("fizz"))),
eq(&(5, ProtoStr::from_str("buzz"))),
eq(&(15, ProtoStr::from_str("fizzbuzz")))
]
);
assert_that!(
map.as_view(),
unordered_elements_are![
eq((3, ProtoStr::from_str("fizz"))),
eq((5, ProtoStr::from_str("buzz"))),
eq((15, ProtoStr::from_str("fizzbuzz")))
]
);
assert_that!(
map.as_mut().iter().collect::<Vec<_>>(),
unordered_elements_are![
eq(&(3, ProtoStr::from_str("fizz"))),
eq(&(5, ProtoStr::from_str("buzz"))),
eq(&(15, ProtoStr::from_str("fizzbuzz")))
]
);
assert_that!(
map.as_mut(),
unordered_elements_are![
eq((3, ProtoStr::from_str("fizz"))),
eq((5, ProtoStr::from_str("buzz"))),
eq((15, ProtoStr::from_str("fizzbuzz")))
]
);
}
#[googletest::test]
fn test_overwrite_insert() {
let mut map: Map<i32, ProtoString> = Map::new();
let mut map_mut = map.as_mut();
assert!(map_mut.insert(0, "fizz"));
// insert should return false when the key is already present
assert!(!map_mut.insert(0, "buzz"));
assert_that!(map.as_mut(), unordered_elements_are![eq((0, ProtoStr::from_str("buzz"))),]);
}
#[googletest::test]
fn test_extend() {
let mut map: Map<i32, ProtoString> = Map::new();
let mut map_mut = map.as_mut();
map_mut.extend([(0, ""); 0]);
assert_that!(map_mut.len(), eq(0));
map_mut.extend([(0, "fizz"), (1, "buzz"), (2, "fizzbuzz")]);
assert_that!(
map.as_view(),
unordered_elements_are![
eq((0, ProtoStr::from_str("fizz"))),
eq((1, ProtoStr::from_str("buzz"))),
eq((2, ProtoStr::from_str("fizzbuzz")))
]
);
let mut map_2: Map<i32, ProtoString> = Map::new();
let mut map_2_mut = map_2.as_mut();
map_2_mut.extend([(2, "bing"), (3, "bong")]);
let mut map_mut = map.as_mut();
map_mut.extend(&map_2);
assert_that!(
map.as_view(),
unordered_elements_are![
eq((0, ProtoStr::from_str("fizz"))),
eq((1, ProtoStr::from_str("buzz"))),
eq((2, ProtoStr::from_str("bing"))),
eq((3, ProtoStr::from_str("bong")))
]
);
}
#[googletest::test]
fn test_copy_from() {
let mut map: Map<i32, ProtoString> = Map::new();
let mut map_mut = map.as_mut();
map_mut.copy_from([(0, "fizz"), (1, "buzz"), (2, "fizzbuzz")]);
assert_that!(
map.as_view(),
unordered_elements_are![
eq((0, ProtoStr::from_str("fizz"))),
eq((1, ProtoStr::from_str("buzz"))),
eq((2, ProtoStr::from_str("fizzbuzz")))
]
);
let mut map_2: Map<i32, ProtoString> = Map::new();
let mut map_2_mut = map_2.as_mut();
map_2_mut.copy_from([(2, "bing"), (3, "bong")]);
let mut map_mut = map.as_mut();
map_mut.copy_from(&map_2);
assert_that!(
map.as_view(),
unordered_elements_are![
eq((2, ProtoStr::from_str("bing"))),
eq((3, ProtoStr::from_str("bong")))
]
);
}
#[googletest::test]
fn test_all_maps_can_be_constructed() {
macro_rules! gen_proto_values {
($key_t:ty, $($value_t:ty),*) => {
$(
let map = Map::<$key_t, $value_t>::new();
assert_that!(map.as_view().len(), eq(0));
)*
}
}
macro_rules! gen_proto_keys {
($($key_t:ty),*) => {
$(
gen_proto_values!($key_t, f32, f64, i32, u32, i64, bool, ProtoString, ProtoBytes);
)*
}
}
gen_proto_keys!(i32, u32, i64, u64, bool, ProtoString);
}
#[googletest::test]
fn test_dbg() {
let mut map = Map::<i32, f64>::new();
assert_that!(format!("{:?}", map.as_view()), eq("MapView(\"i32\", \"f64\")"));
assert_that!(format!("{:?}", map.as_mut()), eq("MapMut(\"i32\", \"f64\")"));
}
}