| 1 | #![ allow(unused_variables)] | 
| 2 | #![ allow(missing_docs)] | 
|---|
| 3 |  | 
|---|
| 4 | use super::*; | 
|---|
| 5 |  | 
|---|
| 6 | /// A slice-backed vector-like data structure. | 
|---|
| 7 | /// | 
|---|
| 8 | /// This is a very similar concept to `ArrayVec`, but instead | 
|---|
| 9 | /// of the backing memory being an owned array, the backing | 
|---|
| 10 | /// memory is a unique-borrowed slice. You can thus create | 
|---|
| 11 | /// one of these structures "around" some slice that you're | 
|---|
| 12 | /// working with to make it easier to manipulate. | 
|---|
| 13 | /// | 
|---|
| 14 | /// * Has a fixed capacity (the initial slice size). | 
|---|
| 15 | /// * Has a variable length. | 
|---|
| 16 | pub struct SliceVec<'s, T> { | 
|---|
| 17 | data: &'s mut [T], | 
|---|
| 18 | len: usize, | 
|---|
| 19 | } | 
|---|
| 20 |  | 
|---|
| 21 | impl<'s, T> Default for SliceVec<'s, T> { | 
|---|
| 22 | #[ inline(always)] | 
|---|
| 23 | #[ must_use] | 
|---|
| 24 | fn default() -> Self { | 
|---|
| 25 | Self { data: &mut [], len: 0 } | 
|---|
| 26 | } | 
|---|
| 27 | } | 
|---|
| 28 |  | 
|---|
| 29 | impl<'s, T> Deref for SliceVec<'s, T> { | 
|---|
| 30 | type Target = [T]; | 
|---|
| 31 | #[ inline(always)] | 
|---|
| 32 | #[ must_use] | 
|---|
| 33 | fn deref(&self) -> &Self::Target { | 
|---|
| 34 | &self.data[..self.len] | 
|---|
| 35 | } | 
|---|
| 36 | } | 
|---|
| 37 |  | 
|---|
| 38 | impl<'s, T> DerefMut for SliceVec<'s, T> { | 
|---|
| 39 | #[ inline(always)] | 
|---|
| 40 | #[ must_use] | 
|---|
| 41 | fn deref_mut(&mut self) -> &mut Self::Target { | 
|---|
| 42 | &mut self.data[..self.len] | 
|---|
| 43 | } | 
|---|
| 44 | } | 
|---|
| 45 |  | 
|---|
| 46 | impl<'s, T, I> Index<I> for SliceVec<'s, T> | 
|---|
| 47 | where | 
|---|
| 48 | I: SliceIndex<[T]>, | 
|---|
| 49 | { | 
|---|
| 50 | type Output = <I as SliceIndex<[T]>>::Output; | 
|---|
| 51 | #[ inline(always)] | 
|---|
| 52 | #[ must_use] | 
|---|
| 53 | fn index(&self, index: I) -> &Self::Output { | 
|---|
| 54 | &self.deref()[index] | 
|---|
| 55 | } | 
|---|
| 56 | } | 
|---|
| 57 |  | 
|---|
| 58 | impl<'s, T, I> IndexMut<I> for SliceVec<'s, T> | 
|---|
| 59 | where | 
|---|
| 60 | I: SliceIndex<[T]>, | 
|---|
| 61 | { | 
|---|
| 62 | #[ inline(always)] | 
|---|
| 63 | #[ must_use] | 
|---|
| 64 | fn index_mut(&mut self, index: I) -> &mut Self::Output { | 
|---|
| 65 | &mut self.deref_mut()[index] | 
|---|
| 66 | } | 
|---|
| 67 | } | 
|---|
| 68 |  | 
|---|
| 69 | impl<'s, T> SliceVec<'s, T> { | 
|---|
| 70 | #[ inline] | 
|---|
| 71 | pub fn append(&mut self, other: &mut Self) | 
|---|
| 72 | where | 
|---|
| 73 | T: Default, | 
|---|
| 74 | { | 
|---|
| 75 | for item in other.drain(..) { | 
|---|
| 76 | self.push(item) | 
|---|
| 77 | } | 
|---|
| 78 | } | 
|---|
| 79 |  | 
|---|
| 80 | /// A `*mut` pointer to the backing slice. | 
|---|
| 81 | /// | 
|---|
| 82 | /// ## Safety | 
|---|
| 83 | /// | 
|---|
| 84 | /// This pointer has provenance over the _entire_ backing slice. | 
|---|
| 85 | #[ inline(always)] | 
|---|
| 86 | #[ must_use] | 
|---|
| 87 | pub fn as_mut_ptr(&mut self) -> *mut T { | 
|---|
| 88 | self.data.as_mut_ptr() | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | /// Performs a `deref_mut`, into unique slice form. | 
|---|
| 92 | #[ inline(always)] | 
|---|
| 93 | #[ must_use] | 
|---|
| 94 | pub fn as_mut_slice(&mut self) -> &mut [T] { | 
|---|
| 95 | self.deref_mut() | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 | /// A `*const` pointer to the backing slice. | 
|---|
| 99 | /// | 
|---|
| 100 | /// ## Safety | 
|---|
| 101 | /// | 
|---|
| 102 | /// This pointer has provenance over the _entire_ backing slice. | 
|---|
| 103 | #[ inline(always)] | 
|---|
| 104 | #[ must_use] | 
|---|
| 105 | pub fn as_ptr(&self) -> *const T { | 
|---|
| 106 | self.data.as_ptr() | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 | /// Performs a `deref`, into shared slice form. | 
|---|
| 110 | #[ inline(always)] | 
|---|
| 111 | #[ must_use] | 
|---|
| 112 | pub fn as_slice(&self) -> &[T] { | 
|---|
| 113 | self.deref() | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 | /// The capacity of the `SliceVec`. | 
|---|
| 117 | /// | 
|---|
| 118 | /// This the length of the initial backing slice. | 
|---|
| 119 | #[ inline(always)] | 
|---|
| 120 | #[ must_use] | 
|---|
| 121 | pub fn capacity(&self) -> usize { | 
|---|
| 122 | self.data.len() | 
|---|
| 123 | } | 
|---|
| 124 |  | 
|---|
| 125 | /// Truncates the `SliceVec` down to length 0. | 
|---|
| 126 | #[ inline(always)] | 
|---|
| 127 | pub fn clear(&mut self) | 
|---|
| 128 | where | 
|---|
| 129 | T: Default, | 
|---|
| 130 | { | 
|---|
| 131 | self.truncate(0) | 
|---|
| 132 | } | 
|---|
| 133 |  | 
|---|
| 134 | /// Creates a draining iterator that removes the specified range in the vector | 
|---|
| 135 | /// and yields the removed items. | 
|---|
| 136 | /// | 
|---|
| 137 | /// ## Panics | 
|---|
| 138 | /// * If the start is greater than the end | 
|---|
| 139 | /// * If the end is past the edge of the vec. | 
|---|
| 140 | /// | 
|---|
| 141 | /// ## Example | 
|---|
| 142 | /// ```rust | 
|---|
| 143 | /// # use tinyvec::*; | 
|---|
| 144 | /// let mut arr = [6, 7, 8]; | 
|---|
| 145 | /// let mut sv = SliceVec::from(&mut arr); | 
|---|
| 146 | /// let drained_values: ArrayVec<[i32; 4]> = sv.drain(1..).collect(); | 
|---|
| 147 | /// assert_eq!(sv.as_slice(), &[6][..]); | 
|---|
| 148 | /// assert_eq!(drained_values.as_slice(), &[7, 8][..]); | 
|---|
| 149 | /// | 
|---|
| 150 | /// sv.drain(..); | 
|---|
| 151 | /// assert_eq!(sv.as_slice(), &[]); | 
|---|
| 152 | /// ``` | 
|---|
| 153 | #[ inline] | 
|---|
| 154 | pub fn drain<'p, R: RangeBounds<usize>>( | 
|---|
| 155 | &'p mut self, range: R, | 
|---|
| 156 | ) -> SliceVecDrain<'p, 's, T> | 
|---|
| 157 | where | 
|---|
| 158 | T: Default, | 
|---|
| 159 | { | 
|---|
| 160 | use core::ops::Bound; | 
|---|
| 161 | let start = match range.start_bound() { | 
|---|
| 162 | Bound::Included(x) => *x, | 
|---|
| 163 | Bound::Excluded(x) => x.saturating_add(1), | 
|---|
| 164 | Bound::Unbounded => 0, | 
|---|
| 165 | }; | 
|---|
| 166 | let end = match range.end_bound() { | 
|---|
| 167 | Bound::Included(x) => x.saturating_add(1), | 
|---|
| 168 | Bound::Excluded(x) => *x, | 
|---|
| 169 | Bound::Unbounded => self.len, | 
|---|
| 170 | }; | 
|---|
| 171 | assert!( | 
|---|
| 172 | start <= end, | 
|---|
| 173 | "SliceVec::drain> Illegal range, {}  to {} ", | 
|---|
| 174 | start, | 
|---|
| 175 | end | 
|---|
| 176 | ); | 
|---|
| 177 | assert!( | 
|---|
| 178 | end <= self.len, | 
|---|
| 179 | "SliceVec::drain> Range ends at {}  but length is only {} !", | 
|---|
| 180 | end, | 
|---|
| 181 | self.len | 
|---|
| 182 | ); | 
|---|
| 183 | SliceVecDrain { | 
|---|
| 184 | parent: self, | 
|---|
| 185 | target_start: start, | 
|---|
| 186 | target_index: start, | 
|---|
| 187 | target_end: end, | 
|---|
| 188 | } | 
|---|
| 189 | } | 
|---|
| 190 |  | 
|---|
| 191 | #[ inline] | 
|---|
| 192 | pub fn extend_from_slice(&mut self, sli: &[T]) | 
|---|
| 193 | where | 
|---|
| 194 | T: Clone, | 
|---|
| 195 | { | 
|---|
| 196 | if sli.is_empty() { | 
|---|
| 197 | return; | 
|---|
| 198 | } | 
|---|
| 199 |  | 
|---|
| 200 | let new_len = self.len + sli.len(); | 
|---|
| 201 | if new_len > self.capacity() { | 
|---|
| 202 | panic!( | 
|---|
| 203 | "SliceVec::extend_from_slice> total length {}  exceeds capacity {} ", | 
|---|
| 204 | new_len, | 
|---|
| 205 | self.capacity() | 
|---|
| 206 | ) | 
|---|
| 207 | } | 
|---|
| 208 |  | 
|---|
| 209 | let target = &mut self.data[self.len..new_len]; | 
|---|
| 210 | target.clone_from_slice(sli); | 
|---|
| 211 | self.set_len(new_len); | 
|---|
| 212 | } | 
|---|
| 213 |  | 
|---|
| 214 | /// Fill the vector until its capacity has been reached. | 
|---|
| 215 | /// | 
|---|
| 216 | /// Successively fills unused space in the spare slice of the vector with | 
|---|
| 217 | /// elements from the iterator. It then returns the remaining iterator | 
|---|
| 218 | /// without exhausting it. This also allows appending the head of an | 
|---|
| 219 | /// infinite iterator. | 
|---|
| 220 | /// | 
|---|
| 221 | /// This is an alternative to `Extend::extend` method for cases where the | 
|---|
| 222 | /// length of the iterator can not be checked. Since this vector can not | 
|---|
| 223 | /// reallocate to increase its capacity, it is unclear what to do with | 
|---|
| 224 | /// remaining elements in the iterator and the iterator itself. The | 
|---|
| 225 | /// interface also provides no way to communicate this to the caller. | 
|---|
| 226 | /// | 
|---|
| 227 | /// ## Panics | 
|---|
| 228 | /// * If the `next` method of the provided iterator panics. | 
|---|
| 229 | /// | 
|---|
| 230 | /// ## Example | 
|---|
| 231 | /// | 
|---|
| 232 | /// ```rust | 
|---|
| 233 | /// # use tinyvec::*; | 
|---|
| 234 | /// let mut arr = [7, 7, 7, 7]; | 
|---|
| 235 | /// let mut sv = SliceVec::from_slice_len(&mut arr, 0); | 
|---|
| 236 | /// let mut to_inf = sv.fill(0..); | 
|---|
| 237 | /// assert_eq!(&sv[..], [0, 1, 2, 3]); | 
|---|
| 238 | /// assert_eq!(to_inf.next(), Some(4)); | 
|---|
| 239 | /// ``` | 
|---|
| 240 | #[ inline] | 
|---|
| 241 | pub fn fill<I: IntoIterator<Item = T>>(&mut self, iter: I) -> I::IntoIter { | 
|---|
| 242 | let mut iter = iter.into_iter(); | 
|---|
| 243 | for element in iter.by_ref().take(self.capacity() - self.len()) { | 
|---|
| 244 | self.push(element); | 
|---|
| 245 | } | 
|---|
| 246 | iter | 
|---|
| 247 | } | 
|---|
| 248 |  | 
|---|
| 249 | /// Wraps up a slice and uses the given length as the initial length. | 
|---|
| 250 | /// | 
|---|
| 251 | /// If you want to simply use the full slice, use `from` instead. | 
|---|
| 252 | /// | 
|---|
| 253 | /// ## Panics | 
|---|
| 254 | /// | 
|---|
| 255 | /// * The length specified must be less than or equal to the capacity of the | 
|---|
| 256 | ///   slice. | 
|---|
| 257 | #[ inline] | 
|---|
| 258 | #[ must_use] | 
|---|
| 259 | #[ allow(clippy::match_wild_err_arm)] | 
|---|
| 260 | pub fn from_slice_len(data: &'s mut [T], len: usize) -> Self { | 
|---|
| 261 | assert!(len <= data.len()); | 
|---|
| 262 | Self { data, len } | 
|---|
| 263 | } | 
|---|
| 264 |  | 
|---|
| 265 | /// Inserts an item at the position given, moving all following elements +1 | 
|---|
| 266 | /// index. | 
|---|
| 267 | /// | 
|---|
| 268 | /// ## Panics | 
|---|
| 269 | /// * If `index` > `len` | 
|---|
| 270 | /// * If the capacity is exhausted | 
|---|
| 271 | /// | 
|---|
| 272 | /// ## Example | 
|---|
| 273 | /// ```rust | 
|---|
| 274 | /// # use tinyvec::*; | 
|---|
| 275 | /// let mut arr = [1, 2, 3, 0, 0]; | 
|---|
| 276 | /// let mut sv = SliceVec::from_slice_len(&mut arr, 3); | 
|---|
| 277 | /// sv.insert(1, 4); | 
|---|
| 278 | /// assert_eq!(sv.as_slice(), &[1, 4, 2, 3]); | 
|---|
| 279 | /// sv.insert(4, 5); | 
|---|
| 280 | /// assert_eq!(sv.as_slice(), &[1, 4, 2, 3, 5]); | 
|---|
| 281 | /// ``` | 
|---|
| 282 | #[ inline] | 
|---|
| 283 | pub fn insert(&mut self, index: usize, item: T) { | 
|---|
| 284 | if index > self.len { | 
|---|
| 285 | panic!( "SliceVec::insert> index {}  is out of bounds {} ", index, self.len); | 
|---|
| 286 | } | 
|---|
| 287 |  | 
|---|
| 288 | // Try to push the element. | 
|---|
| 289 | self.push(item); | 
|---|
| 290 | // And move it into its place. | 
|---|
| 291 | self.as_mut_slice()[index..].rotate_right(1); | 
|---|
| 292 | } | 
|---|
| 293 |  | 
|---|
| 294 | /// Checks if the length is 0. | 
|---|
| 295 | #[ inline(always)] | 
|---|
| 296 | #[ must_use] | 
|---|
| 297 | pub fn is_empty(&self) -> bool { | 
|---|
| 298 | self.len == 0 | 
|---|
| 299 | } | 
|---|
| 300 |  | 
|---|
| 301 | /// The length of the `SliceVec` (in elements). | 
|---|
| 302 | #[ inline(always)] | 
|---|
| 303 | #[ must_use] | 
|---|
| 304 | pub fn len(&self) -> usize { | 
|---|
| 305 | self.len | 
|---|
| 306 | } | 
|---|
| 307 |  | 
|---|
| 308 | /// Remove and return the last element of the vec, if there is one. | 
|---|
| 309 | /// | 
|---|
| 310 | /// ## Failure | 
|---|
| 311 | /// * If the vec is empty you get `None`. | 
|---|
| 312 | /// | 
|---|
| 313 | /// ## Example | 
|---|
| 314 | /// ```rust | 
|---|
| 315 | /// # use tinyvec::*; | 
|---|
| 316 | /// let mut arr = [1, 2]; | 
|---|
| 317 | /// let mut sv = SliceVec::from(&mut arr); | 
|---|
| 318 | /// assert_eq!(sv.pop(), Some(2)); | 
|---|
| 319 | /// assert_eq!(sv.pop(), Some(1)); | 
|---|
| 320 | /// assert_eq!(sv.pop(), None); | 
|---|
| 321 | /// ``` | 
|---|
| 322 | #[ inline] | 
|---|
| 323 | pub fn pop(&mut self) -> Option<T> | 
|---|
| 324 | where | 
|---|
| 325 | T: Default, | 
|---|
| 326 | { | 
|---|
| 327 | if self.len > 0 { | 
|---|
| 328 | self.len -= 1; | 
|---|
| 329 | let out = core::mem::take(&mut self.data[self.len]); | 
|---|
| 330 | Some(out) | 
|---|
| 331 | } else { | 
|---|
| 332 | None | 
|---|
| 333 | } | 
|---|
| 334 | } | 
|---|
| 335 |  | 
|---|
| 336 | /// Place an element onto the end of the vec. | 
|---|
| 337 | /// | 
|---|
| 338 | /// ## Panics | 
|---|
| 339 | /// * If the length of the vec would overflow the capacity. | 
|---|
| 340 | /// | 
|---|
| 341 | /// ## Example | 
|---|
| 342 | /// ```rust | 
|---|
| 343 | /// # use tinyvec::*; | 
|---|
| 344 | /// let mut arr = [0, 0]; | 
|---|
| 345 | /// let mut sv = SliceVec::from_slice_len(&mut arr, 0); | 
|---|
| 346 | /// assert_eq!(&sv[..], []); | 
|---|
| 347 | /// sv.push(1); | 
|---|
| 348 | /// assert_eq!(&sv[..], [1]); | 
|---|
| 349 | /// sv.push(2); | 
|---|
| 350 | /// assert_eq!(&sv[..], [1, 2]); | 
|---|
| 351 | /// // sv.push(3); this would overflow the ArrayVec and panic! | 
|---|
| 352 | /// ``` | 
|---|
| 353 | #[ inline(always)] | 
|---|
| 354 | pub fn push(&mut self, val: T) { | 
|---|
| 355 | if self.len < self.capacity() { | 
|---|
| 356 | self.data[self.len] = val; | 
|---|
| 357 | self.len += 1; | 
|---|
| 358 | } else { | 
|---|
| 359 | panic!( "SliceVec::push> capacity overflow") | 
|---|
| 360 | } | 
|---|
| 361 | } | 
|---|
| 362 |  | 
|---|
| 363 | /// Removes the item at `index`, shifting all others down by one index. | 
|---|
| 364 | /// | 
|---|
| 365 | /// Returns the removed element. | 
|---|
| 366 | /// | 
|---|
| 367 | /// ## Panics | 
|---|
| 368 | /// | 
|---|
| 369 | /// * If the index is out of bounds. | 
|---|
| 370 | /// | 
|---|
| 371 | /// ## Example | 
|---|
| 372 | /// | 
|---|
| 373 | /// ```rust | 
|---|
| 374 | /// # use tinyvec::*; | 
|---|
| 375 | /// let mut arr = [1, 2, 3]; | 
|---|
| 376 | /// let mut sv = SliceVec::from(&mut arr); | 
|---|
| 377 | /// assert_eq!(sv.remove(1), 2); | 
|---|
| 378 | /// assert_eq!(&sv[..], [1, 3]); | 
|---|
| 379 | /// ``` | 
|---|
| 380 | #[ inline] | 
|---|
| 381 | pub fn remove(&mut self, index: usize) -> T | 
|---|
| 382 | where | 
|---|
| 383 | T: Default, | 
|---|
| 384 | { | 
|---|
| 385 | let targets: &mut [T] = &mut self.deref_mut()[index..]; | 
|---|
| 386 | let item = core::mem::take(&mut targets[0]); | 
|---|
| 387 | targets.rotate_left(1); | 
|---|
| 388 | self.len -= 1; | 
|---|
| 389 | item | 
|---|
| 390 | } | 
|---|
| 391 |  | 
|---|
| 392 | /// As [`resize_with`](SliceVec::resize_with) | 
|---|
| 393 | /// and it clones the value as the closure. | 
|---|
| 394 | /// | 
|---|
| 395 | /// ## Example | 
|---|
| 396 | /// | 
|---|
| 397 | /// ```rust | 
|---|
| 398 | /// # use tinyvec::*; | 
|---|
| 399 | /// // bigger | 
|---|
| 400 | /// let mut arr = [ "hello", "", "", "", ""]; | 
|---|
| 401 | /// let mut sv = SliceVec::from_slice_len(&mut arr, 1); | 
|---|
| 402 | /// sv.resize(3, "world"); | 
|---|
| 403 | /// assert_eq!(&sv[..], [ "hello", "world", "world"]); | 
|---|
| 404 | /// | 
|---|
| 405 | /// // smaller | 
|---|
| 406 | /// let mut arr = [ 'a', 'b', 'c', 'd']; | 
|---|
| 407 | /// let mut sv = SliceVec::from(&mut arr); | 
|---|
| 408 | /// sv.resize(2, 'z'); | 
|---|
| 409 | /// assert_eq!(&sv[..], [ 'a', 'b']); | 
|---|
| 410 | /// ``` | 
|---|
| 411 | #[ inline] | 
|---|
| 412 | pub fn resize(&mut self, new_len: usize, new_val: T) | 
|---|
| 413 | where | 
|---|
| 414 | T: Clone, | 
|---|
| 415 | { | 
|---|
| 416 | self.resize_with(new_len, || new_val.clone()) | 
|---|
| 417 | } | 
|---|
| 418 |  | 
|---|
| 419 | /// Resize the vec to the new length. | 
|---|
| 420 | /// | 
|---|
| 421 | /// * If it needs to be longer, it's filled with repeated calls to the | 
|---|
| 422 | ///   provided function. | 
|---|
| 423 | /// * If it needs to be shorter, it's truncated. | 
|---|
| 424 | ///   * If the type needs to drop the truncated slots are filled with calls to | 
|---|
| 425 | ///     the provided function. | 
|---|
| 426 | /// | 
|---|
| 427 | /// ## Example | 
|---|
| 428 | /// | 
|---|
| 429 | /// ```rust | 
|---|
| 430 | /// # use tinyvec::*; | 
|---|
| 431 | /// let mut arr = [1, 2, 3, 7, 7, 7, 7]; | 
|---|
| 432 | /// let mut sv = SliceVec::from_slice_len(&mut arr, 3); | 
|---|
| 433 | /// sv.resize_with(5, Default::default); | 
|---|
| 434 | /// assert_eq!(&sv[..], [1, 2, 3, 0, 0]); | 
|---|
| 435 | /// | 
|---|
| 436 | /// let mut arr = [0, 0, 0, 0]; | 
|---|
| 437 | /// let mut sv = SliceVec::from_slice_len(&mut arr, 0); | 
|---|
| 438 | /// let mut p = 1; | 
|---|
| 439 | /// sv.resize_with(4, || { | 
|---|
| 440 | ///   p *= 2; | 
|---|
| 441 | ///   p | 
|---|
| 442 | /// }); | 
|---|
| 443 | /// assert_eq!(&sv[..], [2, 4, 8, 16]); | 
|---|
| 444 | /// ``` | 
|---|
| 445 | #[ inline] | 
|---|
| 446 | pub fn resize_with<F: FnMut() -> T>(&mut self, new_len: usize, mut f: F) { | 
|---|
| 447 | match new_len.checked_sub(self.len) { | 
|---|
| 448 | None => { | 
|---|
| 449 | if needs_drop::<T>() { | 
|---|
| 450 | while self.len() > new_len { | 
|---|
| 451 | self.len -= 1; | 
|---|
| 452 | self.data[self.len] = f(); | 
|---|
| 453 | } | 
|---|
| 454 | } else { | 
|---|
| 455 | self.len = new_len; | 
|---|
| 456 | } | 
|---|
| 457 | } | 
|---|
| 458 | Some(new_elements) => { | 
|---|
| 459 | for _ in 0..new_elements { | 
|---|
| 460 | self.push(f()); | 
|---|
| 461 | } | 
|---|
| 462 | } | 
|---|
| 463 | } | 
|---|
| 464 | } | 
|---|
| 465 |  | 
|---|
| 466 | /// Walk the vec and keep only the elements that pass the predicate given. | 
|---|
| 467 | /// | 
|---|
| 468 | /// ## Example | 
|---|
| 469 | /// | 
|---|
| 470 | /// ```rust | 
|---|
| 471 | /// # use tinyvec::*; | 
|---|
| 472 | /// | 
|---|
| 473 | /// let mut arr = [1, 1, 2, 3, 3, 4]; | 
|---|
| 474 | /// let mut sv = SliceVec::from(&mut arr); | 
|---|
| 475 | /// sv.retain(|&x| x % 2 == 0); | 
|---|
| 476 | /// assert_eq!(&sv[..], [2, 4]); | 
|---|
| 477 | /// ``` | 
|---|
| 478 | #[ inline] | 
|---|
| 479 | pub fn retain<F: FnMut(&T) -> bool>(&mut self, mut acceptable: F) | 
|---|
| 480 | where | 
|---|
| 481 | T: Default, | 
|---|
| 482 | { | 
|---|
| 483 | // Drop guard to contain exactly the remaining elements when the test | 
|---|
| 484 | // panics. | 
|---|
| 485 | struct JoinOnDrop<'vec, Item> { | 
|---|
| 486 | items: &'vec mut [Item], | 
|---|
| 487 | done_end: usize, | 
|---|
| 488 | // Start of tail relative to `done_end`. | 
|---|
| 489 | tail_start: usize, | 
|---|
| 490 | } | 
|---|
| 491 |  | 
|---|
| 492 | impl<Item> Drop for JoinOnDrop<'_, Item> { | 
|---|
| 493 | fn drop(&mut self) { | 
|---|
| 494 | self.items[self.done_end..].rotate_left(self.tail_start); | 
|---|
| 495 | } | 
|---|
| 496 | } | 
|---|
| 497 |  | 
|---|
| 498 | let mut rest = JoinOnDrop { items: self.data, done_end: 0, tail_start: 0 }; | 
|---|
| 499 |  | 
|---|
| 500 | for idx in 0..self.len { | 
|---|
| 501 | // Loop start invariant: idx = rest.done_end + rest.tail_start | 
|---|
| 502 | if !acceptable(&rest.items[idx]) { | 
|---|
| 503 | let _ = core::mem::take(&mut rest.items[idx]); | 
|---|
| 504 | self.len -= 1; | 
|---|
| 505 | rest.tail_start += 1; | 
|---|
| 506 | } else { | 
|---|
| 507 | rest.items.swap(rest.done_end, idx); | 
|---|
| 508 | rest.done_end += 1; | 
|---|
| 509 | } | 
|---|
| 510 | } | 
|---|
| 511 | } | 
|---|
| 512 |  | 
|---|
| 513 | /// Forces the length of the vector to `new_len`. | 
|---|
| 514 | /// | 
|---|
| 515 | /// ## Panics | 
|---|
| 516 | /// * If `new_len` is greater than the vec's capacity. | 
|---|
| 517 | /// | 
|---|
| 518 | /// ## Safety | 
|---|
| 519 | /// * This is a fully safe operation! The inactive memory already counts as | 
|---|
| 520 | ///   "initialized" by Rust's rules. | 
|---|
| 521 | /// * Other than "the memory is initialized" there are no other guarantees | 
|---|
| 522 | ///   regarding what you find in the inactive portion of the vec. | 
|---|
| 523 | #[ inline(always)] | 
|---|
| 524 | pub fn set_len(&mut self, new_len: usize) { | 
|---|
| 525 | if new_len > self.capacity() { | 
|---|
| 526 | // Note(Lokathor): Technically we don't have to panic here, and we could | 
|---|
| 527 | // just let some other call later on trigger a panic on accident when the | 
|---|
| 528 | // length is wrong. However, it's a lot easier to catch bugs when things | 
|---|
| 529 | // are more "fail-fast". | 
|---|
| 530 | panic!( | 
|---|
| 531 | "SliceVec::set_len> new length {}  exceeds capacity {} ", | 
|---|
| 532 | new_len, | 
|---|
| 533 | self.capacity() | 
|---|
| 534 | ) | 
|---|
| 535 | } else { | 
|---|
| 536 | self.len = new_len; | 
|---|
| 537 | } | 
|---|
| 538 | } | 
|---|
| 539 |  | 
|---|
| 540 | /// Splits the collection at the point given. | 
|---|
| 541 | /// | 
|---|
| 542 | /// * `[0, at)` stays in this vec (and this vec is now full). | 
|---|
| 543 | /// * `[at, len)` ends up in the new vec (with any spare capacity). | 
|---|
| 544 | /// | 
|---|
| 545 | /// ## Panics | 
|---|
| 546 | /// * if `at` > `self.len()` | 
|---|
| 547 | /// | 
|---|
| 548 | /// ## Example | 
|---|
| 549 | /// | 
|---|
| 550 | /// ```rust | 
|---|
| 551 | /// # use tinyvec::*; | 
|---|
| 552 | /// let mut arr = [1, 2, 3]; | 
|---|
| 553 | /// let mut sv = SliceVec::from(&mut arr); | 
|---|
| 554 | /// let sv2 = sv.split_off(1); | 
|---|
| 555 | /// assert_eq!(&sv[..], [1]); | 
|---|
| 556 | /// assert_eq!(&sv2[..], [2, 3]); | 
|---|
| 557 | /// ``` | 
|---|
| 558 | #[ inline] | 
|---|
| 559 | pub fn split_off<'a>(&'a mut self, at: usize) -> SliceVec<'s, T> { | 
|---|
| 560 | let mut new = Self::default(); | 
|---|
| 561 | let backing: &'s mut [T] = core::mem::take(&mut self.data); | 
|---|
| 562 | let (me, other) = backing.split_at_mut(at); | 
|---|
| 563 | new.len = self.len - at; | 
|---|
| 564 | new.data = other; | 
|---|
| 565 | self.len = me.len(); | 
|---|
| 566 | self.data = me; | 
|---|
| 567 | new | 
|---|
| 568 | } | 
|---|
| 569 |  | 
|---|
| 570 | /// Remove an element, swapping the end of the vec into its place. | 
|---|
| 571 | /// | 
|---|
| 572 | /// ## Panics | 
|---|
| 573 | /// * If the index is out of bounds. | 
|---|
| 574 | /// | 
|---|
| 575 | /// ## Example | 
|---|
| 576 | /// ```rust | 
|---|
| 577 | /// # use tinyvec::*; | 
|---|
| 578 | /// let mut arr = [ "foo", "bar", "quack", "zap"]; | 
|---|
| 579 | /// let mut sv = SliceVec::from(&mut arr); | 
|---|
| 580 | /// | 
|---|
| 581 | /// assert_eq!(sv.swap_remove(1), "bar"); | 
|---|
| 582 | /// assert_eq!(&sv[..], [ "foo", "zap", "quack"]); | 
|---|
| 583 | /// | 
|---|
| 584 | /// assert_eq!(sv.swap_remove(0), "foo"); | 
|---|
| 585 | /// assert_eq!(&sv[..], [ "quack", "zap"]); | 
|---|
| 586 | /// ``` | 
|---|
| 587 | #[ inline] | 
|---|
| 588 | pub fn swap_remove(&mut self, index: usize) -> T | 
|---|
| 589 | where | 
|---|
| 590 | T: Default, | 
|---|
| 591 | { | 
|---|
| 592 | assert!( | 
|---|
| 593 | index < self.len, | 
|---|
| 594 | "SliceVec::swap_remove> index {}  is out of bounds {} ", | 
|---|
| 595 | index, | 
|---|
| 596 | self.len | 
|---|
| 597 | ); | 
|---|
| 598 | if index == self.len - 1 { | 
|---|
| 599 | self.pop().unwrap() | 
|---|
| 600 | } else { | 
|---|
| 601 | let i = self.pop().unwrap(); | 
|---|
| 602 | replace(&mut self[index], i) | 
|---|
| 603 | } | 
|---|
| 604 | } | 
|---|
| 605 |  | 
|---|
| 606 | /// Reduces the vec's length to the given value. | 
|---|
| 607 | /// | 
|---|
| 608 | /// If the vec is already shorter than the input, nothing happens. | 
|---|
| 609 | #[ inline] | 
|---|
| 610 | pub fn truncate(&mut self, new_len: usize) | 
|---|
| 611 | where | 
|---|
| 612 | T: Default, | 
|---|
| 613 | { | 
|---|
| 614 | if needs_drop::<T>() { | 
|---|
| 615 | while self.len > new_len { | 
|---|
| 616 | self.pop(); | 
|---|
| 617 | } | 
|---|
| 618 | } else { | 
|---|
| 619 | self.len = self.len.min(new_len); | 
|---|
| 620 | } | 
|---|
| 621 | } | 
|---|
| 622 |  | 
|---|
| 623 | /// Wraps a slice, using the given length as the starting length. | 
|---|
| 624 | /// | 
|---|
| 625 | /// If you want to use the whole length of the slice, you can just use the | 
|---|
| 626 | /// `From` impl. | 
|---|
| 627 | /// | 
|---|
| 628 | /// ## Failure | 
|---|
| 629 | /// | 
|---|
| 630 | /// If the given length is greater than the length of the slice you get | 
|---|
| 631 | /// `None`. | 
|---|
| 632 | #[ inline] | 
|---|
| 633 | pub fn try_from_slice_len(data: &'s mut [T], len: usize) -> Option<Self> { | 
|---|
| 634 | if len <= data.len() { | 
|---|
| 635 | Some(Self { data, len }) | 
|---|
| 636 | } else { | 
|---|
| 637 | None | 
|---|
| 638 | } | 
|---|
| 639 | } | 
|---|
| 640 | } | 
|---|
| 641 |  | 
|---|
| 642 | #[ cfg(feature = "grab_spare_slice")] | 
|---|
| 643 | impl<'s, T> SliceVec<'s, T> { | 
|---|
| 644 | /// Obtain the shared slice of the array _after_ the active memory. | 
|---|
| 645 | /// | 
|---|
| 646 | /// ## Example | 
|---|
| 647 | /// ```rust | 
|---|
| 648 | /// # use tinyvec::*; | 
|---|
| 649 | /// let mut arr = [0; 4]; | 
|---|
| 650 | /// let mut sv = SliceVec::from_slice_len(&mut arr, 0); | 
|---|
| 651 | /// assert_eq!(sv.grab_spare_slice().len(), 4); | 
|---|
| 652 | /// sv.push(10); | 
|---|
| 653 | /// sv.push(11); | 
|---|
| 654 | /// sv.push(12); | 
|---|
| 655 | /// sv.push(13); | 
|---|
| 656 | /// assert_eq!(sv.grab_spare_slice().len(), 0); | 
|---|
| 657 | /// ``` | 
|---|
| 658 | #[ must_use] | 
|---|
| 659 | #[ inline(always)] | 
|---|
| 660 | pub fn grab_spare_slice(&self) -> &[T] { | 
|---|
| 661 | &self.data[self.len..] | 
|---|
| 662 | } | 
|---|
| 663 |  | 
|---|
| 664 | /// Obtain the mutable slice of the array _after_ the active memory. | 
|---|
| 665 | /// | 
|---|
| 666 | /// ## Example | 
|---|
| 667 | /// ```rust | 
|---|
| 668 | /// # use tinyvec::*; | 
|---|
| 669 | /// let mut arr = [0; 4]; | 
|---|
| 670 | /// let mut sv = SliceVec::from_slice_len(&mut arr, 0); | 
|---|
| 671 | /// assert_eq!(sv.grab_spare_slice_mut().len(), 4); | 
|---|
| 672 | /// sv.push(10); | 
|---|
| 673 | /// sv.push(11); | 
|---|
| 674 | /// assert_eq!(sv.grab_spare_slice_mut().len(), 2); | 
|---|
| 675 | /// ``` | 
|---|
| 676 | #[ inline(always)] | 
|---|
| 677 | pub fn grab_spare_slice_mut(&mut self) -> &mut [T] { | 
|---|
| 678 | &mut self.data[self.len..] | 
|---|
| 679 | } | 
|---|
| 680 | } | 
|---|
| 681 |  | 
|---|
| 682 | impl<'s, T> From<&'s mut [T]> for SliceVec<'s, T> { | 
|---|
| 683 | /// Uses the full slice as the initial length. | 
|---|
| 684 | /// ## Example | 
|---|
| 685 | /// ```rust | 
|---|
| 686 | /// # use tinyvec::*; | 
|---|
| 687 | /// let mut arr = [0_i32; 2]; | 
|---|
| 688 | /// let mut sv = SliceVec::from(&mut arr[..]); | 
|---|
| 689 | /// ``` | 
|---|
| 690 | #[ inline] | 
|---|
| 691 | fn from(data: &'s mut [T]) -> Self { | 
|---|
| 692 | let len: usize = data.len(); | 
|---|
| 693 | Self { data, len } | 
|---|
| 694 | } | 
|---|
| 695 | } | 
|---|
| 696 |  | 
|---|
| 697 | impl<'s, T, A> From<&'s mut A> for SliceVec<'s, T> | 
|---|
| 698 | where | 
|---|
| 699 | A: AsMut<[T]>, | 
|---|
| 700 | { | 
|---|
| 701 | /// Calls `AsRef::as_mut` then uses the full slice as the initial length. | 
|---|
| 702 | /// ## Example | 
|---|
| 703 | /// ```rust | 
|---|
| 704 | /// # use tinyvec::*; | 
|---|
| 705 | /// let mut arr = [0, 0]; | 
|---|
| 706 | /// let mut sv = SliceVec::from(&mut arr); | 
|---|
| 707 | /// ``` | 
|---|
| 708 | #[ inline] | 
|---|
| 709 | fn from(a: &'s mut A) -> Self { | 
|---|
| 710 | let data: &mut [T] = a.as_mut(); | 
|---|
| 711 | let len: usize = data.len(); | 
|---|
| 712 | Self { data, len } | 
|---|
| 713 | } | 
|---|
| 714 | } | 
|---|
| 715 |  | 
|---|
| 716 | /// Draining iterator for [`SliceVec`] | 
|---|
| 717 | /// | 
|---|
| 718 | /// See [`SliceVec::drain`](SliceVec::drain) | 
|---|
| 719 | pub struct SliceVecDrain<'p, 's, T: Default> { | 
|---|
| 720 | parent: &'p mut SliceVec<'s, T>, | 
|---|
| 721 | target_start: usize, | 
|---|
| 722 | target_index: usize, | 
|---|
| 723 | target_end: usize, | 
|---|
| 724 | } | 
|---|
| 725 | impl<'p, 's, T: Default> Iterator for SliceVecDrain<'p, 's, T> { | 
|---|
| 726 | type Item = T; | 
|---|
| 727 | #[ inline] | 
|---|
| 728 | fn next(&mut self) -> Option<Self::Item> { | 
|---|
| 729 | if self.target_index != self.target_end { | 
|---|
| 730 | let out: T = core::mem::take(&mut self.parent[self.target_index]); | 
|---|
| 731 | self.target_index += 1; | 
|---|
| 732 | Some(out) | 
|---|
| 733 | } else { | 
|---|
| 734 | None | 
|---|
| 735 | } | 
|---|
| 736 | } | 
|---|
| 737 | } | 
|---|
| 738 | impl<'p, 's, T: Default> FusedIterator for SliceVecDrain<'p, 's, T> {} | 
|---|
| 739 | impl<'p, 's, T: Default> Drop for SliceVecDrain<'p, 's, T> { | 
|---|
| 740 | #[ inline] | 
|---|
| 741 | fn drop(&mut self) { | 
|---|
| 742 | // Changed because it was moving `self`, it's also more clear and the std | 
|---|
| 743 | // does the same | 
|---|
| 744 | self.for_each(drop); | 
|---|
| 745 | // Implementation very similar to [`SliceVec::remove`](SliceVec::remove) | 
|---|
| 746 | let count: usize = self.target_end - self.target_start; | 
|---|
| 747 | let targets: &mut [T] = &mut self.parent.deref_mut()[self.target_start..]; | 
|---|
| 748 | targets.rotate_left(mid:count); | 
|---|
| 749 | self.parent.len -= count; | 
|---|
| 750 | } | 
|---|
| 751 | } | 
|---|
| 752 |  | 
|---|
| 753 | impl<'s, T> AsMut<[T]> for SliceVec<'s, T> { | 
|---|
| 754 | #[ inline(always)] | 
|---|
| 755 | #[ must_use] | 
|---|
| 756 | fn as_mut(&mut self) -> &mut [T] { | 
|---|
| 757 | &mut *self | 
|---|
| 758 | } | 
|---|
| 759 | } | 
|---|
| 760 |  | 
|---|
| 761 | impl<'s, T> AsRef<[T]> for SliceVec<'s, T> { | 
|---|
| 762 | #[ inline(always)] | 
|---|
| 763 | #[ must_use] | 
|---|
| 764 | fn as_ref(&self) -> &[T] { | 
|---|
| 765 | &*self | 
|---|
| 766 | } | 
|---|
| 767 | } | 
|---|
| 768 |  | 
|---|
| 769 | impl<'s, T> Borrow<[T]> for SliceVec<'s, T> { | 
|---|
| 770 | #[ inline(always)] | 
|---|
| 771 | #[ must_use] | 
|---|
| 772 | fn borrow(&self) -> &[T] { | 
|---|
| 773 | &*self | 
|---|
| 774 | } | 
|---|
| 775 | } | 
|---|
| 776 |  | 
|---|
| 777 | impl<'s, T> BorrowMut<[T]> for SliceVec<'s, T> { | 
|---|
| 778 | #[ inline(always)] | 
|---|
| 779 | #[ must_use] | 
|---|
| 780 | fn borrow_mut(&mut self) -> &mut [T] { | 
|---|
| 781 | &mut *self | 
|---|
| 782 | } | 
|---|
| 783 | } | 
|---|
| 784 |  | 
|---|
| 785 | impl<'s, T> Extend<T> for SliceVec<'s, T> { | 
|---|
| 786 | #[ inline] | 
|---|
| 787 | fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) { | 
|---|
| 788 | for t: T in iter { | 
|---|
| 789 | self.push(val:t) | 
|---|
| 790 | } | 
|---|
| 791 | } | 
|---|
| 792 | } | 
|---|
| 793 |  | 
|---|
| 794 | impl<'s, T> IntoIterator for SliceVec<'s, T> { | 
|---|
| 795 | type Item = &'s mut T; | 
|---|
| 796 | type IntoIter = core::slice::IterMut<'s, T>; | 
|---|
| 797 | #[ inline(always)] | 
|---|
| 798 | #[ must_use] | 
|---|
| 799 | fn into_iter(self) -> Self::IntoIter { | 
|---|
| 800 | self.data.iter_mut() | 
|---|
| 801 | } | 
|---|
| 802 | } | 
|---|
| 803 |  | 
|---|
| 804 | impl<'s, T> PartialEq for SliceVec<'s, T> | 
|---|
| 805 | where | 
|---|
| 806 | T: PartialEq, | 
|---|
| 807 | { | 
|---|
| 808 | #[ inline] | 
|---|
| 809 | #[ must_use] | 
|---|
| 810 | fn eq(&self, other: &Self) -> bool { | 
|---|
| 811 | self.as_slice().eq(other.as_slice()) | 
|---|
| 812 | } | 
|---|
| 813 | } | 
|---|
| 814 | impl<'s, T> Eq for SliceVec<'s, T> where T: Eq {} | 
|---|
| 815 |  | 
|---|
| 816 | impl<'s, T> PartialOrd for SliceVec<'s, T> | 
|---|
| 817 | where | 
|---|
| 818 | T: PartialOrd, | 
|---|
| 819 | { | 
|---|
| 820 | #[ inline] | 
|---|
| 821 | #[ must_use] | 
|---|
| 822 | fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> { | 
|---|
| 823 | self.as_slice().partial_cmp(other.as_slice()) | 
|---|
| 824 | } | 
|---|
| 825 | } | 
|---|
| 826 | impl<'s, T> Ord for SliceVec<'s, T> | 
|---|
| 827 | where | 
|---|
| 828 | T: Ord, | 
|---|
| 829 | { | 
|---|
| 830 | #[ inline] | 
|---|
| 831 | #[ must_use] | 
|---|
| 832 | fn cmp(&self, other: &Self) -> core::cmp::Ordering { | 
|---|
| 833 | self.as_slice().cmp(other.as_slice()) | 
|---|
| 834 | } | 
|---|
| 835 | } | 
|---|
| 836 |  | 
|---|
| 837 | impl<'s, T> PartialEq<&[T]> for SliceVec<'s, T> | 
|---|
| 838 | where | 
|---|
| 839 | T: PartialEq, | 
|---|
| 840 | { | 
|---|
| 841 | #[ inline] | 
|---|
| 842 | #[ must_use] | 
|---|
| 843 | fn eq(&self, other: &&[T]) -> bool { | 
|---|
| 844 | self.as_slice().eq(*other) | 
|---|
| 845 | } | 
|---|
| 846 | } | 
|---|
| 847 |  | 
|---|
| 848 | impl<'s, T> Hash for SliceVec<'s, T> | 
|---|
| 849 | where | 
|---|
| 850 | T: Hash, | 
|---|
| 851 | { | 
|---|
| 852 | #[ inline] | 
|---|
| 853 | fn hash<H: Hasher>(&self, state: &mut H) { | 
|---|
| 854 | self.as_slice().hash(state) | 
|---|
| 855 | } | 
|---|
| 856 | } | 
|---|
| 857 |  | 
|---|
| 858 | #[ cfg(feature = "experimental_write_impl")] | 
|---|
| 859 | impl<'s> core::fmt::Write for SliceVec<'s, u8> { | 
|---|
| 860 | fn write_str(&mut self, s: &str) -> core::fmt::Result { | 
|---|
| 861 | let my_len = self.len(); | 
|---|
| 862 | let str_len = s.as_bytes().len(); | 
|---|
| 863 | if my_len + str_len <= self.capacity() { | 
|---|
| 864 | let remainder = &mut self.data[my_len..]; | 
|---|
| 865 | let target = &mut remainder[..str_len]; | 
|---|
| 866 | target.copy_from_slice(s.as_bytes()); | 
|---|
| 867 | Ok(()) | 
|---|
| 868 | } else { | 
|---|
| 869 | Err(core::fmt::Error) | 
|---|
| 870 | } | 
|---|
| 871 | } | 
|---|
| 872 | } | 
|---|
| 873 |  | 
|---|
| 874 | // // // // // // // // | 
|---|
| 875 | // Formatting impls | 
|---|
| 876 | // // // // // // // // | 
|---|
| 877 |  | 
|---|
| 878 | impl<'s, T> Binary for SliceVec<'s, T> | 
|---|
| 879 | where | 
|---|
| 880 | T: Binary, | 
|---|
| 881 | { | 
|---|
| 882 | #[ allow(clippy::missing_inline_in_public_items)] | 
|---|
| 883 | fn fmt(&self, f: &mut Formatter) -> core::fmt::Result { | 
|---|
| 884 | write!(f, "[")?; | 
|---|
| 885 | if f.alternate() { | 
|---|
| 886 | write!(f, "\n     ")?; | 
|---|
| 887 | } | 
|---|
| 888 | for (i: usize, elem: &T) in self.iter().enumerate() { | 
|---|
| 889 | if i > 0 { | 
|---|
| 890 | write!(f, ",{} ", if f.alternate() { "\n     "} else { " "})?; | 
|---|
| 891 | } | 
|---|
| 892 | Binary::fmt(self:elem, f)?; | 
|---|
| 893 | } | 
|---|
| 894 | if f.alternate() { | 
|---|
| 895 | write!(f, ",\n ")?; | 
|---|
| 896 | } | 
|---|
| 897 | write!(f, "]") | 
|---|
| 898 | } | 
|---|
| 899 | } | 
|---|
| 900 |  | 
|---|
| 901 | impl<'s, T> Debug for SliceVec<'s, T> | 
|---|
| 902 | where | 
|---|
| 903 | T: Debug, | 
|---|
| 904 | { | 
|---|
| 905 | #[ allow(clippy::missing_inline_in_public_items)] | 
|---|
| 906 | fn fmt(&self, f: &mut Formatter) -> core::fmt::Result { | 
|---|
| 907 | write!(f, "[")?; | 
|---|
| 908 | if f.alternate() && !self.is_empty() { | 
|---|
| 909 | write!(f, "\n     ")?; | 
|---|
| 910 | } | 
|---|
| 911 | for (i: usize, elem: &T) in self.iter().enumerate() { | 
|---|
| 912 | if i > 0 { | 
|---|
| 913 | write!(f, ",{} ", if f.alternate() { "\n     "} else { " "})?; | 
|---|
| 914 | } | 
|---|
| 915 | Debug::fmt(self:elem, f)?; | 
|---|
| 916 | } | 
|---|
| 917 | if f.alternate() && !self.is_empty() { | 
|---|
| 918 | write!(f, ",\n ")?; | 
|---|
| 919 | } | 
|---|
| 920 | write!(f, "]") | 
|---|
| 921 | } | 
|---|
| 922 | } | 
|---|
| 923 |  | 
|---|
| 924 | impl<'s, T> Display for SliceVec<'s, T> | 
|---|
| 925 | where | 
|---|
| 926 | T: Display, | 
|---|
| 927 | { | 
|---|
| 928 | #[ allow(clippy::missing_inline_in_public_items)] | 
|---|
| 929 | fn fmt(&self, f: &mut Formatter) -> core::fmt::Result { | 
|---|
| 930 | write!(f, "[")?; | 
|---|
| 931 | if f.alternate() { | 
|---|
| 932 | write!(f, "\n     ")?; | 
|---|
| 933 | } | 
|---|
| 934 | for (i: usize, elem: &T) in self.iter().enumerate() { | 
|---|
| 935 | if i > 0 { | 
|---|
| 936 | write!(f, ",{} ", if f.alternate() { "\n     "} else { " "})?; | 
|---|
| 937 | } | 
|---|
| 938 | Display::fmt(self:elem, f)?; | 
|---|
| 939 | } | 
|---|
| 940 | if f.alternate() { | 
|---|
| 941 | write!(f, ",\n ")?; | 
|---|
| 942 | } | 
|---|
| 943 | write!(f, "]") | 
|---|
| 944 | } | 
|---|
| 945 | } | 
|---|
| 946 |  | 
|---|
| 947 | impl<'s, T> LowerExp for SliceVec<'s, T> | 
|---|
| 948 | where | 
|---|
| 949 | T: LowerExp, | 
|---|
| 950 | { | 
|---|
| 951 | #[ allow(clippy::missing_inline_in_public_items)] | 
|---|
| 952 | fn fmt(&self, f: &mut Formatter) -> core::fmt::Result { | 
|---|
| 953 | write!(f, "[")?; | 
|---|
| 954 | if f.alternate() { | 
|---|
| 955 | write!(f, "\n     ")?; | 
|---|
| 956 | } | 
|---|
| 957 | for (i: usize, elem: &T) in self.iter().enumerate() { | 
|---|
| 958 | if i > 0 { | 
|---|
| 959 | write!(f, ",{} ", if f.alternate() { "\n     "} else { " "})?; | 
|---|
| 960 | } | 
|---|
| 961 | LowerExp::fmt(self:elem, f)?; | 
|---|
| 962 | } | 
|---|
| 963 | if f.alternate() { | 
|---|
| 964 | write!(f, ",\n ")?; | 
|---|
| 965 | } | 
|---|
| 966 | write!(f, "]") | 
|---|
| 967 | } | 
|---|
| 968 | } | 
|---|
| 969 |  | 
|---|
| 970 | impl<'s, T> LowerHex for SliceVec<'s, T> | 
|---|
| 971 | where | 
|---|
| 972 | T: LowerHex, | 
|---|
| 973 | { | 
|---|
| 974 | #[ allow(clippy::missing_inline_in_public_items)] | 
|---|
| 975 | fn fmt(&self, f: &mut Formatter) -> core::fmt::Result { | 
|---|
| 976 | write!(f, "[")?; | 
|---|
| 977 | if f.alternate() { | 
|---|
| 978 | write!(f, "\n     ")?; | 
|---|
| 979 | } | 
|---|
| 980 | for (i: usize, elem: &T) in self.iter().enumerate() { | 
|---|
| 981 | if i > 0 { | 
|---|
| 982 | write!(f, ",{} ", if f.alternate() { "\n     "} else { " "})?; | 
|---|
| 983 | } | 
|---|
| 984 | LowerHex::fmt(self:elem, f)?; | 
|---|
| 985 | } | 
|---|
| 986 | if f.alternate() { | 
|---|
| 987 | write!(f, ",\n ")?; | 
|---|
| 988 | } | 
|---|
| 989 | write!(f, "]") | 
|---|
| 990 | } | 
|---|
| 991 | } | 
|---|
| 992 |  | 
|---|
| 993 | impl<'s, T> Octal for SliceVec<'s, T> | 
|---|
| 994 | where | 
|---|
| 995 | T: Octal, | 
|---|
| 996 | { | 
|---|
| 997 | #[ allow(clippy::missing_inline_in_public_items)] | 
|---|
| 998 | fn fmt(&self, f: &mut Formatter) -> core::fmt::Result { | 
|---|
| 999 | write!(f, "[")?; | 
|---|
| 1000 | if f.alternate() { | 
|---|
| 1001 | write!(f, "\n     ")?; | 
|---|
| 1002 | } | 
|---|
| 1003 | for (i: usize, elem: &T) in self.iter().enumerate() { | 
|---|
| 1004 | if i > 0 { | 
|---|
| 1005 | write!(f, ",{} ", if f.alternate() { "\n     "} else { " "})?; | 
|---|
| 1006 | } | 
|---|
| 1007 | Octal::fmt(self:elem, f)?; | 
|---|
| 1008 | } | 
|---|
| 1009 | if f.alternate() { | 
|---|
| 1010 | write!(f, ",\n ")?; | 
|---|
| 1011 | } | 
|---|
| 1012 | write!(f, "]") | 
|---|
| 1013 | } | 
|---|
| 1014 | } | 
|---|
| 1015 |  | 
|---|
| 1016 | impl<'s, T> Pointer for SliceVec<'s, T> | 
|---|
| 1017 | where | 
|---|
| 1018 | T: Pointer, | 
|---|
| 1019 | { | 
|---|
| 1020 | #[ allow(clippy::missing_inline_in_public_items)] | 
|---|
| 1021 | fn fmt(&self, f: &mut Formatter) -> core::fmt::Result { | 
|---|
| 1022 | write!(f, "[")?; | 
|---|
| 1023 | if f.alternate() { | 
|---|
| 1024 | write!(f, "\n     ")?; | 
|---|
| 1025 | } | 
|---|
| 1026 | for (i: usize, elem: &T) in self.iter().enumerate() { | 
|---|
| 1027 | if i > 0 { | 
|---|
| 1028 | write!(f, ",{} ", if f.alternate() { "\n     "} else { " "})?; | 
|---|
| 1029 | } | 
|---|
| 1030 | Pointer::fmt(self:elem, f)?; | 
|---|
| 1031 | } | 
|---|
| 1032 | if f.alternate() { | 
|---|
| 1033 | write!(f, ",\n ")?; | 
|---|
| 1034 | } | 
|---|
| 1035 | write!(f, "]") | 
|---|
| 1036 | } | 
|---|
| 1037 | } | 
|---|
| 1038 |  | 
|---|
| 1039 | impl<'s, T> UpperExp for SliceVec<'s, T> | 
|---|
| 1040 | where | 
|---|
| 1041 | T: UpperExp, | 
|---|
| 1042 | { | 
|---|
| 1043 | #[ allow(clippy::missing_inline_in_public_items)] | 
|---|
| 1044 | fn fmt(&self, f: &mut Formatter) -> core::fmt::Result { | 
|---|
| 1045 | write!(f, "[")?; | 
|---|
| 1046 | if f.alternate() { | 
|---|
| 1047 | write!(f, "\n     ")?; | 
|---|
| 1048 | } | 
|---|
| 1049 | for (i: usize, elem: &T) in self.iter().enumerate() { | 
|---|
| 1050 | if i > 0 { | 
|---|
| 1051 | write!(f, ",{} ", if f.alternate() { "\n     "} else { " "})?; | 
|---|
| 1052 | } | 
|---|
| 1053 | UpperExp::fmt(self:elem, f)?; | 
|---|
| 1054 | } | 
|---|
| 1055 | if f.alternate() { | 
|---|
| 1056 | write!(f, ",\n ")?; | 
|---|
| 1057 | } | 
|---|
| 1058 | write!(f, "]") | 
|---|
| 1059 | } | 
|---|
| 1060 | } | 
|---|
| 1061 |  | 
|---|
| 1062 | impl<'s, T> UpperHex for SliceVec<'s, T> | 
|---|
| 1063 | where | 
|---|
| 1064 | T: UpperHex, | 
|---|
| 1065 | { | 
|---|
| 1066 | #[ allow(clippy::missing_inline_in_public_items)] | 
|---|
| 1067 | fn fmt(&self, f: &mut Formatter) -> core::fmt::Result { | 
|---|
| 1068 | write!(f, "[")?; | 
|---|
| 1069 | if f.alternate() { | 
|---|
| 1070 | write!(f, "\n     ")?; | 
|---|
| 1071 | } | 
|---|
| 1072 | for (i: usize, elem: &T) in self.iter().enumerate() { | 
|---|
| 1073 | if i > 0 { | 
|---|
| 1074 | write!(f, ",{} ", if f.alternate() { "\n     "} else { " "})?; | 
|---|
| 1075 | } | 
|---|
| 1076 | UpperHex::fmt(self:elem, f)?; | 
|---|
| 1077 | } | 
|---|
| 1078 | if f.alternate() { | 
|---|
| 1079 | write!(f, ",\n ")?; | 
|---|
| 1080 | } | 
|---|
| 1081 | write!(f, "]") | 
|---|
| 1082 | } | 
|---|
| 1083 | } | 
|---|
| 1084 |  | 
|---|