1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2023 Google LLC. All rights reserved. 3 // 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file or at 6 // https://developers.google.com/open-source/licenses/bsd 7 8 use crate::{ 9 AsMut, AsView, IntoMut, IntoProxied, IntoView, MutProxied, MutProxy, Proxied, Proxy, View, 10 ViewProxy, 11 __internal::{Private, SealedInternal}, 12 __runtime::{InnerMap, InnerMapMut, RawMap, RawMapIter}, 13 }; 14 use std::marker::PhantomData; 15 16 #[repr(transparent)] 17 pub struct MapView<'msg, K: ?Sized, V: ?Sized> { 18 raw: RawMap, 19 _phantom: PhantomData<(&'msg K, &'msg V)>, 20 } 21 22 impl<'msg, K: ?Sized, V: ?Sized> Copy for MapView<'msg, K, V> {} 23 impl<'msg, K: ?Sized, V: ?Sized> Clone for MapView<'msg, K, V> { clone(&self) -> Self24 fn clone(&self) -> Self { 25 *self 26 } 27 } 28 29 unsafe impl<'msg, K: ?Sized, V: ?Sized> Sync for MapView<'msg, K, V> {} 30 unsafe impl<'msg, K: ?Sized, V: ?Sized> Send for MapView<'msg, K, V> {} 31 32 impl<'msg, K: ?Sized, V: ?Sized> std::fmt::Debug for MapView<'msg, K, V> { fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result33 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 34 f.debug_tuple("MapView") 35 .field(&std::any::type_name::<K>()) 36 .field(&std::any::type_name::<V>()) 37 .finish() 38 } 39 } 40 41 pub struct MapMut<'msg, K: ?Sized, V: ?Sized> { 42 pub(crate) inner: InnerMapMut<'msg>, 43 _phantom: PhantomData<(&'msg mut K, &'msg mut V)>, 44 } 45 46 impl<'msg, K: ?Sized, V: ?Sized> MapMut<'msg, K, V> { inner(&self, _private: Private) -> InnerMapMut47 pub fn inner(&self, _private: Private) -> InnerMapMut { 48 self.inner 49 } 50 } 51 52 unsafe impl<'msg, K: ?Sized, V: ?Sized> Sync for MapMut<'msg, K, V> {} 53 54 impl<'msg, K: ?Sized, V: ?Sized> std::fmt::Debug for MapMut<'msg, K, V> { fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result55 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 56 f.debug_tuple("MapMut") 57 .field(&std::any::type_name::<K>()) 58 .field(&std::any::type_name::<V>()) 59 .finish() 60 } 61 } 62 63 pub struct Map<K: Proxied, V: ProxiedInMapValue<K>> { 64 inner: InnerMap, 65 _phantom: PhantomData<(PhantomData<K>, PhantomData<V>)>, 66 } 67 68 // SAFETY: `Map` is Sync because it does not implement interior mutability. 69 unsafe impl<K: Proxied, V: ProxiedInMapValue<K>> Sync for Map<K, V> {} 70 71 // SAFETY: `Map` is Send because it's not bound to a specific thread e.g. 72 // it does not use thread-local data or similar. 73 unsafe impl<K: Proxied, V: ProxiedInMapValue<K>> Send for Map<K, V> {} 74 75 impl<K: Proxied, V: ProxiedInMapValue<K>> SealedInternal for Map<K, V> {} 76 77 impl<K: Proxied, V: ProxiedInMapValue<K>> Drop for Map<K, V> { drop(&mut self)78 fn drop(&mut self) { 79 // SAFETY: 80 // - `drop` is only called once. 81 // - 'map_free` is only called here. 82 unsafe { V::map_free(Private, self) } 83 } 84 } 85 86 pub trait ProxiedInMapValue<K>: Proxied 87 where 88 K: Proxied, 89 { map_new(_private: Private) -> Map<K, Self>90 fn map_new(_private: Private) -> Map<K, Self>; 91 92 /// # Safety 93 /// - After `map_free`, no other methods on the input are safe to call. map_free(_private: Private, map: &mut Map<K, Self>)94 unsafe fn map_free(_private: Private, map: &mut Map<K, Self>); 95 map_clear(map: MapMut<K, Self>)96 fn map_clear(map: MapMut<K, Self>); map_len(map: MapView<K, Self>) -> usize97 fn map_len(map: MapView<K, Self>) -> usize; map_insert(map: MapMut<K, Self>, key: View<'_, K>, value: impl IntoProxied<Self>) -> bool98 fn map_insert(map: MapMut<K, Self>, key: View<'_, K>, value: impl IntoProxied<Self>) -> bool; map_get<'a>(map: MapView<'a, K, Self>, key: View<'_, K>) -> Option<View<'a, Self>>99 fn map_get<'a>(map: MapView<'a, K, Self>, key: View<'_, K>) -> Option<View<'a, Self>>; map_remove(map: MapMut<K, Self>, key: View<'_, K>) -> bool100 fn map_remove(map: MapMut<K, Self>, key: View<'_, K>) -> bool; 101 map_iter(map: MapView<K, Self>) -> MapIter<K, Self>102 fn map_iter(map: MapView<K, Self>) -> MapIter<K, Self>; map_iter_next<'a>(iter: &mut MapIter<'a, K, Self>) -> Option<(View<'a, K>, View<'a, Self>)>103 fn map_iter_next<'a>(iter: &mut MapIter<'a, K, Self>) -> Option<(View<'a, K>, View<'a, Self>)>; 104 } 105 106 impl<K: Proxied, V: ProxiedInMapValue<K>> Proxied for Map<K, V> { 107 type View<'msg> = MapView<'msg, K, V> where K: 'msg, V: 'msg; 108 } 109 110 impl<K: Proxied, V: ProxiedInMapValue<K>> AsView for Map<K, V> { 111 type Proxied = Self; 112 as_view(&self) -> MapView<'_, K, V>113 fn as_view(&self) -> MapView<'_, K, V> { 114 self.as_view() 115 } 116 } 117 118 impl<K: Proxied, V: ProxiedInMapValue<K>> MutProxied for Map<K, V> { 119 type Mut<'msg> = MapMut<'msg, K, V> where K: 'msg, V: 'msg; 120 } 121 122 impl<K: Proxied, V: ProxiedInMapValue<K>> AsMut for Map<K, V> { 123 type MutProxied = Self; 124 as_mut(&mut self) -> MapMut<'_, K, V>125 fn as_mut(&mut self) -> MapMut<'_, K, V> { 126 self.as_mut() 127 } 128 } 129 130 impl<'msg, K: Proxied, V: ProxiedInMapValue<K>> SealedInternal for MapView<'msg, K, V> {} 131 132 impl<'msg, K: Proxied, V: ProxiedInMapValue<K>> Proxy<'msg> for MapView<'msg, K, V> {} 133 134 impl<'msg, K: Proxied, V: ProxiedInMapValue<K>> AsView for MapView<'msg, K, V> { 135 type Proxied = Map<K, V>; 136 as_view(&self) -> MapView<'_, K, V>137 fn as_view(&self) -> MapView<'_, K, V> { 138 *self 139 } 140 } 141 142 impl<'msg, K: Proxied, V: ProxiedInMapValue<K>> IntoView<'msg> for MapView<'msg, K, V> { into_view<'shorter>(self) -> MapView<'shorter, K, V> where 'msg: 'shorter,143 fn into_view<'shorter>(self) -> MapView<'shorter, K, V> 144 where 145 'msg: 'shorter, 146 { 147 MapView { raw: self.raw, _phantom: PhantomData } 148 } 149 } 150 151 impl<'msg, K: Proxied, V: ProxiedInMapValue<K>> ViewProxy<'msg> for MapView<'msg, K, V> {} 152 153 impl<'msg, K: Proxied, V: ProxiedInMapValue<K>> SealedInternal for MapMut<'msg, K, V> {} 154 155 impl<'msg, K: Proxied, V: ProxiedInMapValue<K>> Proxy<'msg> for MapMut<'msg, K, V> {} 156 157 impl<'msg, K: Proxied, V: ProxiedInMapValue<K>> AsView for MapMut<'msg, K, V> { 158 type Proxied = Map<K, V>; 159 as_view(&self) -> MapView<'_, K, V>160 fn as_view(&self) -> MapView<'_, K, V> { 161 MapView { raw: self.inner.raw, _phantom: PhantomData } 162 } 163 } 164 165 impl<'msg, K: Proxied, V: ProxiedInMapValue<K>> IntoView<'msg> for MapMut<'msg, K, V> { into_view<'shorter>(self) -> MapView<'shorter, K, V> where 'msg: 'shorter,166 fn into_view<'shorter>(self) -> MapView<'shorter, K, V> 167 where 168 'msg: 'shorter, 169 { 170 MapView { raw: self.inner.raw, _phantom: PhantomData } 171 } 172 } 173 174 impl<'msg, K: Proxied, V: ProxiedInMapValue<K>> AsMut for MapMut<'msg, K, V> { 175 type MutProxied = Map<K, V>; 176 as_mut(&mut self) -> MapMut<'_, K, V>177 fn as_mut(&mut self) -> MapMut<'_, K, V> { 178 MapMut { inner: self.inner, _phantom: PhantomData } 179 } 180 } 181 182 impl<'msg, K: Proxied, V: ProxiedInMapValue<K>> IntoMut<'msg> for MapMut<'msg, K, V> { into_mut<'shorter>(self) -> MapMut<'shorter, K, V> where 'msg: 'shorter,183 fn into_mut<'shorter>(self) -> MapMut<'shorter, K, V> 184 where 185 'msg: 'shorter, 186 { 187 MapMut { inner: self.inner, _phantom: PhantomData } 188 } 189 } 190 191 impl<'msg, K: Proxied, V: ProxiedInMapValue<K>> MutProxy<'msg> for MapMut<'msg, K, V> {} 192 193 impl<K, V> Map<K, V> 194 where 195 K: Proxied, 196 V: ProxiedInMapValue<K>, 197 { new() -> Self198 pub fn new() -> Self { 199 V::map_new(Private) 200 } 201 as_mut(&mut self) -> MapMut<'_, K, V>202 pub fn as_mut(&mut self) -> MapMut<'_, K, V> { 203 MapMut { inner: self.inner.as_mut(), _phantom: PhantomData } 204 } 205 as_view(&self) -> MapView<'_, K, V>206 pub fn as_view(&self) -> MapView<'_, K, V> { 207 MapView { raw: self.inner.raw, _phantom: PhantomData } 208 } 209 210 #[doc(hidden)] from_inner(_private: Private, inner: InnerMap) -> Self211 pub fn from_inner(_private: Private, inner: InnerMap) -> Self { 212 Self { inner, _phantom: PhantomData } 213 } 214 as_raw(&self, _private: Private) -> RawMap215 pub fn as_raw(&self, _private: Private) -> RawMap { 216 self.inner.raw 217 } 218 } 219 220 impl<K, V> Default for Map<K, V> 221 where 222 K: Proxied, 223 V: ProxiedInMapValue<K>, 224 { default() -> Self225 fn default() -> Self { 226 Map::new() 227 } 228 } 229 230 #[doc(hidden)] 231 impl<'msg, K: ?Sized, V: ?Sized> MapView<'msg, K, V> { 232 #[doc(hidden)] as_raw(&self, _private: Private) -> RawMap233 pub fn as_raw(&self, _private: Private) -> RawMap { 234 self.raw 235 } 236 237 /// # Safety 238 /// - `raw` must be valid to read from for `'msg`. 239 #[doc(hidden)] from_raw(_private: Private, raw: RawMap) -> Self240 pub unsafe fn from_raw(_private: Private, raw: RawMap) -> Self { 241 Self { raw, _phantom: PhantomData } 242 } 243 } 244 245 impl<'msg, K, V> MapView<'msg, K, V> 246 where 247 K: Proxied + 'msg, 248 V: ProxiedInMapValue<K> + 'msg, 249 { get<'a>(self, key: impl Into<View<'a, K>>) -> Option<View<'msg, V>> where K: 'a,250 pub fn get<'a>(self, key: impl Into<View<'a, K>>) -> Option<View<'msg, V>> 251 where 252 K: 'a, 253 { 254 V::map_get(self, key.into()) 255 } 256 len(self) -> usize257 pub fn len(self) -> usize { 258 V::map_len(self) 259 } 260 is_empty(self) -> bool261 pub fn is_empty(self) -> bool { 262 self.len() == 0 263 } 264 265 /// Returns an iterator visiting all key-value pairs in arbitrary order. 266 /// 267 /// The iterator element type is `(View<K>, View<V>)`. 268 /// This is an alias for `<Self as IntoIterator>::into_iter`. iter(self) -> MapIter<'msg, K, V>269 pub fn iter(self) -> MapIter<'msg, K, V> { 270 self.into_iter() 271 } 272 273 /// Returns an iterator visiting all keys in arbitrary order. 274 /// 275 /// The iterator element type is `View<K>`. keys(self) -> impl Iterator<Item = View<'msg, K>> + 'msg276 pub fn keys(self) -> impl Iterator<Item = View<'msg, K>> + 'msg { 277 self.into_iter().map(|(k, _)| k) 278 } 279 280 /// Returns an iterator visiting all values in arbitrary order. 281 /// 282 /// The iterator element type is `View<V>`. values(self) -> impl Iterator<Item = View<'msg, V>> + 'msg283 pub fn values(self) -> impl Iterator<Item = View<'msg, V>> + 'msg { 284 self.into_iter().map(|(_, v)| v) 285 } 286 } 287 288 #[doc(hidden)] 289 impl<'msg, K: ?Sized, V: ?Sized> MapMut<'msg, K, V> { 290 /// # Safety 291 /// - `inner` must be valid to read and write from for `'msg`. 292 #[doc(hidden)] from_inner(_private: Private, inner: InnerMapMut<'msg>) -> Self293 pub unsafe fn from_inner(_private: Private, inner: InnerMapMut<'msg>) -> Self { 294 Self { inner, _phantom: PhantomData } 295 } 296 297 #[doc(hidden)] as_raw(&mut self, _private: Private) -> RawMap298 pub fn as_raw(&mut self, _private: Private) -> RawMap { 299 self.inner.raw 300 } 301 } 302 303 impl<'msg, K, V> MapMut<'msg, K, V> 304 where 305 K: Proxied + 'msg, 306 V: ProxiedInMapValue<K> + 'msg, 307 { len(&self) -> usize308 pub fn len(&self) -> usize { 309 self.as_view().len() 310 } 311 is_empty(&self) -> bool312 pub fn is_empty(&self) -> bool { 313 self.len() == 0 314 } 315 316 /// Adds a key-value pair to the map. 317 /// 318 /// Returns `true` if the entry was newly inserted. insert<'a>(&mut self, key: impl Into<View<'a, K>>, value: impl IntoProxied<V>) -> bool where K: 'a,319 pub fn insert<'a>(&mut self, key: impl Into<View<'a, K>>, value: impl IntoProxied<V>) -> bool 320 where 321 K: 'a, 322 { 323 V::map_insert(self.as_mut(), key.into(), value) 324 } 325 remove<'a>(&mut self, key: impl Into<View<'a, K>>) -> bool where K: 'a,326 pub fn remove<'a>(&mut self, key: impl Into<View<'a, K>>) -> bool 327 where 328 K: 'a, 329 { 330 V::map_remove(self.as_mut(), key.into()) 331 } 332 clear(&mut self)333 pub fn clear(&mut self) { 334 V::map_clear(self.as_mut()) 335 } 336 get<'a>(&self, key: impl Into<View<'a, K>>) -> Option<View<V>> where K: 'a,337 pub fn get<'a>(&self, key: impl Into<View<'a, K>>) -> Option<View<V>> 338 where 339 K: 'a, 340 { 341 V::map_get(self.as_view(), key.into()) 342 } 343 copy_from<'a>( &mut self, src: impl IntoIterator<Item = (impl Into<View<'a, K>>, impl IntoProxied<V>)>, ) where K: 'a,344 pub fn copy_from<'a>( 345 &mut self, 346 src: impl IntoIterator<Item = (impl Into<View<'a, K>>, impl IntoProxied<V>)>, 347 ) where 348 K: 'a, 349 { 350 //TODO 351 self.clear(); 352 for (k, v) in src.into_iter() { 353 self.insert(k, v); 354 } 355 } 356 357 /// Returns an iterator visiting all key-value pairs in arbitrary order. 358 /// 359 /// The iterator element type is `(View<K>, View<V>)`. iter(&self) -> MapIter<K, V>360 pub fn iter(&self) -> MapIter<K, V> { 361 self.into_iter() 362 } 363 364 /// Returns an iterator visiting all keys in arbitrary order. 365 /// 366 /// The iterator element type is `View<K>`. keys(&self) -> impl Iterator<Item = View<'_, K>> + '_367 pub fn keys(&self) -> impl Iterator<Item = View<'_, K>> + '_ { 368 self.as_view().keys() 369 } 370 371 /// Returns an iterator visiting all values in arbitrary order. 372 /// 373 /// The iterator element type is `View<V>`. values(&self) -> impl Iterator<Item = View<'_, V>> + '_374 pub fn values(&self) -> impl Iterator<Item = View<'_, V>> + '_ { 375 self.as_view().values() 376 } 377 } 378 379 impl<'msg, K, V> IntoProxied<Map<K, V>> for MapView<'msg, K, V> 380 where 381 K: Proxied, 382 V: ProxiedInMapValue<K>, 383 View<'msg, V>: IntoProxied<V>, 384 { into_proxied(self, _private: Private) -> Map<K, V>385 fn into_proxied(self, _private: Private) -> Map<K, V> { 386 let mut m = Map::<K, V>::new(); 387 m.as_mut().copy_from(self); 388 m 389 } 390 } 391 392 impl<'msg, K, V> IntoProxied<Map<K, V>> for MapMut<'msg, K, V> 393 where 394 K: Proxied, 395 V: ProxiedInMapValue<K>, 396 View<'msg, V>: IntoProxied<V>, 397 { into_proxied(self, _private: Private) -> Map<K, V>398 fn into_proxied(self, _private: Private) -> Map<K, V> { 399 self.into_view().into_proxied(Private) 400 } 401 } 402 403 /// An iterator visiting all key-value pairs in arbitrary order. 404 /// 405 /// The iterator element type is `(View<Key>, View<Value>)`. 406 pub struct MapIter<'msg, K: ?Sized, V: ?Sized> { 407 raw: RawMapIter, 408 _phantom: PhantomData<(&'msg K, &'msg V)>, 409 } 410 411 impl<'msg, K: ?Sized, V: ?Sized> MapIter<'msg, K, V> { 412 /// # Safety 413 /// - `raw` must be a valid instance of the raw iterator for `'msg`. 414 /// - The untyped `raw` iterator must be for a map of `K,V`. 415 /// - The backing map must be live and unmodified for `'msg`. 416 #[doc(hidden)] from_raw(_private: Private, raw: RawMapIter) -> Self417 pub unsafe fn from_raw(_private: Private, raw: RawMapIter) -> Self { 418 Self { raw, _phantom: PhantomData } 419 } 420 421 #[doc(hidden)] as_raw_mut(&mut self, _private: Private) -> &mut RawMapIter422 pub fn as_raw_mut(&mut self, _private: Private) -> &mut RawMapIter { 423 &mut self.raw 424 } 425 } 426 427 impl<'msg, K, V> Iterator for MapIter<'msg, K, V> 428 where 429 K: Proxied + 'msg, 430 V: ProxiedInMapValue<K> + 'msg, 431 { 432 type Item = (View<'msg, K>, View<'msg, V>); 433 next(&mut self) -> Option<Self::Item>434 fn next(&mut self) -> Option<Self::Item> { 435 V::map_iter_next(self) 436 } 437 } 438 439 impl<'msg, K, V> IntoIterator for MapView<'msg, K, V> 440 where 441 K: Proxied + 'msg, 442 V: ProxiedInMapValue<K> + 'msg, 443 { 444 type IntoIter = MapIter<'msg, K, V>; 445 type Item = (View<'msg, K>, View<'msg, V>); 446 into_iter(self) -> MapIter<'msg, K, V>447 fn into_iter(self) -> MapIter<'msg, K, V> { 448 V::map_iter(self) 449 } 450 } 451 452 impl<'msg, K, V> IntoIterator for &'msg Map<K, V> 453 where 454 K: Proxied + 'msg, 455 V: ProxiedInMapValue<K> + 'msg, 456 { 457 type IntoIter = MapIter<'msg, K, V>; 458 type Item = (View<'msg, K>, View<'msg, V>); 459 into_iter(self) -> MapIter<'msg, K, V>460 fn into_iter(self) -> MapIter<'msg, K, V> { 461 self.as_view().into_iter() 462 } 463 } 464 465 impl<'a, 'msg, K, V> IntoIterator for &'a MapView<'msg, K, V> 466 where 467 'msg: 'a, 468 K: Proxied + 'msg, 469 V: ProxiedInMapValue<K> + 'msg, 470 { 471 type IntoIter = MapIter<'msg, K, V>; 472 type Item = (View<'msg, K>, View<'msg, V>); 473 into_iter(self) -> MapIter<'msg, K, V>474 fn into_iter(self) -> MapIter<'msg, K, V> { 475 (*self).into_iter() 476 } 477 } 478 479 impl<'a, 'msg, K, V> IntoIterator for &'a MapMut<'msg, K, V> 480 where 481 'msg: 'a, 482 K: Proxied + 'msg, 483 V: ProxiedInMapValue<K> + 'msg, 484 { 485 type IntoIter = MapIter<'a, K, V>; 486 // The View's are valid for 'a instead of 'msg. 487 // This is because the mutator may mutate past 'a but before 'msg expires. 488 type Item = (View<'a, K>, View<'a, V>); 489 into_iter(self) -> MapIter<'a, K, V>490 fn into_iter(self) -> MapIter<'a, K, V> { 491 self.as_view().into_iter() 492 } 493 } 494 495 impl<'msg, 'k, 'v, KView, VView, K, V> Extend<(KView, VView)> for MapMut<'msg, K, V> 496 where 497 K: Proxied + 'msg + 'k, 498 V: ProxiedInMapValue<K> + 'msg + 'v, 499 KView: Into<View<'k, K>>, 500 VView: IntoProxied<V>, 501 { extend<T: IntoIterator<Item = (KView, VView)>>(&mut self, iter: T)502 fn extend<T: IntoIterator<Item = (KView, VView)>>(&mut self, iter: T) { 503 for (k, v) in iter.into_iter() { 504 self.insert(k, v); 505 } 506 } 507 } 508 509 #[cfg(test)] 510 mod tests { 511 use super::*; 512 use crate::{ProtoBytes, ProtoStr, ProtoString}; 513 use googletest::prelude::*; 514 515 #[gtest] test_proxied_scalar()516 fn test_proxied_scalar() { 517 let mut map: Map<i32, i64> = Map::new(); 518 let mut map_mut = map.as_mut(); 519 map_mut.insert(1, 2); 520 assert_that!(map_mut.get(1), eq(Some(2))); 521 522 let map_view_1 = map_mut.as_view(); 523 assert_that!(map_view_1.len(), eq(1)); 524 assert_that!(map_view_1.get(1), eq(Some(2))); 525 526 map_mut.insert(3, 4); 527 528 let map_view_2 = map_mut.into_view(); 529 assert_that!(map_view_2.len(), eq(2)); 530 assert_that!(map_view_2.get(3), eq(Some(4))); 531 532 { 533 let map_view_3 = map_view_2.as_view(); 534 assert_that!(map_view_3.is_empty(), eq(false)); 535 } 536 537 let map_view_4 = map_view_2.into_view(); 538 assert_that!(map_view_4.is_empty(), eq(false)); 539 } 540 541 #[gtest] test_proxied_str()542 fn test_proxied_str() { 543 let mut map: Map<ProtoString, ProtoString> = Map::new(); 544 let mut map_mut = map.as_mut(); 545 map_mut.insert("a", "b"); 546 547 let map_view_1 = map_mut.as_view(); 548 assert_that!(map_view_1.len(), eq(1)); 549 assert_that!(map_view_1.get("a").unwrap(), eq("b")); 550 551 map_mut.insert("c", "d"); 552 553 let map_view_2 = map_mut.into_view(); 554 assert_that!(map_view_2.len(), eq(2)); 555 assert_that!(map_view_2.get("c").unwrap(), eq("d")); 556 557 { 558 let map_view_3 = map_view_2.as_view(); 559 assert_that!(map_view_3.is_empty(), eq(false)); 560 } 561 562 let map_view_4 = map_view_2.into_view(); 563 assert_that!(map_view_4.is_empty(), eq(false)); 564 } 565 566 #[gtest] test_proxied_iter()567 fn test_proxied_iter() { 568 let mut map: Map<i32, ProtoString> = Map::new(); 569 let mut map_mut = map.as_mut(); 570 map_mut.insert(15, "fizzbuzz"); 571 map_mut.insert(5, "buzz"); 572 map_mut.insert(3, "fizz"); 573 574 // ProtoStr::from_str is necessary below because 575 // https://doc.rust-lang.org/std/primitive.tuple.html#impl-PartialEq-for-(T,) 576 // only compares where the types are the same, even when the tuple types can 577 // compare with each other. 578 // googletest-rust matchers also do not currently implement Clone. 579 assert_that!( 580 map.as_view().iter().collect::<Vec<_>>(), 581 unordered_elements_are![ 582 eq(&(3, ProtoStr::from_str("fizz"))), 583 eq(&(5, ProtoStr::from_str("buzz"))), 584 eq(&(15, ProtoStr::from_str("fizzbuzz"))) 585 ] 586 ); 587 assert_that!( 588 map.as_view(), 589 unordered_elements_are![ 590 eq((3, ProtoStr::from_str("fizz"))), 591 eq((5, ProtoStr::from_str("buzz"))), 592 eq((15, ProtoStr::from_str("fizzbuzz"))) 593 ] 594 ); 595 assert_that!( 596 map.as_mut().iter().collect::<Vec<_>>(), 597 unordered_elements_are![ 598 eq(&(3, ProtoStr::from_str("fizz"))), 599 eq(&(5, ProtoStr::from_str("buzz"))), 600 eq(&(15, ProtoStr::from_str("fizzbuzz"))) 601 ] 602 ); 603 assert_that!( 604 map.as_mut(), 605 unordered_elements_are![ 606 eq((3, ProtoStr::from_str("fizz"))), 607 eq((5, ProtoStr::from_str("buzz"))), 608 eq((15, ProtoStr::from_str("fizzbuzz"))) 609 ] 610 ); 611 } 612 613 #[gtest] test_overwrite_insert()614 fn test_overwrite_insert() { 615 let mut map: Map<i32, ProtoString> = Map::new(); 616 let mut map_mut = map.as_mut(); 617 assert!(map_mut.insert(0, "fizz")); 618 // insert should return false when the key is already present 619 assert!(!map_mut.insert(0, "buzz")); 620 assert_that!(map.as_mut(), unordered_elements_are![eq((0, ProtoStr::from_str("buzz"))),]); 621 } 622 623 #[gtest] test_extend()624 fn test_extend() { 625 let mut map: Map<i32, ProtoString> = Map::new(); 626 let mut map_mut = map.as_mut(); 627 628 map_mut.extend([(0, ""); 0]); 629 assert_that!(map_mut.len(), eq(0)); 630 631 map_mut.extend([(0, "fizz"), (1, "buzz"), (2, "fizzbuzz")]); 632 633 assert_that!( 634 map.as_view(), 635 unordered_elements_are![ 636 eq((0, ProtoStr::from_str("fizz"))), 637 eq((1, ProtoStr::from_str("buzz"))), 638 eq((2, ProtoStr::from_str("fizzbuzz"))) 639 ] 640 ); 641 642 let mut map_2: Map<i32, ProtoString> = Map::new(); 643 let mut map_2_mut = map_2.as_mut(); 644 map_2_mut.extend([(2, "bing"), (3, "bong")]); 645 646 let mut map_mut = map.as_mut(); 647 map_mut.extend(&map_2); 648 649 assert_that!( 650 map.as_view(), 651 unordered_elements_are![ 652 eq((0, ProtoStr::from_str("fizz"))), 653 eq((1, ProtoStr::from_str("buzz"))), 654 eq((2, ProtoStr::from_str("bing"))), 655 eq((3, ProtoStr::from_str("bong"))) 656 ] 657 ); 658 } 659 660 #[gtest] test_copy_from()661 fn test_copy_from() { 662 let mut map: Map<i32, ProtoString> = Map::new(); 663 let mut map_mut = map.as_mut(); 664 map_mut.copy_from([(0, "fizz"), (1, "buzz"), (2, "fizzbuzz")]); 665 666 assert_that!( 667 map.as_view(), 668 unordered_elements_are![ 669 eq((0, ProtoStr::from_str("fizz"))), 670 eq((1, ProtoStr::from_str("buzz"))), 671 eq((2, ProtoStr::from_str("fizzbuzz"))) 672 ] 673 ); 674 675 let mut map_2: Map<i32, ProtoString> = Map::new(); 676 let mut map_2_mut = map_2.as_mut(); 677 map_2_mut.copy_from([(2, "bing"), (3, "bong")]); 678 679 let mut map_mut = map.as_mut(); 680 map_mut.copy_from(&map_2); 681 682 assert_that!( 683 map.as_view(), 684 unordered_elements_are![ 685 eq((2, ProtoStr::from_str("bing"))), 686 eq((3, ProtoStr::from_str("bong"))) 687 ] 688 ); 689 } 690 691 #[gtest] test_all_maps_can_be_constructed()692 fn test_all_maps_can_be_constructed() { 693 macro_rules! gen_proto_values { 694 ($key_t:ty, $($value_t:ty),*) => { 695 $( 696 let map = Map::<$key_t, $value_t>::new(); 697 assert_that!(map.as_view().len(), eq(0)); 698 )* 699 } 700 } 701 702 macro_rules! gen_proto_keys { 703 ($($key_t:ty),*) => { 704 $( 705 gen_proto_values!($key_t, f32, f64, i32, u32, i64, bool, ProtoString, ProtoBytes); 706 )* 707 } 708 } 709 710 gen_proto_keys!(i32, u32, i64, u64, bool, ProtoString); 711 } 712 713 #[gtest] test_dbg()714 fn test_dbg() { 715 let mut map = Map::<i32, f64>::new(); 716 assert_that!(format!("{:?}", map.as_view()), eq("MapView(\"i32\", \"f64\")")); 717 assert_that!(format!("{:?}", map.as_mut()), eq("MapMut(\"i32\", \"f64\")")); 718 } 719 } 720