zerovec/map/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
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
use super::*;
use crate::ule::{AsULE, EncodeAsVarULE, VarULE};
use crate::{VarZeroVec, ZeroSlice, ZeroVec, ZeroVecError};
use alloc::borrow::Borrow;
use alloc::boxed::Box;
use core::cmp::Ordering;
use core::fmt;
use core::iter::FromIterator;
/// A zero-copy map datastructure, built on sorted binary-searchable [`ZeroVec`]
/// and [`VarZeroVec`].
///
/// This type, like [`ZeroVec`] and [`VarZeroVec`], is able to zero-copy
/// deserialize from appropriately formatted byte buffers. It is internally copy-on-write, so it can be mutated
/// afterwards as necessary.
///
/// Internally, a `ZeroMap` is a zero-copy vector for keys paired with a zero-copy vector for
/// values, sorted by the keys. Therefore, all types used in `ZeroMap` need to work with either
/// [`ZeroVec`] or [`VarZeroVec`].
///
/// This does mean that for fixed-size data, one must use the regular type (`u32`, `u8`, `char`, etc),
/// whereas for variable-size data, `ZeroMap` will use the dynamically sized version (`str` not `String`,
/// `ZeroSlice` not `ZeroVec`, `FooULE` not `Foo` for custom types)
///
/// # Examples
///
/// ```
/// use zerovec::ZeroMap;
///
/// #[derive(serde::Serialize, serde::Deserialize)]
/// struct Data<'a> {
/// #[serde(borrow)]
/// map: ZeroMap<'a, u32, str>,
/// }
///
/// let mut map = ZeroMap::new();
/// map.insert(&1, "one");
/// map.insert(&2, "two");
/// map.insert(&4, "four");
///
/// let data = Data { map };
///
/// let bincode_bytes =
/// bincode::serialize(&data).expect("Serialization should be successful");
///
/// // Will deserialize without any allocations
/// let deserialized: Data = bincode::deserialize(&bincode_bytes)
/// .expect("Deserialization should be successful");
///
/// assert_eq!(data.map.get(&1), Some("one"));
/// assert_eq!(data.map.get(&2), Some("two"));
/// ```
///
/// [`VarZeroVec`]: crate::VarZeroVec
// ZeroMap has only one invariant: keys.len() == values.len()
// It is also expected that the keys are sorted, but this is not an invariant. See #1433
pub struct ZeroMap<'a, K, V>
where
K: ZeroMapKV<'a> + ?Sized,
V: ZeroMapKV<'a> + ?Sized,
{
pub(crate) keys: K::Container,
pub(crate) values: V::Container,
}
impl<'a, K, V> Default for ZeroMap<'a, K, V>
where
K: ZeroMapKV<'a> + ?Sized,
V: ZeroMapKV<'a> + ?Sized,
{
fn default() -> Self {
Self::new()
}
}
impl<'a, K, V> ZeroMap<'a, K, V>
where
K: ZeroMapKV<'a> + ?Sized,
V: ZeroMapKV<'a> + ?Sized,
{
/// Creates a new, empty `ZeroMap<K, V>`.
///
/// # Examples
///
/// ```
/// use zerovec::ZeroMap;
///
/// let zm: ZeroMap<u16, str> = ZeroMap::new();
/// assert!(zm.is_empty());
/// ```
pub fn new() -> Self {
Self {
keys: K::Container::zvl_with_capacity(0),
values: V::Container::zvl_with_capacity(0),
}
}
#[doc(hidden)] // databake internal
pub const unsafe fn from_parts_unchecked(keys: K::Container, values: V::Container) -> Self {
Self { keys, values }
}
/// Construct a new [`ZeroMap`] with a given capacity
pub fn with_capacity(capacity: usize) -> Self {
Self {
keys: K::Container::zvl_with_capacity(capacity),
values: V::Container::zvl_with_capacity(capacity),
}
}
/// Obtain a borrowed version of this map
pub fn as_borrowed(&'a self) -> ZeroMapBorrowed<'a, K, V> {
ZeroMapBorrowed {
keys: self.keys.zvl_as_borrowed(),
values: self.values.zvl_as_borrowed(),
}
}
/// The number of elements in the [`ZeroMap`]
pub fn len(&self) -> usize {
self.values.zvl_len()
}
/// Whether the [`ZeroMap`] is empty
pub fn is_empty(&self) -> bool {
self.values.zvl_len() == 0
}
/// Remove all elements from the [`ZeroMap`]
pub fn clear(&mut self) {
self.keys.zvl_clear();
self.values.zvl_clear();
}
/// Reserve capacity for `additional` more elements to be inserted into
/// the [`ZeroMap`] to avoid frequent reallocations.
///
/// See [`Vec::reserve()`](alloc::vec::Vec::reserve) for more information.
pub fn reserve(&mut self, additional: usize) {
self.keys.zvl_reserve(additional);
self.values.zvl_reserve(additional);
}
}
impl<'a, K, V> ZeroMap<'a, K, V>
where
K: ZeroMapKV<'a> + ?Sized + Ord,
V: ZeroMapKV<'a> + ?Sized,
{
/// Get the value associated with `key`, if it exists.
///
/// For fixed-size ([`AsULE`]) `V` types, this _will_ return
/// their corresponding [`AsULE::ULE`] type. If you wish to work with the `V`
/// type directly, [`Self::get_copied()`] exists for convenience.
///
/// ```rust
/// use zerovec::ZeroMap;
///
/// let mut map = ZeroMap::new();
/// map.insert(&1, "one");
/// map.insert(&2, "two");
/// assert_eq!(map.get(&1), Some("one"));
/// assert_eq!(map.get(&3), None);
/// ```
pub fn get(&self, key: &K) -> Option<&V::GetType> {
let index = self.keys.zvl_binary_search(key).ok()?;
self.values.zvl_get(index)
}
/// Binary search the map with `predicate` to find a key, returning the value.
///
/// ```rust
/// use zerovec::ZeroMap;
///
/// let mut map = ZeroMap::new();
/// map.insert(&1, "one");
/// map.insert(&2, "two");
/// assert_eq!(map.get_by(|probe| probe.cmp(&1)), Some("one"));
/// assert_eq!(map.get_by(|probe| probe.cmp(&3)), None);
/// ```
pub fn get_by(&self, predicate: impl FnMut(&K) -> Ordering) -> Option<&V::GetType> {
let index = self.keys.zvl_binary_search_by(predicate).ok()?;
self.values.zvl_get(index)
}
/// Returns whether `key` is contained in this map
///
/// ```rust
/// use zerovec::ZeroMap;
///
/// let mut map = ZeroMap::new();
/// map.insert(&1, "one");
/// map.insert(&2, "two");
/// assert!(map.contains_key(&1));
/// assert!(!map.contains_key(&3));
/// ```
pub fn contains_key(&self, key: &K) -> bool {
self.keys.zvl_binary_search(key).is_ok()
}
/// Insert `value` with `key`, returning the existing value if it exists.
///
/// ```rust
/// use zerovec::ZeroMap;
///
/// let mut map = ZeroMap::new();
/// map.insert(&1, "one");
/// map.insert(&2, "two");
/// assert_eq!(map.get(&1), Some("one"));
/// assert_eq!(map.get(&3), None);
/// ```
pub fn insert(&mut self, key: &K, value: &V) -> Option<V::OwnedType> {
match self.keys.zvl_binary_search(key) {
Ok(index) => Some(self.values.zvl_replace(index, value)),
Err(index) => {
self.keys.zvl_insert(index, key);
self.values.zvl_insert(index, value);
None
}
}
}
/// Remove the value at `key`, returning it if it exists.
///
/// ```rust
/// use zerovec::ZeroMap;
///
/// let mut map = ZeroMap::new();
/// map.insert(&1, "one");
/// map.insert(&2, "two");
/// assert_eq!(map.remove(&1), Some("one".to_owned().into_boxed_str()));
/// assert_eq!(map.get(&1), None);
/// ```
pub fn remove(&mut self, key: &K) -> Option<V::OwnedType> {
let idx = self.keys.zvl_binary_search(key).ok()?;
self.keys.zvl_remove(idx);
Some(self.values.zvl_remove(idx))
}
/// Appends `value` with `key` to the end of the underlying vector, returning
/// `key` and `value` _if it failed_. Useful for extending with an existing
/// sorted list.
/// ```rust
/// use zerovec::ZeroMap;
///
/// let mut map = ZeroMap::new();
/// assert!(map.try_append(&1, "uno").is_none());
/// assert!(map.try_append(&3, "tres").is_none());
///
/// let unsuccessful = map.try_append(&3, "tres-updated");
/// assert!(unsuccessful.is_some(), "append duplicate of last key");
///
/// let unsuccessful = map.try_append(&2, "dos");
/// assert!(unsuccessful.is_some(), "append out of order");
///
/// assert_eq!(map.get(&1), Some("uno"));
///
/// // contains the original value for the key: 3
/// assert_eq!(map.get(&3), Some("tres"));
///
/// // not appended since it wasn't in order
/// assert_eq!(map.get(&2), None);
/// ```
#[must_use]
pub fn try_append<'b>(&mut self, key: &'b K, value: &'b V) -> Option<(&'b K, &'b V)> {
if self.keys.zvl_len() != 0 {
if let Some(last) = self.keys.zvl_get(self.keys.zvl_len() - 1) {
if K::Container::t_cmp_get(key, last) != Ordering::Greater {
return Some((key, value));
}
}
}
self.keys.zvl_push(key);
self.values.zvl_push(value);
None
}
}
impl<'a, K, V> ZeroMap<'a, K, V>
where
K: ZeroMapKV<'a> + ?Sized,
V: ZeroMapKV<'a> + ?Sized,
{
/// Produce an ordered iterator over key-value pairs
pub fn iter<'b>(
&'b self,
) -> impl ExactSizeIterator<
Item = (
&'b <K as ZeroMapKV<'a>>::GetType,
&'b <V as ZeroMapKV<'a>>::GetType,
),
> {
(0..self.keys.zvl_len()).map(move |idx| {
(
#[allow(clippy::unwrap_used)] // idx is in-range
self.keys.zvl_get(idx).unwrap(),
#[allow(clippy::unwrap_used)] // idx is in-range
self.values.zvl_get(idx).unwrap(),
)
})
}
/// Produce an ordered iterator over keys
pub fn iter_keys<'b>(
&'b self,
) -> impl ExactSizeIterator<Item = &'b <K as ZeroMapKV<'a>>::GetType> {
#[allow(clippy::unwrap_used)] // idx is in-range
(0..self.keys.zvl_len()).map(move |idx| self.keys.zvl_get(idx).unwrap())
}
/// Produce an iterator over values, ordered by keys
pub fn iter_values<'b>(
&'b self,
) -> impl ExactSizeIterator<Item = &'b <V as ZeroMapKV<'a>>::GetType> {
#[allow(clippy::unwrap_used)] // idx is in-range
(0..self.values.zvl_len()).map(move |idx| self.values.zvl_get(idx).unwrap())
}
}
impl<'a, K, V> ZeroMap<'a, K, V>
where
K: ZeroMapKV<'a, Container = ZeroVec<'a, K>> + ?Sized,
V: ZeroMapKV<'a> + ?Sized,
K: AsULE,
{
/// Cast a `ZeroMap<K, V>` to `ZeroMap<P, V>` where `K` and `P` are [`AsULE`] types
/// with the same representation.
///
/// # Unchecked Invariants
///
/// If `K` and `P` have different ordering semantics, unexpected behavior may occur.
pub fn cast_zv_k_unchecked<P>(self) -> ZeroMap<'a, P, V>
where
P: AsULE<ULE = K::ULE> + ZeroMapKV<'a, Container = ZeroVec<'a, P>> + ?Sized,
{
ZeroMap {
keys: self.keys.cast(),
values: self.values,
}
}
/// Convert a `ZeroMap<K, V>` to `ZeroMap<P, V>` where `K` and `P` are [`AsULE`] types
/// with the same size.
///
/// # Unchecked Invariants
///
/// If `K` and `P` have different ordering semantics, unexpected behavior may occur.
///
/// # Panics
///
/// Panics if `K::ULE` and `P::ULE` are not the same size.
pub fn try_convert_zv_k_unchecked<P>(self) -> Result<ZeroMap<'a, P, V>, ZeroVecError>
where
P: AsULE + ZeroMapKV<'a, Container = ZeroVec<'a, P>> + ?Sized,
{
Ok(ZeroMap {
keys: self.keys.try_into_converted()?,
values: self.values,
})
}
}
impl<'a, K, V> ZeroMap<'a, K, V>
where
K: ZeroMapKV<'a> + ?Sized,
V: ZeroMapKV<'a, Container = ZeroVec<'a, V>> + ?Sized,
V: AsULE,
{
/// Cast a `ZeroMap<K, V>` to `ZeroMap<K, P>` where `V` and `P` are [`AsULE`] types
/// with the same representation.
///
/// # Unchecked Invariants
///
/// If `V` and `P` have different ordering semantics, unexpected behavior may occur.
pub fn cast_zv_v_unchecked<P>(self) -> ZeroMap<'a, K, P>
where
P: AsULE<ULE = V::ULE> + ZeroMapKV<'a, Container = ZeroVec<'a, P>> + ?Sized,
{
ZeroMap {
keys: self.keys,
values: self.values.cast(),
}
}
/// Convert a `ZeroMap<K, V>` to `ZeroMap<K, P>` where `V` and `P` are [`AsULE`] types
/// with the same size.
///
/// # Unchecked Invariants
///
/// If `V` and `P` have different ordering semantics, unexpected behavior may occur.
///
/// # Panics
///
/// Panics if `V::ULE` and `P::ULE` are not the same size.
pub fn try_convert_zv_v_unchecked<P>(self) -> Result<ZeroMap<'a, K, P>, ZeroVecError>
where
P: AsULE + ZeroMapKV<'a, Container = ZeroVec<'a, P>> + ?Sized,
{
Ok(ZeroMap {
keys: self.keys,
values: self.values.try_into_converted()?,
})
}
}
impl<'a, K, V> ZeroMap<'a, K, V>
where
K: ZeroMapKV<'a> + ?Sized + Ord,
V: ZeroMapKV<'a, Container = VarZeroVec<'a, V>> + ?Sized,
V: VarULE,
{
/// Same as `insert()`, but allows using [EncodeAsVarULE](crate::ule::EncodeAsVarULE)
/// types with the value to avoid an extra allocation when dealing with custom ULE types.
///
/// ```rust
/// use std::borrow::Cow;
/// use zerovec::ZeroMap;
///
/// #[zerovec::make_varule(PersonULE)]
/// #[derive(Clone, Eq, PartialEq, Ord, PartialOrd)]
/// struct Person<'a> {
/// age: u8,
/// name: Cow<'a, str>,
/// }
///
/// let mut map: ZeroMap<u32, PersonULE> = ZeroMap::new();
/// map.insert_var_v(
/// &1,
/// &Person {
/// age: 20,
/// name: "Joseph".into(),
/// },
/// );
/// map.insert_var_v(
/// &1,
/// &Person {
/// age: 35,
/// name: "Carla".into(),
/// },
/// );
/// assert_eq!(&map.get(&1).unwrap().name, "Carla");
/// assert!(map.get(&3).is_none());
/// ```
pub fn insert_var_v<VE: EncodeAsVarULE<V>>(&mut self, key: &K, value: &VE) -> Option<Box<V>> {
match self.keys.zvl_binary_search(key) {
Ok(index) => {
#[allow(clippy::unwrap_used)] // binary search
let ret = self.values.get(index).unwrap().to_boxed();
self.values.make_mut().replace(index, value);
Some(ret)
}
Err(index) => {
self.keys.zvl_insert(index, key);
self.values.make_mut().insert(index, value);
None
}
}
}
// insert_var_k, insert_var_kv are not possible since one cannot perform the binary search with EncodeAsVarULE
// though we might be able to do it in the future if we add a trait for cross-Ord requirements
}
impl<'a, K, V> ZeroMap<'a, K, V>
where
K: ZeroMapKV<'a> + ?Sized + Ord,
V: ZeroMapKV<'a> + ?Sized,
V: Copy,
{
/// For cases when `V` is fixed-size, obtain a direct copy of `V` instead of `V::ULE`.
///
/// # Examples
///
/// ```rust
/// use zerovec::ZeroMap;
///
/// let mut map = ZeroMap::new();
/// map.insert(&1, &'a');
/// map.insert(&2, &'b');
/// assert_eq!(map.get_copied(&1), Some('a'));
/// assert_eq!(map.get_copied(&3), None);
#[inline]
pub fn get_copied(&self, key: &K) -> Option<V> {
let index = self.keys.zvl_binary_search(key).ok()?;
self.get_copied_at(index)
}
/// Binary search the map with `predicate` to find a key, returning the value.
///
/// For cases when `V` is fixed-size, use this method to obtain a direct copy of `V`
/// instead of `V::ULE`.
///
/// # Examples
///
/// ```rust
/// use zerovec::ZeroMap;
///
/// let mut map = ZeroMap::new();
/// map.insert(&1, &'a');
/// map.insert(&2, &'b');
/// assert_eq!(map.get_copied_by(|probe| probe.cmp(&1)), Some('a'));
/// assert_eq!(map.get_copied_by(|probe| probe.cmp(&3)), None);
/// ```
#[inline]
pub fn get_copied_by(&self, predicate: impl FnMut(&K) -> Ordering) -> Option<V> {
let index = self.keys.zvl_binary_search_by(predicate).ok()?;
self.get_copied_at(index)
}
fn get_copied_at(&self, index: usize) -> Option<V> {
let ule = self.values.zvl_get(index)?;
let mut result = Option::<V>::None;
V::Container::zvl_get_as_t(ule, |v| result.replace(*v));
#[allow(clippy::unwrap_used)] // `zvl_get_as_t` guarantees that the callback is invoked
Some(result.unwrap())
}
}
impl<'a, K, V> ZeroMap<'a, K, V>
where
K: ZeroMapKV<'a> + ?Sized,
V: ZeroMapKV<'a, Container = ZeroVec<'a, V>> + ?Sized,
V: AsULE + Copy,
{
/// Similar to [`Self::iter()`] except it returns a direct copy of the values instead of references
/// to `V::ULE`, in cases when `V` is fixed-size
pub fn iter_copied_values<'b>(
&'b self,
) -> impl Iterator<Item = (&'b <K as ZeroMapKV<'a>>::GetType, V)> {
(0..self.keys.zvl_len()).map(move |idx| {
(
#[allow(clippy::unwrap_used)] // idx in 0..keys.zvl_len()
self.keys.zvl_get(idx).unwrap(),
#[allow(clippy::unwrap_used)] // idx in 0..keys.zvl_len() = values.zvl_len()
ZeroSlice::get(&*self.values, idx).unwrap(),
)
})
}
}
impl<'a, K, V> ZeroMap<'a, K, V>
where
K: ZeroMapKV<'a, Container = ZeroVec<'a, K>> + ?Sized,
V: ZeroMapKV<'a, Container = ZeroVec<'a, V>> + ?Sized,
K: AsULE + Copy,
V: AsULE + Copy,
{
/// Similar to [`Self::iter()`] except it returns a direct copy of the keys values instead of references
/// to `K::ULE` and `V::ULE`, in cases when `K` and `V` are fixed-size
#[allow(clippy::needless_lifetimes)] // Lifetime is necessary in impl Trait
pub fn iter_copied<'b>(&'b self) -> impl Iterator<Item = (K, V)> + 'b {
let keys = &self.keys;
let values = &self.values;
(0..keys.len()).map(move |idx| {
(
#[allow(clippy::unwrap_used)] // idx in 0..keys.zvl_len()
ZeroSlice::get(&**keys, idx).unwrap(),
#[allow(clippy::unwrap_used)] // idx in 0..keys.zvl_len() = values.zvl_len()
ZeroSlice::get(&**values, idx).unwrap(),
)
})
}
}
impl<'a, K, V> From<ZeroMapBorrowed<'a, K, V>> for ZeroMap<'a, K, V>
where
K: ZeroMapKV<'a>,
V: ZeroMapKV<'a>,
K: ?Sized,
V: ?Sized,
{
fn from(other: ZeroMapBorrowed<'a, K, V>) -> Self {
Self {
keys: K::Container::zvl_from_borrowed(other.keys),
values: V::Container::zvl_from_borrowed(other.values),
}
}
}
// We can't use the default PartialEq because ZeroMap is invariant
// so otherwise rustc will not automatically allow you to compare ZeroMaps
// with different lifetimes
impl<'a, 'b, K, V> PartialEq<ZeroMap<'b, K, V>> for ZeroMap<'a, K, V>
where
K: for<'c> ZeroMapKV<'c> + ?Sized,
V: for<'c> ZeroMapKV<'c> + ?Sized,
<K as ZeroMapKV<'a>>::Container: PartialEq<<K as ZeroMapKV<'b>>::Container>,
<V as ZeroMapKV<'a>>::Container: PartialEq<<V as ZeroMapKV<'b>>::Container>,
{
fn eq(&self, other: &ZeroMap<'b, K, V>) -> bool {
self.keys.eq(&other.keys) && self.values.eq(&other.values)
}
}
impl<'a, K, V> fmt::Debug for ZeroMap<'a, K, V>
where
K: ZeroMapKV<'a> + ?Sized,
V: ZeroMapKV<'a> + ?Sized,
<K as ZeroMapKV<'a>>::Container: fmt::Debug,
<V as ZeroMapKV<'a>>::Container: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
f.debug_struct("ZeroMap")
.field("keys", &self.keys)
.field("values", &self.values)
.finish()
}
}
impl<'a, K, V> Clone for ZeroMap<'a, K, V>
where
K: ZeroMapKV<'a> + ?Sized,
V: ZeroMapKV<'a> + ?Sized,
<K as ZeroMapKV<'a>>::Container: Clone,
<V as ZeroMapKV<'a>>::Container: Clone,
{
fn clone(&self) -> Self {
Self {
keys: self.keys.clone(),
values: self.values.clone(),
}
}
}
impl<'a, A, B, K, V> FromIterator<(A, B)> for ZeroMap<'a, K, V>
where
A: Borrow<K>,
B: Borrow<V>,
K: ZeroMapKV<'a> + ?Sized + Ord,
V: ZeroMapKV<'a> + ?Sized,
{
fn from_iter<T>(iter: T) -> Self
where
T: IntoIterator<Item = (A, B)>,
{
let iter = iter.into_iter();
let mut map = match iter.size_hint() {
(_, Some(upper)) => Self::with_capacity(upper),
(lower, None) => Self::with_capacity(lower),
};
for (key, value) in iter {
if let Some((key, value)) = map.try_append(key.borrow(), value.borrow()) {
map.insert(key, value);
}
}
map
}
}