spinoso_array/array/vec/mod.rs
1//! Ruby `Array` based on [`Vec`].
2
3use alloc::boxed::Box;
4use alloc::vec;
5use alloc::vec::Vec;
6use core::cmp;
7use core::slice::{Iter, IterMut};
8
9#[doc(inline)]
10pub use raw_parts::RawParts;
11
12mod convert;
13mod eq;
14mod impls;
15mod iter;
16
17/// A contiguous growable array type based on [`Vec<T>`](Vec).
18///
19/// `Array` implements indexing and mutating APIs that make an ideal backend for
20/// the [Ruby `Array` core class][ruby-array]. In practice, this results in less
21/// generic, more single-use APIs. For example, instead of [`Vec::drain`],
22/// `Array` implements [`shift`], [`shift_n`], [`pop`], and [`pop_n`].
23///
24/// Similarly, slicing APIs are more specialized, such as [`first_n`] and
25/// [`last_n`]. Slicing APIs do not return [`Option`], instead preferring to
26/// return an empty slice.
27///
28/// # Examples
29///
30/// ```
31/// # use spinoso_array::Array;
32/// let mut ary = Array::new();
33/// ary.push(1);
34/// ary.push(2);
35///
36/// assert_eq!(ary.len(), 2);
37/// assert_eq!(ary[0], 1);
38///
39/// assert_eq!(ary.pop(), Some(2));
40/// assert_eq!(ary.len(), 1);
41///
42/// ary[0] = 7;
43/// assert_eq!(ary[0], 7);
44///
45/// ary.extend([1, 2, 3].iter().copied());
46///
47/// for x in &ary {
48/// println!("{}", x);
49/// }
50/// assert_eq!(ary, &[7, 1, 2, 3]);
51/// ```
52///
53/// [ruby-array]: https://ruby-doc.org/core-3.1.2/Array.html
54/// [`shift`]: Array::shift
55/// [`shift_n`]: Array::shift_n
56/// [`drop_n`]: Array::drop_n
57/// [`pop`]: Array::pop
58/// [`pop_n`]: Array::pop_n
59/// [`first_n`]: Array::first_n
60/// [`last_n`]: Array::last_n
61#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
62pub struct Array<T>(Vec<T>);
63
64impl<T> Default for Array<T> {
65 #[inline]
66 fn default() -> Self {
67 Self::new()
68 }
69}
70
71impl<T> Array<T> {
72 /// Construct a new, empty `Array<T>`.
73 ///
74 /// The vector will not allocate until elements are pushed into it.
75 ///
76 /// # Examples
77 ///
78 /// ```
79 /// # use spinoso_array::Array;
80 /// let ary: Array<i32> = Array::new();
81 /// assert!(ary.is_empty());
82 /// assert_eq!(ary.capacity(), 0);
83 /// ```
84 #[inline]
85 #[must_use]
86 pub const fn new() -> Self {
87 Self(Vec::new())
88 }
89
90 /// Construct a new, empty `Array<T>` with the specified capacity.
91 ///
92 /// The vector will be able to hold exactly `capacity` elements without
93 /// reallocating. If `capacity` is 0, the vector will not allocate.
94 ///
95 /// It is important to note that although the returned vector has the
96 /// _capacity_ specified, the vector will have a zero _length_.
97 ///
98 /// # Examples
99 ///
100 /// ```
101 /// # use spinoso_array::Array;
102 /// let mut ary: Array<i32> = Array::with_capacity(10);
103 /// assert_eq!(ary.len(), 0);
104 /// assert_eq!(ary.capacity(), 10);
105 ///
106 /// // These are pushes all done without reallocating...
107 /// for i in 0..10 {
108 /// ary.push(i);
109 /// }
110 ///
111 /// // ...but this may make the vector reallocate
112 /// ary.push(11);
113 /// ```
114 #[inline]
115 #[must_use]
116 pub fn with_capacity(capacity: usize) -> Self {
117 Self(Vec::with_capacity(capacity))
118 }
119
120 /// Construct a new two-element `Array` from the given arguments.
121 ///
122 /// The vector is constructed with `capacity` of 2.
123 ///
124 /// # Examples
125 ///
126 /// ```
127 /// # use spinoso_array::Array;
128 /// let ary = Array::assoc(0, 100);
129 /// assert_eq!(ary.capacity(), 2);
130 /// assert_eq!(ary.len(), 2);
131 /// assert_eq!(ary[0], 0);
132 /// assert_eq!(ary[1], 100);
133 /// ```
134 #[inline]
135 #[must_use]
136 pub fn assoc(first: T, second: T) -> Self {
137 Self(vec![first, second])
138 }
139
140 /// Returns an iterator over the slice.
141 ///
142 /// # Examples
143 ///
144 /// ```
145 /// # use spinoso_array::Array;
146 /// let ary = Array::from(&[1, 2, 4]);
147 /// let mut iterator = ary.iter();
148 ///
149 /// assert_eq!(iterator.next(), Some(&1));
150 /// assert_eq!(iterator.next(), Some(&2));
151 /// assert_eq!(iterator.next(), Some(&4));
152 /// assert_eq!(iterator.next(), None);
153 /// ```
154 #[inline]
155 pub fn iter(&self) -> Iter<'_, T> {
156 self.into_iter()
157 }
158
159 /// Returns an iterator that allows modifying each value.
160 ///
161 /// # Examples
162 ///
163 /// ```
164 /// # use spinoso_array::Array;
165 /// let mut ary = Array::from(&[1, 2, 4]);
166 /// for elem in ary.iter_mut() {
167 /// *elem += 2;
168 /// }
169 ///
170 /// assert_eq!(ary, &[3, 4, 6]);
171 /// ```
172 #[inline]
173 pub fn iter_mut(&mut self) -> IterMut<'_, T> {
174 self.into_iter()
175 }
176
177 /// Extracts a slice containing the entire vector.
178 ///
179 /// Equivalent to `&ary[..]`.
180 ///
181 /// # Examples
182 ///
183 /// ```
184 /// # use spinoso_array::Array;
185 /// let ary = Array::from(&[1, 2, 4]);
186 /// let four_index = ary.as_slice().binary_search(&4);
187 /// assert_eq!(four_index, Ok(2));
188 /// ```
189 #[inline]
190 #[must_use]
191 pub fn as_slice(&self) -> &[T] {
192 self.0.as_slice()
193 }
194
195 /// Extracts a mutable slice containing the entire vector.
196 ///
197 /// Equivalent to `&mut ary[..]`.
198 ///
199 /// # Examples
200 ///
201 /// ```
202 /// # use spinoso_array::Array;
203 /// let mut ary = Array::from(&[2, 1, 4]);
204 /// ary.as_mut_slice().sort();
205 /// assert_eq!(ary, &[1, 2, 4]);
206 /// ```
207 #[inline]
208 #[must_use]
209 pub fn as_mut_slice(&mut self) -> &mut [T] {
210 self.0.as_mut_slice()
211 }
212
213 /// Returns a raw pointer to the vector's buffer.
214 ///
215 /// The caller must ensure that the vector outlives the pointer this
216 /// function returns, or else it will end up pointing to garbage. Modifying
217 /// the vector may cause its buffer to be reallocated, which would also make
218 /// any pointers to it invalid.
219 ///
220 /// The caller must also ensure that the memory the pointer
221 /// (non-transitively) points to is never written to (except inside an
222 /// `UnsafeCell`) using this pointer or any pointer derived from it. If you
223 /// need to mutate the contents of the slice, use
224 /// [`as_mut_ptr`](Self::as_mut_ptr).
225 ///
226 /// # Examples
227 ///
228 /// ```
229 /// # use spinoso_array::Array;
230 /// let ary = Array::from(&[1, 2, 4]);
231 /// let ary_ptr = ary.as_ptr();
232 ///
233 /// unsafe {
234 /// for i in 0..ary.len() {
235 /// assert_eq!(*ary_ptr.add(i), 1 << i);
236 /// }
237 /// }
238 /// ```
239 #[inline]
240 #[must_use]
241 pub fn as_ptr(&self) -> *const T {
242 self.0.as_ptr()
243 }
244
245 /// Returns an unsafe mutable pointer to the vector's buffer.
246 ///
247 /// The caller must ensure that the vector outlives the pointer this
248 /// function returns, or else it will end up pointing to garbage.
249 /// Modifying the vector may cause its buffer to be reallocated, which would
250 /// also make any pointers to it invalid.
251 ///
252 /// # Examples
253 ///
254 /// This method is primarily used when mutating a `Array` via a raw pointer
255 /// passed over FFI.
256 ///
257 /// See the [`ARY_PTR`] macro in mruby.
258 ///
259 /// [`ARY_PTR`]: https://github.com/artichoke/mruby/blob/d66440864d08f1c3ac5820d45f11df031b7d43c6/include/mruby/array.h#L52
260 #[inline]
261 #[must_use]
262 pub fn as_mut_ptr(&mut self) -> *mut T {
263 self.0.as_mut_ptr()
264 }
265
266 /// Set the vector's length without dropping or moving out elements
267 ///
268 /// This method is unsafe because it changes the notion of the number of
269 /// "valid" elements in the vector. Use with care.
270 ///
271 /// # Safety
272 ///
273 /// - `new_len` must be less than or equal to `capacity()`.
274 /// - The elements at `old_len..new_len` must be initialized.
275 ///
276 /// # Examples
277 ///
278 /// This method is primarily used when mutating a `Array` via a raw pointer
279 /// passed over FFI.
280 ///
281 /// See the [`ARY_PTR`] macro in mruby.
282 ///
283 /// [`ARY_PTR`]: https://github.com/artichoke/mruby/blob/d66440864d08f1c3ac5820d45f11df031b7d43c6/include/mruby/array.h#L52
284 #[inline]
285 pub unsafe fn set_len(&mut self, new_len: usize) {
286 // SAFETY: The caller must uphold the documented safety contract, which
287 // is the same as each array's inner buffer.
288 unsafe {
289 self.0.set_len(new_len);
290 }
291 }
292
293 /// Creates an `Array<T>` directly from the raw components of another array.
294 ///
295 /// # Safety
296 ///
297 /// This is highly unsafe, due to the number of invariants that aren't
298 /// checked:
299 ///
300 /// - `ptr` needs to have been previously allocated via `Array<T>` (at
301 /// least, it's highly likely to be incorrect if it wasn't).
302 /// - `T` needs to have the same size and alignment as what `ptr` was
303 /// allocated with. (`T` having a less strict alignment is not sufficient,
304 /// the alignment really needs to be equal to satisfy the `dealloc`
305 /// requirement that memory must be allocated and deallocated with the
306 /// same layout.)
307 /// - `length` needs to be less than or equal to `capacity`.
308 /// - `capacity` needs to be the `capacity` that the pointer was allocated
309 /// with.
310 ///
311 /// Violating these may cause problems like corrupting the allocator's
312 /// internal data structures.
313 ///
314 /// The ownership of `ptr` is effectively transferred to the `Array<T>`
315 /// which may then deallocate, reallocate or change the contents of memory
316 /// pointed to by the pointer at will. Ensure that nothing else uses the
317 /// pointer after calling this function.
318 #[must_use]
319 pub unsafe fn from_raw_parts(raw_parts: RawParts<T>) -> Self {
320 // SAFETY: The caller must uphold the documented safety contract, which
321 // is the same as each array's inner buffer.
322 let vec = unsafe { raw_parts.into_vec() };
323 Self(vec)
324 }
325
326 /// Decomposes an `Array<T>` into its raw components.
327 ///
328 /// Returns the raw pointer to the underlying data, the length of the array
329 /// (in elements), and the allocated capacity of the data (in elements).
330 ///
331 /// After calling this function, the caller is responsible for the memory
332 /// previously managed by the `Array`. The only way to do this is to convert
333 /// the raw pointer, length, and capacity back into a `Array` with the
334 /// [`from_raw_parts`] function, allowing the destructor to perform the
335 /// cleanup.
336 ///
337 /// [`from_raw_parts`]: Array::from_raw_parts
338 #[must_use]
339 pub fn into_raw_parts(self) -> RawParts<T> {
340 RawParts::from_vec(self.0)
341 }
342
343 /// Consume the array and return the inner [`Vec<T>`].
344 ///
345 /// # Examples
346 ///
347 /// ```
348 /// # use spinoso_array::Array;
349 /// let ary = Array::from(&[1, 2, 4]);
350 /// let vec: Vec<i32> = ary.into_inner();
351 /// ```
352 ///
353 /// [`Vec<T>`]: alloc::vec::Vec
354 #[inline]
355 #[must_use]
356 pub fn into_inner(self) -> Vec<T> {
357 self.0
358 }
359
360 /// Consume the array and return its elements as a [`Vec<T>`].
361 ///
362 /// For `Array`, this is a cheap operation that unwraps the inner `Vec` and
363 /// is an alias for [`into_inner`](Self::into_inner).
364 ///
365 /// # Examples
366 ///
367 /// ```
368 /// # use spinoso_array::Array;
369 /// let ary = Array::from(&[1, 2, 4]);
370 /// let vec: Vec<i32> = ary.into_vec();
371 /// ```
372 ///
373 /// [`Vec<T>`]: alloc::vec::Vec
374 #[inline]
375 #[must_use]
376 pub fn into_vec(self) -> Vec<T> {
377 self.0
378 }
379
380 /// Converts the vector into [`Box<[T]>`](Box).
381 ///
382 /// This will drop any excess capacity.
383 ///
384 /// # Examples
385 ///
386 /// ```
387 /// # use spinoso_array::Array;
388 /// let ary = Array::from(&[1, 2, 4]);
389 /// let slice: Box<[i32]> = ary.into_boxed_slice();
390 /// ```
391 #[inline]
392 #[must_use]
393 pub fn into_boxed_slice(self) -> Box<[T]> {
394 self.0.into_boxed_slice()
395 }
396
397 /// Returns the number of elements the vector can hold without reallocating.
398 ///
399 /// # Examples
400 ///
401 /// ```
402 /// # use spinoso_array::Array;
403 /// let ary: Array<i32> = Array::with_capacity(10);
404 /// assert_eq!(ary.capacity(), 10);
405 /// ```
406 #[inline]
407 #[must_use]
408 pub fn capacity(&self) -> usize {
409 self.0.capacity()
410 }
411
412 /// Reserves capacity for at least `additional` more elements to be inserted
413 /// in the given `Array<T>`. The collection may reserve more space to avoid
414 /// frequent reallocations. After calling reserve, capacity will be greater
415 /// than or equal to `self.len() + additional`. Does nothing if capacity is
416 /// already sufficient.
417 ///
418 /// # Panics
419 ///
420 /// Panics if the new capacity overflows `usize`.
421 ///
422 /// # Examples
423 ///
424 /// ```
425 /// # use spinoso_array::Array;
426 /// let mut ary = Array::from(&[1]);
427 /// ary.reserve(10);
428 /// assert!(ary.capacity() >= 11);
429 /// ```
430 #[inline]
431 pub fn reserve(&mut self, additional: usize) {
432 self.0.reserve(additional);
433 }
434
435 /// Shrinks the capacity of the vector as much as possible.
436 ///
437 /// It will drop down as close as possible to the length but the allocator
438 /// may still inform the vector that there is space for a few more elements.
439 ///
440 /// # Examples
441 ///
442 /// ```
443 /// # use spinoso_array::Array;
444 /// let mut ary = Array::with_capacity(10);
445 /// ary.extend([1, 2, 3].iter().copied());
446 /// assert_eq!(ary.capacity(), 10);
447 /// ary.shrink_to_fit();
448 /// assert!(ary.capacity() >= 3);
449 /// ```
450 #[inline]
451 pub fn shrink_to_fit(&mut self) {
452 self.0.shrink_to_fit();
453 }
454
455 /// Clears the vector, removing all values.
456 ///
457 /// Note that this method has no effect on the allocated capacity of the
458 /// vector.
459 ///
460 /// # Examples
461 ///
462 /// ```
463 /// # use spinoso_array::Array;
464 /// let mut ary = Array::from(&[1, 2, 4]);
465 /// let capacity = ary.capacity();
466 /// ary.clear();
467 /// assert!(ary.is_empty());
468 /// assert_eq!(ary.capacity(), capacity);
469 /// ```
470 #[inline]
471 pub fn clear(&mut self) {
472 self.0.clear();
473 }
474
475 /// Returns the number of elements in the vector, also referred to as its
476 /// "length".
477 ///
478 /// # Examples
479 ///
480 /// ```
481 /// # use spinoso_array::Array;
482 /// let ary = Array::from(&[1, 2, 4]);
483 /// assert_eq!(ary.len(), 3);
484 /// ```
485 #[inline]
486 #[must_use]
487 pub fn len(&self) -> usize {
488 self.0.len()
489 }
490
491 /// Returns `true` if the vector contains no elements.
492 ///
493 /// # Examples
494 ///
495 /// ```
496 /// # use spinoso_array::Array;
497 /// let mut ary = Array::new();
498 /// assert!(ary.is_empty());
499 /// ary.push(1);
500 /// assert!(!ary.is_empty());
501 /// ```
502 #[inline]
503 #[must_use]
504 pub fn is_empty(&self) -> bool {
505 self.0.is_empty()
506 }
507
508 /// Returns a reference to an element at the index.
509 ///
510 /// Unlike [`Vec`], this method does not support indexing with a range. See
511 /// the [`slice`](Self::slice) method for retrieving a sub-slice from the
512 /// array.
513 ///
514 /// # Examples
515 ///
516 /// ```
517 /// # use spinoso_array::Array;
518 /// let ary = Array::from(&[1, 2, 4]);
519 /// assert_eq!(ary.get(1), Some(&2));
520 /// assert_eq!(ary.get(3), None);
521 /// ```
522 #[inline]
523 #[must_use]
524 pub fn get(&self, index: usize) -> Option<&T> {
525 self.0.get(index)
526 }
527
528 /// Deletes the element at the specified `index`, returning that element, or
529 /// [`None`] if the `index` is out of range.
530 ///
531 /// # Examples
532 ///
533 /// ```
534 /// # use spinoso_array::Array;
535 /// let mut ary = Array::from(&[1, 2, 4]);
536 /// assert_eq!(ary.delete_at(1), Some(2));
537 /// assert_eq!(ary.delete_at(10), None);
538 /// ```
539 #[inline]
540 #[must_use]
541 pub fn delete_at(&mut self, index: usize) -> Option<T> {
542 if index < self.0.len() {
543 Some(self.0.remove(index))
544 } else {
545 None
546 }
547 }
548
549 /// Returns the first element from the vector, or [`None`] if the vector is
550 /// empty.
551 ///
552 /// To retrieve a slice of the first elements in the vector, use
553 /// [`first_n`](Self::first_n).
554 ///
555 /// # Examples
556 ///
557 /// ```
558 /// # use spinoso_array::Array;
559 /// let mut ary = Array::new();
560 /// assert_eq!(ary.first(), None);
561 /// ary.push(1);
562 /// assert_eq!(ary.first(), Some(&1));
563 /// ary.push(2);
564 /// assert_eq!(ary.first(), Some(&1));
565 /// ```
566 #[inline]
567 #[must_use]
568 pub fn first(&self) -> Option<&T> {
569 self.0.first()
570 }
571
572 /// Returns up to `n` of the first elements from the vector, or `&[]` if the
573 /// vector is empty.
574 ///
575 /// To retrieve only the first element in the vector, use
576 /// [`first`](Self::first).
577 ///
578 /// # Examples
579 ///
580 /// ```
581 /// # use spinoso_array::Array;
582 /// let mut ary = Array::new();
583 /// assert_eq!(ary.first_n(0), &[]);
584 /// assert_eq!(ary.first_n(4), &[]);
585 ///
586 /// ary.push(1);
587 /// ary.push(2);
588 /// assert_eq!(ary.first_n(0), &[]);
589 /// assert_eq!(ary.first_n(4), &[1, 2]);
590 ///
591 /// ary.concat(&[3, 4, 5, 6, 7, 8, 9, 10]);
592 /// assert_eq!(ary.first_n(0), &[]);
593 /// assert_eq!(ary.first_n(4), &[1, 2, 3, 4]);
594 /// ```
595 #[inline]
596 #[must_use]
597 pub fn first_n(&self, n: usize) -> &[T] {
598 self.0.get(..n).unwrap_or_else(|| &self.0[..])
599 }
600
601 /// Returns the last element from the vector, or [`None`] if the vector is
602 /// empty.
603 ///
604 /// To retrieve a slice of the last elements in the vector, use
605 /// [`last_n`](Self::last_n).
606 ///
607 /// # Examples
608 ///
609 /// ```
610 /// # use spinoso_array::Array;
611 /// let mut ary = Array::new();
612 /// assert_eq!(ary.last(), None);
613 /// ary.push(1);
614 /// assert_eq!(ary.last(), Some(&1));
615 /// ary.push(2);
616 /// assert_eq!(ary.last(), Some(&2));
617 /// ```
618 #[inline]
619 #[must_use]
620 pub fn last(&self) -> Option<&T> {
621 self.0.last()
622 }
623
624 /// Returns up to `n` of the last elements from the vector, or `&[]` if the
625 /// vector is empty.
626 ///
627 /// To retrieve only the last element in the vector, use
628 /// [`last`](Self::last).
629 ///
630 /// # Examples
631 ///
632 /// ```
633 /// # use spinoso_array::Array;
634 /// let mut ary = Array::new();
635 /// assert_eq!(ary.last_n(0), &[]);
636 /// assert_eq!(ary.last_n(4), &[]);
637 ///
638 /// ary.push(1);
639 /// ary.push(2);
640 /// assert_eq!(ary.last_n(0), &[]);
641 /// assert_eq!(ary.last_n(4), &[1, 2]);
642 ///
643 /// ary.concat(&[3, 4, 5, 6, 7, 8, 9, 10]);
644 /// assert_eq!(ary.last_n(0), &[]);
645 /// assert_eq!(ary.last_n(4), &[7, 8, 9, 10]);
646 /// ```
647 #[inline]
648 #[must_use]
649 pub fn last_n(&self, n: usize) -> &[T] {
650 let begin = self.len().checked_sub(n).unwrap_or_default();
651 &self.0[begin..]
652 }
653
654 /// Returns a slice of the underlying vector that includes only the first
655 /// `n` elements.
656 ///
657 /// If `n` is greater than or equal to the length of the vector, `&self[..]`
658 /// is returned.
659 ///
660 /// The inverse of this operation is [`drop_n`](Self::drop_n).
661 ///
662 /// # Examples
663 ///
664 /// ```
665 /// # use spinoso_array::Array;
666 /// let ary = Array::from(&[1, 2, 4, 7, 8, 9]);
667 /// assert_eq!(ary.take_n(0), &[]);
668 /// assert_eq!(ary.take_n(2), &[1, 2]);
669 /// assert_eq!(ary.take_n(10), &[1, 2, 4, 7, 8, 9]);
670 /// ```
671 #[inline]
672 #[must_use]
673 pub fn take_n(&self, n: usize) -> &[T] {
674 self.0.get(..n).unwrap_or_else(|| &self.0[..])
675 }
676
677 /// Returns a slice of the underlying vector that excludes the first `n`
678 /// elements.
679 ///
680 /// If `n` is greater than or equal to the length of the vector, `&[]` is
681 /// returned.
682 ///
683 /// The inverse of this operation is [`take_n`](Self::take_n).
684 ///
685 /// # Examples
686 ///
687 /// ```
688 /// # use spinoso_array::Array;
689 /// let ary = Array::from(&[1, 2, 4, 7, 8, 9]);
690 /// assert_eq!(ary.drop_n(0), &[1, 2, 4, 7, 8, 9]);
691 /// assert_eq!(ary.drop_n(4), &[8, 9]);
692 /// assert_eq!(ary.drop_n(10), &[]);
693 /// ```
694 #[inline]
695 #[must_use]
696 pub fn drop_n(&self, n: usize) -> &[T] {
697 self.0.get(n..).unwrap_or_default()
698 }
699
700 /// Removes the last element from the vector and returns it, or [`None`] if
701 /// the vector is empty.
702 ///
703 /// To pop more than one element from the end of the vector, use
704 /// [`pop_n`](Self::pop_n).
705 ///
706 /// # Examples
707 ///
708 /// ```
709 /// # use spinoso_array::Array;
710 /// let mut ary = Array::from(&[1, 2, 4]);
711 /// assert_eq!(ary.pop(), Some(4));
712 /// assert_eq!(ary, &[1, 2]);
713 /// ```
714 #[inline]
715 #[must_use]
716 pub fn pop(&mut self) -> Option<T> {
717 self.0.pop()
718 }
719
720 /// Removes the last `n` elements from the vector.
721 ///
722 /// To pop a single element from the end of the vector, use
723 /// [`pop`](Self::pop).
724 ///
725 /// # Examples
726 ///
727 /// ```
728 /// # use spinoso_array::Array;
729 /// let mut ary = Array::from(&[1, 2, 4, 7, 8, 9]);
730 /// assert_eq!(ary.pop_n(0), &[]);
731 /// assert_eq!(ary, &[1, 2, 4, 7, 8, 9]);
732 ///
733 /// assert_eq!(ary.pop_n(3), &[7, 8, 9]);
734 /// assert_eq!(ary, &[1, 2, 4]);
735 ///
736 /// assert_eq!(ary.pop_n(100), &[1, 2, 4]);
737 /// assert!(ary.is_empty());
738 ///
739 /// assert_eq!(ary.pop_n(1), &[]);
740 /// assert!(ary.is_empty());
741 /// ```
742 #[inline]
743 #[must_use]
744 pub fn pop_n(&mut self, n: usize) -> Self {
745 if n == 0 {
746 return Self::new();
747 }
748 let split_at = self.len().checked_sub(n).unwrap_or_default();
749 let popped = self.0.split_off(split_at);
750 Self(popped)
751 }
752
753 /// Appends an element to the back of the vector.
754 ///
755 /// To push more than one element to the end of the vector, use
756 /// [`concat`](Self::concat) or `extend`.
757 ///
758 /// # Panics
759 ///
760 /// Panics if the number of elements in the vector overflows a `usize`.
761 ///
762 /// # Examples
763 ///
764 /// ```
765 /// # use spinoso_array::Array;
766 /// let mut ary = Array::from(&[1, 2]);
767 /// ary.push(3);
768 /// assert_eq!(ary, &[1, 2, 3]);
769 /// ```
770 #[inline]
771 pub fn push(&mut self, elem: T) {
772 self.0.push(elem);
773 }
774
775 /// Reverses the order of elements of the vector, in place.
776 ///
777 /// # Examples
778 ///
779 /// ```
780 /// # use spinoso_array::Array;
781 /// let mut ary = Array::from(&[1, 2, 4]);
782 /// ary.reverse();
783 /// assert_eq!(ary, &[4, 2, 1]);
784 /// ```
785 #[inline]
786 pub fn reverse(&mut self) {
787 self.0.reverse();
788 }
789
790 /// Removes the first element of the vector and returns it (shifting all
791 /// other elements down by one). Returns [`None`] if the vector is empty.
792 ///
793 /// This operation is also known as "pop front".
794 ///
795 /// To remove more than one element from the front of the vector, use
796 /// [`shift_n`](Self::shift_n).
797 ///
798 /// # Examples
799 ///
800 /// ```
801 /// # use spinoso_array::Array;
802 /// let mut ary = Array::from(&[1, 2]);
803 /// assert_eq!(ary.shift(), Some(1));
804 /// assert_eq!(ary.shift(), Some(2));
805 /// assert_eq!(ary.shift(), None);
806 /// ```
807 #[inline]
808 #[must_use]
809 pub fn shift(&mut self) -> Option<T> {
810 if self.is_empty() { None } else { Some(self.0.remove(0)) }
811 }
812
813 /// Removes the first `n` elements from the vector.
814 ///
815 /// To shift a single element from the front of the vector, use
816 /// [`shift`](Self::shift).
817 ///
818 /// # Examples
819 ///
820 /// ```
821 /// # use spinoso_array::Array;
822 /// let mut ary = Array::from(&[1, 2, 4, 7, 8, 9]);
823 /// assert_eq!(ary.shift_n(0), &[]);
824 /// assert_eq!(ary, &[1, 2, 4, 7, 8, 9]);
825 ///
826 /// assert_eq!(ary.shift_n(3), &[1, 2, 4]);
827 /// assert_eq!(ary, &[7, 8, 9]);
828 ///
829 /// assert_eq!(ary.shift_n(100), &[7, 8, 9]);
830 /// assert!(ary.is_empty());
831 ///
832 /// assert_eq!(ary.shift_n(1), &[]);
833 /// assert!(ary.is_empty());
834 /// ```
835 #[inline]
836 #[must_use]
837 pub fn shift_n(&mut self, n: usize) -> Self {
838 match n {
839 0 => Self::new(),
840 n if n < self.0.len() => self.0.drain(..n).collect(),
841 _ => {
842 let shifted = self.0.split_off(0);
843 Self(shifted)
844 }
845 }
846 }
847
848 /// Inserts an element to the front of the vector.
849 ///
850 /// To insert more than one element to the front of the vector, use
851 /// [`unshift_n`](Self::unshift_n).
852 ///
853 /// This operation is also known as "prepend".
854 ///
855 /// # Panics
856 ///
857 /// Panics if the number of elements in the vector overflows a `usize`.
858 ///
859 /// # Examples
860 ///
861 /// ```
862 /// # use spinoso_array::Array;
863 /// let mut ary = Array::from(&[1, 2]);
864 /// ary.unshift(3);
865 /// assert_eq!(ary, &[3, 1, 2]);
866 /// ```
867 #[inline]
868 pub fn unshift(&mut self, elem: T) {
869 self.0.insert(0, elem);
870 }
871
872 /// Return a reference to a subslice of the vector.
873 ///
874 /// This function always returns a slice. If the range specified by `start`
875 /// and `end` overlaps the vector (even if only partially), the overlapping
876 /// slice is returned. If the range does not overlap the vector, an empty
877 /// slice is returned.
878 ///
879 /// # Examples
880 ///
881 /// ```
882 /// # use spinoso_array::Array;
883 /// let empty: Array<i32> = Array::new();
884 /// assert_eq!(empty.slice(0, 0), &[]);
885 /// assert_eq!(empty.slice(0, 4), &[]);
886 /// assert_eq!(empty.slice(2, 4), &[]);
887 ///
888 /// let ary = Array::from(&[1, 2, 3]);
889 /// assert_eq!(ary.slice(0, 0), &[]);
890 /// assert_eq!(ary.slice(0, 4), &[1, 2, 3]);
891 /// assert_eq!(ary.slice(2, 0), &[]);
892 /// assert_eq!(ary.slice(2, 4), &[3]);
893 /// assert_eq!(ary.slice(10, 100), &[]);
894 /// ```
895 #[inline]
896 #[must_use]
897 pub fn slice(&self, start: usize, len: usize) -> &[T] {
898 if self.0.is_empty() || len == 0 {
899 return &[];
900 }
901 if let Some(end) = start.checked_add(len) {
902 self.0
903 .get(start..end)
904 .or_else(|| self.0.get(start..))
905 .unwrap_or_default()
906 } else {
907 self.0.get(start..).unwrap_or_default()
908 }
909 }
910}
911
912impl<T> Array<T>
913where
914 T: Clone,
915{
916 /// Construct a new `Array<T>` with length `len` and all elements set to
917 /// `default`. The `Array` will have capacity `len`.
918 ///
919 /// # Examples
920 ///
921 /// ```
922 /// # use spinoso_array::Array;
923 /// let ary: Array<&str> = Array::with_len_and_default(3, "spinoso");
924 /// assert_eq!(ary.len(), 3);
925 /// assert_eq!(ary.capacity(), 3);
926 /// assert_eq!(ary, &["spinoso", "spinoso", "spinoso"]);
927 /// ```
928 #[inline]
929 #[must_use]
930 pub fn with_len_and_default(len: usize, default: T) -> Self {
931 Self(vec![default; len])
932 }
933
934 /// Appends the elements of `other` to self.
935 ///
936 /// Slice version of `extend`. This operation is analogous to "push n".
937 ///
938 /// # Examples
939 ///
940 /// ```
941 /// # use spinoso_array::Array;
942 /// let mut ary = Array::from(&[1, 2, 4]);
943 /// ary.concat(&[7, 8, 9]);
944 /// assert_eq!(ary.len(), 6);
945 /// ```
946 #[inline]
947 pub fn concat(&mut self, other: &[T]) {
948 self.0.extend_from_slice(other);
949 }
950
951 /// Prepends the elements of `other` to self.
952 ///
953 /// To insert one element to the front of the vector, use
954 /// [`unshift`](Self::unshift).
955 ///
956 /// This operation is also known as "prepend".
957 ///
958 /// # Panics
959 ///
960 /// Panics if the number of elements in the vector overflows a `usize`.
961 ///
962 /// # Examples
963 ///
964 /// ```
965 /// # use spinoso_array::Array;
966 /// let mut ary = Array::from(&[1, 2]);
967 /// ary.unshift_n(&[0, 5, 9]);
968 /// assert_eq!(ary, &[0, 5, 9, 1, 2]);
969 /// ```
970 #[inline]
971 pub fn unshift_n(&mut self, other: &[T]) {
972 self.0.reserve(other.len());
973 let mut tail = self.0.split_off(0);
974 self.0.extend_from_slice(other);
975 self.0.append(&mut tail);
976 }
977}
978
979impl<T> Array<T>
980where
981 T: Copy,
982{
983 /// Creates a new array by repeating this array `n` times.
984 ///
985 /// This function will not panic. If the resulting `Array`'s capacity would
986 /// overflow, [`None`] is returned.
987 ///
988 /// # Examples
989 ///
990 /// Basic usage:
991 ///
992 /// ```
993 /// # use spinoso_array::Array;
994 /// # fn example() -> Option<()> {
995 /// let mut ary = Array::from(&[1, 2]);
996 /// let repeated_ary = ary.repeat(3)?;
997 /// assert_eq!(repeated_ary, &[1, 2, 1, 2, 1, 2]);
998 /// # Some(())
999 /// # }
1000 /// # example().unwrap();
1001 /// ```
1002 ///
1003 /// [`None`] should be returned on overflow:
1004 ///
1005 /// ```
1006 /// # use spinoso_array::Array;
1007 /// let mut ary = Array::from(&[1, 2]);
1008 /// let repeated_ary = ary.repeat(usize::MAX);
1009 /// assert_eq!(repeated_ary, None);
1010 /// ```
1011 #[must_use]
1012 pub fn repeat(&self, n: usize) -> Option<Self> {
1013 let slice = self.0.as_slice();
1014 if slice.len().checked_mul(n).is_some() {
1015 Some(Self::from(slice.repeat(n)))
1016 } else {
1017 None
1018 }
1019 }
1020}
1021
1022impl<T> Array<T>
1023where
1024 T: Default,
1025{
1026 /// Set element at position `index` within the vector, extending the vector
1027 /// with `T::default()` if `index` is out of bounds.
1028 ///
1029 /// # Panics
1030 ///
1031 /// If inserting the element would overflow the capacity of the vector, this
1032 /// method will panic.
1033 ///
1034 /// # Examples
1035 ///
1036 /// ```
1037 /// # use spinoso_array::Array;
1038 /// let mut ary = Array::from(&[1, 2, 4]);
1039 /// ary.set(1, 11);
1040 /// assert_eq!(ary, &[1, 11, 4]);
1041 /// ary.set(5, 263);
1042 /// assert_eq!(ary, &[1, 11, 4, 0, 0, 263]);
1043 ///
1044 /// let mut ary: Array<i32> = Array::from(&[]);
1045 /// ary.set(5, 11);
1046 /// assert_eq!(ary, &[0, 0, 0, 0, 0, 11]);
1047 /// ```
1048 #[inline]
1049 pub fn set(&mut self, index: usize, elem: T) {
1050 if let Some(cell) = self.0.get_mut(index) {
1051 *cell = elem;
1052 } else {
1053 let buflen = self.len();
1054 // index is *at least* `buflen`, so this calculation never underflows
1055 // and ensures we allocate an additional slot.
1056 let additional = (index - buflen).checked_add(1).expect("capacity overflow");
1057 self.0.reserve(additional);
1058 self.0.resize_with(index, T::default);
1059 self.0.push(elem);
1060 }
1061 }
1062
1063 /// Insert element at position `start` within the vector and remove the
1064 /// following `drain` elements. If `start` is out of bounds, the vector will
1065 /// be extended with `T::default()`.
1066 ///
1067 /// This method sets a slice of the `Array` to a single element, including
1068 /// the zero-length slice. It is similar in intent to calling
1069 /// [`Vec::splice`] with a one-element iterator.
1070 ///
1071 /// `set_with_drain` will only drain up to the end of the vector.
1072 ///
1073 /// To set a single element without draining, use [`set`](Self::set).
1074 ///
1075 /// # Panics
1076 ///
1077 /// If inserting the element would overflow the capacity of the vector, this
1078 /// method will panic.
1079 ///
1080 /// # Examples
1081 ///
1082 /// ```
1083 /// # use spinoso_array::Array;
1084 /// let mut ary = Array::from(&[1, 2, 4]);
1085 /// ary.set_with_drain(1, 0, 10);
1086 /// assert_eq!(ary, &[1, 10, 2, 4]);
1087 /// ary.set_with_drain(2, 5, 20);
1088 /// assert_eq!(ary, &[1, 10, 20]);
1089 /// ary.set_with_drain(5, 5, 30);
1090 /// assert_eq!(ary, &[1, 10, 20, 0, 0, 30]);
1091 /// ```
1092 #[inline]
1093 pub fn set_with_drain(&mut self, start: usize, drain: usize, elem: T) -> usize {
1094 let buflen = self.0.len();
1095 let drained = cmp::min(buflen.checked_sub(start).unwrap_or_default(), drain);
1096
1097 if let Some(cell) = self.0.get_mut(start) {
1098 match drain {
1099 0 => self.0.insert(start, elem),
1100 1 => *cell = elem,
1101 _ => {
1102 *cell = elem;
1103 let drain_end_idx = cmp::min(start.saturating_add(drain), buflen);
1104 self.0.drain(start.saturating_add(1)..drain_end_idx);
1105 }
1106 }
1107 } else {
1108 // start is *at least* `buflen`, so this calculation never underflows
1109 // and ensures we allocate an additional slot.
1110 let additional = (start - buflen).checked_add(1).expect("capacity overflow");
1111 self.0.reserve(additional);
1112 self.0.resize_with(start, T::default);
1113 self.0.push(elem);
1114 }
1115
1116 drained
1117 }
1118}
1119
1120impl<T> Array<T>
1121where
1122 T: Default + Clone,
1123{
1124 /// Insert the elements from a slice at a position `index` in the vector,
1125 /// extending the vector with `T::default()` if `index` is out of bounds.
1126 ///
1127 /// This method is similar to [`Vec::splice`] when called with a zero-length
1128 /// range.
1129 ///
1130 /// # Panics
1131 ///
1132 /// If inserting the slice would overflow the capacity of the vector, this
1133 /// method will panic.
1134 ///
1135 /// # Examples
1136 ///
1137 /// ```
1138 /// # use spinoso_array::Array;
1139 /// let mut ary = Array::from(&[1, 2, 4]);
1140 /// ary.insert_slice(1, &[7, 8, 9]);
1141 /// assert_eq!(ary, &[1, 7, 8, 9, 2, 4]);
1142 /// ary.insert_slice(8, &[100, 200]);
1143 /// assert_eq!(ary, &[1, 7, 8, 9, 2, 4, 0, 0, 100, 200]);
1144 /// ```
1145 #[inline]
1146 pub fn insert_slice(&mut self, index: usize, values: &[T]) {
1147 if let Some(overflow) = index.checked_sub(self.0.len()) {
1148 let additional = overflow.checked_add(values.len()).expect("capacity overflow");
1149 self.0.reserve(additional);
1150 self.0.resize_with(index, T::default);
1151 } else {
1152 self.0.reserve(values.len());
1153 }
1154 if index == self.0.len() {
1155 self.0.extend_from_slice(values);
1156 } else {
1157 let mut tail = self.0.split_off(index);
1158 self.0.extend_from_slice(values);
1159 self.0.append(&mut tail);
1160 }
1161 }
1162
1163 /// Insert the elements from a slice at a position `index` in the vector and
1164 /// remove the following `drain` elements. The vector is extended with
1165 /// `T::default()` if `index` is out of bounds.
1166 ///
1167 /// This method is similar to [`Vec::splice`] when called with a
1168 /// nonzero-length range.
1169 ///
1170 /// When called with `drain == 0`, this method is equivalent to
1171 /// [`insert_slice`](Self::insert_slice).
1172 ///
1173 /// If `drain >= src.len()` or the tail of the vector is replaced, this
1174 /// method is efficient. Otherwise, a temporary buffer is used to move the
1175 /// elements.
1176 ///
1177 /// # Panics
1178 ///
1179 /// If inserting the slice would overflow the capacity of the vector, this
1180 /// method will panic.
1181 ///
1182 /// # Examples
1183 ///
1184 /// ```
1185 /// # use spinoso_array::Array;
1186 /// let mut ary = Array::from(&[1, 2, 4]);
1187 /// ary.set_slice(1, 5, &[7, 8, 9]);
1188 /// assert_eq!(ary, &[1, 7, 8, 9]);
1189 /// ary.set_slice(6, 1, &[100, 200]);
1190 /// assert_eq!(ary, &[1, 7, 8, 9, 0, 0, 100, 200]);
1191 /// ```
1192 #[inline]
1193 pub fn set_slice(&mut self, index: usize, drain: usize, values: &[T]) -> usize {
1194 let buflen = self.0.len();
1195 let drained = cmp::min(buflen.checked_sub(index).unwrap_or_default(), drain);
1196
1197 if let Some(overflow) = index.checked_sub(self.0.len()) {
1198 let additional = overflow.saturating_add(values.len());
1199 self.0.reserve(additional);
1200 self.0.resize_with(index, T::default);
1201 }
1202 if index == self.0.len() {
1203 self.0.extend_from_slice(values);
1204 } else {
1205 self.0
1206 .splice(index..index.saturating_add(drained), values.iter().cloned());
1207 }
1208
1209 drained
1210 }
1211}
1212
1213#[cfg(test)]
1214#[expect(
1215 clippy::undocumented_unsafe_blocks,
1216 reason = "avoid linenoise when testing unsafe fn"
1217)]
1218mod test {
1219 use crate::array::vec::{Array, RawParts};
1220
1221 // `insert_slice`
1222
1223 #[test]
1224 fn non_empty_array_insert_slice_end_empty() {
1225 let mut ary = Array::from([1, 2, 3, 4, 5]);
1226 ary.insert_slice(5, &[]);
1227 assert_eq!(ary, [1, 2, 3, 4, 5]);
1228 }
1229
1230 #[test]
1231 fn non_empty_array_insert_slice_out_of_bounds_empty() {
1232 let mut ary = Array::from([1, 2, 3, 4, 5]);
1233 ary.insert_slice(10, &[]);
1234 assert_eq!(ary, [1, 2, 3, 4, 5, 0, 0, 0, 0, 0]);
1235 }
1236
1237 #[test]
1238 fn non_empty_array_insert_slice_interior_empty() {
1239 let mut ary = Array::from([1, 2, 3, 4, 5]);
1240 ary.insert_slice(2, &[]);
1241 assert_eq!(ary, [1, 2, 3, 4, 5]);
1242 }
1243
1244 #[test]
1245 fn non_empty_array_insert_slice_begin_empty() {
1246 let mut ary = Array::from([1, 2, 3, 4, 5]);
1247 ary.insert_slice(0, &[]);
1248 assert_eq!(ary, [1, 2, 3, 4, 5]);
1249 }
1250
1251 #[test]
1252 fn empty_array_insert_slice_end_empty() {
1253 let mut ary = Array::<i32>::new();
1254 ary.insert_slice(0, &[]);
1255 assert_eq!(ary, []);
1256 }
1257
1258 #[test]
1259 fn empty_array_insert_slice_out_of_bounds_empty() {
1260 let mut ary = Array::<i32>::new();
1261 ary.insert_slice(10, &[]);
1262 assert_eq!(ary, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
1263 }
1264
1265 #[test]
1266 fn empty_array_insert_slice_begin_empty() {
1267 let mut ary = Array::<i32>::new();
1268 ary.insert_slice(0, &[]);
1269 assert_eq!(ary, []);
1270 }
1271
1272 #[test]
1273 fn non_empty_array_insert_slice_end() {
1274 let mut ary = Array::from([1, 2, 3, 4, 5]);
1275 ary.insert_slice(5, &[8, 9, 10]);
1276 assert_eq!(ary, [1, 2, 3, 4, 5, 8, 9, 10]);
1277 }
1278
1279 #[test]
1280 fn non_empty_array_insert_slice_out_of_bounds() {
1281 let mut ary = Array::from([1, 2, 3, 4, 5]);
1282 ary.insert_slice(10, &[8, 9, 10]);
1283 assert_eq!(ary, [1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 8, 9, 10]);
1284 }
1285
1286 #[test]
1287 fn non_empty_array_insert_slice_interior() {
1288 let mut ary = Array::from([1, 2, 3, 4, 5]);
1289 ary.insert_slice(2, &[8, 9, 10]);
1290 assert_eq!(ary, [1, 2, 8, 9, 10, 3, 4, 5]);
1291 }
1292
1293 #[test]
1294 fn non_empty_array_insert_slice_begin() {
1295 let mut ary = Array::from([1, 2, 3, 4, 5]);
1296 ary.insert_slice(0, &[8, 9, 10]);
1297 assert_eq!(ary, [8, 9, 10, 1, 2, 3, 4, 5]);
1298 }
1299
1300 #[test]
1301 fn empty_array_insert_slice_end() {
1302 let mut ary = Array::<i32>::new();
1303 ary.insert_slice(0, &[8, 9, 10]);
1304 assert_eq!(ary, [8, 9, 10]);
1305 }
1306
1307 #[test]
1308 fn empty_array_insert_slice_out_of_bounds() {
1309 let mut ary = Array::<i32>::new();
1310 ary.insert_slice(10, &[8, 9, 10]);
1311 assert_eq!(ary, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 9, 10]);
1312 }
1313
1314 #[test]
1315 fn empty_array_insert_slice_begin() {
1316 let mut ary = Array::<i32>::new();
1317 ary.insert_slice(0, &[8, 9, 10]);
1318 assert_eq!(ary, [8, 9, 10]);
1319 }
1320
1321 // `set_slice`
1322
1323 #[test]
1324 fn non_empty_array_set_slice_end_empty_drain_0() {
1325 let mut ary = Array::from([1, 2, 3, 4, 5]);
1326 let drained = ary.set_slice(5, 0, &[]);
1327 assert_eq!(drained, 0);
1328 assert_eq!(ary, [1, 2, 3, 4, 5]);
1329 }
1330
1331 #[test]
1332 fn non_empty_array_set_slice_end_empty_drain_less_than_insert_length() {
1333 let mut ary = Array::from([1, 2, 3, 4, 5]);
1334 let drained = ary.set_slice(5, 0, &[]);
1335 assert_eq!(drained, 0);
1336 assert_eq!(ary, [1, 2, 3, 4, 5]);
1337 }
1338
1339 #[test]
1340 fn non_empty_array_set_slice_end_empty_drain_equal_to_insert_length() {
1341 let mut ary = Array::from([1, 2, 3, 4, 5]);
1342 let drained = ary.set_slice(5, 0, &[]);
1343 assert_eq!(drained, 0);
1344 assert_eq!(ary, [1, 2, 3, 4, 5]);
1345 }
1346
1347 #[test]
1348 fn non_empty_array_set_slice_end_empty_drain_greater_than_insert_length() {
1349 let mut ary = Array::from([1, 2, 3, 4, 5]);
1350 let drained = ary.set_slice(5, 5, &[]);
1351 assert_eq!(drained, 0);
1352 assert_eq!(ary, [1, 2, 3, 4, 5]);
1353 }
1354
1355 #[test]
1356 fn non_empty_array_set_slice_out_of_bounds_empty_drain_0() {
1357 let mut ary = Array::from([1, 2, 3, 4, 5]);
1358 let drained = ary.set_slice(10, 0, &[]);
1359 assert_eq!(drained, 0);
1360 assert_eq!(ary, [1, 2, 3, 4, 5, 0, 0, 0, 0, 0]);
1361 }
1362
1363 #[test]
1364 fn non_empty_array_set_slice_out_of_bounds_empty_drain_less_than_insert_length() {
1365 let mut ary = Array::from([1, 2, 3, 4, 5]);
1366 let drained = ary.set_slice(10, 0, &[]);
1367 assert_eq!(drained, 0);
1368 assert_eq!(ary, [1, 2, 3, 4, 5, 0, 0, 0, 0, 0]);
1369 }
1370
1371 #[test]
1372 fn non_empty_array_set_slice_out_of_bounds_empty_drain_equal_to_insert_length() {
1373 let mut ary = Array::from([1, 2, 3, 4, 5]);
1374 let drained = ary.set_slice(10, 0, &[]);
1375 assert_eq!(drained, 0);
1376 assert_eq!(ary, [1, 2, 3, 4, 5, 0, 0, 0, 0, 0]);
1377 }
1378
1379 #[test]
1380 fn non_empty_array_set_slice_out_of_bounds_empty_drain_greater_than_insert_length() {
1381 let mut ary = Array::from([1, 2, 3, 4, 5]);
1382 let drained = ary.set_slice(10, 0, &[]);
1383 assert_eq!(drained, 0);
1384 assert_eq!(ary, [1, 2, 3, 4, 5, 0, 0, 0, 0, 0]);
1385 }
1386
1387 #[test]
1388 fn non_empty_array_set_slice_interior_empty_drain_0() {
1389 let mut ary = Array::from([1, 2, 3, 4, 5]);
1390 let drained = ary.set_slice(1, 0, &[]);
1391 assert_eq!(drained, 0);
1392 assert_eq!(ary, [1, 2, 3, 4, 5]);
1393 }
1394
1395 #[test]
1396 fn non_empty_array_set_slice_interior_empty_drain_less_than_insert_length() {
1397 let mut ary = Array::from([1, 2, 3, 4, 5]);
1398 let drained = ary.set_slice(1, 0, &[]);
1399 assert_eq!(drained, 0);
1400 assert_eq!(ary, [1, 2, 3, 4, 5]);
1401 }
1402
1403 #[test]
1404 fn non_empty_array_set_slice_interior_empty_drain_equal_to_insert_length() {
1405 let mut ary = Array::from([1, 2, 3, 4, 5]);
1406 let drained = ary.set_slice(1, 0, &[]);
1407 assert_eq!(drained, 0);
1408 assert_eq!(ary, [1, 2, 3, 4, 5]);
1409 }
1410
1411 #[test]
1412 fn non_empty_array_set_slice_interior_empty_drain_greater_than_insert_length() {
1413 let mut ary = Array::from([1, 2, 3, 4, 5]);
1414 let drained = ary.set_slice(1, 2, &[]);
1415 assert_eq!(drained, 2);
1416 assert_eq!(ary, [1, 4, 5]);
1417 }
1418
1419 #[test]
1420 fn non_empty_array_set_slice_interior_empty_drain_greater_than_insert_length_to_end() {
1421 let mut ary = Array::from([1, 2, 3, 4, 5]);
1422 let drained = ary.set_slice(1, 4, &[]);
1423 assert_eq!(drained, 4);
1424 assert_eq!(ary, [1]);
1425 }
1426
1427 #[test]
1428 fn non_empty_array_set_slice_interior_empty_drain_greater_than_insert_length_overrun_end() {
1429 let mut ary = Array::from([1, 2, 3, 4, 5]);
1430 let drained = ary.set_slice(1, 10, &[]);
1431 assert_eq!(drained, 4);
1432 assert_eq!(ary, [1]);
1433 }
1434
1435 #[test]
1436 fn non_empty_array_set_slice_end_non_empty_drain_0() {
1437 let mut ary = Array::from([1, 2, 3, 4, 5]);
1438 let drained = ary.set_slice(5, 0, &[7, 8, 9]);
1439 assert_eq!(drained, 0);
1440 assert_eq!(ary, [1, 2, 3, 4, 5, 7, 8, 9]);
1441 }
1442
1443 #[test]
1444 fn non_empty_array_set_slice_end_non_empty_drain_less_than_insert_length() {
1445 let mut ary = Array::from([1, 2, 3, 4, 5]);
1446 let drained = ary.set_slice(5, 2, &[7, 8, 9]);
1447 assert_eq!(drained, 0);
1448 assert_eq!(ary, [1, 2, 3, 4, 5, 7, 8, 9]);
1449 }
1450
1451 #[test]
1452 fn non_empty_array_set_slice_end_non_empty_drain_equal_to_insert_length() {
1453 let mut ary = Array::from([1, 2, 3, 4, 5]);
1454 let drained = ary.set_slice(5, 3, &[7, 8, 9]);
1455 assert_eq!(drained, 0);
1456 assert_eq!(ary, [1, 2, 3, 4, 5, 7, 8, 9]);
1457 }
1458
1459 #[test]
1460 fn non_empty_array_set_slice_end_non_empty_drain_greater_than_insert_length() {
1461 let mut ary = Array::from([1, 2, 3, 4, 5]);
1462 let drained = ary.set_slice(5, 5, &[7, 8, 9]);
1463 assert_eq!(drained, 0);
1464 assert_eq!(ary, [1, 2, 3, 4, 5, 7, 8, 9]);
1465 }
1466
1467 #[test]
1468 fn non_empty_array_set_slice_out_of_bounds_non_empty_drain_0() {
1469 let mut ary = Array::from([1, 2, 3, 4, 5]);
1470 let drained = ary.set_slice(10, 0, &[7, 8, 9]);
1471 assert_eq!(drained, 0);
1472 assert_eq!(ary, [1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 7, 8, 9]);
1473 }
1474
1475 #[test]
1476 fn non_empty_array_set_slice_out_of_bounds_non_empty_drain_less_than_insert_length() {
1477 let mut ary = Array::from([1, 2, 3, 4, 5]);
1478 let drained = ary.set_slice(10, 2, &[7, 8, 9]);
1479 assert_eq!(drained, 0);
1480 assert_eq!(ary, [1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 7, 8, 9]);
1481 }
1482
1483 #[test]
1484 fn non_empty_array_set_slice_out_of_bounds_non_empty_drain_equal_to_insert_length() {
1485 let mut ary = Array::from([1, 2, 3, 4, 5]);
1486 let drained = ary.set_slice(10, 3, &[7, 8, 9]);
1487 assert_eq!(drained, 0);
1488 assert_eq!(ary, [1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 7, 8, 9]);
1489 }
1490
1491 #[test]
1492 fn non_empty_array_set_slice_out_of_bounds_non_empty_drain_greater_than_insert_length() {
1493 let mut ary = Array::from([1, 2, 3, 4, 5]);
1494 let drained = ary.set_slice(10, 5, &[7, 8, 9]);
1495 assert_eq!(drained, 0);
1496 assert_eq!(ary, [1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 7, 8, 9]);
1497 }
1498
1499 #[test]
1500 fn non_empty_array_set_slice_interior_non_empty_drain_0() {
1501 let mut ary = Array::from([1, 2, 3, 4, 5]);
1502 let drained = ary.set_slice(1, 0, &[7, 8, 9]);
1503 assert_eq!(drained, 0);
1504 assert_eq!(ary, [1, 7, 8, 9, 2, 3, 4, 5]);
1505 }
1506
1507 #[test]
1508 fn non_empty_array_set_slice_interior_non_empty_drain_less_than_insert_length() {
1509 let mut ary = Array::from([1, 2, 3, 4, 5]);
1510 let drained = ary.set_slice(1, 2, &[7, 8, 9]);
1511 assert_eq!(drained, 2);
1512 assert_eq!(ary, [1, 7, 8, 9, 4, 5]);
1513 }
1514
1515 #[test]
1516 fn non_empty_array_set_slice_interior_non_empty_drain_equal_to_insert_length() {
1517 let mut ary = Array::from([1, 2, 3, 4, 5]);
1518 let drained = ary.set_slice(1, 3, &[7, 8, 9]);
1519 assert_eq!(drained, 3);
1520 assert_eq!(ary, [1, 7, 8, 9, 5]);
1521 }
1522
1523 #[test]
1524 fn non_empty_array_set_slice_interior_non_empty_drain_greater_than_insert_length() {
1525 let mut ary = Array::from([1, 2, 3, 4, 5, 6]);
1526 let drained = ary.set_slice(1, 4, &[7, 8, 9]);
1527 assert_eq!(drained, 4);
1528 assert_eq!(ary, [1, 7, 8, 9, 6]);
1529 assert_eq!(ary.len(), 5);
1530 }
1531
1532 #[test]
1533 fn non_empty_array_set_slice_interior_non_empty_drain_equal_to_insert_length_to_tail() {
1534 let mut ary = Array::from([1, 2, 3, 4]);
1535 let drained = ary.set_slice(1, 3, &[7, 8, 9]);
1536 assert_eq!(drained, 3);
1537 assert_eq!(ary, [1, 7, 8, 9]);
1538 }
1539
1540 #[test]
1541 fn non_empty_array_set_slice_interior_non_empty_drain_greater_than_insert_length_to_tail() {
1542 let mut ary = Array::from([1, 2, 3, 4]);
1543 let drained = ary.set_slice(1, 10, &[7, 8, 9]);
1544 assert_eq!(drained, 3);
1545 assert_eq!(ary, [1, 7, 8, 9]);
1546 }
1547
1548 #[test]
1549 fn non_empty_array_set_slice_interior_non_empty_drain_less_than_insert_length_overrun_tail() {
1550 let mut ary = Array::from([1, 2, 3, 4]);
1551 let drained = ary.set_slice(3, 2, &[7, 8, 9]);
1552 assert_eq!(drained, 1);
1553 assert_eq!(ary, [1, 2, 3, 7, 8, 9]);
1554 }
1555
1556 #[test]
1557 fn non_empty_array_set_slice_interior_non_empty_drain_equal_to_insert_length_overrun_tail() {
1558 let mut ary = Array::from([1, 2, 3, 4]);
1559 let drained = ary.set_slice(3, 3, &[7, 8, 9]);
1560 assert_eq!(drained, 1);
1561 assert_eq!(ary, [1, 2, 3, 7, 8, 9]);
1562 }
1563
1564 #[test]
1565 fn non_empty_array_set_slice_interior_non_empty_drain_greater_than_insert_length_overrun_tail() {
1566 let mut ary = Array::from([1, 2, 3, 4]);
1567 let drained = ary.set_slice(3, 10, &[7, 8, 9]);
1568 assert_eq!(drained, 1);
1569 assert_eq!(ary, [1, 2, 3, 7, 8, 9]);
1570 }
1571
1572 #[test]
1573 fn non_empty_array_set_slice_begin_non_empty_drain_0() {
1574 let mut ary = Array::from([1, 2, 3, 4, 5]);
1575 let drained = ary.set_slice(0, 0, &[7, 8, 9]);
1576 assert_eq!(drained, 0);
1577 assert_eq!(ary, [7, 8, 9, 1, 2, 3, 4, 5]);
1578 }
1579
1580 #[test]
1581 fn non_empty_array_set_slice_begin_non_empty_drain_less_than_insert_length() {
1582 let mut ary = Array::from([1, 2, 3, 4, 5]);
1583 let drained = ary.set_slice(0, 2, &[7, 8, 9]);
1584 assert_eq!(drained, 2);
1585 assert_eq!(ary, [7, 8, 9, 3, 4, 5]);
1586 }
1587
1588 #[test]
1589 fn non_empty_array_set_slice_begin_non_empty_drain_equal_to_insert_length() {
1590 let mut ary = Array::from([1, 2, 3, 4, 5]);
1591 let drained = ary.set_slice(0, 3, &[7, 8, 9]);
1592 assert_eq!(drained, 3);
1593 assert_eq!(ary, [7, 8, 9, 4, 5]);
1594 }
1595
1596 #[test]
1597 fn non_empty_array_set_slice_begin_non_empty_drain_greater_than_insert_length() {
1598 let mut ary = Array::from([1, 2, 3, 4, 5, 6]);
1599 let drained = ary.set_slice(0, 4, &[7, 8, 9]);
1600 assert_eq!(drained, 4);
1601 assert_eq!(ary, [7, 8, 9, 5, 6]);
1602 }
1603
1604 #[test]
1605 fn non_empty_array_set_slice_begin_non_empty_drain_equal_to_insert_length_to_tail() {
1606 let mut ary = Array::from([1, 2, 3, 4]);
1607 let drained = ary.set_slice(0, 4, &[7, 8, 9, 10]);
1608 assert_eq!(drained, 4);
1609 assert_eq!(ary, [7, 8, 9, 10]);
1610 }
1611
1612 #[test]
1613 fn non_empty_array_set_slice_begin_non_empty_drain_greater_than_insert_length_to_tail() {
1614 let mut ary = Array::from([1, 2, 3, 4]);
1615 let drained = ary.set_slice(0, 10, &[7, 8, 9, 10]);
1616 assert_eq!(drained, 4);
1617 assert_eq!(ary, [7, 8, 9, 10]);
1618 }
1619
1620 #[test]
1621 fn non_empty_array_set_slice_begin_non_empty_drain_less_than_insert_length_overrun_tail() {
1622 let mut ary = Array::from([1, 2, 3, 4]);
1623 let drained = ary.set_slice(0, 4, &[7, 8, 9, 10, 11]);
1624 assert_eq!(drained, 4);
1625 assert_eq!(ary, [7, 8, 9, 10, 11]);
1626 }
1627
1628 #[test]
1629 fn non_empty_array_set_slice_begin_non_empty_drain_equal_to_insert_length_overrun_tail() {
1630 let mut ary = Array::from([1, 2, 3, 4]);
1631 let drained = ary.set_slice(0, 5, &[7, 8, 9, 10, 11]);
1632 assert_eq!(drained, 4);
1633 assert_eq!(ary, [7, 8, 9, 10, 11]);
1634 }
1635
1636 #[test]
1637 fn non_empty_array_set_slice_begin_non_empty_drain_greater_than_insert_length_overrun_tail() {
1638 let mut ary = Array::from([1, 2, 3, 4]);
1639 let drained = ary.set_slice(0, 10, &[7, 8, 9, 10, 11]);
1640 assert_eq!(drained, 4);
1641 assert_eq!(ary, [7, 8, 9, 10, 11]);
1642 }
1643
1644 #[test]
1645 fn empty_array_set_slice_non_empty_drain_0() {
1646 let mut ary = Array::<i32>::new();
1647 let drained = ary.set_slice(0, 0, &[7, 8, 9]);
1648 assert_eq!(drained, 0);
1649 assert_eq!(ary, [7, 8, 9]);
1650 }
1651
1652 #[test]
1653 fn empty_array_set_slice_non_empty_drain_less_than_insert_length() {
1654 let mut ary = Array::<i32>::new();
1655 let drained = ary.set_slice(0, 1, &[7, 8, 9]);
1656 assert_eq!(drained, 0);
1657 assert_eq!(ary, [7, 8, 9]);
1658 }
1659
1660 #[test]
1661 fn empty_array_set_slice_non_empty_drain_equal_to_insert_length() {
1662 let mut ary = Array::<i32>::new();
1663 let drained = ary.set_slice(0, 3, &[7, 8, 9]);
1664 assert_eq!(drained, 0);
1665 assert_eq!(ary, [7, 8, 9]);
1666 }
1667
1668 #[test]
1669 fn empty_array_set_slice_begin_non_empty_drain_greater_than_insert_length() {
1670 let mut ary = Array::<i32>::new();
1671 let drained = ary.set_slice(0, 10, &[7, 8, 9]);
1672 assert_eq!(drained, 0);
1673 assert_eq!(ary, [7, 8, 9]);
1674 }
1675
1676 #[test]
1677 fn empty_array_set_slice_out_of_bounds_non_empty_drain_0() {
1678 let mut ary = Array::<i32>::new();
1679 let drained = ary.set_slice(5, 0, &[7, 8, 9]);
1680 assert_eq!(drained, 0);
1681 assert_eq!(ary, [0, 0, 0, 0, 0, 7, 8, 9]);
1682 }
1683
1684 #[test]
1685 fn empty_array_set_slice_out_of_bounds_non_empty_drain_less_than_insert_length() {
1686 let mut ary = Array::<i32>::new();
1687 let drained = ary.set_slice(5, 1, &[7, 8, 9]);
1688 assert_eq!(drained, 0);
1689 assert_eq!(ary, [0, 0, 0, 0, 0, 7, 8, 9]);
1690 }
1691
1692 #[test]
1693 fn empty_array_set_slice_out_of_bounds_non_empty_drain_equal_to_insert_length() {
1694 let mut ary = Array::<i32>::new();
1695 let drained = ary.set_slice(5, 3, &[7, 8, 9]);
1696 assert_eq!(drained, 0);
1697 assert_eq!(ary, [0, 0, 0, 0, 0, 7, 8, 9]);
1698 }
1699
1700 #[test]
1701 fn empty_array_set_slice_out_of_bounds_non_empty_drain_greater_than_insert_length() {
1702 let mut ary = Array::<i32>::new();
1703 let drained = ary.set_slice(5, 10, &[7, 8, 9]);
1704 assert_eq!(drained, 0);
1705 assert_eq!(ary, [0, 0, 0, 0, 0, 7, 8, 9]);
1706 }
1707
1708 #[test]
1709 fn empty_array_set_slice_empty_drain_0() {
1710 let mut ary = Array::<i32>::new();
1711 let drained = ary.set_slice(0, 0, &[]);
1712 assert_eq!(drained, 0);
1713 assert_eq!(ary, []);
1714 }
1715
1716 #[test]
1717 fn empty_array_set_slice_empty_drain_less_than_insert_length() {
1718 let mut ary = Array::<i32>::new();
1719 let drained = ary.set_slice(0, 0, &[]);
1720 assert_eq!(drained, 0);
1721 assert_eq!(ary, []);
1722 }
1723
1724 #[test]
1725 fn empty_array_set_slice_empty_drain_equal_to_insert_length() {
1726 let mut ary = Array::<i32>::new();
1727 let drained = ary.set_slice(0, 0, &[]);
1728 assert_eq!(drained, 0);
1729 assert_eq!(ary, []);
1730 }
1731
1732 #[test]
1733 fn empty_array_set_slice_begin_empty_drain_greater_than_insert_length() {
1734 let mut ary = Array::<i32>::new();
1735 let drained = ary.set_slice(0, 10, &[]);
1736 assert_eq!(drained, 0);
1737 assert_eq!(ary, []);
1738 }
1739
1740 #[test]
1741 fn empty_array_set_slice_out_of_bounds_empty_drain_0() {
1742 let mut ary = Array::<i32>::new();
1743 let drained = ary.set_slice(5, 0, &[]);
1744 assert_eq!(drained, 0);
1745 assert_eq!(ary, [0, 0, 0, 0, 0]);
1746 }
1747
1748 #[test]
1749 fn empty_array_set_slice_out_of_bounds_empty_drain_less_than_insert_length() {
1750 let mut ary = Array::<i32>::new();
1751 let drained = ary.set_slice(5, 1, &[]);
1752 assert_eq!(drained, 0);
1753 assert_eq!(ary, [0, 0, 0, 0, 0]);
1754 }
1755
1756 #[test]
1757 fn empty_array_set_slice_out_of_bounds_empty_drain_equal_to_insert_length() {
1758 let mut ary = Array::<i32>::new();
1759 let drained = ary.set_slice(5, 3, &[]);
1760 assert_eq!(drained, 0);
1761 assert_eq!(ary, [0, 0, 0, 0, 0]);
1762 }
1763
1764 #[test]
1765 fn empty_array_set_slice_out_of_bounds_empty_drain_greater_than_insert_length() {
1766 let mut ary = Array::<i32>::new();
1767 let drained = ary.set_slice(5, 10, &[]);
1768 assert_eq!(drained, 0);
1769 assert_eq!(ary, [0, 0, 0, 0, 0]);
1770 }
1771
1772 #[test]
1773 fn into_raw_parts_from_raw_parts_round_trip_empty_no_alloc() {
1774 let ary = Array::<i32>::new();
1775 let RawParts { ptr, length, capacity } = ary.into_raw_parts();
1776 let ary = unsafe { Array::from_raw_parts(RawParts { ptr, length, capacity }) };
1777 assert_eq!(ary.len(), 0);
1778 assert_eq!(ary.capacity(), 0);
1779 }
1780
1781 #[test]
1782 fn into_raw_parts_from_raw_parts_round_trip_empty_with_capacity() {
1783 let ary = Array::<i32>::with_capacity(100);
1784 let RawParts { ptr, length, capacity } = ary.into_raw_parts();
1785 let ary = unsafe { Array::from_raw_parts(RawParts { ptr, length, capacity }) };
1786 assert_eq!(ary.len(), 0);
1787 assert_eq!(ary.capacity(), 100);
1788 }
1789
1790 #[test]
1791 fn into_raw_parts_from_raw_parts_round_trip_assoc() {
1792 let ary = Array::<i32>::assoc(1, 2);
1793 let RawParts { ptr, length, capacity } = ary.into_raw_parts();
1794 let ary = unsafe { Array::from_raw_parts(RawParts { ptr, length, capacity }) };
1795 assert_eq!(ary.len(), 2);
1796 assert_eq!(ary.capacity(), 2);
1797 assert_eq!(ary, [1, 2]);
1798 }
1799
1800 #[test]
1801 fn into_raw_parts_from_raw_parts_round_trip_from_slice() {
1802 let ary = Array::<i32>::from(&[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]);
1803 let RawParts { ptr, length, capacity } = ary.into_raw_parts();
1804 let ary = unsafe { Array::from_raw_parts(RawParts { ptr, length, capacity }) };
1805 assert_eq!(ary.len(), 10);
1806 assert!(ary.capacity() >= 10);
1807 assert_eq!(ary, [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]);
1808 }
1809}