1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! A growable list type, written `Vec<T>` but pronounced 'vector.'
13 //! Vectors have `O(1)` indexing, push (to the end) and pop (from the end).
17 //! Explicitly creating a `Vec<T>` with `new()`:
20 //! let xs: Vec<i32> = Vec::new();
23 //! Using the `vec!` macro:
26 //! let ys: Vec<i32> = vec![];
28 //! let zs = vec![1i32, 2, 3, 4, 5];
34 //! let mut xs = vec![1i32, 2];
42 //! let mut xs = vec![1i32, 2];
44 //! let two = xs.pop();
49 use alloc::boxed::Box;
50 use alloc::heap::{EMPTY, allocate, reallocate, deallocate};
51 use core::borrow::{Cow, IntoCow};
53 use core::cmp::{Equiv, Ordering};
54 use core::default::Default;
56 use core::hash::{mod, Hash};
57 use core::iter::{repeat, FromIterator};
58 use core::kinds::marker::{ContravariantLifetime, InvariantType};
60 use core::nonzero::NonZero;
61 use core::num::{Int, UnsignedInt};
62 use core::ops::{Index, IndexMut, Deref, Add};
65 use core::raw::Slice as RawSlice;
68 /// A growable list type, written `Vec<T>` but pronounced 'vector.'
73 /// let mut vec = Vec::new();
77 /// assert_eq!(vec.len(), 2);
78 /// assert_eq!(vec[0], 1);
80 /// assert_eq!(vec.pop(), Some(2));
81 /// assert_eq!(vec.len(), 1);
84 /// assert_eq!(vec[0], 7);
86 /// vec.push_all(&[1, 2, 3]);
88 /// for x in vec.iter() {
89 /// println!("{}", x);
91 /// assert_eq!(vec, vec![7i, 1, 2, 3]);
94 /// The `vec!` macro is provided to make initialization more convenient:
97 /// let mut vec = vec![1i, 2i, 3i];
99 /// assert_eq!(vec, vec![1, 2, 3, 4]);
102 /// Use a `Vec<T>` as an efficient stack:
105 /// let mut stack = Vec::new();
112 /// let top = match stack.pop() {
113 /// None => break, // empty
116 /// // Prints 3, 2, 1
117 /// println!("{}", top);
121 /// # Capacity and reallocation
123 /// The capacity of a vector is the amount of space allocated for any future elements that will be
124 /// added onto the vector. This is not to be confused with the *length* of a vector, which
125 /// specifies the number of actual elements within the vector. If a vector's length exceeds its
126 /// capacity, its capacity will automatically be increased, but its elements will have to be
129 /// For example, a vector with capacity 10 and length 0 would be an empty vector with space for 10
130 /// more elements. Pushing 10 or fewer elements onto the vector will not change its capacity or
131 /// cause reallocation to occur. However, if the vector's length is increased to 11, it will have
132 /// to reallocate, which can be slow. For this reason, it is recommended to use
133 /// `Vec::with_capacity` whenever possible to specify how big the vector is expected to get.
134 #[unsafe_no_drop_flag]
137 ptr: NonZero<*mut T>,
142 unsafe impl<T: Send> Send for Vec<T> { }
143 unsafe impl<T: Sync> Sync for Vec<T> { }
145 ////////////////////////////////////////////////////////////////////////////////
147 ////////////////////////////////////////////////////////////////////////////////
150 /// Constructs a new, empty `Vec<T>`.
152 /// The vector will not allocate until elements are pushed onto it.
157 /// let mut vec: Vec<int> = Vec::new();
161 pub fn new() -> Vec<T> {
162 // We want ptr to never be NULL so instead we set it to some arbitrary
163 // non-null value which is fine since we never call deallocate on the ptr
164 // if cap is 0. The reason for this is because the pointer of a slice
165 // being NULL would break the null pointer optimization for enums.
166 Vec { ptr: unsafe { NonZero::new(EMPTY as *mut T) }, len: 0, cap: 0 }
169 /// Constructs a new, empty `Vec<T>` with the specified capacity.
171 /// The vector will be able to hold exactly `capacity` elements without reallocating. If
172 /// `capacity` is 0, the vector will not allocate.
174 /// It is important to note that this function does not specify the *length* of the returned
175 /// vector, but only the *capacity*. (For an explanation of the difference between length and
176 /// capacity, see the main `Vec<T>` docs above, 'Capacity and reallocation'.)
181 /// let mut vec: Vec<int> = Vec::with_capacity(10);
183 /// // The vector contains no items, even though it has capacity for more
184 /// assert_eq!(vec.len(), 0);
186 /// // These are all done without reallocating...
187 /// for i in range(0i, 10) {
191 /// // ...but this may make the vector reallocate
196 pub fn with_capacity(capacity: uint) -> Vec<T> {
197 if mem::size_of::<T>() == 0 {
198 Vec { ptr: unsafe { NonZero::new(EMPTY as *mut T) }, len: 0, cap: uint::MAX }
199 } else if capacity == 0 {
202 let size = capacity.checked_mul(mem::size_of::<T>())
203 .expect("capacity overflow");
204 let ptr = unsafe { allocate(size, mem::min_align_of::<T>()) };
205 if ptr.is_null() { ::alloc::oom() }
206 Vec { ptr: unsafe { NonZero::new(ptr as *mut T) }, len: 0, cap: capacity }
210 /// Deprecated: use `iter::range(0, length).map(op).collect()` instead
212 #[deprecated = "use iter::range(0, length).map(op).collect() instead"]
213 pub fn from_fn<F>(length: uint, op: F) -> Vec<T> where F: FnMut(uint) -> T {
214 range(0, length).map(op).collect()
217 /// Creates a `Vec<T>` directly from the raw components of another vector.
219 /// This is highly unsafe, due to the number of invariants that aren't checked.
228 /// let mut v = vec![1i, 2, 3];
230 /// // Pull out the various important pieces of information about `v`
231 /// let p = v.as_mut_ptr();
232 /// let len = v.len();
233 /// let cap = v.capacity();
236 /// // Cast `v` into the void: no destructor run, so we are in
237 /// // complete control of the allocation to which `p` points.
240 /// // Overwrite memory with 4, 5, 6
241 /// for i in range(0, len as int) {
242 /// ptr::write(p.offset(i), 4 + i);
245 /// // Put everything back together into a Vec
246 /// let rebuilt = Vec::from_raw_parts(p, len, cap);
247 /// assert_eq!(rebuilt, vec![4i, 5i, 6i]);
252 pub unsafe fn from_raw_parts(ptr: *mut T, length: uint,
253 capacity: uint) -> Vec<T> {
254 Vec { ptr: NonZero::new(ptr), len: length, cap: capacity }
257 /// Creates a vector by copying the elements from a raw pointer.
259 /// This function will copy `elts` contiguous elements starting at `ptr` into a new allocation
260 /// owned by the returned `Vec<T>`. The elements of the buffer are copied into the vector
261 /// without cloning, as if `ptr::read()` were called on them.
263 #[unstable = "may be better expressed via composition"]
264 pub unsafe fn from_raw_buf(ptr: *const T, elts: uint) -> Vec<T> {
265 let mut dst = Vec::with_capacity(elts);
267 ptr::copy_nonoverlapping_memory(dst.as_mut_ptr(), ptr, elts);
271 /// Deprecated: use `into_iter().partition(f)` instead.
273 #[deprecated = "use into_iter().partition(f) instead"]
274 pub fn partition<F>(self, f: F) -> (Vec<T>, Vec<T>) where F: FnMut(&T) -> bool {
275 self.into_iter().partition(f)
278 /// Returns the number of elements the vector can hold without
284 /// let vec: Vec<int> = Vec::with_capacity(10);
285 /// assert_eq!(vec.capacity(), 10);
289 pub fn capacity(&self) -> uint {
293 /// Deprecated: Renamed to `reserve`.
294 #[deprecated = "Renamed to `reserve`"]
295 pub fn reserve_additional(&mut self, extra: uint) {
299 /// Reserves capacity for at least `additional` more elements to be inserted in the given
300 /// `Vec<T>`. The collection may reserve more space to avoid frequent reallocations.
304 /// Panics if the new capacity overflows `uint`.
309 /// let mut vec: Vec<int> = vec![1];
311 /// assert!(vec.capacity() >= 11);
314 pub fn reserve(&mut self, additional: uint) {
315 if self.cap - self.len < additional {
316 let err_msg = "Vec::reserve: `uint` overflow";
317 let new_cap = self.len.checked_add(additional).expect(err_msg)
318 .checked_next_power_of_two().expect(err_msg);
319 self.grow_capacity(new_cap);
323 /// Reserves the minimum capacity for exactly `additional` more elements to
324 /// be inserted in the given `Vec<T>`. Does nothing if the capacity is already
327 /// Note that the allocator may give the collection more space than it
328 /// requests. Therefore capacity can not be relied upon to be precisely
329 /// minimal. Prefer `reserve` if future insertions are expected.
333 /// Panics if the new capacity overflows `uint`.
338 /// let mut vec: Vec<int> = vec![1];
339 /// vec.reserve_exact(10);
340 /// assert!(vec.capacity() >= 11);
343 pub fn reserve_exact(&mut self, additional: uint) {
344 if self.cap - self.len < additional {
345 match self.len.checked_add(additional) {
346 None => panic!("Vec::reserve: `uint` overflow"),
347 Some(new_cap) => self.grow_capacity(new_cap)
352 /// Shrinks the capacity of the vector as much as possible.
354 /// It will drop down as close as possible to the length but the allocator
355 /// may still inform the vector that there is space for a few more elements.
360 /// let mut vec: Vec<int> = Vec::with_capacity(10);
361 /// vec.push_all(&[1, 2, 3]);
362 /// assert_eq!(vec.capacity(), 10);
363 /// vec.shrink_to_fit();
364 /// assert!(vec.capacity() >= 3);
367 pub fn shrink_to_fit(&mut self) {
368 if mem::size_of::<T>() == 0 { return }
373 dealloc(*self.ptr, self.cap)
379 // Overflow check is unnecessary as the vector is already at
381 let ptr = reallocate(*self.ptr as *mut u8,
382 self.cap * mem::size_of::<T>(),
383 self.len * mem::size_of::<T>(),
384 mem::min_align_of::<T>()) as *mut T;
385 if ptr.is_null() { ::alloc::oom() }
386 self.ptr = NonZero::new(ptr);
392 /// Convert the vector into Box<[T]>.
394 /// Note that this will drop any excess capacity. Calling this and
395 /// converting back to a vector with `into_vec()` is equivalent to calling
396 /// `shrink_to_fit()`.
398 pub fn into_boxed_slice(mut self) -> Box<[T]> {
399 self.shrink_to_fit();
401 let xs: Box<[T]> = mem::transmute(self.as_mut_slice());
407 /// Shorten a vector, dropping excess elements.
409 /// If `len` is greater than the vector's current length, this has no
415 /// let mut vec = vec![1i, 2, 3, 4];
417 /// assert_eq!(vec, vec![1, 2]);
420 pub fn truncate(&mut self, len: uint) {
422 // drop any extra elements
423 while len < self.len {
424 // decrement len before the read(), so a panic on Drop doesn't
425 // re-drop the just-failed value.
427 ptr::read(self.get_unchecked(self.len));
432 /// Returns a mutable slice of the elements of `self`.
437 /// fn foo(slice: &mut [int]) {}
439 /// let mut vec = vec![1i, 2];
440 /// foo(vec.as_mut_slice());
444 pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T] {
446 mem::transmute(RawSlice {
447 data: *self.ptr as *const T,
453 /// Creates a consuming iterator, that is, one that moves each value out of
454 /// the vector (from start to end). The vector cannot be used after calling
460 /// let v = vec!["a".to_string(), "b".to_string()];
461 /// for s in v.into_iter() {
462 /// // s has type String, not &String
463 /// println!("{}", s);
468 pub fn into_iter(self) -> IntoIter<T> {
472 let begin = ptr as *const T;
473 let end = if mem::size_of::<T>() == 0 {
474 (ptr as uint + self.len()) as *const T
476 ptr.offset(self.len() as int) as *const T
479 IntoIter { allocation: ptr, cap: cap, ptr: begin, end: end }
483 /// Sets the length of a vector.
485 /// This will explicitly set the size of the vector, without actually
486 /// modifying its buffers, so it is up to the caller to ensure that the
487 /// vector is actually the specified size.
492 /// let mut v = vec![1u, 2, 3, 4];
499 pub unsafe fn set_len(&mut self, len: uint) {
503 /// Removes an element from anywhere in the vector and return it, replacing
504 /// it with the last element.
506 /// This does not preserve ordering, but is O(1).
510 /// Panics if `index` is out of bounds.
515 /// let mut v = vec!["foo", "bar", "baz", "qux"];
517 /// assert_eq!(v.swap_remove(1), "bar");
518 /// assert_eq!(v, vec!["foo", "qux", "baz"]);
520 /// assert_eq!(v.swap_remove(0), "foo");
521 /// assert_eq!(v, vec!["baz", "qux"]);
525 pub fn swap_remove(&mut self, index: uint) -> T {
526 let length = self.len();
527 self.swap(index, length - 1);
531 /// Inserts an element at position `index` within the vector, shifting all
532 /// elements after position `i` one position to the right.
536 /// Panics if `index` is not between `0` and the vector's length (both
537 /// bounds inclusive).
542 /// let mut vec = vec![1i, 2, 3];
543 /// vec.insert(1, 4);
544 /// assert_eq!(vec, vec![1, 4, 2, 3]);
545 /// vec.insert(4, 5);
546 /// assert_eq!(vec, vec![1, 4, 2, 3, 5]);
549 pub fn insert(&mut self, index: uint, element: T) {
550 let len = self.len();
551 assert!(index <= len);
552 // space for the new element
555 unsafe { // infallible
556 // The spot to put the new value
558 let p = self.as_mut_ptr().offset(index as int);
559 // Shift everything over to make space. (Duplicating the
560 // `index`th element into two consecutive places.)
561 ptr::copy_memory(p.offset(1), &*p, len - index);
562 // Write it in, overwriting the first copy of the `index`th
564 ptr::write(&mut *p, element);
566 self.set_len(len + 1);
570 /// Removes and returns the element at position `index` within the vector,
571 /// shifting all elements after position `index` one position to the left.
575 /// Panics if `i` is out of bounds.
580 /// let mut v = vec![1i, 2, 3];
581 /// assert_eq!(v.remove(1), 2);
582 /// assert_eq!(v, vec![1, 3]);
585 pub fn remove(&mut self, index: uint) -> T {
586 let len = self.len();
587 assert!(index < len);
588 unsafe { // infallible
591 // the place we are taking from.
592 let ptr = self.as_mut_ptr().offset(index as int);
593 // copy it out, unsafely having a copy of the value on
594 // the stack and in the vector at the same time.
595 ret = ptr::read(ptr as *const T);
597 // Shift everything down to fill in that spot.
598 ptr::copy_memory(ptr, &*ptr.offset(1), len - index - 1);
600 self.set_len(len - 1);
605 /// Retains only the elements specified by the predicate.
607 /// In other words, remove all elements `e` such that `f(&e)` returns false.
608 /// This method operates in place and preserves the order of the retained
614 /// let mut vec = vec![1i, 2, 3, 4];
615 /// vec.retain(|&x| x%2 == 0);
616 /// assert_eq!(vec, vec![2, 4]);
619 pub fn retain<F>(&mut self, mut f: F) where F: FnMut(&T) -> bool {
620 let len = self.len();
623 let v = self.as_mut_slice();
625 for i in range(0u, len) {
634 self.truncate(len - del);
638 /// Deprecated: use `extend(range(0, n).map(f))` instead.
639 #[deprecated = "use extend(range(0, n).map(f)) instead"]
640 pub fn grow_fn<F>(&mut self, n: uint, f: F) where F: FnMut(uint) -> T {
641 self.extend(range(0, n).map(f));
644 /// Appends an element to the back of a collection.
648 /// Panics if the number of elements in the vector overflows a `uint`.
653 /// let mut vec = vec!(1i, 2);
655 /// assert_eq!(vec, vec!(1, 2, 3));
659 pub fn push(&mut self, value: T) {
660 if mem::size_of::<T>() == 0 {
661 // zero-size types consume no memory, so we can't rely on the
662 // address space running out
663 self.len = self.len.checked_add(1).expect("length overflow");
664 unsafe { mem::forget(value); }
667 if self.len == self.cap {
668 let old_size = self.cap * mem::size_of::<T>();
669 let size = max(old_size, 2 * mem::size_of::<T>()) * 2;
670 if old_size > size { panic!("capacity overflow") }
672 let ptr = alloc_or_realloc(*self.ptr, old_size, size);
673 if ptr.is_null() { ::alloc::oom() }
674 self.ptr = NonZero::new(ptr);
676 self.cap = max(self.cap, 2) * 2;
680 let end = (*self.ptr).offset(self.len as int);
681 ptr::write(&mut *end, value);
686 /// Removes the last element from a vector and returns it, or `None` if it is empty.
691 /// let mut vec = vec![1i, 2, 3];
692 /// assert_eq!(vec.pop(), Some(3));
693 /// assert_eq!(vec, vec![1, 2]);
697 pub fn pop(&mut self) -> Option<T> {
703 Some(ptr::read(self.get_unchecked(self.len())))
708 /// Creates a draining iterator that clears the `Vec` and iterates over
709 /// the removed items from start to end.
714 /// let mut v = vec!["a".to_string(), "b".to_string()];
715 /// for s in v.drain() {
716 /// // s has type String, not &String
717 /// println!("{}", s);
719 /// assert!(v.is_empty());
722 #[unstable = "matches collection reform specification, waiting for dust to settle"]
723 pub fn drain<'a>(&'a mut self) -> Drain<'a, T> {
725 let begin = *self.ptr as *const T;
726 let end = if mem::size_of::<T>() == 0 {
727 (*self.ptr as uint + self.len()) as *const T
729 (*self.ptr).offset(self.len() as int) as *const T
735 marker: ContravariantLifetime,
740 /// Clears the vector, removing all values.
745 /// let mut v = vec![1i, 2, 3];
749 /// assert!(v.is_empty());
753 pub fn clear(&mut self) {
757 /// Returns the number of elements in the vector.
762 /// let a = vec![1i, 2, 3];
763 /// assert_eq!(a.len(), 3);
767 pub fn len(&self) -> uint { self.len }
769 /// Returns `true` if the vector contains no elements.
774 /// let mut v = Vec::new();
775 /// assert!(v.is_empty());
778 /// assert!(!v.is_empty());
781 pub fn is_empty(&self) -> bool { self.len() == 0 }
783 /// Converts a `Vec<T>` to a `Vec<U>` where `T` and `U` have the same
784 /// size and in case they are not zero-sized the same minimal alignment.
788 /// Panics if `T` and `U` have differing sizes or are not zero-sized and
789 /// have differing minimal alignments.
794 /// let v = vec![0u, 1, 2];
795 /// let w = v.map_in_place(|i| i + 3);
796 /// assert_eq!(w.as_slice(), [3, 4, 5].as_slice());
798 /// #[deriving(PartialEq, Show)]
799 /// struct Newtype(u8);
800 /// let bytes = vec![0x11, 0x22];
801 /// let newtyped_bytes = bytes.map_in_place(|x| Newtype(x));
802 /// assert_eq!(newtyped_bytes.as_slice(), [Newtype(0x11), Newtype(0x22)].as_slice());
804 #[experimental = "API may change to provide stronger guarantees"]
805 pub fn map_in_place<U, F>(self, mut f: F) -> Vec<U> where F: FnMut(T) -> U {
806 // FIXME: Assert statically that the types `T` and `U` have the same
808 assert!(mem::size_of::<T>() == mem::size_of::<U>());
812 if mem::size_of::<T>() != 0 {
813 // FIXME: Assert statically that the types `T` and `U` have the
814 // same minimal alignment in case they are not zero-sized.
816 // These asserts are necessary because the `min_align_of` of the
817 // types are passed to the allocator by `Vec`.
818 assert!(mem::min_align_of::<T>() == mem::min_align_of::<U>());
820 // This `as int` cast is safe, because the size of the elements of the
821 // vector is not 0, and:
823 // 1) If the size of the elements in the vector is 1, the `int` may
824 // overflow, but it has the correct bit pattern so that the
825 // `.offset()` function will work.
828 // Address space 0x0-0xF.
829 // `u8` array at: 0x1.
830 // Size of `u8` array: 0x8.
831 // Calculated `offset`: -0x8.
832 // After `array.offset(offset)`: 0x9.
833 // (0x1 + 0x8 = 0x1 - 0x8)
835 // 2) If the size of the elements in the vector is >1, the `uint` ->
836 // `int` conversion can't overflow.
837 let offset = vec.len() as int;
838 let start = vec.as_mut_ptr();
840 let mut pv = PartialVecNonZeroSized {
844 // This points inside the vector, as the vector has length
846 end_t: unsafe { start.offset(offset) },
847 start_u: start as *mut U,
848 end_u: start as *mut U,
859 while pv.end_u as *mut T != pv.end_t {
863 // +-+-+-+-+-+-+-+-+-+
864 // |U|...|U|T|T|...|T|
865 // +-+-+-+-+-+-+-+-+-+
869 let t = ptr::read(pv.start_t as *const T);
872 // +-+-+-+-+-+-+-+-+-+
873 // |U|...|U|X|T|...|T|
874 // +-+-+-+-+-+-+-+-+-+
877 // We must not panic here, one cell is marked as `T`
878 // although it is not `T`.
880 pv.start_t = pv.start_t.offset(1);
883 // +-+-+-+-+-+-+-+-+-+
884 // |U|...|U|X|T|...|T|
885 // +-+-+-+-+-+-+-+-+-+
888 // We may panic again.
890 // The function given by the user might panic.
893 ptr::write(pv.end_u, u);
896 // +-+-+-+-+-+-+-+-+-+
897 // |U|...|U|U|T|...|T|
898 // +-+-+-+-+-+-+-+-+-+
901 // We should not panic here, because that would leak the `U`
902 // pointed to by `end_u`.
904 pv.end_u = pv.end_u.offset(1);
907 // +-+-+-+-+-+-+-+-+-+
908 // |U|...|U|U|T|...|T|
909 // +-+-+-+-+-+-+-+-+-+
912 // We may panic again.
924 // Extract `vec` and prevent the destructor of
925 // `PartialVecNonZeroSized` from running. Note that none of the
926 // function calls can panic, thus no resources can be leaked (as the
927 // `vec` member of `PartialVec` is the only one which holds
928 // allocations -- and it is returned from this function. None of
931 let vec_len = pv.vec.len();
932 let vec_cap = pv.vec.capacity();
933 let vec_ptr = pv.vec.as_mut_ptr() as *mut U;
935 Vec::from_raw_parts(vec_ptr, vec_len, vec_cap)
938 // Put the `Vec` into the `PartialVecZeroSized` structure and
939 // prevent the destructor of the `Vec` from running. Since the
940 // `Vec` contained zero-sized objects, it did not allocate, so we
941 // are not leaking memory here.
942 let mut pv = PartialVecZeroSized::<T,U> {
945 marker_t: InvariantType,
946 marker_u: InvariantType,
948 unsafe { mem::forget(vec); }
950 while pv.num_t != 0 {
952 // Create a `T` out of thin air and decrement `num_t`. This
953 // must not panic between these steps, as otherwise a
954 // destructor of `T` which doesn't exist runs.
955 let t = mem::uninitialized();
958 // The function given by the user might panic.
961 // Forget the `U` and increment `num_u`. This increment
962 // cannot overflow the `uint` as we only do this for a
963 // number of times that fits into a `uint` (and start with
964 // `0`). Again, we should not panic between these steps.
969 // Create a `Vec` from our `PartialVecZeroSized` and make sure the
970 // destructor of the latter will not run. None of this can panic.
971 let mut result = Vec::new();
973 result.set_len(pv.num_u);
981 impl<T: Clone> Vec<T> {
982 /// Deprecated: use `repeat(value).take(length).collect()` instead.
984 #[deprecated = "use repeat(value).take(length).collect() instead"]
985 pub fn from_elem(length: uint, value: T) -> Vec<T> {
986 repeat(value).take(length).collect()
989 /// Resizes the `Vec` in-place so that `len()` is equal to `new_len`.
991 /// Calls either `extend()` or `truncate()` depending on whether `new_len`
992 /// is larger than the current value of `len()` or not.
997 /// let mut vec = vec!["hello"];
998 /// vec.resize(3, "world");
999 /// assert_eq!(vec, vec!["hello", "world", "world"]);
1001 /// let mut vec = vec![1i, 2, 3, 4];
1002 /// vec.resize(2, 0);
1003 /// assert_eq!(vec, vec![1, 2]);
1005 #[unstable = "matches collection reform specification; waiting for dust to settle"]
1006 pub fn resize(&mut self, new_len: uint, value: T) {
1007 let len = self.len();
1010 self.extend(repeat(value).take(new_len - len));
1012 self.truncate(new_len);
1016 /// Appends all elements in a slice to the `Vec`.
1018 /// Iterates over the slice `other`, clones each element, and then appends
1019 /// it to this `Vec`. The `other` vector is traversed in-order.
1024 /// let mut vec = vec![1i];
1025 /// vec.push_all(&[2i, 3, 4]);
1026 /// assert_eq!(vec, vec![1, 2, 3, 4]);
1029 #[experimental = "likely to be replaced by a more optimized extend"]
1030 pub fn push_all(&mut self, other: &[T]) {
1031 self.reserve(other.len());
1033 for i in range(0, other.len()) {
1034 let len = self.len();
1036 // Unsafe code so this can be optimised to a memcpy (or something similarly
1037 // fast) when T is Copy. LLVM is easily confused, so any extra operations
1038 // during the loop can prevent this optimisation.
1041 self.get_unchecked_mut(len),
1042 other.get_unchecked(i).clone());
1043 self.set_len(len + 1);
1048 /// Deprecated: use `extend(repeat(value).take(n))` instead
1049 #[deprecated = "use extend(repeat(value).take(n)) instead"]
1050 pub fn grow(&mut self, n: uint, value: T) {
1051 self.extend(repeat(value).take(n))
1054 /// Deprecated: use `iter().cloned().partition(f)` instead.
1055 #[deprecated = "use iter().cloned().partition(f) instead"]
1056 pub fn partitioned<F>(&self, f: F) -> (Vec<T>, Vec<T>) where F: FnMut(&T) -> bool {
1057 self.iter().cloned().partition(f)
1061 impl<T: PartialEq> Vec<T> {
1062 /// Removes consecutive repeated elements in the vector.
1064 /// If the vector is sorted, this removes all duplicates.
1069 /// let mut vec = vec![1i, 2, 2, 3, 2];
1073 /// assert_eq!(vec, vec![1i, 2, 3, 2]);
1076 pub fn dedup(&mut self) {
1078 // Although we have a mutable reference to `self`, we cannot make
1079 // *arbitrary* changes. The `PartialEq` comparisons could panic, so we
1080 // must ensure that the vector is in a valid state at all time.
1082 // The way that we handle this is by using swaps; we iterate
1083 // over all the elements, swapping as we go so that at the end
1084 // the elements we wish to keep are in the front, and those we
1085 // wish to reject are at the back. We can then truncate the
1086 // vector. This operation is still O(n).
1088 // Example: We start in this state, where `r` represents "next
1089 // read" and `w` represents "next_write`.
1092 // +---+---+---+---+---+---+
1093 // | 0 | 1 | 1 | 2 | 3 | 3 |
1094 // +---+---+---+---+---+---+
1097 // Comparing self[r] against self[w-1], this is not a duplicate, so
1098 // we swap self[r] and self[w] (no effect as r==w) and then increment both
1099 // r and w, leaving us with:
1102 // +---+---+---+---+---+---+
1103 // | 0 | 1 | 1 | 2 | 3 | 3 |
1104 // +---+---+---+---+---+---+
1107 // Comparing self[r] against self[w-1], this value is a duplicate,
1108 // so we increment `r` but leave everything else unchanged:
1111 // +---+---+---+---+---+---+
1112 // | 0 | 1 | 1 | 2 | 3 | 3 |
1113 // +---+---+---+---+---+---+
1116 // Comparing self[r] against self[w-1], this is not a duplicate,
1117 // so swap self[r] and self[w] and advance r and w:
1120 // +---+---+---+---+---+---+
1121 // | 0 | 1 | 2 | 1 | 3 | 3 |
1122 // +---+---+---+---+---+---+
1125 // Not a duplicate, repeat:
1128 // +---+---+---+---+---+---+
1129 // | 0 | 1 | 2 | 3 | 1 | 3 |
1130 // +---+---+---+---+---+---+
1133 // Duplicate, advance r. End of vec. Truncate to w.
1135 let ln = self.len();
1136 if ln < 1 { return; }
1138 // Avoid bounds checks by using unsafe pointers.
1139 let p = self.as_mut_ptr();
1144 let p_r = p.offset(r as int);
1145 let p_wm1 = p.offset((w - 1) as int);
1148 let p_w = p_wm1.offset(1);
1149 mem::swap(&mut *p_r, &mut *p_w);
1161 ////////////////////////////////////////////////////////////////////////////////
1163 ////////////////////////////////////////////////////////////////////////////////
1165 /// Deprecated: use `unzip` directly on the iterator instead.
1166 #[deprecated = "use unzip directly on the iterator instead"]
1167 pub fn unzip<T, U, V: Iterator<(T, U)>>(iter: V) -> (Vec<T>, Vec<U>) {
1171 ////////////////////////////////////////////////////////////////////////////////
1172 // Internal methods and functions
1173 ////////////////////////////////////////////////////////////////////////////////
1176 /// Reserves capacity for exactly `capacity` elements in the given vector.
1178 /// If the capacity for `self` is already equal to or greater than the
1179 /// requested capacity, then no action is taken.
1180 fn grow_capacity(&mut self, capacity: uint) {
1181 if mem::size_of::<T>() == 0 { return }
1183 if capacity > self.cap {
1184 let size = capacity.checked_mul(mem::size_of::<T>())
1185 .expect("capacity overflow");
1187 let ptr = alloc_or_realloc(*self.ptr, self.cap * mem::size_of::<T>(), size);
1188 if ptr.is_null() { ::alloc::oom() }
1189 self.ptr = NonZero::new(ptr);
1191 self.cap = capacity;
1196 // FIXME: #13996: need a way to mark the return value as `noalias`
1198 unsafe fn alloc_or_realloc<T>(ptr: *mut T, old_size: uint, size: uint) -> *mut T {
1200 allocate(size, mem::min_align_of::<T>()) as *mut T
1202 reallocate(ptr as *mut u8, old_size, size, mem::min_align_of::<T>()) as *mut T
1207 unsafe fn dealloc<T>(ptr: *mut T, len: uint) {
1208 if mem::size_of::<T>() != 0 {
1209 deallocate(ptr as *mut u8,
1210 len * mem::size_of::<T>(),
1211 mem::min_align_of::<T>())
1215 ////////////////////////////////////////////////////////////////////////////////
1216 // Common trait implementations for Vec
1217 ////////////////////////////////////////////////////////////////////////////////
1220 impl<T:Clone> Clone for Vec<T> {
1221 fn clone(&self) -> Vec<T> { ::slice::SliceExt::to_vec(self.as_slice()) }
1223 fn clone_from(&mut self, other: &Vec<T>) {
1224 // drop anything in self that will not be overwritten
1225 if self.len() > other.len() {
1226 self.truncate(other.len())
1229 // reuse the contained values' allocations/resources.
1230 for (place, thing) in self.iter_mut().zip(other.iter()) {
1231 place.clone_from(thing)
1234 // self.len <= other.len due to the truncate above, so the
1235 // slice here is always in-bounds.
1236 let slice = other[self.len()..];
1237 self.push_all(slice);
1241 impl<S: hash::Writer, T: Hash<S>> Hash<S> for Vec<T> {
1243 fn hash(&self, state: &mut S) {
1244 self.as_slice().hash(state);
1248 #[experimental = "waiting on Index stability"]
1249 impl<T> Index<uint,T> for Vec<T> {
1251 fn index<'a>(&'a self, index: &uint) -> &'a T {
1252 &self.as_slice()[*index]
1256 impl<T> IndexMut<uint,T> for Vec<T> {
1258 fn index_mut<'a>(&'a mut self, index: &uint) -> &'a mut T {
1259 &mut self.as_mut_slice()[*index]
1263 impl<T> ops::Slice<uint, [T]> for Vec<T> {
1265 fn as_slice_<'a>(&'a self) -> &'a [T] {
1270 fn slice_from_or_fail<'a>(&'a self, start: &uint) -> &'a [T] {
1271 self.as_slice().slice_from_or_fail(start)
1275 fn slice_to_or_fail<'a>(&'a self, end: &uint) -> &'a [T] {
1276 self.as_slice().slice_to_or_fail(end)
1279 fn slice_or_fail<'a>(&'a self, start: &uint, end: &uint) -> &'a [T] {
1280 self.as_slice().slice_or_fail(start, end)
1284 impl<T> ops::SliceMut<uint, [T]> for Vec<T> {
1286 fn as_mut_slice_<'a>(&'a mut self) -> &'a mut [T] {
1291 fn slice_from_or_fail_mut<'a>(&'a mut self, start: &uint) -> &'a mut [T] {
1292 self.as_mut_slice().slice_from_or_fail_mut(start)
1296 fn slice_to_or_fail_mut<'a>(&'a mut self, end: &uint) -> &'a mut [T] {
1297 self.as_mut_slice().slice_to_or_fail_mut(end)
1300 fn slice_or_fail_mut<'a>(&'a mut self, start: &uint, end: &uint) -> &'a mut [T] {
1301 self.as_mut_slice().slice_or_fail_mut(start, end)
1305 #[experimental = "waiting on Deref stability"]
1306 impl<T> ops::Deref for Vec<T> {
1309 fn deref<'a>(&'a self) -> &'a [T] { self.as_slice() }
1312 #[experimental = "waiting on DerefMut stability"]
1313 impl<T> ops::DerefMut for Vec<T> {
1314 fn deref_mut<'a>(&'a mut self) -> &'a mut [T] { self.as_mut_slice() }
1317 #[experimental = "waiting on FromIterator stability"]
1318 impl<T> FromIterator<T> for Vec<T> {
1320 fn from_iter<I:Iterator<T>>(mut iterator: I) -> Vec<T> {
1321 let (lower, _) = iterator.size_hint();
1322 let mut vector = Vec::with_capacity(lower);
1323 for element in iterator {
1324 vector.push(element)
1330 #[experimental = "waiting on Extend stability"]
1331 impl<T> Extend<T> for Vec<T> {
1333 fn extend<I: Iterator<T>>(&mut self, mut iterator: I) {
1334 let (lower, _) = iterator.size_hint();
1335 self.reserve(lower);
1336 for element in iterator {
1342 impl<A, B> PartialEq<Vec<B>> for Vec<A> where A: PartialEq<B> {
1344 fn eq(&self, other: &Vec<B>) -> bool { PartialEq::eq(&**self, &**other) }
1346 fn ne(&self, other: &Vec<B>) -> bool { PartialEq::ne(&**self, &**other) }
1349 macro_rules! impl_eq {
1350 ($lhs:ty, $rhs:ty) => {
1351 impl<'b, A, B> PartialEq<$rhs> for $lhs where A: PartialEq<B> {
1353 fn eq(&self, other: &$rhs) -> bool { PartialEq::eq(&**self, &**other) }
1355 fn ne(&self, other: &$rhs) -> bool { PartialEq::ne(&**self, &**other) }
1358 impl<'b, A, B> PartialEq<$lhs> for $rhs where B: PartialEq<A> {
1360 fn eq(&self, other: &$lhs) -> bool { PartialEq::eq(&**self, &**other) }
1362 fn ne(&self, other: &$lhs) -> bool { PartialEq::ne(&**self, &**other) }
1367 impl_eq! { Vec<A>, &'b [B] }
1368 impl_eq! { Vec<A>, &'b mut [B] }
1370 impl<'a, A, B> PartialEq<Vec<B>> for CowVec<'a, A> where A: PartialEq<B> + Clone {
1372 fn eq(&self, other: &Vec<B>) -> bool { PartialEq::eq(&**self, &**other) }
1374 fn ne(&self, other: &Vec<B>) -> bool { PartialEq::ne(&**self, &**other) }
1377 impl<'a, A, B> PartialEq<CowVec<'a, A>> for Vec<B> where A: Clone, B: PartialEq<A> {
1379 fn eq(&self, other: &CowVec<'a, A>) -> bool { PartialEq::eq(&**self, &**other) }
1381 fn ne(&self, other: &CowVec<'a, A>) -> bool { PartialEq::ne(&**self, &**other) }
1384 macro_rules! impl_eq_for_cowvec {
1386 impl<'a, 'b, A, B> PartialEq<$rhs> for CowVec<'a, A> where A: PartialEq<B> + Clone {
1388 fn eq(&self, other: &$rhs) -> bool { PartialEq::eq(&**self, &**other) }
1390 fn ne(&self, other: &$rhs) -> bool { PartialEq::ne(&**self, &**other) }
1393 impl<'a, 'b, A, B> PartialEq<CowVec<'a, A>> for $rhs where A: Clone, B: PartialEq<A> {
1395 fn eq(&self, other: &CowVec<'a, A>) -> bool { PartialEq::eq(&**self, &**other) }
1397 fn ne(&self, other: &CowVec<'a, A>) -> bool { PartialEq::ne(&**self, &**other) }
1402 impl_eq_for_cowvec! { &'b [B] }
1403 impl_eq_for_cowvec! { &'b mut [B] }
1405 #[unstable = "waiting on PartialOrd stability"]
1406 impl<T: PartialOrd> PartialOrd for Vec<T> {
1408 fn partial_cmp(&self, other: &Vec<T>) -> Option<Ordering> {
1409 self.as_slice().partial_cmp(other.as_slice())
1413 #[unstable = "waiting on Eq stability"]
1414 impl<T: Eq> Eq for Vec<T> {}
1416 #[allow(deprecated)]
1417 #[deprecated = "Use overloaded `core::cmp::PartialEq`"]
1418 impl<T: PartialEq, Sized? V: AsSlice<T>> Equiv<V> for Vec<T> {
1420 fn equiv(&self, other: &V) -> bool { self.as_slice() == other.as_slice() }
1423 #[unstable = "waiting on Ord stability"]
1424 impl<T: Ord> Ord for Vec<T> {
1426 fn cmp(&self, other: &Vec<T>) -> Ordering {
1427 self.as_slice().cmp(other.as_slice())
1431 impl<T> AsSlice<T> for Vec<T> {
1432 /// Returns a slice into `self`.
1437 /// fn foo(slice: &[int]) {}
1439 /// let vec = vec![1i, 2];
1440 /// foo(vec.as_slice());
1444 fn as_slice<'a>(&'a self) -> &'a [T] {
1446 mem::transmute(RawSlice {
1447 data: *self.ptr as *const T,
1454 impl<'a, T: Clone> Add<&'a [T], Vec<T>> for Vec<T> {
1456 fn add(mut self, rhs: &[T]) -> Vec<T> {
1462 #[unsafe_destructor]
1463 impl<T> Drop for Vec<T> {
1464 fn drop(&mut self) {
1465 // This is (and should always remain) a no-op if the fields are
1466 // zeroed (when moving out, because of #[unsafe_no_drop_flag]).
1469 for x in self.iter() {
1472 dealloc(*self.ptr, self.cap)
1479 impl<T> Default for Vec<T> {
1481 fn default() -> Vec<T> {
1486 #[experimental = "waiting on Show stability"]
1487 impl<T:fmt::Show> fmt::Show for Vec<T> {
1488 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1489 self.as_slice().fmt(f)
1493 impl<'a> fmt::Writer for Vec<u8> {
1494 fn write_str(&mut self, s: &str) -> fmt::Result {
1495 self.push_all(s.as_bytes());
1500 ////////////////////////////////////////////////////////////////////////////////
1502 ////////////////////////////////////////////////////////////////////////////////
1504 #[experimental = "unclear how valuable this alias is"]
1505 /// A clone-on-write vector
1506 pub type CowVec<'a, T> = Cow<'a, Vec<T>, [T]>;
1508 impl<'a, T> FromIterator<T> for CowVec<'a, T> where T: Clone {
1509 fn from_iter<I: Iterator<T>>(it: I) -> CowVec<'a, T> {
1510 Cow::Owned(FromIterator::from_iter(it))
1514 impl<'a, T: 'a> IntoCow<'a, Vec<T>, [T]> for Vec<T> where T: Clone {
1515 fn into_cow(self) -> CowVec<'a, T> {
1520 impl<'a, T> IntoCow<'a, Vec<T>, [T]> for &'a [T] where T: Clone {
1521 fn into_cow(self) -> CowVec<'a, T> {
1526 ////////////////////////////////////////////////////////////////////////////////
1528 ////////////////////////////////////////////////////////////////////////////////
1530 /// An iterator that moves out of a vector.
1532 pub struct IntoIter<T> {
1533 allocation: *mut T, // the block of memory allocated for the vector
1534 cap: uint, // the capacity of the vector
1539 #[deprecated = "use IntoIter instead"]
1540 pub type MoveItems<T> = IntoIter<T>;
1542 impl<T> IntoIter<T> {
1544 /// Drops all items that have not yet been moved and returns the empty vector.
1546 pub fn into_inner(mut self) -> Vec<T> {
1549 let IntoIter { allocation, cap, ptr: _ptr, end: _end } = self;
1551 Vec { ptr: NonZero::new(allocation), cap: cap, len: 0 }
1555 /// Deprecated, use .into_inner() instead
1556 #[deprecated = "use .into_inner() instead"]
1557 pub fn unwrap(self) -> Vec<T> { self.into_inner() }
1560 impl<T> Iterator<T> for IntoIter<T> {
1562 fn next<'a>(&'a mut self) -> Option<T> {
1564 if self.ptr == self.end {
1567 if mem::size_of::<T>() == 0 {
1568 // purposefully don't use 'ptr.offset' because for
1569 // vectors with 0-size elements this would return the
1571 self.ptr = mem::transmute(self.ptr as uint + 1);
1573 // Use a non-null pointer value
1574 Some(ptr::read(mem::transmute(1u)))
1577 self.ptr = self.ptr.offset(1);
1579 Some(ptr::read(old))
1586 fn size_hint(&self) -> (uint, Option<uint>) {
1587 let diff = (self.end as uint) - (self.ptr as uint);
1588 let size = mem::size_of::<T>();
1589 let exact = diff / (if size == 0 {1} else {size});
1590 (exact, Some(exact))
1594 impl<T> DoubleEndedIterator<T> for IntoIter<T> {
1596 fn next_back<'a>(&'a mut self) -> Option<T> {
1598 if self.end == self.ptr {
1601 if mem::size_of::<T>() == 0 {
1602 // See above for why 'ptr.offset' isn't used
1603 self.end = mem::transmute(self.end as uint - 1);
1605 // Use a non-null pointer value
1606 Some(ptr::read(mem::transmute(1u)))
1608 self.end = self.end.offset(-1);
1610 Some(ptr::read(mem::transmute(self.end)))
1617 impl<T> ExactSizeIterator<T> for IntoIter<T> {}
1619 #[unsafe_destructor]
1620 impl<T> Drop for IntoIter<T> {
1621 fn drop(&mut self) {
1622 // destroy the remaining elements
1626 dealloc(self.allocation, self.cap);
1632 /// An iterator that drains a vector.
1633 #[unsafe_no_drop_flag]
1634 #[unstable = "recently added as part of collections reform 2"]
1635 pub struct Drain<'a, T> {
1638 marker: ContravariantLifetime<'a>,
1641 impl<'a, T> Iterator<T> for Drain<'a, T> {
1643 fn next(&mut self) -> Option<T> {
1645 if self.ptr == self.end {
1648 if mem::size_of::<T>() == 0 {
1649 // purposefully don't use 'ptr.offset' because for
1650 // vectors with 0-size elements this would return the
1652 self.ptr = mem::transmute(self.ptr as uint + 1);
1654 // Use a non-null pointer value
1655 Some(ptr::read(mem::transmute(1u)))
1658 self.ptr = self.ptr.offset(1);
1660 Some(ptr::read(old))
1667 fn size_hint(&self) -> (uint, Option<uint>) {
1668 let diff = (self.end as uint) - (self.ptr as uint);
1669 let size = mem::size_of::<T>();
1670 let exact = diff / (if size == 0 {1} else {size});
1671 (exact, Some(exact))
1675 impl<'a, T> DoubleEndedIterator<T> for Drain<'a, T> {
1677 fn next_back(&mut self) -> Option<T> {
1679 if self.end == self.ptr {
1682 if mem::size_of::<T>() == 0 {
1683 // See above for why 'ptr.offset' isn't used
1684 self.end = mem::transmute(self.end as uint - 1);
1686 // Use a non-null pointer value
1687 Some(ptr::read(mem::transmute(1u)))
1689 self.end = self.end.offset(-1);
1691 Some(ptr::read(self.end))
1698 impl<'a, T> ExactSizeIterator<T> for Drain<'a, T> {}
1700 #[unsafe_destructor]
1701 impl<'a, T> Drop for Drain<'a, T> {
1702 fn drop(&mut self) {
1703 // self.ptr == self.end == null if drop has already been called,
1704 // so we can use #[unsafe_no_drop_flag].
1706 // destroy the remaining elements
1711 ////////////////////////////////////////////////////////////////////////////////
1712 // Conversion from &[T] to &Vec<T>
1713 ////////////////////////////////////////////////////////////////////////////////
1715 /// Wrapper type providing a `&Vec<T>` reference via `Deref`.
1717 pub struct DerefVec<'a, T> {
1719 l: ContravariantLifetime<'a>
1723 impl<'a, T> Deref for DerefVec<'a, T> {
1724 type Target = Vec<T>;
1726 fn deref<'b>(&'b self) -> &'b Vec<T> {
1731 // Prevent the inner `Vec<T>` from attempting to deallocate memory.
1732 #[unsafe_destructor]
1734 impl<'a, T> Drop for DerefVec<'a, T> {
1735 fn drop(&mut self) {
1741 /// Convert a slice to a wrapper type providing a `&Vec<T>` reference.
1743 pub fn as_vec<'a, T>(x: &'a [T]) -> DerefVec<'a, T> {
1746 x: Vec::from_raw_parts(x.as_ptr() as *mut T, x.len(), x.len()),
1747 l: ContravariantLifetime::<'a>
1752 ////////////////////////////////////////////////////////////////////////////////
1753 // Raw module (deprecated)
1754 ////////////////////////////////////////////////////////////////////////////////
1756 /// Unsafe vector operations.
1761 /// Constructs a vector from an unsafe pointer to a buffer.
1763 /// The elements of the buffer are copied into the vector without cloning,
1764 /// as if `ptr::read()` were called on them.
1766 #[deprecated = "renamed to Vec::from_raw_buf"]
1767 pub unsafe fn from_buf<T>(ptr: *const T, elts: uint) -> Vec<T> {
1768 Vec::from_raw_buf(ptr, elts)
1772 ////////////////////////////////////////////////////////////////////////////////
1773 // Partial vec, used for map_in_place
1774 ////////////////////////////////////////////////////////////////////////////////
1776 /// An owned, partially type-converted vector of elements with non-zero size.
1778 /// `T` and `U` must have the same, non-zero size. They must also have the same
1781 /// When the destructor of this struct runs, all `U`s from `start_u` (incl.) to
1782 /// `end_u` (excl.) and all `T`s from `start_t` (incl.) to `end_t` (excl.) are
1783 /// destructed. Additionally the underlying storage of `vec` will be freed.
1784 struct PartialVecNonZeroSized<T,U> {
1793 /// An owned, partially type-converted vector of zero-sized elements.
1795 /// When the destructor of this struct runs, all `num_t` `T`s and `num_u` `U`s
1797 struct PartialVecZeroSized<T,U> {
1800 marker_t: InvariantType<T>,
1801 marker_u: InvariantType<U>,
1804 #[unsafe_destructor]
1805 impl<T,U> Drop for PartialVecNonZeroSized<T,U> {
1806 fn drop(&mut self) {
1808 // `vec` hasn't been modified until now. As it has a length
1809 // currently, this would run destructors of `T`s which might not be
1810 // there. So at first, set `vec`s length to `0`. This must be done
1811 // at first to remain memory-safe as the destructors of `U` or `T`
1812 // might cause unwinding where `vec`s destructor would be executed.
1813 self.vec.set_len(0);
1815 // We have instances of `U`s and `T`s in `vec`. Destruct them.
1816 while self.start_u != self.end_u {
1817 let _ = ptr::read(self.start_u as *const U); // Run a `U` destructor.
1818 self.start_u = self.start_u.offset(1);
1820 while self.start_t != self.end_t {
1821 let _ = ptr::read(self.start_t as *const T); // Run a `T` destructor.
1822 self.start_t = self.start_t.offset(1);
1824 // After this destructor ran, the destructor of `vec` will run,
1825 // deallocating the underlying memory.
1830 #[unsafe_destructor]
1831 impl<T,U> Drop for PartialVecZeroSized<T,U> {
1832 fn drop(&mut self) {
1834 // Destruct the instances of `T` and `U` this struct owns.
1835 while self.num_t != 0 {
1836 let _: T = mem::uninitialized(); // Run a `T` destructor.
1839 while self.num_u != 0 {
1840 let _: U = mem::uninitialized(); // Run a `U` destructor.
1850 use core::mem::size_of;
1852 use super::{as_vec, unzip, raw};
1854 struct DropCounter<'a> {
1858 #[unsafe_destructor]
1859 impl<'a> Drop for DropCounter<'a> {
1860 fn drop(&mut self) {
1867 let xs = [1u8, 2u8, 3u8];
1868 assert_eq!(as_vec(&xs).as_slice(), xs);
1872 fn test_as_vec_dtor() {
1873 let (mut count_x, mut count_y) = (0, 0);
1875 let xs = &[DropCounter { count: &mut count_x }, DropCounter { count: &mut count_y }];
1876 assert_eq!(as_vec(xs).len(), 2);
1878 assert_eq!(count_x, 1);
1879 assert_eq!(count_y, 1);
1883 fn test_small_vec_struct() {
1884 assert!(size_of::<Vec<u8>>() == size_of::<uint>() * 3);
1888 fn test_double_drop() {
1894 let (mut count_x, mut count_y) = (0, 0);
1896 let mut tv = TwoVec {
1900 tv.x.push(DropCounter {count: &mut count_x});
1901 tv.y.push(DropCounter {count: &mut count_y});
1903 // If Vec had a drop flag, here is where it would be zeroed.
1904 // Instead, it should rely on its internal state to prevent
1905 // doing anything significant when dropped multiple times.
1908 // Here tv goes out of scope, tv.y should be dropped, but not tv.x.
1911 assert_eq!(count_x, 1);
1912 assert_eq!(count_y, 1);
1917 let mut v = Vec::new();
1918 assert_eq!(v.capacity(), 0);
1921 assert!(v.capacity() >= 2);
1923 for i in range(0i, 16) {
1927 assert!(v.capacity() >= 16);
1929 assert!(v.capacity() >= 32);
1934 assert!(v.capacity() >= 33)
1939 let mut v = Vec::new();
1940 let mut w = Vec::new();
1942 v.extend(range(0i, 3));
1943 for i in range(0i, 3) { w.push(i) }
1947 v.extend(range(3i, 10));
1948 for i in range(3i, 10) { w.push(i) }
1954 fn test_slice_from_mut() {
1955 let mut values = vec![1u8,2,3,4,5];
1957 let slice = values.slice_from_mut(2);
1958 assert!(slice == [3, 4, 5]);
1959 for p in slice.iter_mut() {
1964 assert!(values == [1, 2, 5, 6, 7]);
1968 fn test_slice_to_mut() {
1969 let mut values = vec![1u8,2,3,4,5];
1971 let slice = values.slice_to_mut(2);
1972 assert!(slice == [1, 2]);
1973 for p in slice.iter_mut() {
1978 assert!(values == [2, 3, 3, 4, 5]);
1982 fn test_split_at_mut() {
1983 let mut values = vec![1u8,2,3,4,5];
1985 let (left, right) = values.split_at_mut(2);
1987 let left: &[_] = left;
1988 assert!(left[0..left.len()] == [1, 2][]);
1990 for p in left.iter_mut() {
1995 let right: &[_] = right;
1996 assert!(right[0..right.len()] == [3, 4, 5][]);
1998 for p in right.iter_mut() {
2003 assert!(values == vec![2u8, 3, 5, 6, 7]);
2008 let v: Vec<int> = vec!();
2009 let w = vec!(1i, 2, 3);
2011 assert_eq!(v, v.clone());
2015 // they should be disjoint in memory.
2016 assert!(w.as_ptr() != z.as_ptr())
2020 fn test_clone_from() {
2022 let three = vec!(box 1i, box 2, box 3);
2023 let two = vec!(box 4i, box 5);
2025 v.clone_from(&three);
2026 assert_eq!(v, three);
2029 v.clone_from(&three);
2030 assert_eq!(v, three);
2037 v.clone_from(&three);
2038 assert_eq!(v, three)
2043 let mut v = vec![0u, 1];
2044 v.grow_fn(3, |i| i);
2045 assert!(v == vec![0u, 1, 0, 1, 2]);
2050 let mut vec = vec![1u, 2, 3, 4];
2051 vec.retain(|&x| x % 2 == 0);
2052 assert!(vec == vec![2u, 4]);
2056 fn zero_sized_values() {
2057 let mut v = Vec::new();
2058 assert_eq!(v.len(), 0);
2060 assert_eq!(v.len(), 1);
2062 assert_eq!(v.len(), 2);
2063 assert_eq!(v.pop(), Some(()));
2064 assert_eq!(v.pop(), Some(()));
2065 assert_eq!(v.pop(), None);
2067 assert_eq!(v.iter().count(), 0);
2069 assert_eq!(v.iter().count(), 1);
2071 assert_eq!(v.iter().count(), 2);
2073 for &() in v.iter() {}
2075 assert_eq!(v.iter_mut().count(), 2);
2077 assert_eq!(v.iter_mut().count(), 3);
2079 assert_eq!(v.iter_mut().count(), 4);
2081 for &() in v.iter_mut() {}
2082 unsafe { v.set_len(0); }
2083 assert_eq!(v.iter_mut().count(), 0);
2087 fn test_partition() {
2088 assert_eq!(vec![].partition(|x: &int| *x < 3), (vec![], vec![]));
2089 assert_eq!(vec![1i, 2, 3].partition(|x: &int| *x < 4), (vec![1, 2, 3], vec![]));
2090 assert_eq!(vec![1i, 2, 3].partition(|x: &int| *x < 2), (vec![1], vec![2, 3]));
2091 assert_eq!(vec![1i, 2, 3].partition(|x: &int| *x < 0), (vec![], vec![1, 2, 3]));
2095 fn test_partitioned() {
2096 assert_eq!(vec![].partitioned(|x: &int| *x < 3), (vec![], vec![]));
2097 assert_eq!(vec![1i, 2, 3].partitioned(|x: &int| *x < 4), (vec![1, 2, 3], vec![]));
2098 assert_eq!(vec![1i, 2, 3].partitioned(|x: &int| *x < 2), (vec![1], vec![2, 3]));
2099 assert_eq!(vec![1i, 2, 3].partitioned(|x: &int| *x < 0), (vec![], vec![1, 2, 3]));
2103 fn test_zip_unzip() {
2104 let z1 = vec![(1i, 4i), (2, 5), (3, 6)];
2106 let (left, right) = unzip(z1.iter().map(|&x| x));
2108 assert_eq!((1, 4), (left[0], right[0]));
2109 assert_eq!((2, 5), (left[1], right[1]));
2110 assert_eq!((3, 6), (left[2], right[2]));
2114 fn test_unsafe_ptrs() {
2116 // Test on-stack copy-from-buf.
2118 let ptr = a.as_ptr();
2119 let b = raw::from_buf(ptr, 3u);
2120 assert_eq!(b, vec![1, 2, 3]);
2122 // Test on-heap copy-from-buf.
2123 let c = vec![1i, 2, 3, 4, 5];
2124 let ptr = c.as_ptr();
2125 let d = raw::from_buf(ptr, 5u);
2126 assert_eq!(d, vec![1, 2, 3, 4, 5]);
2131 fn test_vec_truncate_drop() {
2132 static mut drops: uint = 0;
2134 impl Drop for Elem {
2135 fn drop(&mut self) {
2136 unsafe { drops += 1; }
2140 let mut v = vec![Elem(1), Elem(2), Elem(3), Elem(4), Elem(5)];
2141 assert_eq!(unsafe { drops }, 0);
2143 assert_eq!(unsafe { drops }, 2);
2145 assert_eq!(unsafe { drops }, 5);
2150 fn test_vec_truncate_fail() {
2151 struct BadElem(int);
2152 impl Drop for BadElem {
2153 fn drop(&mut self) {
2154 let BadElem(ref mut x) = *self;
2155 if *x == 0xbadbeef {
2156 panic!("BadElem panic: 0xbadbeef")
2161 let mut v = vec![BadElem(1), BadElem(2), BadElem(0xbadbeef), BadElem(4)];
2167 let vec = vec!(1i, 2, 3);
2168 assert!(vec[1] == 2);
2173 fn test_index_out_of_bounds() {
2174 let vec = vec!(1i, 2, 3);
2180 fn test_slice_out_of_bounds_1() {
2181 let x: Vec<int> = vec![1, 2, 3, 4, 5];
2187 fn test_slice_out_of_bounds_2() {
2188 let x: Vec<int> = vec![1, 2, 3, 4, 5];
2194 fn test_slice_out_of_bounds_3() {
2195 let x: Vec<int> = vec![1, 2, 3, 4, 5];
2201 fn test_slice_out_of_bounds_4() {
2202 let x: Vec<int> = vec![1, 2, 3, 4, 5];
2208 fn test_slice_out_of_bounds_5() {
2209 let x: Vec<int> = vec![1, 2, 3, 4, 5];
2215 fn test_swap_remove_empty() {
2216 let mut vec: Vec<uint> = vec!();
2221 fn test_move_iter_unwrap() {
2222 let mut vec: Vec<uint> = Vec::with_capacity(7);
2225 let ptr = vec.as_ptr();
2226 vec = vec.into_iter().unwrap();
2227 assert_eq!(vec.as_ptr(), ptr);
2228 assert_eq!(vec.capacity(), 7);
2229 assert_eq!(vec.len(), 0);
2234 fn test_map_in_place_incompatible_types_fail() {
2235 let v = vec![0u, 1, 2];
2236 v.map_in_place(|_| ());
2240 fn test_map_in_place() {
2241 let v = vec![0u, 1, 2];
2242 assert_eq!(v.map_in_place(|i: uint| i as int - 1), [-1i, 0, 1]);
2246 fn test_map_in_place_zero_sized() {
2247 let v = vec![(), ()];
2248 #[deriving(PartialEq, Show)]
2250 assert_eq!(v.map_in_place(|_| ZeroSized), [ZeroSized, ZeroSized]);
2254 fn test_map_in_place_zero_drop_count() {
2255 use std::sync::atomic;
2256 use std::sync::atomic::AtomicUint;
2258 #[deriving(Clone, PartialEq, Show)]
2260 impl Drop for Nothing { fn drop(&mut self) { } }
2262 #[deriving(Clone, PartialEq, Show)]
2264 impl Drop for ZeroSized {
2265 fn drop(&mut self) {
2266 DROP_COUNTER.fetch_add(1, atomic::Relaxed);
2269 const NUM_ELEMENTS: uint = 2;
2270 static DROP_COUNTER: AtomicUint = atomic::ATOMIC_UINT_INIT;
2272 let v = Vec::from_elem(NUM_ELEMENTS, Nothing);
2274 DROP_COUNTER.store(0, atomic::Relaxed);
2276 let v = v.map_in_place(|_| ZeroSized);
2277 assert_eq!(DROP_COUNTER.load(atomic::Relaxed), 0);
2279 assert_eq!(DROP_COUNTER.load(atomic::Relaxed), NUM_ELEMENTS);
2283 fn test_move_items() {
2284 let vec = vec![1, 2, 3];
2285 let mut vec2 : Vec<i32> = vec![];
2286 for i in vec.into_iter() {
2289 assert!(vec2 == vec![1, 2, 3]);
2293 fn test_move_items_reverse() {
2294 let vec = vec![1, 2, 3];
2295 let mut vec2 : Vec<i32> = vec![];
2296 for i in vec.into_iter().rev() {
2299 assert!(vec2 == vec![3, 2, 1]);
2303 fn test_move_items_zero_sized() {
2304 let vec = vec![(), (), ()];
2305 let mut vec2 : Vec<()> = vec![];
2306 for i in vec.into_iter() {
2309 assert!(vec2 == vec![(), (), ()]);
2313 fn test_drain_items() {
2314 let mut vec = vec![1, 2, 3];
2315 let mut vec2: Vec<i32> = vec![];
2316 for i in vec.drain() {
2319 assert_eq!(vec, []);
2320 assert_eq!(vec2, [ 1, 2, 3 ]);
2324 fn test_drain_items_reverse() {
2325 let mut vec = vec![1, 2, 3];
2326 let mut vec2: Vec<i32> = vec![];
2327 for i in vec.drain().rev() {
2330 assert_eq!(vec, []);
2331 assert_eq!(vec2, [ 3, 2, 1 ]);
2335 fn test_drain_items_zero_sized() {
2336 let mut vec = vec![(), (), ()];
2337 let mut vec2: Vec<()> = vec![];
2338 for i in vec.drain() {
2341 assert_eq!(vec, []);
2342 assert_eq!(vec2, [(), (), ()]);
2346 fn test_into_boxed_slice() {
2347 let xs = vec![1u, 2, 3];
2348 let ys = xs.into_boxed_slice();
2349 assert_eq!(ys.as_slice(), [1u, 2, 3]);
2353 fn bench_new(b: &mut Bencher) {
2355 let v: Vec<uint> = Vec::new();
2356 assert_eq!(v.len(), 0);
2357 assert_eq!(v.capacity(), 0);
2361 fn do_bench_with_capacity(b: &mut Bencher, src_len: uint) {
2362 b.bytes = src_len as u64;
2365 let v: Vec<uint> = Vec::with_capacity(src_len);
2366 assert_eq!(v.len(), 0);
2367 assert_eq!(v.capacity(), src_len);
2372 fn bench_with_capacity_0000(b: &mut Bencher) {
2373 do_bench_with_capacity(b, 0)
2377 fn bench_with_capacity_0010(b: &mut Bencher) {
2378 do_bench_with_capacity(b, 10)
2382 fn bench_with_capacity_0100(b: &mut Bencher) {
2383 do_bench_with_capacity(b, 100)
2387 fn bench_with_capacity_1000(b: &mut Bencher) {
2388 do_bench_with_capacity(b, 1000)
2391 fn do_bench_from_fn(b: &mut Bencher, src_len: uint) {
2392 b.bytes = src_len as u64;
2395 let dst = Vec::from_fn(src_len, |i| i);
2396 assert_eq!(dst.len(), src_len);
2397 assert!(dst.iter().enumerate().all(|(i, x)| i == *x));
2402 fn bench_from_fn_0000(b: &mut Bencher) {
2403 do_bench_from_fn(b, 0)
2407 fn bench_from_fn_0010(b: &mut Bencher) {
2408 do_bench_from_fn(b, 10)
2412 fn bench_from_fn_0100(b: &mut Bencher) {
2413 do_bench_from_fn(b, 100)
2417 fn bench_from_fn_1000(b: &mut Bencher) {
2418 do_bench_from_fn(b, 1000)
2421 fn do_bench_from_elem(b: &mut Bencher, src_len: uint) {
2422 b.bytes = src_len as u64;
2425 let dst: Vec<uint> = Vec::from_elem(src_len, 5);
2426 assert_eq!(dst.len(), src_len);
2427 assert!(dst.iter().all(|x| *x == 5));
2432 fn bench_from_elem_0000(b: &mut Bencher) {
2433 do_bench_from_elem(b, 0)
2437 fn bench_from_elem_0010(b: &mut Bencher) {
2438 do_bench_from_elem(b, 10)
2442 fn bench_from_elem_0100(b: &mut Bencher) {
2443 do_bench_from_elem(b, 100)
2447 fn bench_from_elem_1000(b: &mut Bencher) {
2448 do_bench_from_elem(b, 1000)
2451 fn do_bench_from_slice(b: &mut Bencher, src_len: uint) {
2452 let src: Vec<uint> = FromIterator::from_iter(range(0, src_len));
2454 b.bytes = src_len as u64;
2457 let dst = src.clone().as_slice().to_vec();
2458 assert_eq!(dst.len(), src_len);
2459 assert!(dst.iter().enumerate().all(|(i, x)| i == *x));
2464 fn bench_from_slice_0000(b: &mut Bencher) {
2465 do_bench_from_slice(b, 0)
2469 fn bench_from_slice_0010(b: &mut Bencher) {
2470 do_bench_from_slice(b, 10)
2474 fn bench_from_slice_0100(b: &mut Bencher) {
2475 do_bench_from_slice(b, 100)
2479 fn bench_from_slice_1000(b: &mut Bencher) {
2480 do_bench_from_slice(b, 1000)
2483 fn do_bench_from_iter(b: &mut Bencher, src_len: uint) {
2484 let src: Vec<uint> = FromIterator::from_iter(range(0, src_len));
2486 b.bytes = src_len as u64;
2489 let dst: Vec<uint> = FromIterator::from_iter(src.clone().into_iter());
2490 assert_eq!(dst.len(), src_len);
2491 assert!(dst.iter().enumerate().all(|(i, x)| i == *x));
2496 fn bench_from_iter_0000(b: &mut Bencher) {
2497 do_bench_from_iter(b, 0)
2501 fn bench_from_iter_0010(b: &mut Bencher) {
2502 do_bench_from_iter(b, 10)
2506 fn bench_from_iter_0100(b: &mut Bencher) {
2507 do_bench_from_iter(b, 100)
2511 fn bench_from_iter_1000(b: &mut Bencher) {
2512 do_bench_from_iter(b, 1000)
2515 fn do_bench_extend(b: &mut Bencher, dst_len: uint, src_len: uint) {
2516 let dst: Vec<uint> = FromIterator::from_iter(range(0, dst_len));
2517 let src: Vec<uint> = FromIterator::from_iter(range(dst_len, dst_len + src_len));
2519 b.bytes = src_len as u64;
2522 let mut dst = dst.clone();
2523 dst.extend(src.clone().into_iter());
2524 assert_eq!(dst.len(), dst_len + src_len);
2525 assert!(dst.iter().enumerate().all(|(i, x)| i == *x));
2530 fn bench_extend_0000_0000(b: &mut Bencher) {
2531 do_bench_extend(b, 0, 0)
2535 fn bench_extend_0000_0010(b: &mut Bencher) {
2536 do_bench_extend(b, 0, 10)
2540 fn bench_extend_0000_0100(b: &mut Bencher) {
2541 do_bench_extend(b, 0, 100)
2545 fn bench_extend_0000_1000(b: &mut Bencher) {
2546 do_bench_extend(b, 0, 1000)
2550 fn bench_extend_0010_0010(b: &mut Bencher) {
2551 do_bench_extend(b, 10, 10)
2555 fn bench_extend_0100_0100(b: &mut Bencher) {
2556 do_bench_extend(b, 100, 100)
2560 fn bench_extend_1000_1000(b: &mut Bencher) {
2561 do_bench_extend(b, 1000, 1000)
2564 fn do_bench_push_all(b: &mut Bencher, dst_len: uint, src_len: uint) {
2565 let dst: Vec<uint> = FromIterator::from_iter(range(0, dst_len));
2566 let src: Vec<uint> = FromIterator::from_iter(range(dst_len, dst_len + src_len));
2568 b.bytes = src_len as u64;
2571 let mut dst = dst.clone();
2572 dst.push_all(src.as_slice());
2573 assert_eq!(dst.len(), dst_len + src_len);
2574 assert!(dst.iter().enumerate().all(|(i, x)| i == *x));
2579 fn bench_push_all_0000_0000(b: &mut Bencher) {
2580 do_bench_push_all(b, 0, 0)
2584 fn bench_push_all_0000_0010(b: &mut Bencher) {
2585 do_bench_push_all(b, 0, 10)
2589 fn bench_push_all_0000_0100(b: &mut Bencher) {
2590 do_bench_push_all(b, 0, 100)
2594 fn bench_push_all_0000_1000(b: &mut Bencher) {
2595 do_bench_push_all(b, 0, 1000)
2599 fn bench_push_all_0010_0010(b: &mut Bencher) {
2600 do_bench_push_all(b, 10, 10)
2604 fn bench_push_all_0100_0100(b: &mut Bencher) {
2605 do_bench_push_all(b, 100, 100)
2609 fn bench_push_all_1000_1000(b: &mut Bencher) {
2610 do_bench_push_all(b, 1000, 1000)
2613 fn do_bench_push_all_move(b: &mut Bencher, dst_len: uint, src_len: uint) {
2614 let dst: Vec<uint> = FromIterator::from_iter(range(0u, dst_len));
2615 let src: Vec<uint> = FromIterator::from_iter(range(dst_len, dst_len + src_len));
2617 b.bytes = src_len as u64;
2620 let mut dst = dst.clone();
2621 dst.extend(src.clone().into_iter());
2622 assert_eq!(dst.len(), dst_len + src_len);
2623 assert!(dst.iter().enumerate().all(|(i, x)| i == *x));
2628 fn bench_push_all_move_0000_0000(b: &mut Bencher) {
2629 do_bench_push_all_move(b, 0, 0)
2633 fn bench_push_all_move_0000_0010(b: &mut Bencher) {
2634 do_bench_push_all_move(b, 0, 10)
2638 fn bench_push_all_move_0000_0100(b: &mut Bencher) {
2639 do_bench_push_all_move(b, 0, 100)
2643 fn bench_push_all_move_0000_1000(b: &mut Bencher) {
2644 do_bench_push_all_move(b, 0, 1000)
2648 fn bench_push_all_move_0010_0010(b: &mut Bencher) {
2649 do_bench_push_all_move(b, 10, 10)
2653 fn bench_push_all_move_0100_0100(b: &mut Bencher) {
2654 do_bench_push_all_move(b, 100, 100)
2658 fn bench_push_all_move_1000_1000(b: &mut Bencher) {
2659 do_bench_push_all_move(b, 1000, 1000)
2662 fn do_bench_clone(b: &mut Bencher, src_len: uint) {
2663 let src: Vec<uint> = FromIterator::from_iter(range(0, src_len));
2665 b.bytes = src_len as u64;
2668 let dst = src.clone();
2669 assert_eq!(dst.len(), src_len);
2670 assert!(dst.iter().enumerate().all(|(i, x)| i == *x));
2675 fn bench_clone_0000(b: &mut Bencher) {
2676 do_bench_clone(b, 0)
2680 fn bench_clone_0010(b: &mut Bencher) {
2681 do_bench_clone(b, 10)
2685 fn bench_clone_0100(b: &mut Bencher) {
2686 do_bench_clone(b, 100)
2690 fn bench_clone_1000(b: &mut Bencher) {
2691 do_bench_clone(b, 1000)
2694 fn do_bench_clone_from(b: &mut Bencher, times: uint, dst_len: uint, src_len: uint) {
2695 let dst: Vec<uint> = FromIterator::from_iter(range(0, src_len));
2696 let src: Vec<uint> = FromIterator::from_iter(range(dst_len, dst_len + src_len));
2698 b.bytes = (times * src_len) as u64;
2701 let mut dst = dst.clone();
2703 for _ in range(0, times) {
2704 dst.clone_from(&src);
2706 assert_eq!(dst.len(), src_len);
2707 assert!(dst.iter().enumerate().all(|(i, x)| dst_len + i == *x));
2713 fn bench_clone_from_01_0000_0000(b: &mut Bencher) {
2714 do_bench_clone_from(b, 1, 0, 0)
2718 fn bench_clone_from_01_0000_0010(b: &mut Bencher) {
2719 do_bench_clone_from(b, 1, 0, 10)
2723 fn bench_clone_from_01_0000_0100(b: &mut Bencher) {
2724 do_bench_clone_from(b, 1, 0, 100)
2728 fn bench_clone_from_01_0000_1000(b: &mut Bencher) {
2729 do_bench_clone_from(b, 1, 0, 1000)
2733 fn bench_clone_from_01_0010_0010(b: &mut Bencher) {
2734 do_bench_clone_from(b, 1, 10, 10)
2738 fn bench_clone_from_01_0100_0100(b: &mut Bencher) {
2739 do_bench_clone_from(b, 1, 100, 100)
2743 fn bench_clone_from_01_1000_1000(b: &mut Bencher) {
2744 do_bench_clone_from(b, 1, 1000, 1000)
2748 fn bench_clone_from_01_0010_0100(b: &mut Bencher) {
2749 do_bench_clone_from(b, 1, 10, 100)
2753 fn bench_clone_from_01_0100_1000(b: &mut Bencher) {
2754 do_bench_clone_from(b, 1, 100, 1000)
2758 fn bench_clone_from_01_0010_0000(b: &mut Bencher) {
2759 do_bench_clone_from(b, 1, 10, 0)
2763 fn bench_clone_from_01_0100_0010(b: &mut Bencher) {
2764 do_bench_clone_from(b, 1, 100, 10)
2768 fn bench_clone_from_01_1000_0100(b: &mut Bencher) {
2769 do_bench_clone_from(b, 1, 1000, 100)
2773 fn bench_clone_from_10_0000_0000(b: &mut Bencher) {
2774 do_bench_clone_from(b, 10, 0, 0)
2778 fn bench_clone_from_10_0000_0010(b: &mut Bencher) {
2779 do_bench_clone_from(b, 10, 0, 10)
2783 fn bench_clone_from_10_0000_0100(b: &mut Bencher) {
2784 do_bench_clone_from(b, 10, 0, 100)
2788 fn bench_clone_from_10_0000_1000(b: &mut Bencher) {
2789 do_bench_clone_from(b, 10, 0, 1000)
2793 fn bench_clone_from_10_0010_0010(b: &mut Bencher) {
2794 do_bench_clone_from(b, 10, 10, 10)
2798 fn bench_clone_from_10_0100_0100(b: &mut Bencher) {
2799 do_bench_clone_from(b, 10, 100, 100)
2803 fn bench_clone_from_10_1000_1000(b: &mut Bencher) {
2804 do_bench_clone_from(b, 10, 1000, 1000)
2808 fn bench_clone_from_10_0010_0100(b: &mut Bencher) {
2809 do_bench_clone_from(b, 10, 10, 100)
2813 fn bench_clone_from_10_0100_1000(b: &mut Bencher) {
2814 do_bench_clone_from(b, 10, 100, 1000)
2818 fn bench_clone_from_10_0010_0000(b: &mut Bencher) {
2819 do_bench_clone_from(b, 10, 10, 0)
2823 fn bench_clone_from_10_0100_0010(b: &mut Bencher) {
2824 do_bench_clone_from(b, 10, 100, 10)
2828 fn bench_clone_from_10_1000_0100(b: &mut Bencher) {
2829 do_bench_clone_from(b, 10, 1000, 100)