| 1 | use alloc::borrow::Cow; |
| 2 | use alloc::boxed::Box; |
| 3 | use alloc::collections::TryReserveError; |
| 4 | use alloc::ffi::CString; |
| 5 | use alloc::string::String; |
| 6 | use alloc::vec::{self, Drain, Splice, Vec}; |
| 7 | use core::borrow::{Borrow, BorrowMut}; |
| 8 | use core::cmp::Ordering; |
| 9 | use core::hash::{Hash, Hasher}; |
| 10 | use core::iter::FromIterator; |
| 11 | use core::marker::PhantomData; |
| 12 | use core::mem::MaybeUninit; |
| 13 | use core::ops::{Deref, DerefMut, Index, IndexMut, RangeBounds}; |
| 14 | use core::{fmt, slice}; |
| 15 | #[cfg (feature = "std" )] |
| 16 | use std::io::{IoSlice, Result as IoResult, Write}; |
| 17 | |
| 18 | #[cfg (all(feature = "alloc" , feature = "serde" ))] |
| 19 | use serde::de::{Deserialize, Deserializer}; |
| 20 | #[cfg (feature = "serde" )] |
| 21 | use serde::ser::{Serialize, Serializer}; |
| 22 | |
| 23 | use crate::{TiEnumerated, TiRangeBounds, TiSlice, TiSliceIndex}; |
| 24 | |
| 25 | /// A contiguous growable array type |
| 26 | /// that only accepts keys of the type `K`. |
| 27 | /// |
| 28 | /// `TiVec<K, V>` is a wrapper around Rust container type [`std::vec::Vec`]. |
| 29 | /// The struct mirrors the stable API of Rust [`std::vec::Vec`] |
| 30 | /// and forwards to it as much as possible. |
| 31 | /// |
| 32 | /// `TiVec<K, V>` uses `K` instead of `usize` for element indices and |
| 33 | /// require the index to implement |
| 34 | /// [`From<usize>`][`From`] and [`Into<usize>`][`Into`] traits. |
| 35 | /// Their implementation can be easily done |
| 36 | /// with [`derive_more`] crate and `#[derive(From, Into)]`. |
| 37 | /// |
| 38 | /// `TiVec<K, V>` can be converted to [`std::vec::Vec<V>`][`std::vec::Vec`] and |
| 39 | /// back using [`From`] and [`Into`]. |
| 40 | /// |
| 41 | /// There are also zero-cost conversions available between references: |
| 42 | /// - [`&std::vec::Vec<V>`][`std::vec::Vec`] and `&TiVec<K, V>` with [`AsRef`], |
| 43 | /// - [`&mut std::vec::Vec<V>`][`std::vec::Vec`] and `&mut TiVec<K, V>` with |
| 44 | /// [`AsMut`], |
| 45 | /// |
| 46 | /// Added methods: |
| 47 | /// - [`from_ref`] - Converts a [`&std::vec::Vec<V>`][`std::vec::Vec`] into a |
| 48 | /// `&TiVec<K, V>`. |
| 49 | /// - [`from_mut`] - Converts a [`&mut std::vec::Vec<V>`][`std::vec::Vec`] into |
| 50 | /// a `&mut TiVec<K, V>`. |
| 51 | /// - [`push_and_get_key`] - Appends an element to the back of a collection and |
| 52 | /// returns its index of type `K`. |
| 53 | /// - [`pop_key_value`] - Removes the last element from a vector and returns it |
| 54 | /// with its index of type `K`, or [`None`] if the vector is empty. |
| 55 | /// - [`drain_enumerated`] - Creates a draining iterator that removes the |
| 56 | /// specified range in the vector and yields the current count and the removed |
| 57 | /// items. It acts like `self.drain(range).enumerate()`, but instead of |
| 58 | /// `usize` it returns index of type `K`. |
| 59 | /// - [`into_iter_enumerated`] - Converts the vector into iterator over all |
| 60 | /// key-value pairs with `K` used for iteration indices. It acts like |
| 61 | /// `self.into_iter().enumerate()`, but use `K` instead of `usize` for |
| 62 | /// iteration indices. |
| 63 | /// |
| 64 | /// # Example |
| 65 | /// |
| 66 | /// ``` |
| 67 | /// use derive_more::{From, Into}; |
| 68 | /// use typed_index_collections::TiVec; |
| 69 | /// |
| 70 | /// #[derive(From, Into)] |
| 71 | /// struct FooId(usize); |
| 72 | /// |
| 73 | /// let mut foos: TiVec<FooId, usize> = std::vec![10, 11, 13].into(); |
| 74 | /// foos.insert(FooId(2), 12); |
| 75 | /// assert_eq!(foos[FooId(2)], 12); |
| 76 | /// ``` |
| 77 | /// |
| 78 | /// [`from_ref`]: #method.from_ref |
| 79 | /// [`from_mut`]: #method.from_mut |
| 80 | /// [`push_and_get_key`]: #method.push_and_get_key |
| 81 | /// [`pop_key_value`]: #method.pop_key_value |
| 82 | /// [`drain_enumerated`]: #method.drain_enumerated |
| 83 | /// [`into_iter_enumerated`]: #method.into_iter_enumerated |
| 84 | /// [`std::vec::Vec`]: https://doc.rust-lang.org/std/vec/struct.Vec.html |
| 85 | /// [`From`]: https://doc.rust-lang.org/std/convert/trait.From.html |
| 86 | /// [`Into`]: https://doc.rust-lang.org/std/convert/trait.Into.html |
| 87 | /// [`AsRef`]: https://doc.rust-lang.org/std/convert/trait.AsRef.html |
| 88 | /// [`AsMut`]: https://doc.rust-lang.org/std/convert/trait.AsMut.html |
| 89 | /// [`derive_more`]: https://crates.io/crates/derive_more |
| 90 | #[repr (transparent)] |
| 91 | pub struct TiVec<K, V> { |
| 92 | /// Raw slice property |
| 93 | pub raw: Vec<V>, |
| 94 | |
| 95 | /// Tied slice index type |
| 96 | /// |
| 97 | /// `fn(T) -> T` is *[PhantomData pattern][phantomdata patterns]* |
| 98 | /// used to relax auto trait implementations bounds for |
| 99 | /// [`Send`], [`Sync`], [`Unpin`], [`UnwindSafe`] and [`RefUnwindSafe`]. |
| 100 | /// |
| 101 | /// Derive attribute is not used for trait implementations because it also |
| 102 | /// requires the same trait implemented for K that is an unnecessary |
| 103 | /// requirement. |
| 104 | /// |
| 105 | /// [phantomdata patterns]: https://doc.rust-lang.org/nomicon/phantom-data.html#table-of-phantomdata-patterns |
| 106 | /// [`Send`]: https://doc.rust-lang.org/core/marker/trait.Send.html |
| 107 | /// [`Sync`]: https://doc.rust-lang.org/core/marker/trait.Sync.html |
| 108 | /// [`Unpin`]: https://doc.rust-lang.org/core/marker/trait.Unpin.html |
| 109 | /// [`UnwindSafe`]: https://doc.rust-lang.org/core/std/panic/trait.UnwindSafe.html |
| 110 | /// [`RefUnwindSafe`]: https://doc.rust-lang.org/core/std/panic/trait.RefUnwindSafe.html |
| 111 | _marker: PhantomData<fn(K) -> K>, |
| 112 | } |
| 113 | |
| 114 | impl<K, V> TiVec<K, V> { |
| 115 | /// Constructs a new, empty `TiVec<K, V>`. |
| 116 | /// |
| 117 | /// See [`Vec::new`] for more details. |
| 118 | /// |
| 119 | /// [`Vec::new`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.new |
| 120 | #[inline ] |
| 121 | #[must_use ] |
| 122 | pub const fn new() -> Self { |
| 123 | Self { |
| 124 | raw: Vec::new(), |
| 125 | _marker: PhantomData, |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /// Constructs a new, empty `TiVec<K, V>` with the specified capacity. |
| 130 | /// |
| 131 | /// See [`Vec::with_capacity`] for more details. |
| 132 | /// |
| 133 | /// [`Vec::with_capacity`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.with_capacity |
| 134 | #[inline ] |
| 135 | #[must_use ] |
| 136 | pub fn with_capacity(capacity: usize) -> Self { |
| 137 | Self { |
| 138 | raw: Vec::with_capacity(capacity), |
| 139 | _marker: PhantomData, |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /// Creates a `TiVec<K, V>` directly from the raw components of another |
| 144 | /// vector. |
| 145 | /// |
| 146 | /// See [`Vec::from_raw_parts`] for more details. |
| 147 | /// |
| 148 | /// # Safety |
| 149 | /// |
| 150 | /// This is highly unsafe, due to the number of invariants that aren't |
| 151 | /// checked. |
| 152 | /// See [`Vec::from_raw_parts`] for more details. |
| 153 | /// |
| 154 | /// [`Vec::from_raw_parts`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.from_raw_parts |
| 155 | #[inline ] |
| 156 | pub unsafe fn from_raw_parts(ptr: *mut V, length: usize, capacity: usize) -> Self { |
| 157 | Self { |
| 158 | raw: Vec::from_raw_parts(ptr, length, capacity), |
| 159 | _marker: PhantomData, |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /// Converts a [`&std::vec::Vec<V>`] into a `&TiVec<K, V>`. |
| 164 | /// |
| 165 | /// Vector reference is intentionally used in the argument |
| 166 | /// instead of slice reference for conversion with no-op. |
| 167 | /// |
| 168 | /// # Example |
| 169 | /// |
| 170 | /// ``` |
| 171 | /// # use typed_index_collections::TiVec; |
| 172 | /// pub struct Id(usize); |
| 173 | /// let vec: &TiVec<Id, usize> = TiVec::from_ref(&vec![1, 2, 4]); |
| 174 | /// ``` |
| 175 | /// |
| 176 | /// [`&std::vec::Vec<V>`]: https://doc.rust-lang.org/std/vec/struct.Vec.html |
| 177 | #[inline ] |
| 178 | #[must_use ] |
| 179 | pub const fn from_ref(raw: &Vec<V>) -> &Self { |
| 180 | // SAFETY: `TiVec<K, V>` is `repr(transparent)` over a `Vec<V>` type. |
| 181 | unsafe { &*core::ptr::from_ref::<Vec<V>>(raw).cast::<Self>() } |
| 182 | } |
| 183 | |
| 184 | /// Converts a [`&mut std::vec::Vec<V>`] into a `&mut TiVec<K, V>`. |
| 185 | /// |
| 186 | /// # Example |
| 187 | /// |
| 188 | /// ``` |
| 189 | /// # use typed_index_collections::TiVec; |
| 190 | /// pub struct Id(usize); |
| 191 | /// let vec: &mut TiVec<Id, usize> = TiVec::from_mut(&mut vec![1, 2, 4]); |
| 192 | /// ``` |
| 193 | /// |
| 194 | /// [`&mut std::vec::Vec<V>`]: https://doc.rust-lang.org/std/vec/struct.Vec.html |
| 195 | #[inline ] |
| 196 | pub fn from_mut(raw: &mut Vec<V>) -> &mut Self { |
| 197 | // SAFETY: `TiVec<K, V>` is `repr(transparent)` over a `Vec<V>` type. |
| 198 | unsafe { &mut *core::ptr::from_mut::<Vec<V>>(raw).cast::<Self>() } |
| 199 | } |
| 200 | |
| 201 | /// Returns the number of elements the vector can hold without |
| 202 | /// reallocating. |
| 203 | /// |
| 204 | /// See [`Vec::capacity`] for more details. |
| 205 | /// |
| 206 | /// [`Vec::capacity`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.capacity |
| 207 | #[inline ] |
| 208 | #[must_use ] |
| 209 | pub fn capacity(&self) -> usize { |
| 210 | self.raw.capacity() |
| 211 | } |
| 212 | |
| 213 | /// Reserves capacity for at least `additional` more elements to be inserted |
| 214 | /// in the given `TiVec<K, V>`. The collection may reserve more space to |
| 215 | /// avoid frequent reallocations. After calling `reserve`, capacity will |
| 216 | /// be greater than or equal to `self.len() + additional`. Does nothing |
| 217 | /// if capacity is already sufficient. |
| 218 | /// |
| 219 | /// See [`Vec::reserve`] for more details. |
| 220 | /// |
| 221 | /// [`Vec::reserve`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.reserve |
| 222 | #[inline ] |
| 223 | pub fn reserve(&mut self, additional: usize) { |
| 224 | self.raw.reserve(additional); |
| 225 | } |
| 226 | |
| 227 | /// Reserves the minimum capacity for exactly `additional` more elements to |
| 228 | /// be inserted in the given `TiVec<K, V>`. After calling `reserve_exact`, |
| 229 | /// capacity will be greater than or equal to `self.len() + additional`. |
| 230 | /// Does nothing if the capacity is already sufficient. |
| 231 | /// |
| 232 | /// See [`Vec::reserve_exact`] for more details. |
| 233 | /// |
| 234 | /// [`Vec::reserve_exact`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.reserve_exact |
| 235 | #[inline ] |
| 236 | pub fn reserve_exact(&mut self, additional: usize) { |
| 237 | self.raw.reserve_exact(additional); |
| 238 | } |
| 239 | |
| 240 | /// Tries to reserve capacity for at least `additional` more elements to be |
| 241 | /// inserted in the given `Vec<T>`. |
| 242 | /// |
| 243 | /// See [`Vec::try_reserve`] for more details. |
| 244 | /// |
| 245 | /// # Errors |
| 246 | /// |
| 247 | /// If the capacity overflows, or the allocator reports a failure, then an |
| 248 | /// error is returned. |
| 249 | /// |
| 250 | /// [`Vec::try_reserve`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.try_reserve |
| 251 | #[inline ] |
| 252 | pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> { |
| 253 | self.raw.try_reserve(additional) |
| 254 | } |
| 255 | |
| 256 | /// Tries to reserve the minimum capacity for at least `additional` |
| 257 | /// elements to be inserted in the given `Vec<T>`. |
| 258 | /// |
| 259 | /// See [`Vec::try_reserve_exact`] for more details. |
| 260 | /// |
| 261 | /// # Errors |
| 262 | /// |
| 263 | /// If the capacity overflows, or the allocator reports a failure, then an |
| 264 | /// error is returned. |
| 265 | /// |
| 266 | /// [`Vec::try_reserve_exact`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.try_reserve_exact |
| 267 | #[inline ] |
| 268 | pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> { |
| 269 | self.raw.try_reserve_exact(additional) |
| 270 | } |
| 271 | |
| 272 | /// Shrinks the capacity of the vector as much as possible. |
| 273 | /// |
| 274 | /// See [`Vec::shrink_to_fit`] for more details. |
| 275 | /// |
| 276 | /// [`Vec::shrink_to_fit`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.shrink_to_fit |
| 277 | #[inline ] |
| 278 | pub fn shrink_to_fit(&mut self) { |
| 279 | self.raw.shrink_to_fit(); |
| 280 | } |
| 281 | |
| 282 | /// Shrinks the capacity of the vector with a lower bound. |
| 283 | /// |
| 284 | /// See [`Vec::shrink_to`] for more details. |
| 285 | /// |
| 286 | /// [`Vec::shrink_to`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.shrink_to |
| 287 | #[inline ] |
| 288 | pub fn shrink_to(&mut self, min_capacity: usize) { |
| 289 | self.raw.shrink_to(min_capacity); |
| 290 | } |
| 291 | /// Converts the vector into [`Box<TiSlice<K, V>>`][`Box`]. |
| 292 | /// |
| 293 | /// See [`Vec::into_boxed_slice`] for more details. |
| 294 | /// |
| 295 | /// [`Vec::into_boxed_slice`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.into_boxed_slice |
| 296 | /// [`Box`]: https://doc.rust-lang.org/std/boxed/struct.Box.html |
| 297 | #[inline ] |
| 298 | #[must_use ] |
| 299 | pub fn into_boxed_slice(self) -> Box<TiSlice<K, V>> { |
| 300 | self.raw.into_boxed_slice().into() |
| 301 | } |
| 302 | |
| 303 | /// Shortens the vector, keeping the first `len` elements and dropping |
| 304 | /// the rest. |
| 305 | /// |
| 306 | /// See [`Vec::truncate`] for more details. |
| 307 | /// |
| 308 | /// [`Vec::truncate`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.truncate |
| 309 | #[inline ] |
| 310 | pub fn truncate(&mut self, len: usize) { |
| 311 | self.raw.truncate(len); |
| 312 | } |
| 313 | |
| 314 | /// Extracts a slice containing the entire vector. |
| 315 | /// |
| 316 | /// See [`Vec::as_slice`] for more details. |
| 317 | /// |
| 318 | /// [`Vec::as_slice`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.as_slice |
| 319 | #[inline ] |
| 320 | #[must_use ] |
| 321 | pub fn as_slice(&self) -> &TiSlice<K, V> { |
| 322 | self.raw.as_slice().as_ref() |
| 323 | } |
| 324 | |
| 325 | /// Extracts a mutable slice of the entire vector. |
| 326 | /// |
| 327 | /// See [`Vec::as_mut_slice`] for more details. |
| 328 | /// |
| 329 | /// [`Vec::as_mut_slice`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.as_mut_slice |
| 330 | #[inline ] |
| 331 | pub fn as_mut_slice(&mut self) -> &mut TiSlice<K, V> { |
| 332 | self.raw.as_mut_slice().as_mut() |
| 333 | } |
| 334 | |
| 335 | /// Returns a raw pointer to the vector's buffer. |
| 336 | /// |
| 337 | /// See [`Vec::as_ptr`] for more details. |
| 338 | /// |
| 339 | /// [`Vec::as_ptr`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.as_ptr |
| 340 | #[inline ] |
| 341 | #[must_use ] |
| 342 | pub fn as_ptr(&self) -> *const V { |
| 343 | self.raw.as_ptr() |
| 344 | } |
| 345 | |
| 346 | /// Returns an unsafe mutable pointer to the vector's buffer. |
| 347 | /// |
| 348 | /// See [`Vec::as_mut_ptr`] for more details. |
| 349 | /// |
| 350 | /// [`Vec::as_mut_ptr`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.as_mut_ptr |
| 351 | #[inline ] |
| 352 | pub fn as_mut_ptr(&mut self) -> *mut V { |
| 353 | self.raw.as_mut_ptr() |
| 354 | } |
| 355 | |
| 356 | /// Forces the length of the vector to `new_len`. |
| 357 | /// |
| 358 | /// See [`Vec::set_len`] for more details. |
| 359 | /// |
| 360 | /// # Safety |
| 361 | /// |
| 362 | /// - `new_len` must be less than or equal to [`capacity()`]. |
| 363 | /// - The elements at `old_len..new_len` must be initialized. |
| 364 | /// |
| 365 | /// [`Vec::set_len`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.set_len |
| 366 | /// [`capacity()`]: #method.capacity |
| 367 | #[inline ] |
| 368 | pub unsafe fn set_len(&mut self, new_len: usize) { |
| 369 | self.raw.set_len(new_len); |
| 370 | } |
| 371 | |
| 372 | /// Removes an element from the vector and returns it. |
| 373 | /// |
| 374 | /// The removed element is replaced by the last element of the vector. |
| 375 | /// |
| 376 | /// See [`Vec::swap_remove`] for more details. |
| 377 | /// |
| 378 | /// [`Vec::swap_remove`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.swap_remove |
| 379 | #[inline ] |
| 380 | pub fn swap_remove(&mut self, index: K) -> V |
| 381 | where |
| 382 | usize: From<K>, |
| 383 | { |
| 384 | self.raw.swap_remove(index.into()) |
| 385 | } |
| 386 | |
| 387 | /// Inserts an element at position `index` within the vector, shifting all |
| 388 | /// elements after it to the right. |
| 389 | /// |
| 390 | /// See [`Vec::insert`] for more details. |
| 391 | /// |
| 392 | /// [`Vec::insert`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.insert |
| 393 | #[inline ] |
| 394 | pub fn insert(&mut self, index: K, element: V) |
| 395 | where |
| 396 | usize: From<K>, |
| 397 | { |
| 398 | self.raw.insert(index.into(), element); |
| 399 | } |
| 400 | |
| 401 | /// Removes and returns the element at position `index` within the vector, |
| 402 | /// shifting all elements after it to the left. |
| 403 | /// |
| 404 | /// See [`Vec::remove`] for more details. |
| 405 | /// |
| 406 | /// [`Vec::remove`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.remove |
| 407 | #[inline ] |
| 408 | pub fn remove(&mut self, index: K) -> V |
| 409 | where |
| 410 | usize: From<K>, |
| 411 | { |
| 412 | self.raw.remove(index.into()) |
| 413 | } |
| 414 | |
| 415 | /// Retains only the elements specified by the predicate. |
| 416 | /// |
| 417 | /// See [`Vec::retain`] for more details. |
| 418 | /// |
| 419 | /// [`Vec::retain`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.retain |
| 420 | #[inline ] |
| 421 | pub fn retain<F>(&mut self, f: F) |
| 422 | where |
| 423 | F: FnMut(&V) -> bool, |
| 424 | { |
| 425 | self.raw.retain(f); |
| 426 | } |
| 427 | |
| 428 | /// Retains only the elements specified by the predicate, passing a mutable |
| 429 | /// reference to it. |
| 430 | /// |
| 431 | /// See [`Vec::retain_mut`] for more details. |
| 432 | /// |
| 433 | /// [`Vec::retain_mut`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.retain_mut |
| 434 | #[inline ] |
| 435 | pub fn retain_mut<F>(&mut self, f: F) |
| 436 | where |
| 437 | F: FnMut(&mut V) -> bool, |
| 438 | { |
| 439 | self.raw.retain_mut(f); |
| 440 | } |
| 441 | |
| 442 | /// Removes all but the first of consecutive elements in the vector that |
| 443 | /// resolve to the same key. |
| 444 | /// |
| 445 | /// See [`Vec::dedup_by_key`] for more details. |
| 446 | /// |
| 447 | /// [`Vec::dedup_by_key`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.dedup_by_key |
| 448 | #[inline ] |
| 449 | pub fn dedup_by_key<F, K2>(&mut self, key: F) |
| 450 | where |
| 451 | F: FnMut(&mut V) -> K2, |
| 452 | K2: PartialEq, |
| 453 | { |
| 454 | self.raw.dedup_by_key(key); |
| 455 | } |
| 456 | |
| 457 | /// Removes all but the first of consecutive elements in the vector |
| 458 | /// satisfying a given equality relation. |
| 459 | /// |
| 460 | /// See [`Vec::dedup_by`] for more details. |
| 461 | /// |
| 462 | /// [`Vec::dedup_by`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.dedup_by |
| 463 | #[inline ] |
| 464 | pub fn dedup_by<F>(&mut self, same_bucket: F) |
| 465 | where |
| 466 | F: FnMut(&mut V, &mut V) -> bool, |
| 467 | { |
| 468 | self.raw.dedup_by(same_bucket); |
| 469 | } |
| 470 | |
| 471 | /// Appends an element to the back of a collection. |
| 472 | /// |
| 473 | /// See [`Vec::push`] for more details. |
| 474 | /// |
| 475 | /// [`Vec::push`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.push |
| 476 | #[inline ] |
| 477 | pub fn push(&mut self, value: V) { |
| 478 | self.raw.push(value); |
| 479 | } |
| 480 | |
| 481 | /// Appends an element to the back of a collection and returns its index of |
| 482 | /// type `K`. |
| 483 | /// |
| 484 | /// It acts like `{ vec.push(...); vec.last_key().unwrap() }`, |
| 485 | /// but is optimized better. |
| 486 | /// |
| 487 | /// See [`Vec::push`] for more details. |
| 488 | /// |
| 489 | /// # Example |
| 490 | /// |
| 491 | /// ``` |
| 492 | /// # use derive_more::{From, Into}; |
| 493 | /// # use typed_index_collections::TiVec; |
| 494 | /// #[derive(Eq, Debug, From, Into, PartialEq)] |
| 495 | /// pub struct Id(usize); |
| 496 | /// let mut vec: TiVec<Id, usize> = vec![1, 2, 4].into(); |
| 497 | /// assert_eq!(vec.push_and_get_key(8), Id(3)); |
| 498 | /// assert_eq!(vec.push_and_get_key(16), Id(4)); |
| 499 | /// assert_eq!(vec.push_and_get_key(32), Id(5)); |
| 500 | /// ``` |
| 501 | /// |
| 502 | /// [`Vec::push`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.push |
| 503 | #[inline ] |
| 504 | pub fn push_and_get_key(&mut self, value: V) -> K |
| 505 | where |
| 506 | K: From<usize>, |
| 507 | { |
| 508 | let key = self.next_key(); |
| 509 | self.raw.push(value); |
| 510 | key |
| 511 | } |
| 512 | |
| 513 | /// Removes the last element from a vector and returns it, or [`None`] if it |
| 514 | /// is empty. |
| 515 | /// |
| 516 | /// See [`Vec::pop`] for more details. |
| 517 | /// |
| 518 | /// [`Vec::pop`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.pop |
| 519 | #[inline ] |
| 520 | pub fn pop(&mut self) -> Option<V> { |
| 521 | self.raw.pop() |
| 522 | } |
| 523 | |
| 524 | /// Removes the last element from a vector and returns it with |
| 525 | /// its index of type `K`, or [`None`] if the vector is empty. |
| 526 | /// |
| 527 | /// See [`Vec::pop`] for more details. |
| 528 | /// |
| 529 | /// [`Vec::pop`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.pop |
| 530 | /// |
| 531 | /// # Example |
| 532 | /// |
| 533 | /// ``` |
| 534 | /// # use derive_more::{From, Into}; |
| 535 | /// # use typed_index_collections::TiVec; |
| 536 | /// #[derive(Eq, Debug, From, Into, PartialEq)] |
| 537 | /// pub struct Id(usize); |
| 538 | /// let mut vec: TiVec<Id, usize> = vec![1, 2, 4].into(); |
| 539 | /// assert_eq!(vec.pop_key_value(), Some((Id(2), 4))); |
| 540 | /// assert_eq!(vec.pop_key_value(), Some((Id(1), 2))); |
| 541 | /// assert_eq!(vec.pop_key_value(), Some((Id(0), 1))); |
| 542 | /// assert_eq!(vec.pop_key_value(), None); |
| 543 | /// ``` |
| 544 | /// |
| 545 | /// [`Vec::push`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.push |
| 546 | #[inline ] |
| 547 | pub fn pop_key_value(&mut self) -> Option<(K, V)> |
| 548 | where |
| 549 | K: From<usize>, |
| 550 | { |
| 551 | self.raw.pop().map(|value| (self.raw.len().into(), value)) |
| 552 | } |
| 553 | |
| 554 | /// Moves all the elements of `other` into `Self`, leaving `other` empty. |
| 555 | /// |
| 556 | /// See [`Vec::append`] for more details. |
| 557 | /// |
| 558 | /// [`Vec::append`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.append |
| 559 | #[inline ] |
| 560 | pub fn append(&mut self, other: &mut Self) { |
| 561 | self.raw.append(&mut other.raw); |
| 562 | } |
| 563 | |
| 564 | /// Creates a draining iterator that removes the specified range in the |
| 565 | /// vector and yields the removed items. |
| 566 | /// |
| 567 | /// See [`Vec::drain`] for more details. |
| 568 | /// |
| 569 | /// [`Vec::drain`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.drain |
| 570 | #[inline ] |
| 571 | pub fn drain<R>(&mut self, range: R) -> Drain<'_, V> |
| 572 | where |
| 573 | R: TiRangeBounds<K>, |
| 574 | { |
| 575 | self.raw.drain(range.into_range()) |
| 576 | } |
| 577 | |
| 578 | /// Creates a draining iterator that removes the specified |
| 579 | /// range in the vector and yields the current count and the removed items. |
| 580 | /// |
| 581 | /// It acts like `self.drain(range).enumerate()`, |
| 582 | /// but instead of `usize` it returns index of type `K`. |
| 583 | /// |
| 584 | /// Note that the indices started from `K::from_usize(0)`, |
| 585 | /// regardless of the range starting point. |
| 586 | /// |
| 587 | /// See [`Vec::drain`] for more details. |
| 588 | /// |
| 589 | /// # Example |
| 590 | /// |
| 591 | /// ``` |
| 592 | /// # use derive_more::{From, Into}; |
| 593 | /// # use typed_index_collections::{TiSlice, TiVec}; |
| 594 | /// #[derive(Eq, Debug, From, Into, PartialEq)] |
| 595 | /// pub struct Id(usize); |
| 596 | /// let mut vec: TiVec<Id, usize> = vec![1, 2, 4].into(); |
| 597 | /// { |
| 598 | /// let mut iterator = vec.drain_enumerated(Id(1)..); |
| 599 | /// assert_eq!(iterator.next(), Some((Id(0), 2))); |
| 600 | /// assert_eq!(iterator.next(), Some((Id(1), 4))); |
| 601 | /// assert_eq!(iterator.next(), None); |
| 602 | /// } |
| 603 | /// assert_eq!(vec.as_slice(), TiSlice::from_ref(&[1])); |
| 604 | /// ``` |
| 605 | /// |
| 606 | /// [`Vec::drain`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.drain |
| 607 | #[inline ] |
| 608 | pub fn drain_enumerated<R>(&mut self, range: R) -> TiEnumerated<Drain<'_, V>, K, V> |
| 609 | where |
| 610 | K: From<usize>, |
| 611 | R: TiRangeBounds<K>, |
| 612 | { |
| 613 | self.raw |
| 614 | .drain(range.into_range()) |
| 615 | .enumerate() |
| 616 | .map(|(key, value)| (key.into(), value)) |
| 617 | } |
| 618 | |
| 619 | /// Clears the vector, removing all values. |
| 620 | /// |
| 621 | /// See [`Vec::clear`] for more details. |
| 622 | /// |
| 623 | /// [`Vec::clear`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.clear |
| 624 | #[inline ] |
| 625 | pub fn clear(&mut self) { |
| 626 | self.raw.clear(); |
| 627 | } |
| 628 | |
| 629 | /// Returns the number of elements in the vector, also referred to |
| 630 | /// as its 'length'. |
| 631 | /// |
| 632 | /// See [`Vec::len`] for more details. |
| 633 | /// |
| 634 | /// [`Vec::len`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.len |
| 635 | #[inline ] |
| 636 | #[must_use ] |
| 637 | pub fn len(&self) -> usize { |
| 638 | self.raw.len() |
| 639 | } |
| 640 | |
| 641 | /// Returns `true` if the vector contains no elements. |
| 642 | /// |
| 643 | /// See [`Vec::is_empty`] for more details. |
| 644 | /// |
| 645 | /// [`Vec::is_empty`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.is_empty |
| 646 | #[inline ] |
| 647 | #[must_use ] |
| 648 | pub fn is_empty(&self) -> bool { |
| 649 | self.raw.is_empty() |
| 650 | } |
| 651 | |
| 652 | /// Splits the collection into two at the given index. |
| 653 | /// |
| 654 | /// See [`Vec::split_off`] for more details. |
| 655 | /// |
| 656 | /// [`Vec::split_off`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.split_off |
| 657 | #[inline ] |
| 658 | #[must_use = "use `.truncate()` if you don't need the other half" ] |
| 659 | pub fn split_off(&mut self, at: K) -> Self |
| 660 | where |
| 661 | usize: From<K>, |
| 662 | { |
| 663 | self.raw.split_off(at.into()).into() |
| 664 | } |
| 665 | |
| 666 | /// Resizes the `TiVec` in-place so that `len` is equal to `new_len`. |
| 667 | /// |
| 668 | /// See [`Vec::resize_with`] for more details. |
| 669 | /// |
| 670 | /// [`Vec::resize_with`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.resize_with |
| 671 | #[inline ] |
| 672 | pub fn resize_with<F>(&mut self, new_len: usize, f: F) |
| 673 | where |
| 674 | F: FnMut() -> V, |
| 675 | { |
| 676 | self.raw.resize_with(new_len, f); |
| 677 | } |
| 678 | |
| 679 | /// Resizes the `TiVec` in-place so that `len` is equal to `new_len`. |
| 680 | /// |
| 681 | /// See [`Vec::resize`] for more details. |
| 682 | /// |
| 683 | /// [`Vec::resize`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.resize |
| 684 | #[inline ] |
| 685 | pub fn resize(&mut self, new_len: usize, value: V) |
| 686 | where |
| 687 | V: Clone, |
| 688 | { |
| 689 | self.raw.resize(new_len, value); |
| 690 | } |
| 691 | |
| 692 | /// Consumes and leaks the `Vec`, returning a mutable reference to the |
| 693 | /// contents, `&'a mut [T]`. Note that the type `T` must outlive the |
| 694 | /// chosen lifetime `'a`. If the type has only static references, or |
| 695 | /// none at all, then this may be chosen to be `'static`. |
| 696 | /// |
| 697 | /// See [`Vec::leak`] for more details. |
| 698 | /// |
| 699 | /// [`Vec::leak`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.leak |
| 700 | #[expect (clippy::must_use_candidate, reason = "not used in `Vec::leak`" )] |
| 701 | #[inline ] |
| 702 | pub fn leak<'a>(self) -> &'a mut TiSlice<K, V> { |
| 703 | self.raw.leak().as_mut() |
| 704 | } |
| 705 | |
| 706 | /// Returns the remaining spare capacity of the vector as a slice of |
| 707 | /// `MaybeUninit<T>`. |
| 708 | /// |
| 709 | /// See [`Vec::spare_capacity_mut`] for more details. |
| 710 | /// |
| 711 | /// [`Vec::spare_capacity_mut`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.spare_capacity_mut |
| 712 | #[inline ] |
| 713 | pub fn spare_capacity_mut(&mut self) -> &mut TiSlice<K, MaybeUninit<V>> { |
| 714 | self.raw.spare_capacity_mut().as_mut() |
| 715 | } |
| 716 | |
| 717 | /// Clones and appends all elements in a slice to the `TiVec`. |
| 718 | /// |
| 719 | /// See [`Vec::extend_from_slice`] for more details. |
| 720 | /// |
| 721 | /// [`Vec::extend_from_slice`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.extend_from_slice |
| 722 | #[inline ] |
| 723 | pub fn extend_from_slice(&mut self, other: &TiSlice<K, V>) |
| 724 | where |
| 725 | V: Clone, |
| 726 | { |
| 727 | self.raw.extend_from_slice(&other.raw); |
| 728 | } |
| 729 | |
| 730 | /// Copies elements from `src` range to the end of the vector. |
| 731 | /// |
| 732 | /// See [`Vec::extend_from_within`] for more details. |
| 733 | /// |
| 734 | /// # Panics |
| 735 | /// |
| 736 | /// Panics if the starting point is greater than the end point or if |
| 737 | /// the end point is greater than the length of the vector. |
| 738 | /// |
| 739 | /// [`Vec::extend_from_within`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.extend_from_within |
| 740 | #[inline ] |
| 741 | pub fn extend_from_within<R>(&mut self, src: R) |
| 742 | where |
| 743 | V: Clone, |
| 744 | R: RangeBounds<usize>, |
| 745 | { |
| 746 | self.raw.extend_from_within(src); |
| 747 | } |
| 748 | |
| 749 | /// Removes consecutive repeated elements in the vector according to the |
| 750 | /// [`PartialEq`] trait implementation. |
| 751 | /// |
| 752 | /// See [`Vec::dedup`] for more details. |
| 753 | /// |
| 754 | /// [`PartialEq`]: https://doc.rust-lang.org/std/cmp/trait.PartialEq.html |
| 755 | /// [`Vec::dedup`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.dedup |
| 756 | #[inline ] |
| 757 | pub fn dedup(&mut self) |
| 758 | where |
| 759 | V: PartialEq, |
| 760 | { |
| 761 | self.raw.dedup(); |
| 762 | } |
| 763 | |
| 764 | /// Creates a splicing iterator that replaces the specified range in the |
| 765 | /// vector with the given `replace_with` iterator and yields the removed |
| 766 | /// items. `replace_with` does not need to be the same length as |
| 767 | /// `range`. |
| 768 | /// |
| 769 | /// See [`Vec::splice`] for more details. |
| 770 | /// |
| 771 | /// [`Vec::splice`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.splice |
| 772 | #[inline ] |
| 773 | pub fn splice<R, I>(&mut self, range: R, replace_with: I) -> Splice<'_, I::IntoIter> |
| 774 | where |
| 775 | R: TiRangeBounds<K>, |
| 776 | I: IntoIterator<Item = V>, |
| 777 | { |
| 778 | self.raw.splice(range.into_range(), replace_with) |
| 779 | } |
| 780 | |
| 781 | /// Converts the vector into iterator over all key-value pairs |
| 782 | /// with `K` used for iteration indices. |
| 783 | /// |
| 784 | /// It acts like `self.into_iter().enumerate()`, |
| 785 | /// but use `K` instead of `usize` for iteration indices. |
| 786 | /// |
| 787 | /// # Example |
| 788 | /// |
| 789 | /// ``` |
| 790 | /// # use derive_more::{From, Into}; |
| 791 | /// # use typed_index_collections::TiVec; |
| 792 | /// #[derive(Eq, Debug, From, Into, PartialEq)] |
| 793 | /// pub struct Id(usize); |
| 794 | /// let vec: TiVec<Id, usize> = vec![1, 2, 4].into(); |
| 795 | /// let mut iterator = vec.into_iter_enumerated(); |
| 796 | /// assert_eq!(iterator.next(), Some((Id(0), 1))); |
| 797 | /// assert_eq!(iterator.next(), Some((Id(1), 2))); |
| 798 | /// assert_eq!(iterator.next(), Some((Id(2), 4))); |
| 799 | /// assert_eq!(iterator.next(), None); |
| 800 | /// ``` |
| 801 | #[inline ] |
| 802 | pub fn into_iter_enumerated(self) -> TiEnumerated<vec::IntoIter<V>, K, V> |
| 803 | where |
| 804 | K: From<usize>, |
| 805 | { |
| 806 | self.raw |
| 807 | .into_iter() |
| 808 | .enumerate() |
| 809 | .map(|(key, value)| (key.into(), value)) |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | impl<K, V> fmt::Debug for TiVec<K, V> |
| 814 | where |
| 815 | K: fmt::Debug + From<usize>, |
| 816 | V: fmt::Debug, |
| 817 | { |
| 818 | #[allow (clippy::allow_attributes, reason = "rust-lang/rust#130021" )] |
| 819 | #[allow ( |
| 820 | clippy::missing_inline_in_public_items, |
| 821 | reason = "use default inlining behavior" |
| 822 | )] |
| 823 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 824 | f.debug_map().entries(self.iter_enumerated()).finish() |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | impl<K, V> AsRef<Self> for TiVec<K, V> { |
| 829 | #[inline ] |
| 830 | fn as_ref(&self) -> &Self { |
| 831 | self |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | impl<K, V> AsMut<Self> for TiVec<K, V> { |
| 836 | #[inline ] |
| 837 | fn as_mut(&mut self) -> &mut Self { |
| 838 | self |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | impl<K, V> AsRef<TiSlice<K, V>> for TiVec<K, V> { |
| 843 | #[inline ] |
| 844 | fn as_ref(&self) -> &TiSlice<K, V> { |
| 845 | self |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | impl<K, V> AsMut<TiSlice<K, V>> for TiVec<K, V> { |
| 850 | #[inline ] |
| 851 | fn as_mut(&mut self) -> &mut TiSlice<K, V> { |
| 852 | self |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | impl<K, V> AsRef<Vec<V>> for TiVec<K, V> { |
| 857 | #[inline ] |
| 858 | fn as_ref(&self) -> &Vec<V> { |
| 859 | &self.raw |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | impl<K, V> AsMut<Vec<V>> for TiVec<K, V> { |
| 864 | #[inline ] |
| 865 | fn as_mut(&mut self) -> &mut Vec<V> { |
| 866 | &mut self.raw |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | impl<K, V> AsRef<[V]> for TiVec<K, V> { |
| 871 | #[inline ] |
| 872 | fn as_ref(&self) -> &[V] { |
| 873 | &self.raw |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | impl<K, V> AsMut<[V]> for TiVec<K, V> { |
| 878 | #[inline ] |
| 879 | fn as_mut(&mut self) -> &mut [V] { |
| 880 | &mut self.raw |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | impl<K, V> AsRef<TiVec<K, V>> for Vec<V> { |
| 885 | #[inline ] |
| 886 | fn as_ref(&self) -> &TiVec<K, V> { |
| 887 | TiVec::from_ref(self) |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | impl<K, V> AsMut<TiVec<K, V>> for Vec<V> { |
| 892 | #[inline ] |
| 893 | fn as_mut(&mut self) -> &mut TiVec<K, V> { |
| 894 | TiVec::from_mut(self) |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | impl<K, V> Borrow<TiSlice<K, V>> for TiVec<K, V> { |
| 899 | #[inline ] |
| 900 | fn borrow(&self) -> &TiSlice<K, V> { |
| 901 | self.as_slice() |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | impl<K, V> BorrowMut<TiSlice<K, V>> for TiVec<K, V> { |
| 906 | #[inline ] |
| 907 | fn borrow_mut(&mut self) -> &mut TiSlice<K, V> { |
| 908 | self.as_mut_slice() |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | impl<K, V> Deref for TiVec<K, V> { |
| 913 | type Target = TiSlice<K, V>; |
| 914 | |
| 915 | #[inline ] |
| 916 | fn deref(&self) -> &TiSlice<K, V> { |
| 917 | Self::Target::from_ref(&self.raw) |
| 918 | } |
| 919 | } |
| 920 | |
| 921 | impl<K, V> DerefMut for TiVec<K, V> { |
| 922 | #[inline ] |
| 923 | fn deref_mut(&mut self) -> &mut TiSlice<K, V> { |
| 924 | Self::Target::from_mut(&mut self.raw) |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | impl<K, V> From<Vec<V>> for TiVec<K, V> { |
| 929 | #[inline ] |
| 930 | fn from(vec: Vec<V>) -> Self { |
| 931 | Self { |
| 932 | raw: vec, |
| 933 | _marker: PhantomData, |
| 934 | } |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | impl<K, V> From<TiVec<K, V>> for Vec<V> { |
| 939 | #[inline ] |
| 940 | fn from(vec: TiVec<K, V>) -> Self { |
| 941 | vec.raw |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | impl<K, V> From<&TiSlice<K, V>> for TiVec<K, V> |
| 946 | where |
| 947 | V: Clone, |
| 948 | { |
| 949 | #[inline ] |
| 950 | fn from(slice: &TiSlice<K, V>) -> Self { |
| 951 | slice.to_vec() |
| 952 | } |
| 953 | } |
| 954 | |
| 955 | impl<K, V> From<&mut TiSlice<K, V>> for TiVec<K, V> |
| 956 | where |
| 957 | V: Clone, |
| 958 | { |
| 959 | #[inline ] |
| 960 | fn from(slice: &mut TiSlice<K, V>) -> Self { |
| 961 | slice.to_vec() |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | impl<K, V> From<Cow<'_, TiSlice<K, V>>> for TiVec<K, V> |
| 966 | where |
| 967 | V: Clone, |
| 968 | { |
| 969 | #[inline ] |
| 970 | fn from(slice: Cow<'_, TiSlice<K, V>>) -> Self { |
| 971 | slice.into_owned() |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | impl<K, V> From<TiVec<K, V>> for Cow<'_, TiSlice<K, V>> |
| 976 | where |
| 977 | V: Clone, |
| 978 | { |
| 979 | #[inline ] |
| 980 | fn from(vec: TiVec<K, V>) -> Self { |
| 981 | Cow::Owned(vec) |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | impl<K> From<&str> for TiVec<K, u8> { |
| 986 | #[inline ] |
| 987 | fn from(s: &str) -> Self { |
| 988 | s.as_bytes().to_vec().into() |
| 989 | } |
| 990 | } |
| 991 | |
| 992 | impl<K> From<String> for TiVec<K, u8> { |
| 993 | #[inline ] |
| 994 | fn from(s: String) -> Self { |
| 995 | s.into_bytes().into() |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | #[cfg (feature = "alloc" )] |
| 1000 | #[cfg_attr (docsrs, doc(cfg(feature = "alloc" )))] |
| 1001 | impl<K> From<CString> for TiVec<K, u8> { |
| 1002 | #[inline ] |
| 1003 | fn from(s: CString) -> Self { |
| 1004 | s.into_bytes().into() |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | impl<K, V> Clone for TiVec<K, V> |
| 1009 | where |
| 1010 | V: Clone, |
| 1011 | { |
| 1012 | #[inline ] |
| 1013 | fn clone(&self) -> Self { |
| 1014 | self.raw.clone().into() |
| 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | impl<K, V> Eq for TiVec<K, V> where V: Eq {} |
| 1019 | |
| 1020 | impl<K, A, B> PartialEq<TiVec<K, B>> for TiVec<K, A> |
| 1021 | where |
| 1022 | A: PartialEq<B>, |
| 1023 | { |
| 1024 | #[inline ] |
| 1025 | fn eq(&self, other: &TiVec<K, B>) -> bool { |
| 1026 | self.raw == other.raw |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | impl<K, A, B> PartialEq<TiSlice<K, B>> for TiVec<K, A> |
| 1031 | where |
| 1032 | A: PartialEq<B>, |
| 1033 | { |
| 1034 | #[inline ] |
| 1035 | fn eq(&self, other: &TiSlice<K, B>) -> bool { |
| 1036 | *self.raw == other.raw |
| 1037 | } |
| 1038 | } |
| 1039 | |
| 1040 | impl<K, A, B> PartialEq<TiVec<K, B>> for TiSlice<K, A> |
| 1041 | where |
| 1042 | A: PartialEq<B>, |
| 1043 | { |
| 1044 | #[inline ] |
| 1045 | fn eq(&self, other: &TiVec<K, B>) -> bool { |
| 1046 | self.raw == *other.raw |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | impl<'a, K, A, B> PartialEq<&'a TiSlice<K, B>> for TiVec<K, A> |
| 1051 | where |
| 1052 | A: PartialEq<B>, |
| 1053 | { |
| 1054 | #[inline ] |
| 1055 | fn eq(&self, other: &&'a TiSlice<K, B>) -> bool { |
| 1056 | *self.raw == other.raw |
| 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | impl<K, A, B> PartialEq<TiVec<K, B>> for &TiSlice<K, A> |
| 1061 | where |
| 1062 | A: PartialEq<B>, |
| 1063 | { |
| 1064 | #[inline ] |
| 1065 | fn eq(&self, other: &TiVec<K, B>) -> bool { |
| 1066 | self.raw == *other.raw |
| 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | impl<'a, K, A, B> PartialEq<&'a mut TiSlice<K, B>> for TiVec<K, A> |
| 1071 | where |
| 1072 | A: PartialEq<B>, |
| 1073 | { |
| 1074 | #[inline ] |
| 1075 | fn eq(&self, other: &&'a mut TiSlice<K, B>) -> bool { |
| 1076 | *self.raw == other.raw |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | impl<K, A, B> PartialEq<TiVec<K, B>> for &mut TiSlice<K, A> |
| 1081 | where |
| 1082 | A: PartialEq<B>, |
| 1083 | { |
| 1084 | #[inline ] |
| 1085 | fn eq(&self, other: &TiVec<K, B>) -> bool { |
| 1086 | self.raw == *other.raw |
| 1087 | } |
| 1088 | } |
| 1089 | |
| 1090 | impl<K, V> Ord for TiVec<K, V> |
| 1091 | where |
| 1092 | V: Ord, |
| 1093 | { |
| 1094 | #[inline ] |
| 1095 | fn cmp(&self, other: &Self) -> Ordering { |
| 1096 | self.raw.cmp(&other.raw) |
| 1097 | } |
| 1098 | } |
| 1099 | |
| 1100 | impl<K, V> PartialOrd<Self> for TiVec<K, V> |
| 1101 | where |
| 1102 | V: PartialOrd<V>, |
| 1103 | { |
| 1104 | #[inline ] |
| 1105 | fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
| 1106 | self.raw.partial_cmp(&other.raw) |
| 1107 | } |
| 1108 | } |
| 1109 | |
| 1110 | impl<K, V> Hash for TiVec<K, V> |
| 1111 | where |
| 1112 | V: Hash, |
| 1113 | { |
| 1114 | #[inline ] |
| 1115 | fn hash<H: Hasher>(&self, state: &mut H) { |
| 1116 | self.raw.hash(state); |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | impl<K, V> Default for TiVec<K, V> { |
| 1121 | #[inline ] |
| 1122 | fn default() -> Self { |
| 1123 | Vec::default().into() |
| 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | impl<I, K, V> Index<I> for TiVec<K, V> |
| 1128 | where |
| 1129 | I: TiSliceIndex<K, V>, |
| 1130 | { |
| 1131 | type Output = I::Output; |
| 1132 | |
| 1133 | #[inline ] |
| 1134 | fn index(&self, index: I) -> &Self::Output { |
| 1135 | index.index(self) |
| 1136 | } |
| 1137 | } |
| 1138 | |
| 1139 | impl<I, K, V> IndexMut<I> for TiVec<K, V> |
| 1140 | where |
| 1141 | I: TiSliceIndex<K, V>, |
| 1142 | { |
| 1143 | #[inline ] |
| 1144 | fn index_mut(&mut self, index: I) -> &mut Self::Output { |
| 1145 | index.index_mut(self) |
| 1146 | } |
| 1147 | } |
| 1148 | |
| 1149 | impl<K, V> Extend<V> for TiVec<K, V> { |
| 1150 | #[inline ] |
| 1151 | fn extend<I: IntoIterator<Item = V>>(&mut self, iter: I) { |
| 1152 | self.raw.extend(iter); |
| 1153 | } |
| 1154 | } |
| 1155 | |
| 1156 | impl<'a, K, V: 'a + Copy> Extend<&'a V> for TiVec<K, V> { |
| 1157 | #[inline ] |
| 1158 | fn extend<I: IntoIterator<Item = &'a V>>(&mut self, iter: I) { |
| 1159 | self.raw.extend(iter); |
| 1160 | } |
| 1161 | } |
| 1162 | |
| 1163 | impl<K, V> FromIterator<V> for TiVec<K, V> { |
| 1164 | #[inline ] |
| 1165 | fn from_iter<I: IntoIterator<Item = V>>(iter: I) -> Self { |
| 1166 | Self { |
| 1167 | raw: Vec::from_iter(iter), |
| 1168 | _marker: PhantomData, |
| 1169 | } |
| 1170 | } |
| 1171 | } |
| 1172 | |
| 1173 | impl<K, V> IntoIterator for TiVec<K, V> { |
| 1174 | type Item = V; |
| 1175 | type IntoIter = vec::IntoIter<V>; |
| 1176 | |
| 1177 | #[inline ] |
| 1178 | fn into_iter(self) -> vec::IntoIter<V> { |
| 1179 | self.raw.into_iter() |
| 1180 | } |
| 1181 | } |
| 1182 | |
| 1183 | impl<'a, K, V> IntoIterator for &'a TiVec<K, V> { |
| 1184 | type Item = &'a V; |
| 1185 | type IntoIter = slice::Iter<'a, V>; |
| 1186 | |
| 1187 | #[inline ] |
| 1188 | fn into_iter(self) -> slice::Iter<'a, V> { |
| 1189 | self.raw.iter() |
| 1190 | } |
| 1191 | } |
| 1192 | |
| 1193 | impl<'a, K, V> IntoIterator for &'a mut TiVec<K, V> { |
| 1194 | type Item = &'a mut V; |
| 1195 | type IntoIter = slice::IterMut<'a, V>; |
| 1196 | |
| 1197 | #[inline ] |
| 1198 | fn into_iter(self) -> slice::IterMut<'a, V> { |
| 1199 | self.raw.iter_mut() |
| 1200 | } |
| 1201 | } |
| 1202 | |
| 1203 | /// Write is implemented for `Vec<u8>` by appending to the vector. |
| 1204 | /// The vector will grow as needed. |
| 1205 | #[cfg (feature = "std" )] |
| 1206 | #[cfg_attr (docsrs, doc(cfg(feature = "std" )))] |
| 1207 | impl<K> Write for TiVec<K, u8> { |
| 1208 | #[inline ] |
| 1209 | fn write(&mut self, buf: &[u8]) -> IoResult<usize> { |
| 1210 | self.raw.write(buf) |
| 1211 | } |
| 1212 | |
| 1213 | #[inline ] |
| 1214 | fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> IoResult<usize> { |
| 1215 | self.raw.write_vectored(bufs) |
| 1216 | } |
| 1217 | |
| 1218 | #[inline ] |
| 1219 | fn write_all(&mut self, buf: &[u8]) -> IoResult<()> { |
| 1220 | self.raw.write_all(buf) |
| 1221 | } |
| 1222 | |
| 1223 | #[inline ] |
| 1224 | fn flush(&mut self) -> IoResult<()> { |
| 1225 | self.raw.flush() |
| 1226 | } |
| 1227 | } |
| 1228 | |
| 1229 | #[cfg (feature = "serde" )] |
| 1230 | #[cfg_attr (docsrs, doc(cfg(feature = "serde" )))] |
| 1231 | impl<K, V: Serialize> Serialize for TiVec<K, V> { |
| 1232 | #[inline ] |
| 1233 | fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { |
| 1234 | self.raw.as_slice().serialize(serializer) |
| 1235 | } |
| 1236 | } |
| 1237 | |
| 1238 | #[cfg (all(feature = "alloc" , feature = "serde" ))] |
| 1239 | #[cfg_attr (docsrs, doc(cfg(all(feature = "alloc" , feature = "serde" ))))] |
| 1240 | impl<'de, K, V: Deserialize<'de>> Deserialize<'de> for TiVec<K, V> { |
| 1241 | #[inline ] |
| 1242 | fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> { |
| 1243 | Vec::deserialize(deserializer).map(Into::into) |
| 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | #[expect ( |
| 1248 | dead_code, |
| 1249 | unused_imports, |
| 1250 | unused_mut, |
| 1251 | clippy::into_iter_on_ref, |
| 1252 | clippy::op_ref, |
| 1253 | clippy::too_many_lines, |
| 1254 | clippy::undocumented_unsafe_blocks, |
| 1255 | clippy::unwrap_used, |
| 1256 | reason = "okay in tests" |
| 1257 | )] |
| 1258 | #[cfg (test)] |
| 1259 | mod test { |
| 1260 | use alloc::borrow::{Cow, ToOwned}; |
| 1261 | use alloc::boxed::Box; |
| 1262 | use alloc::ffi::CString; |
| 1263 | use alloc::string::ToString; |
| 1264 | use alloc::vec::Vec; |
| 1265 | use core::borrow::{Borrow, BorrowMut}; |
| 1266 | use core::hash::{Hash, Hasher}; |
| 1267 | use core::ops::Bound; |
| 1268 | #[cfg (feature = "std" )] |
| 1269 | use std::hash::DefaultHasher; |
| 1270 | #[cfg (feature = "std" )] |
| 1271 | use std::io::{IoSlice, Write}; |
| 1272 | |
| 1273 | use crate::test_util::{AsSliceAndCapacity, Id}; |
| 1274 | use crate::{TiSlice, TiVec}; |
| 1275 | |
| 1276 | #[test ] |
| 1277 | fn test_vec_read_api_compatibility() { |
| 1278 | assert_eq!( |
| 1279 | TiVec::<Id, u32>::new().as_slice_and_capacity(), |
| 1280 | Vec::<u32>::new().as_slice_and_capacity(), |
| 1281 | ); |
| 1282 | for c in [0, 1, 2, 4] { |
| 1283 | assert_eq!( |
| 1284 | TiVec::<Id, u32>::with_capacity(c).as_slice_and_capacity(), |
| 1285 | Vec::<u32>::with_capacity(c).as_slice_and_capacity(), |
| 1286 | ); |
| 1287 | } |
| 1288 | |
| 1289 | for v in [ |
| 1290 | &[0_u32; 0][..], |
| 1291 | &[1], |
| 1292 | &[1, 1234], |
| 1293 | &[1, 2, 4], |
| 1294 | &[1, 5, 3, 2], |
| 1295 | &[1, 1, 9, 2, 4, 1, 12345, 12], |
| 1296 | ] { |
| 1297 | let cv = (v.to_vec(), TiVec::<Id, _>::from(v.to_vec())); |
| 1298 | let mut cv = (&cv.0, &cv.1); |
| 1299 | |
| 1300 | let mut mv = (v.to_vec(), TiVec::<Id, _>::from(v.to_vec())); |
| 1301 | let mut mv = (&mut mv.0, &mut mv.1); |
| 1302 | |
| 1303 | assert_eq_api!(cv, v => AsRef::<[_]>::as_ref(v)); |
| 1304 | assert_eq_api!(mv, v => AsMut::<[_]>::as_mut(v)); |
| 1305 | assert_eq_api!(cv, v => AsRef::<Vec<_>>::as_ref(v)); |
| 1306 | assert_eq_api!(mv, v => AsMut::<Vec<_>>::as_mut(v)); |
| 1307 | assert_eq_api!(cv, v => AsRef::<TiVec<_, _>>::as_ref(v)); |
| 1308 | assert_eq_api!(mv, v => AsMut::<TiVec<_, _>>::as_mut(v)); |
| 1309 | assert_eq!( |
| 1310 | AsRef::<[_]>::as_ref(cv.0), |
| 1311 | AsRef::<[_]>::as_ref(AsRef::<TiSlice<_, _>>::as_ref(cv.1)) |
| 1312 | ); |
| 1313 | assert_eq!( |
| 1314 | AsMut::<[_]>::as_mut(mv.0), |
| 1315 | AsMut::<[_]>::as_mut(AsMut::<TiSlice<_, _>>::as_mut(mv.1)) |
| 1316 | ); |
| 1317 | assert_eq!( |
| 1318 | Borrow::<[_]>::borrow(cv.0), |
| 1319 | AsRef::<[_]>::as_ref(Borrow::<TiSlice<_, _>>::borrow(cv.1)) |
| 1320 | ); |
| 1321 | assert_eq!( |
| 1322 | BorrowMut::<[_]>::borrow_mut(mv.0), |
| 1323 | AsMut::<[_]>::as_mut(BorrowMut::<TiSlice<_, _>>::borrow_mut(mv.1)) |
| 1324 | ); |
| 1325 | |
| 1326 | assert_eq_api!(cv, v => v.len()); |
| 1327 | assert_eq_api!(cv, v => v.is_empty()); |
| 1328 | assert_eq_api!(cv, v => v.capacity()); |
| 1329 | assert_eq_api!(cv, v => v.as_slice().into_std()); |
| 1330 | assert_eq_api!(mv, v => v.as_mut_slice().into_std()); |
| 1331 | assert_eq_api!(cv, v => TheVec::from(v.as_slice()).into_std()); |
| 1332 | assert_eq_api!(mv, v => TheVec::from(v.as_mut_slice()).into_std()); |
| 1333 | assert_eq_api!(cv, v => TheVec::from(Cow::Borrowed(v.as_slice())).into_std()); |
| 1334 | assert_eq_api!(mv, v => Cow::from(v.clone()).into_std()); |
| 1335 | |
| 1336 | if !v.is_empty() { |
| 1337 | assert_ne!(cv.0.as_ptr(), cv.1.as_ptr()); |
| 1338 | assert_ne!(cv.0.as_ptr_range(), cv.1.as_ptr_range()); |
| 1339 | assert_ne!(mv.0.as_mut_ptr(), mv.1.as_mut_ptr()); |
| 1340 | assert_ne!(mv.0.as_mut_ptr_range(), mv.1.as_mut_ptr_range()); |
| 1341 | } |
| 1342 | |
| 1343 | assert_eq_api!(cv, v => *v == TheVec::<u32>::default()); |
| 1344 | assert_eq_api!(cv, v => v == v.as_slice()); |
| 1345 | assert_eq_api!(cv, v => v.as_slice() == v); |
| 1346 | assert_eq_api!(cv, v => v == &v.as_slice()); |
| 1347 | assert_eq_api!(cv, v => &v.as_slice() == v); |
| 1348 | assert_eq_api!(mv, v => v == &(&mut [1_u32, 1234][..]).into_tic()); |
| 1349 | assert_eq_api!(mv, v => &(&mut [1_u32, 1234][..]).into_tic() == v); |
| 1350 | assert_eq_api!(cv, v => v.cmp(&alloc::vec![1, 1234].into_tic())); |
| 1351 | assert_eq_api!(cv, v => v.partial_cmp(&alloc::vec![1, 1234].into_tic())); |
| 1352 | |
| 1353 | for i in 0..v.len() { |
| 1354 | assert_eq_api!(cv, v => v[i.into_tic()]); |
| 1355 | assert_eq_api!(mv, v => v[i.into_tic()] = v[i.into_tic()]); |
| 1356 | } |
| 1357 | |
| 1358 | unsafe { |
| 1359 | assert_eq_api!(cv, v => { |
| 1360 | let mut v = core::mem::ManuallyDrop::new(v.clone()); |
| 1361 | TheVec::from_raw_parts(v.as_mut_ptr(), v.len(), v.capacity()).into_std() |
| 1362 | }); |
| 1363 | } |
| 1364 | } |
| 1365 | } |
| 1366 | |
| 1367 | #[test ] |
| 1368 | fn test_vec_write_api_compatibility() { |
| 1369 | for v in [ |
| 1370 | &[0_u32; 0][..], |
| 1371 | &[1], |
| 1372 | &[1, 1234], |
| 1373 | &[1, 2, 4], |
| 1374 | &[1, 5, 3, 2], |
| 1375 | &[1, 1, 9, 2, 4, 1, 12345, 12], |
| 1376 | ] { |
| 1377 | let mut mv = (v.to_vec(), TiVec::<Id, _>::from(v.to_vec())); |
| 1378 | let mut mv = (&mut mv.0, &mut mv.1); |
| 1379 | |
| 1380 | let restore = |mv: &mut (&mut Vec<u32>, &mut TiVec<Id, u32>)| { |
| 1381 | *mv.0 = v.to_vec(); |
| 1382 | *mv.1 = TiVec::from(v.to_vec()); |
| 1383 | }; |
| 1384 | |
| 1385 | restore(&mut mv); |
| 1386 | assert_eq_api!(mv, v => v.try_reserve(usize::MAX)); |
| 1387 | restore(&mut mv); |
| 1388 | assert_eq_api!(mv, v => v.try_reserve_exact(usize::MAX)); |
| 1389 | |
| 1390 | for i in 0..8 { |
| 1391 | restore(&mut mv); |
| 1392 | assert_eq_api!(mv, v => v.resize(i, 123)); |
| 1393 | restore(&mut mv); |
| 1394 | assert_eq_api!(mv, v => { let mut a = 1; v.resize_with(i, || { a *= 2; a }) }); |
| 1395 | restore(&mut mv); |
| 1396 | assert_eq_api!(mv, v => v.reserve(i)); |
| 1397 | assert_eq_api!(mv, v => v.spare_capacity_mut().len()); |
| 1398 | restore(&mut mv); |
| 1399 | assert_eq_api!(mv, v => v.try_reserve(i)); |
| 1400 | restore(&mut mv); |
| 1401 | assert_eq_api!(mv, v => v.reserve_exact(i)); |
| 1402 | restore(&mut mv); |
| 1403 | assert_eq_api!(mv, v => v.try_reserve_exact(i)); |
| 1404 | restore(&mut mv); |
| 1405 | assert_eq_api!(mv, v => v.reserve_exact(i)); |
| 1406 | assert_eq_api!(mv, v => v.shrink_to_fit()); |
| 1407 | restore(&mut mv); |
| 1408 | assert_eq_api!(mv, v => v.reserve_exact(i * 2)); |
| 1409 | assert_eq_api!(mv, v => v.shrink_to(i)); |
| 1410 | restore(&mut mv); |
| 1411 | assert_eq_api!(mv, v => v.truncate(i)); |
| 1412 | } |
| 1413 | |
| 1414 | let l1: Vec<_> = mv.0.clone(); |
| 1415 | let l1c = l1.capacity(); |
| 1416 | let l1 = l1.leak(); |
| 1417 | let l2: TiVec<_, _> = mv.1.clone(); |
| 1418 | let l2c = l2.capacity(); |
| 1419 | let l2 = l2.leak(); |
| 1420 | assert_eq!(l1, &l2.raw); |
| 1421 | drop(unsafe { Vec::from_raw_parts(l1.as_mut_ptr(), l1.len(), l1c) }); |
| 1422 | drop(unsafe { TiVec::<Id, _>::from_raw_parts(l2.as_mut_ptr(), l2.len(), l2c) }); |
| 1423 | |
| 1424 | restore(&mut mv); |
| 1425 | assert_eq_api!(mv, v => (&*v).into_iter().copied().collect::<Vec<_>>()); |
| 1426 | assert_eq_api!(mv, v => v.iter_mut().collect::<Vec<_>>()); |
| 1427 | assert_eq_api!(mv, v => v.clone().into_iter().collect::<Vec<_>>()); |
| 1428 | |
| 1429 | restore(&mut mv); |
| 1430 | assert_eq_api!(mv, v => v.pop()); |
| 1431 | assert_eq_api!(mv, v => v.push(123)); |
| 1432 | assert_eq_api!(mv, v => v.pop()); |
| 1433 | assert_eq_api!(mv, v => v.append(&mut v.clone())); |
| 1434 | restore(&mut mv); |
| 1435 | assert_eq_api!(mv, v => v.extend(v.clone().as_slice())); |
| 1436 | restore(&mut mv); |
| 1437 | assert_eq_api!(mv, v => v.extend(v.clone().iter().copied())); |
| 1438 | restore(&mut mv); |
| 1439 | assert_eq_api!(mv, v => v.extend_from_slice(&v.clone())); |
| 1440 | restore(&mut mv); |
| 1441 | assert_eq_api!(mv, v => v.into_iter().collect::<TheVec<_>>().into_std()); |
| 1442 | |
| 1443 | restore(&mut mv); |
| 1444 | assert_eq_api!(mv, v => v.retain(|value| value % 3 == 0 || value % 4 == 0)); |
| 1445 | |
| 1446 | restore(&mut mv); |
| 1447 | assert_eq_api!(mv, v => v.retain_mut(|value| { |
| 1448 | *value += 1; |
| 1449 | *value % 3 == 0 || *value % 4 == 0 |
| 1450 | })); |
| 1451 | |
| 1452 | restore(&mut mv); |
| 1453 | assert_eq_api!(mv, v => v.dedup()); |
| 1454 | |
| 1455 | restore(&mut mv); |
| 1456 | assert_eq_api!(mv, v => v.dedup_by(|lhs, rhs| lhs < rhs)); |
| 1457 | |
| 1458 | restore(&mut mv); |
| 1459 | assert_eq_api!(mv, v => v.dedup_by_key(|value| *value % 3)); |
| 1460 | |
| 1461 | for i in 0..v.len() { |
| 1462 | restore(&mut mv); |
| 1463 | assert_eq_api!(mv, v => v.swap_remove(i.into_tic())); |
| 1464 | restore(&mut mv); |
| 1465 | assert_eq_api!(mv, v => v.insert(i.into_tic(), 123)); |
| 1466 | restore(&mut mv); |
| 1467 | assert_eq_api!(mv, v => v.remove(i.into_tic())); |
| 1468 | restore(&mut mv); |
| 1469 | unsafe { assert_eq_api!(mv, v => v.set_len(i)) }; |
| 1470 | restore(&mut mv); |
| 1471 | assert_eq_api!(mv, v => v.split_off(i.into_tic()).into_std()); |
| 1472 | } |
| 1473 | |
| 1474 | for a in 0..v.len() { |
| 1475 | for b in a..v.len() { |
| 1476 | restore(&mut mv); |
| 1477 | assert_eq_api!(mv, v => v.drain((a..b).into_tic()).collect::<Vec<_>>()); |
| 1478 | restore(&mut mv); |
| 1479 | assert_eq_api!(mv, v => v.extend_from_within(a..b)); |
| 1480 | restore(&mut mv); |
| 1481 | assert_eq_api!( |
| 1482 | mv, v => v.splice((a..b).into_tic(), [1, 2, 3]).collect::<Vec<_>>() |
| 1483 | ); |
| 1484 | } |
| 1485 | } |
| 1486 | restore(&mut mv); |
| 1487 | assert_eq_api!(mv, v => v.splice(.., [1, 2, 3]).collect::<Vec<_>>()); |
| 1488 | |
| 1489 | restore(&mut mv); |
| 1490 | assert_eq_api!(mv, v => v.clear()); |
| 1491 | } |
| 1492 | } |
| 1493 | |
| 1494 | #[cfg (feature = "std" )] |
| 1495 | #[test ] |
| 1496 | fn test_vec_hash_compatibility() { |
| 1497 | for v in [ |
| 1498 | &[0_u32; 0][..], |
| 1499 | &[1], |
| 1500 | &[1, 1234], |
| 1501 | &[1, 2, 4], |
| 1502 | &[1, 5, 3, 2], |
| 1503 | &[1, 1, 9, 2, 4, 1, 12345, 12], |
| 1504 | ] { |
| 1505 | let cv = (v.to_vec(), TiVec::<Id, _>::from(v.to_vec())); |
| 1506 | let mut cv = (&cv.0, &cv.1); |
| 1507 | assert_eq_api!(cv, v => { |
| 1508 | let mut hasher = DefaultHasher::new(); |
| 1509 | v.hash(&mut hasher); |
| 1510 | hasher.finish() |
| 1511 | }); |
| 1512 | } |
| 1513 | } |
| 1514 | |
| 1515 | #[test ] |
| 1516 | fn test_u8_vec_api_compatibility() { |
| 1517 | assert_eq!( |
| 1518 | Vec::from(TiVec::<Id, u8>::from("abc" )), |
| 1519 | Vec::<u8>::from("abc" ), |
| 1520 | ); |
| 1521 | assert_eq!( |
| 1522 | Vec::from(TiVec::<Id, u8>::from("abc" .to_owned())), |
| 1523 | Vec::<u8>::from("abc" .to_owned()), |
| 1524 | ); |
| 1525 | assert_eq!( |
| 1526 | Vec::from(TiVec::<Id, u8>::from(CString::new("abc" ).unwrap())), |
| 1527 | Vec::<u8>::from(CString::new("abc" ).unwrap()), |
| 1528 | ); |
| 1529 | |
| 1530 | for v in [&b"abc" [..], b"aBc" , b"ABC" , b"abd" , b"a \x80\x81b" ] { |
| 1531 | let cv = (v.to_vec(), TiVec::<Id, _>::from(v.to_vec())); |
| 1532 | let mut cv = (&cv.0, &cv.1); |
| 1533 | |
| 1534 | assert_eq_api!(cv, v => TheVec::from(v.as_slice()).into_std()); |
| 1535 | } |
| 1536 | } |
| 1537 | |
| 1538 | #[test ] |
| 1539 | fn test_vec_debug() { |
| 1540 | let s0: TiVec<Id, u32> = TiVec::from(alloc::vec![]); |
| 1541 | let s1: TiVec<Id, u32> = TiVec::from(alloc::vec![12]); |
| 1542 | let s2: TiVec<Id, u32> = TiVec::from(alloc::vec![23, 34]); |
| 1543 | assert_eq!(&alloc::format!("{s0:?}" ), "{}" ); |
| 1544 | assert_eq!(&alloc::format!("{s1:?}" ), "{Id(0): 12}" ); |
| 1545 | assert_eq!(&alloc::format!("{s2:?}" ), "{Id(0): 23, Id(1): 34}" ); |
| 1546 | } |
| 1547 | |
| 1548 | #[cfg (feature = "std" )] |
| 1549 | #[test ] |
| 1550 | fn test_vec_write() { |
| 1551 | let mut mv = (Vec::<u8>::new(), TiVec::<Id, u8>::new()); |
| 1552 | let mut mv = (&mut mv.0, &mut mv.1); |
| 1553 | |
| 1554 | assert_eq_api!(mv, v => v.write(&[1, 2, 3]).unwrap()); |
| 1555 | assert_eq_api!(mv, v => v.write_vectored( |
| 1556 | &[IoSlice::new(&[1, 2, 3]), IoSlice::new(&[4, 5])] |
| 1557 | ).unwrap()); |
| 1558 | assert_eq_api!(mv, v => v.write_all(&[1, 2, 3]).unwrap()); |
| 1559 | assert_eq_api!(mv, v => v.flush().unwrap()); |
| 1560 | } |
| 1561 | |
| 1562 | #[cfg (feature = "serde" )] |
| 1563 | #[test ] |
| 1564 | fn test_vec_serialize() { |
| 1565 | let s0: TiVec<Id, u32> = TiVec::from(alloc::vec![]); |
| 1566 | let s1: TiVec<Id, u32> = TiVec::from(alloc::vec![12]); |
| 1567 | let s2: TiVec<Id, u32> = TiVec::from(alloc::vec![23, 34]); |
| 1568 | assert_eq!(&serde_json::to_string(&s0).unwrap(), "[]" ); |
| 1569 | assert_eq!(&serde_json::to_string(&s1).unwrap(), "[12]" ); |
| 1570 | assert_eq!(&serde_json::to_string(&s2).unwrap(), "[23,34]" ); |
| 1571 | } |
| 1572 | |
| 1573 | #[cfg (feature = "serde" )] |
| 1574 | #[test ] |
| 1575 | fn test_vec_deserialize() { |
| 1576 | let s0: TiVec<Id, u32> = serde_json::from_str("[]" ).unwrap(); |
| 1577 | let s1: TiVec<Id, u32> = serde_json::from_str("[12]" ).unwrap(); |
| 1578 | let s2: TiVec<Id, u32> = serde_json::from_str("[23, 34]" ).unwrap(); |
| 1579 | assert_eq!(s0.as_slice().raw, [0; 0][..]); |
| 1580 | assert_eq!(s1.as_slice().raw, [12][..]); |
| 1581 | assert_eq!(s2.as_slice().raw, [23, 34][..]); |
| 1582 | } |
| 1583 | } |
| 1584 | |