]> git.lizzy.rs Git - rust.git/blob - src/libcollections/vec.rs
rollup merge of #21036: renato-zannon/into-iter-impls
[rust.git] / src / libcollections / vec.rs
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.
4 //
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.
10
11 //! A growable list type, written `Vec<T>` but pronounced 'vector.'
12 //!
13 //! Vectors have `O(1)` indexing, push (to the end) and pop (from the end).
14 //!
15 //! # Examples
16 //!
17 //! Explicitly creating a `Vec<T>` with `new()`:
18 //!
19 //! ```
20 //! let xs: Vec<i32> = Vec::new();
21 //! ```
22 //!
23 //! Using the `vec!` macro:
24 //!
25 //! ```
26 //! let ys: Vec<i32> = vec![];
27 //!
28 //! let zs = vec![1i32, 2, 3, 4, 5];
29 //! ```
30 //!
31 //! Push:
32 //!
33 //! ```
34 //! let mut xs = vec![1i32, 2];
35 //!
36 //! xs.push(3);
37 //! ```
38 //!
39 //! And pop:
40 //!
41 //! ```
42 //! let mut xs = vec![1i32, 2];
43 //!
44 //! let two = xs.pop();
45 //! ```
46
47 #![stable]
48
49 use core::prelude::*;
50
51 use alloc::boxed::Box;
52 use alloc::heap::{EMPTY, allocate, reallocate, deallocate};
53 use core::borrow::{Cow, IntoCow};
54 use core::cmp::max;
55 use core::cmp::{Ordering};
56 use core::default::Default;
57 use core::fmt;
58 use core::hash::{self, Hash};
59 use core::iter::{repeat, FromIterator};
60 use core::marker::{ContravariantLifetime, InvariantType};
61 use core::mem;
62 use core::nonzero::NonZero;
63 use core::num::{Int, UnsignedInt};
64 use core::ops::{Index, IndexMut, Deref, Add};
65 use core::ops;
66 use core::ptr;
67 use core::raw::Slice as RawSlice;
68 use core::uint;
69
70 /// A growable list type, written `Vec<T>` but pronounced 'vector.'
71 ///
72 /// # Examples
73 ///
74 /// ```
75 /// let mut vec = Vec::new();
76 /// vec.push(1i);
77 /// vec.push(2i);
78 ///
79 /// assert_eq!(vec.len(), 2);
80 /// assert_eq!(vec[0], 1);
81 ///
82 /// assert_eq!(vec.pop(), Some(2));
83 /// assert_eq!(vec.len(), 1);
84 ///
85 /// vec[0] = 7i;
86 /// assert_eq!(vec[0], 7);
87 ///
88 /// vec.push_all(&[1, 2, 3]);
89 ///
90 /// for x in vec.iter() {
91 ///     println!("{}", x);
92 /// }
93 /// assert_eq!(vec, vec![7i, 1, 2, 3]);
94 /// ```
95 ///
96 /// The `vec!` macro is provided to make initialization more convenient:
97 ///
98 /// ```
99 /// let mut vec = vec![1i, 2i, 3i];
100 /// vec.push(4);
101 /// assert_eq!(vec, vec![1, 2, 3, 4]);
102 /// ```
103 ///
104 /// Use a `Vec<T>` as an efficient stack:
105 ///
106 /// ```
107 /// let mut stack = Vec::new();
108 ///
109 /// stack.push(1i);
110 /// stack.push(2i);
111 /// stack.push(3i);
112 ///
113 /// loop {
114 ///     let top = match stack.pop() {
115 ///         None => break, // empty
116 ///         Some(x) => x,
117 ///     };
118 ///     // Prints 3, 2, 1
119 ///     println!("{}", top);
120 /// }
121 /// ```
122 ///
123 /// # Capacity and reallocation
124 ///
125 /// The capacity of a vector is the amount of space allocated for any future elements that will be
126 /// added onto the vector. This is not to be confused with the *length* of a vector, which
127 /// specifies the number of actual elements within the vector. If a vector's length exceeds its
128 /// capacity, its capacity will automatically be increased, but its elements will have to be
129 /// reallocated.
130 ///
131 /// For example, a vector with capacity 10 and length 0 would be an empty vector with space for 10
132 /// more elements. Pushing 10 or fewer elements onto the vector will not change its capacity or
133 /// cause reallocation to occur. However, if the vector's length is increased to 11, it will have
134 /// to reallocate, which can be slow. For this reason, it is recommended to use
135 /// `Vec::with_capacity` whenever possible to specify how big the vector is expected to get.
136 #[unsafe_no_drop_flag]
137 #[stable]
138 pub struct Vec<T> {
139     ptr: NonZero<*mut T>,
140     len: uint,
141     cap: uint,
142 }
143
144 unsafe impl<T: Send> Send for Vec<T> { }
145 unsafe impl<T: Sync> Sync for Vec<T> { }
146
147 ////////////////////////////////////////////////////////////////////////////////
148 // Inherent methods
149 ////////////////////////////////////////////////////////////////////////////////
150
151 impl<T> Vec<T> {
152     /// Constructs a new, empty `Vec<T>`.
153     ///
154     /// The vector will not allocate until elements are pushed onto it.
155     ///
156     /// # Examples
157     ///
158     /// ```
159     /// let mut vec: Vec<int> = Vec::new();
160     /// ```
161     #[inline]
162     #[stable]
163     pub fn new() -> Vec<T> {
164         // We want ptr to never be NULL so instead we set it to some arbitrary
165         // non-null value which is fine since we never call deallocate on the ptr
166         // if cap is 0. The reason for this is because the pointer of a slice
167         // being NULL would break the null pointer optimization for enums.
168         Vec { ptr: unsafe { NonZero::new(EMPTY as *mut T) }, len: 0, cap: 0 }
169     }
170
171     /// Constructs a new, empty `Vec<T>` with the specified capacity.
172     ///
173     /// The vector will be able to hold exactly `capacity` elements without reallocating. If
174     /// `capacity` is 0, the vector will not allocate.
175     ///
176     /// It is important to note that this function does not specify the *length* of the returned
177     /// vector, but only the *capacity*. (For an explanation of the difference between length and
178     /// capacity, see the main `Vec<T>` docs above, 'Capacity and reallocation'.)
179     ///
180     /// # Examples
181     ///
182     /// ```
183     /// let mut vec: Vec<int> = Vec::with_capacity(10);
184     ///
185     /// // The vector contains no items, even though it has capacity for more
186     /// assert_eq!(vec.len(), 0);
187     ///
188     /// // These are all done without reallocating...
189     /// for i in range(0i, 10) {
190     ///     vec.push(i);
191     /// }
192     ///
193     /// // ...but this may make the vector reallocate
194     /// vec.push(11);
195     /// ```
196     #[inline]
197     #[stable]
198     pub fn with_capacity(capacity: uint) -> Vec<T> {
199         if mem::size_of::<T>() == 0 {
200             Vec { ptr: unsafe { NonZero::new(EMPTY as *mut T) }, len: 0, cap: uint::MAX }
201         } else if capacity == 0 {
202             Vec::new()
203         } else {
204             let size = capacity.checked_mul(mem::size_of::<T>())
205                                .expect("capacity overflow");
206             let ptr = unsafe { allocate(size, mem::min_align_of::<T>()) };
207             if ptr.is_null() { ::alloc::oom() }
208             Vec { ptr: unsafe { NonZero::new(ptr as *mut T) }, len: 0, cap: capacity }
209         }
210     }
211
212     /// Creates a `Vec<T>` directly from the raw components of another vector.
213     ///
214     /// This is highly unsafe, due to the number of invariants that aren't checked.
215     ///
216     /// # Examples
217     ///
218     /// ```
219     /// use std::ptr;
220     /// use std::mem;
221     ///
222     /// fn main() {
223     ///     let mut v = vec![1i, 2, 3];
224     ///
225     ///     // Pull out the various important pieces of information about `v`
226     ///     let p = v.as_mut_ptr();
227     ///     let len = v.len();
228     ///     let cap = v.capacity();
229     ///
230     ///     unsafe {
231     ///         // Cast `v` into the void: no destructor run, so we are in
232     ///         // complete control of the allocation to which `p` points.
233     ///         mem::forget(v);
234     ///
235     ///         // Overwrite memory with 4, 5, 6
236     ///         for i in range(0, len as int) {
237     ///             ptr::write(p.offset(i), 4 + i);
238     ///         }
239     ///
240     ///         // Put everything back together into a Vec
241     ///         let rebuilt = Vec::from_raw_parts(p, len, cap);
242     ///         assert_eq!(rebuilt, vec![4i, 5i, 6i]);
243     ///     }
244     /// }
245     /// ```
246     #[stable]
247     pub unsafe fn from_raw_parts(ptr: *mut T, length: uint,
248                                  capacity: uint) -> Vec<T> {
249         Vec { ptr: NonZero::new(ptr), len: length, cap: capacity }
250     }
251
252     /// Creates a vector by copying the elements from a raw pointer.
253     ///
254     /// This function will copy `elts` contiguous elements starting at `ptr` into a new allocation
255     /// owned by the returned `Vec<T>`. The elements of the buffer are copied into the vector
256     /// without cloning, as if `ptr::read()` were called on them.
257     #[inline]
258     #[unstable = "may be better expressed via composition"]
259     pub unsafe fn from_raw_buf(ptr: *const T, elts: uint) -> Vec<T> {
260         let mut dst = Vec::with_capacity(elts);
261         dst.set_len(elts);
262         ptr::copy_nonoverlapping_memory(dst.as_mut_ptr(), ptr, elts);
263         dst
264     }
265
266     /// Returns the number of elements the vector can hold without
267     /// reallocating.
268     ///
269     /// # Examples
270     ///
271     /// ```
272     /// let vec: Vec<int> = Vec::with_capacity(10);
273     /// assert_eq!(vec.capacity(), 10);
274     /// ```
275     #[inline]
276     #[stable]
277     pub fn capacity(&self) -> uint {
278         self.cap
279     }
280
281     /// Reserves capacity for at least `additional` more elements to be inserted in the given
282     /// `Vec<T>`. The collection may reserve more space to avoid frequent reallocations.
283     ///
284     /// # Panics
285     ///
286     /// Panics if the new capacity overflows `uint`.
287     ///
288     /// # Examples
289     ///
290     /// ```
291     /// let mut vec: Vec<int> = vec![1];
292     /// vec.reserve(10);
293     /// assert!(vec.capacity() >= 11);
294     /// ```
295     #[stable]
296     pub fn reserve(&mut self, additional: uint) {
297         if self.cap - self.len < additional {
298             let err_msg = "Vec::reserve: `uint` overflow";
299             let new_cap = self.len.checked_add(additional).expect(err_msg)
300                 .checked_next_power_of_two().expect(err_msg);
301             self.grow_capacity(new_cap);
302         }
303     }
304
305     /// Reserves the minimum capacity for exactly `additional` more elements to
306     /// be inserted in the given `Vec<T>`. Does nothing if the capacity is already
307     /// sufficient.
308     ///
309     /// Note that the allocator may give the collection more space than it
310     /// requests. Therefore capacity can not be relied upon to be precisely
311     /// minimal. Prefer `reserve` if future insertions are expected.
312     ///
313     /// # Panics
314     ///
315     /// Panics if the new capacity overflows `uint`.
316     ///
317     /// # Examples
318     ///
319     /// ```
320     /// let mut vec: Vec<int> = vec![1];
321     /// vec.reserve_exact(10);
322     /// assert!(vec.capacity() >= 11);
323     /// ```
324     #[stable]
325     pub fn reserve_exact(&mut self, additional: uint) {
326         if self.cap - self.len < additional {
327             match self.len.checked_add(additional) {
328                 None => panic!("Vec::reserve: `uint` overflow"),
329                 Some(new_cap) => self.grow_capacity(new_cap)
330             }
331         }
332     }
333
334     /// Shrinks the capacity of the vector as much as possible.
335     ///
336     /// It will drop down as close as possible to the length but the allocator
337     /// may still inform the vector that there is space for a few more elements.
338     ///
339     /// # Examples
340     ///
341     /// ```
342     /// let mut vec: Vec<int> = Vec::with_capacity(10);
343     /// vec.push_all(&[1, 2, 3]);
344     /// assert_eq!(vec.capacity(), 10);
345     /// vec.shrink_to_fit();
346     /// assert!(vec.capacity() >= 3);
347     /// ```
348     #[stable]
349     pub fn shrink_to_fit(&mut self) {
350         if mem::size_of::<T>() == 0 { return }
351
352         if self.len == 0 {
353             if self.cap != 0 {
354                 unsafe {
355                     dealloc(*self.ptr, self.cap)
356                 }
357                 self.cap = 0;
358             }
359         } else {
360             unsafe {
361                 // Overflow check is unnecessary as the vector is already at
362                 // least this large.
363                 let ptr = reallocate(*self.ptr as *mut u8,
364                                      self.cap * mem::size_of::<T>(),
365                                      self.len * mem::size_of::<T>(),
366                                      mem::min_align_of::<T>()) as *mut T;
367                 if ptr.is_null() { ::alloc::oom() }
368                 self.ptr = NonZero::new(ptr);
369             }
370             self.cap = self.len;
371         }
372     }
373
374     /// Convert the vector into Box<[T]>.
375     ///
376     /// Note that this will drop any excess capacity. Calling this and
377     /// converting back to a vector with `into_vec()` is equivalent to calling
378     /// `shrink_to_fit()`.
379     #[unstable]
380     pub fn into_boxed_slice(mut self) -> Box<[T]> {
381         self.shrink_to_fit();
382         unsafe {
383             let xs: Box<[T]> = mem::transmute(self.as_mut_slice());
384             mem::forget(self);
385             xs
386         }
387     }
388
389     /// Shorten a vector, dropping excess elements.
390     ///
391     /// If `len` is greater than the vector's current length, this has no
392     /// effect.
393     ///
394     /// # Examples
395     ///
396     /// ```
397     /// let mut vec = vec![1i, 2, 3, 4];
398     /// vec.truncate(2);
399     /// assert_eq!(vec, vec![1, 2]);
400     /// ```
401     #[stable]
402     pub fn truncate(&mut self, len: uint) {
403         unsafe {
404             // drop any extra elements
405             while len < self.len {
406                 // decrement len before the read(), so a panic on Drop doesn't
407                 // re-drop the just-failed value.
408                 self.len -= 1;
409                 ptr::read(self.get_unchecked(self.len));
410             }
411         }
412     }
413
414     /// Returns a mutable slice of the elements of `self`.
415     ///
416     /// # Examples
417     ///
418     /// ```
419     /// fn foo(slice: &mut [int]) {}
420     ///
421     /// let mut vec = vec![1i, 2];
422     /// foo(vec.as_mut_slice());
423     /// ```
424     #[inline]
425     #[stable]
426     pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T] {
427         unsafe {
428             mem::transmute(RawSlice {
429                 data: *self.ptr as *const T,
430                 len: self.len,
431             })
432         }
433     }
434
435     /// Creates a consuming iterator, that is, one that moves each value out of
436     /// the vector (from start to end). The vector cannot be used after calling
437     /// this.
438     ///
439     /// # Examples
440     ///
441     /// ```
442     /// let v = vec!["a".to_string(), "b".to_string()];
443     /// for s in v.into_iter() {
444     ///     // s has type String, not &String
445     ///     println!("{}", s);
446     /// }
447     /// ```
448     #[inline]
449     #[stable]
450     pub fn into_iter(self) -> IntoIter<T> {
451         unsafe {
452             let ptr = *self.ptr;
453             let cap = self.cap;
454             let begin = ptr as *const T;
455             let end = if mem::size_of::<T>() == 0 {
456                 (ptr as uint + self.len()) as *const T
457             } else {
458                 ptr.offset(self.len() as int) as *const T
459             };
460             mem::forget(self);
461             IntoIter { allocation: ptr, cap: cap, ptr: begin, end: end }
462         }
463     }
464
465     /// Sets the length of a vector.
466     ///
467     /// This will explicitly set the size of the vector, without actually
468     /// modifying its buffers, so it is up to the caller to ensure that the
469     /// vector is actually the specified size.
470     ///
471     /// # Examples
472     ///
473     /// ```
474     /// let mut v = vec![1u, 2, 3, 4];
475     /// unsafe {
476     ///     v.set_len(1);
477     /// }
478     /// ```
479     #[inline]
480     #[stable]
481     pub unsafe fn set_len(&mut self, len: uint) {
482         self.len = len;
483     }
484
485     /// Removes an element from anywhere in the vector and return it, replacing
486     /// it with the last element.
487     ///
488     /// This does not preserve ordering, but is O(1).
489     ///
490     /// # Panics
491     ///
492     /// Panics if `index` is out of bounds.
493     ///
494     /// # Examples
495     ///
496     /// ```
497     /// let mut v = vec!["foo", "bar", "baz", "qux"];
498     ///
499     /// assert_eq!(v.swap_remove(1), "bar");
500     /// assert_eq!(v, vec!["foo", "qux", "baz"]);
501     ///
502     /// assert_eq!(v.swap_remove(0), "foo");
503     /// assert_eq!(v, vec!["baz", "qux"]);
504     /// ```
505     #[inline]
506     #[stable]
507     pub fn swap_remove(&mut self, index: uint) -> T {
508         let length = self.len();
509         self.swap(index, length - 1);
510         self.pop().unwrap()
511     }
512
513     /// Inserts an element at position `index` within the vector, shifting all
514     /// elements after position `i` one position to the right.
515     ///
516     /// # Panics
517     ///
518     /// Panics if `index` is not between `0` and the vector's length (both
519     /// bounds inclusive).
520     ///
521     /// # Examples
522     ///
523     /// ```
524     /// let mut vec = vec![1i, 2, 3];
525     /// vec.insert(1, 4);
526     /// assert_eq!(vec, vec![1, 4, 2, 3]);
527     /// vec.insert(4, 5);
528     /// assert_eq!(vec, vec![1, 4, 2, 3, 5]);
529     /// ```
530     #[stable]
531     pub fn insert(&mut self, index: uint, element: T) {
532         let len = self.len();
533         assert!(index <= len);
534         // space for the new element
535         self.reserve(1);
536
537         unsafe { // infallible
538             // The spot to put the new value
539             {
540                 let p = self.as_mut_ptr().offset(index as int);
541                 // Shift everything over to make space. (Duplicating the
542                 // `index`th element into two consecutive places.)
543                 ptr::copy_memory(p.offset(1), &*p, len - index);
544                 // Write it in, overwriting the first copy of the `index`th
545                 // element.
546                 ptr::write(&mut *p, element);
547             }
548             self.set_len(len + 1);
549         }
550     }
551
552     /// Removes and returns the element at position `index` within the vector,
553     /// shifting all elements after position `index` one position to the left.
554     ///
555     /// # Panics
556     ///
557     /// Panics if `i` is out of bounds.
558     ///
559     /// # Examples
560     ///
561     /// ```
562     /// let mut v = vec![1i, 2, 3];
563     /// assert_eq!(v.remove(1), 2);
564     /// assert_eq!(v, vec![1, 3]);
565     /// ```
566     #[stable]
567     pub fn remove(&mut self, index: uint) -> T {
568         let len = self.len();
569         assert!(index < len);
570         unsafe { // infallible
571             let ret;
572             {
573                 // the place we are taking from.
574                 let ptr = self.as_mut_ptr().offset(index as int);
575                 // copy it out, unsafely having a copy of the value on
576                 // the stack and in the vector at the same time.
577                 ret = ptr::read(ptr as *const T);
578
579                 // Shift everything down to fill in that spot.
580                 ptr::copy_memory(ptr, &*ptr.offset(1), len - index - 1);
581             }
582             self.set_len(len - 1);
583             ret
584         }
585     }
586
587     /// Retains only the elements specified by the predicate.
588     ///
589     /// In other words, remove all elements `e` such that `f(&e)` returns false.
590     /// This method operates in place and preserves the order of the retained
591     /// elements.
592     ///
593     /// # Examples
594     ///
595     /// ```
596     /// let mut vec = vec![1i, 2, 3, 4];
597     /// vec.retain(|&x| x%2 == 0);
598     /// assert_eq!(vec, vec![2, 4]);
599     /// ```
600     #[stable]
601     pub fn retain<F>(&mut self, mut f: F) where F: FnMut(&T) -> bool {
602         let len = self.len();
603         let mut del = 0u;
604         {
605             let v = self.as_mut_slice();
606
607             for i in range(0u, len) {
608                 if !f(&v[i]) {
609                     del += 1;
610                 } else if del > 0 {
611                     v.swap(i-del, i);
612                 }
613             }
614         }
615         if del > 0 {
616             self.truncate(len - del);
617         }
618     }
619
620     /// Appends an element to the back of a collection.
621     ///
622     /// # Panics
623     ///
624     /// Panics if the number of elements in the vector overflows a `uint`.
625     ///
626     /// # Examples
627     ///
628     /// ```rust
629     /// let mut vec = vec!(1i, 2);
630     /// vec.push(3);
631     /// assert_eq!(vec, vec!(1, 2, 3));
632     /// ```
633     #[inline]
634     #[stable]
635     pub fn push(&mut self, value: T) {
636         if mem::size_of::<T>() == 0 {
637             // zero-size types consume no memory, so we can't rely on the
638             // address space running out
639             self.len = self.len.checked_add(1).expect("length overflow");
640             unsafe { mem::forget(value); }
641             return
642         }
643         if self.len == self.cap {
644             let old_size = self.cap * mem::size_of::<T>();
645             let size = max(old_size, 2 * mem::size_of::<T>()) * 2;
646             if old_size > size { panic!("capacity overflow") }
647             unsafe {
648                 let ptr = alloc_or_realloc(*self.ptr, old_size, size);
649                 if ptr.is_null() { ::alloc::oom() }
650                 self.ptr = NonZero::new(ptr);
651             }
652             self.cap = max(self.cap, 2) * 2;
653         }
654
655         unsafe {
656             let end = (*self.ptr).offset(self.len as int);
657             ptr::write(&mut *end, value);
658             self.len += 1;
659         }
660     }
661
662     /// Removes the last element from a vector and returns it, or `None` if it is empty.
663     ///
664     /// # Examples
665     ///
666     /// ```rust
667     /// let mut vec = vec![1i, 2, 3];
668     /// assert_eq!(vec.pop(), Some(3));
669     /// assert_eq!(vec, vec![1, 2]);
670     /// ```
671     #[inline]
672     #[stable]
673     pub fn pop(&mut self) -> Option<T> {
674         if self.len == 0 {
675             None
676         } else {
677             unsafe {
678                 self.len -= 1;
679                 Some(ptr::read(self.get_unchecked(self.len())))
680             }
681         }
682     }
683
684     /// Creates a draining iterator that clears the `Vec` and iterates over
685     /// the removed items from start to end.
686     ///
687     /// # Examples
688     ///
689     /// ```
690     /// let mut v = vec!["a".to_string(), "b".to_string()];
691     /// for s in v.drain() {
692     ///     // s has type String, not &String
693     ///     println!("{}", s);
694     /// }
695     /// assert!(v.is_empty());
696     /// ```
697     #[inline]
698     #[unstable = "matches collection reform specification, waiting for dust to settle"]
699     pub fn drain<'a>(&'a mut self) -> Drain<'a, T> {
700         unsafe {
701             let begin = *self.ptr as *const T;
702             let end = if mem::size_of::<T>() == 0 {
703                 (*self.ptr as uint + self.len()) as *const T
704             } else {
705                 (*self.ptr).offset(self.len() as int) as *const T
706             };
707             self.set_len(0);
708             Drain {
709                 ptr: begin,
710                 end: end,
711                 marker: ContravariantLifetime,
712             }
713         }
714     }
715
716     /// Clears the vector, removing all values.
717     ///
718     /// # Examples
719     ///
720     /// ```
721     /// let mut v = vec![1i, 2, 3];
722     ///
723     /// v.clear();
724     ///
725     /// assert!(v.is_empty());
726     /// ```
727     #[inline]
728     #[stable]
729     pub fn clear(&mut self) {
730         self.truncate(0)
731     }
732
733     /// Returns the number of elements in the vector.
734     ///
735     /// # Examples
736     ///
737     /// ```
738     /// let a = vec![1i, 2, 3];
739     /// assert_eq!(a.len(), 3);
740     /// ```
741     #[inline]
742     #[stable]
743     pub fn len(&self) -> uint { self.len }
744
745     /// Returns `true` if the vector contains no elements.
746     ///
747     /// # Examples
748     ///
749     /// ```
750     /// let mut v = Vec::new();
751     /// assert!(v.is_empty());
752     ///
753     /// v.push(1i);
754     /// assert!(!v.is_empty());
755     /// ```
756     #[stable]
757     pub fn is_empty(&self) -> bool { self.len() == 0 }
758
759     /// Converts a `Vec<T>` to a `Vec<U>` where `T` and `U` have the same
760     /// size and in case they are not zero-sized the same minimal alignment.
761     ///
762     /// # Panics
763     ///
764     /// Panics if `T` and `U` have differing sizes or are not zero-sized and
765     /// have differing minimal alignments.
766     ///
767     /// # Examples
768     ///
769     /// ```
770     /// let v = vec![0u, 1, 2];
771     /// let w = v.map_in_place(|i| i + 3);
772     /// assert_eq!(w.as_slice(), [3, 4, 5].as_slice());
773     ///
774     /// #[derive(PartialEq, Show)]
775     /// struct Newtype(u8);
776     /// let bytes = vec![0x11, 0x22];
777     /// let newtyped_bytes = bytes.map_in_place(|x| Newtype(x));
778     /// assert_eq!(newtyped_bytes.as_slice(), [Newtype(0x11), Newtype(0x22)].as_slice());
779     /// ```
780     #[unstable = "API may change to provide stronger guarantees"]
781     pub fn map_in_place<U, F>(self, mut f: F) -> Vec<U> where F: FnMut(T) -> U {
782         // FIXME: Assert statically that the types `T` and `U` have the same
783         // size.
784         assert!(mem::size_of::<T>() == mem::size_of::<U>());
785
786         let mut vec = self;
787
788         if mem::size_of::<T>() != 0 {
789             // FIXME: Assert statically that the types `T` and `U` have the
790             // same minimal alignment in case they are not zero-sized.
791
792             // These asserts are necessary because the `min_align_of` of the
793             // types are passed to the allocator by `Vec`.
794             assert!(mem::min_align_of::<T>() == mem::min_align_of::<U>());
795
796             // This `as int` cast is safe, because the size of the elements of the
797             // vector is not 0, and:
798             //
799             // 1) If the size of the elements in the vector is 1, the `int` may
800             //    overflow, but it has the correct bit pattern so that the
801             //    `.offset()` function will work.
802             //
803             //    Example:
804             //        Address space 0x0-0xF.
805             //        `u8` array at: 0x1.
806             //        Size of `u8` array: 0x8.
807             //        Calculated `offset`: -0x8.
808             //        After `array.offset(offset)`: 0x9.
809             //        (0x1 + 0x8 = 0x1 - 0x8)
810             //
811             // 2) If the size of the elements in the vector is >1, the `uint` ->
812             //    `int` conversion can't overflow.
813             let offset = vec.len() as int;
814             let start = vec.as_mut_ptr();
815
816             let mut pv = PartialVecNonZeroSized {
817                 vec: vec,
818
819                 start_t: start,
820                 // This points inside the vector, as the vector has length
821                 // `offset`.
822                 end_t: unsafe { start.offset(offset) },
823                 start_u: start as *mut U,
824                 end_u: start as *mut U,
825             };
826             //  start_t
827             //  start_u
828             //  |
829             // +-+-+-+-+-+-+
830             // |T|T|T|...|T|
831             // +-+-+-+-+-+-+
832             //  |           |
833             //  end_u       end_t
834
835             while pv.end_u as *mut T != pv.end_t {
836                 unsafe {
837                     //  start_u start_t
838                     //  |       |
839                     // +-+-+-+-+-+-+-+-+-+
840                     // |U|...|U|T|T|...|T|
841                     // +-+-+-+-+-+-+-+-+-+
842                     //          |         |
843                     //          end_u     end_t
844
845                     let t = ptr::read(pv.start_t as *const T);
846                     //  start_u start_t
847                     //  |       |
848                     // +-+-+-+-+-+-+-+-+-+
849                     // |U|...|U|X|T|...|T|
850                     // +-+-+-+-+-+-+-+-+-+
851                     //          |         |
852                     //          end_u     end_t
853                     // We must not panic here, one cell is marked as `T`
854                     // although it is not `T`.
855
856                     pv.start_t = pv.start_t.offset(1);
857                     //  start_u   start_t
858                     //  |         |
859                     // +-+-+-+-+-+-+-+-+-+
860                     // |U|...|U|X|T|...|T|
861                     // +-+-+-+-+-+-+-+-+-+
862                     //          |         |
863                     //          end_u     end_t
864                     // We may panic again.
865
866                     // The function given by the user might panic.
867                     let u = f(t);
868
869                     ptr::write(pv.end_u, u);
870                     //  start_u   start_t
871                     //  |         |
872                     // +-+-+-+-+-+-+-+-+-+
873                     // |U|...|U|U|T|...|T|
874                     // +-+-+-+-+-+-+-+-+-+
875                     //          |         |
876                     //          end_u     end_t
877                     // We should not panic here, because that would leak the `U`
878                     // pointed to by `end_u`.
879
880                     pv.end_u = pv.end_u.offset(1);
881                     //  start_u   start_t
882                     //  |         |
883                     // +-+-+-+-+-+-+-+-+-+
884                     // |U|...|U|U|T|...|T|
885                     // +-+-+-+-+-+-+-+-+-+
886                     //            |       |
887                     //            end_u   end_t
888                     // We may panic again.
889                 }
890             }
891
892             //  start_u     start_t
893             //  |           |
894             // +-+-+-+-+-+-+
895             // |U|...|U|U|U|
896             // +-+-+-+-+-+-+
897             //              |
898             //              end_t
899             //              end_u
900             // Extract `vec` and prevent the destructor of
901             // `PartialVecNonZeroSized` from running. Note that none of the
902             // function calls can panic, thus no resources can be leaked (as the
903             // `vec` member of `PartialVec` is the only one which holds
904             // allocations -- and it is returned from this function. None of
905             // this can panic.
906             unsafe {
907                 let vec_len = pv.vec.len();
908                 let vec_cap = pv.vec.capacity();
909                 let vec_ptr = pv.vec.as_mut_ptr() as *mut U;
910                 mem::forget(pv);
911                 Vec::from_raw_parts(vec_ptr, vec_len, vec_cap)
912             }
913         } else {
914             // Put the `Vec` into the `PartialVecZeroSized` structure and
915             // prevent the destructor of the `Vec` from running. Since the
916             // `Vec` contained zero-sized objects, it did not allocate, so we
917             // are not leaking memory here.
918             let mut pv = PartialVecZeroSized::<T,U> {
919                 num_t: vec.len(),
920                 num_u: 0,
921                 marker_t: InvariantType,
922                 marker_u: InvariantType,
923             };
924             unsafe { mem::forget(vec); }
925
926             while pv.num_t != 0 {
927                 unsafe {
928                     // Create a `T` out of thin air and decrement `num_t`. This
929                     // must not panic between these steps, as otherwise a
930                     // destructor of `T` which doesn't exist runs.
931                     let t = mem::uninitialized();
932                     pv.num_t -= 1;
933
934                     // The function given by the user might panic.
935                     let u = f(t);
936
937                     // Forget the `U` and increment `num_u`. This increment
938                     // cannot overflow the `uint` as we only do this for a
939                     // number of times that fits into a `uint` (and start with
940                     // `0`). Again, we should not panic between these steps.
941                     mem::forget(u);
942                     pv.num_u += 1;
943                 }
944             }
945             // Create a `Vec` from our `PartialVecZeroSized` and make sure the
946             // destructor of the latter will not run. None of this can panic.
947             let mut result = Vec::new();
948             unsafe {
949                 result.set_len(pv.num_u);
950                 mem::forget(pv);
951             }
952             result
953         }
954     }
955 }
956
957 impl<T: Clone> Vec<T> {
958     /// Resizes the `Vec` in-place so that `len()` is equal to `new_len`.
959     ///
960     /// Calls either `extend()` or `truncate()` depending on whether `new_len`
961     /// is larger than the current value of `len()` or not.
962     ///
963     /// # Examples
964     ///
965     /// ```
966     /// let mut vec = vec!["hello"];
967     /// vec.resize(3, "world");
968     /// assert_eq!(vec, vec!["hello", "world", "world"]);
969     ///
970     /// let mut vec = vec![1i, 2, 3, 4];
971     /// vec.resize(2, 0);
972     /// assert_eq!(vec, vec![1, 2]);
973     /// ```
974     #[unstable = "matches collection reform specification; waiting for dust to settle"]
975     pub fn resize(&mut self, new_len: uint, value: T) {
976         let len = self.len();
977
978         if new_len > len {
979             self.extend(repeat(value).take(new_len - len));
980         } else {
981             self.truncate(new_len);
982         }
983     }
984
985     /// Appends all elements in a slice to the `Vec`.
986     ///
987     /// Iterates over the slice `other`, clones each element, and then appends
988     /// it to this `Vec`. The `other` vector is traversed in-order.
989     ///
990     /// # Examples
991     ///
992     /// ```
993     /// let mut vec = vec![1i];
994     /// vec.push_all(&[2i, 3, 4]);
995     /// assert_eq!(vec, vec![1, 2, 3, 4]);
996     /// ```
997     #[inline]
998     #[unstable = "likely to be replaced by a more optimized extend"]
999     pub fn push_all(&mut self, other: &[T]) {
1000         self.reserve(other.len());
1001
1002         for i in range(0, other.len()) {
1003             let len = self.len();
1004
1005             // Unsafe code so this can be optimised to a memcpy (or something similarly
1006             // fast) when T is Copy. LLVM is easily confused, so any extra operations
1007             // during the loop can prevent this optimisation.
1008             unsafe {
1009                 ptr::write(
1010                     self.get_unchecked_mut(len),
1011                     other.get_unchecked(i).clone());
1012                 self.set_len(len + 1);
1013             }
1014         }
1015     }
1016 }
1017
1018 impl<T: PartialEq> Vec<T> {
1019     /// Removes consecutive repeated elements in the vector.
1020     ///
1021     /// If the vector is sorted, this removes all duplicates.
1022     ///
1023     /// # Examples
1024     ///
1025     /// ```
1026     /// let mut vec = vec![1i, 2, 2, 3, 2];
1027     ///
1028     /// vec.dedup();
1029     ///
1030     /// assert_eq!(vec, vec![1i, 2, 3, 2]);
1031     /// ```
1032     #[stable]
1033     pub fn dedup(&mut self) {
1034         unsafe {
1035             // Although we have a mutable reference to `self`, we cannot make
1036             // *arbitrary* changes. The `PartialEq` comparisons could panic, so we
1037             // must ensure that the vector is in a valid state at all time.
1038             //
1039             // The way that we handle this is by using swaps; we iterate
1040             // over all the elements, swapping as we go so that at the end
1041             // the elements we wish to keep are in the front, and those we
1042             // wish to reject are at the back. We can then truncate the
1043             // vector. This operation is still O(n).
1044             //
1045             // Example: We start in this state, where `r` represents "next
1046             // read" and `w` represents "next_write`.
1047             //
1048             //           r
1049             //     +---+---+---+---+---+---+
1050             //     | 0 | 1 | 1 | 2 | 3 | 3 |
1051             //     +---+---+---+---+---+---+
1052             //           w
1053             //
1054             // Comparing self[r] against self[w-1], this is not a duplicate, so
1055             // we swap self[r] and self[w] (no effect as r==w) and then increment both
1056             // r and w, leaving us with:
1057             //
1058             //               r
1059             //     +---+---+---+---+---+---+
1060             //     | 0 | 1 | 1 | 2 | 3 | 3 |
1061             //     +---+---+---+---+---+---+
1062             //               w
1063             //
1064             // Comparing self[r] against self[w-1], this value is a duplicate,
1065             // so we increment `r` but leave everything else unchanged:
1066             //
1067             //                   r
1068             //     +---+---+---+---+---+---+
1069             //     | 0 | 1 | 1 | 2 | 3 | 3 |
1070             //     +---+---+---+---+---+---+
1071             //               w
1072             //
1073             // Comparing self[r] against self[w-1], this is not a duplicate,
1074             // so swap self[r] and self[w] and advance r and w:
1075             //
1076             //                       r
1077             //     +---+---+---+---+---+---+
1078             //     | 0 | 1 | 2 | 1 | 3 | 3 |
1079             //     +---+---+---+---+---+---+
1080             //                   w
1081             //
1082             // Not a duplicate, repeat:
1083             //
1084             //                           r
1085             //     +---+---+---+---+---+---+
1086             //     | 0 | 1 | 2 | 3 | 1 | 3 |
1087             //     +---+---+---+---+---+---+
1088             //                       w
1089             //
1090             // Duplicate, advance r. End of vec. Truncate to w.
1091
1092             let ln = self.len();
1093             if ln < 1 { return; }
1094
1095             // Avoid bounds checks by using unsafe pointers.
1096             let p = self.as_mut_ptr();
1097             let mut r = 1;
1098             let mut w = 1;
1099
1100             while r < ln {
1101                 let p_r = p.offset(r as int);
1102                 let p_wm1 = p.offset((w - 1) as int);
1103                 if *p_r != *p_wm1 {
1104                     if r != w {
1105                         let p_w = p_wm1.offset(1);
1106                         mem::swap(&mut *p_r, &mut *p_w);
1107                     }
1108                     w += 1;
1109                 }
1110                 r += 1;
1111             }
1112
1113             self.truncate(w);
1114         }
1115     }
1116 }
1117
1118 ////////////////////////////////////////////////////////////////////////////////
1119 // Internal methods and functions
1120 ////////////////////////////////////////////////////////////////////////////////
1121
1122 impl<T> Vec<T> {
1123     /// Reserves capacity for exactly `capacity` elements in the given vector.
1124     ///
1125     /// If the capacity for `self` is already equal to or greater than the
1126     /// requested capacity, then no action is taken.
1127     fn grow_capacity(&mut self, capacity: uint) {
1128         if mem::size_of::<T>() == 0 { return }
1129
1130         if capacity > self.cap {
1131             let size = capacity.checked_mul(mem::size_of::<T>())
1132                                .expect("capacity overflow");
1133             unsafe {
1134                 let ptr = alloc_or_realloc(*self.ptr, self.cap * mem::size_of::<T>(), size);
1135                 if ptr.is_null() { ::alloc::oom() }
1136                 self.ptr = NonZero::new(ptr);
1137             }
1138             self.cap = capacity;
1139         }
1140     }
1141 }
1142
1143 // FIXME: #13996: need a way to mark the return value as `noalias`
1144 #[inline(never)]
1145 unsafe fn alloc_or_realloc<T>(ptr: *mut T, old_size: uint, size: uint) -> *mut T {
1146     if old_size == 0 {
1147         allocate(size, mem::min_align_of::<T>()) as *mut T
1148     } else {
1149         reallocate(ptr as *mut u8, old_size, size, mem::min_align_of::<T>()) as *mut T
1150     }
1151 }
1152
1153 #[inline]
1154 unsafe fn dealloc<T>(ptr: *mut T, len: uint) {
1155     if mem::size_of::<T>() != 0 {
1156         deallocate(ptr as *mut u8,
1157                    len * mem::size_of::<T>(),
1158                    mem::min_align_of::<T>())
1159     }
1160 }
1161
1162 ////////////////////////////////////////////////////////////////////////////////
1163 // Common trait implementations for Vec
1164 ////////////////////////////////////////////////////////////////////////////////
1165
1166 #[unstable]
1167 impl<T:Clone> Clone for Vec<T> {
1168     fn clone(&self) -> Vec<T> { ::slice::SliceExt::to_vec(self.as_slice()) }
1169
1170     fn clone_from(&mut self, other: &Vec<T>) {
1171         // drop anything in self that will not be overwritten
1172         if self.len() > other.len() {
1173             self.truncate(other.len())
1174         }
1175
1176         // reuse the contained values' allocations/resources.
1177         for (place, thing) in self.iter_mut().zip(other.iter()) {
1178             place.clone_from(thing)
1179         }
1180
1181         // self.len <= other.len due to the truncate above, so the
1182         // slice here is always in-bounds.
1183         let slice = &other[self.len()..];
1184         self.push_all(slice);
1185     }
1186 }
1187
1188 #[cfg(stage0)]
1189 impl<S: hash::Writer, T: Hash<S>> Hash<S> for Vec<T> {
1190     #[inline]
1191     fn hash(&self, state: &mut S) {
1192         self.as_slice().hash(state);
1193     }
1194 }
1195 #[cfg(not(stage0))]
1196 impl<S: hash::Writer + hash::Hasher, T: Hash<S>> Hash<S> for Vec<T> {
1197     #[inline]
1198     fn hash(&self, state: &mut S) {
1199         self.as_slice().hash(state);
1200     }
1201 }
1202
1203 #[unstable = "waiting on Index stability"]
1204 impl<T> Index<uint> for Vec<T> {
1205     type Output = T;
1206
1207     #[inline]
1208     fn index<'a>(&'a self, index: &uint) -> &'a T {
1209         &self.as_slice()[*index]
1210     }
1211 }
1212
1213 impl<T> IndexMut<uint> for Vec<T> {
1214     type Output = T;
1215
1216     #[inline]
1217     fn index_mut<'a>(&'a mut self, index: &uint) -> &'a mut T {
1218         &mut self.as_mut_slice()[*index]
1219     }
1220 }
1221
1222
1223 impl<T> ops::Index<ops::Range<uint>> for Vec<T> {
1224     type Output = [T];
1225     #[inline]
1226     fn index(&self, index: &ops::Range<uint>) -> &[T] {
1227         self.as_slice().index(index)
1228     }
1229 }
1230 impl<T> ops::Index<ops::RangeTo<uint>> for Vec<T> {
1231     type Output = [T];
1232     #[inline]
1233     fn index(&self, index: &ops::RangeTo<uint>) -> &[T] {
1234         self.as_slice().index(index)
1235     }
1236 }
1237 impl<T> ops::Index<ops::RangeFrom<uint>> for Vec<T> {
1238     type Output = [T];
1239     #[inline]
1240     fn index(&self, index: &ops::RangeFrom<uint>) -> &[T] {
1241         self.as_slice().index(index)
1242     }
1243 }
1244 impl<T> ops::Index<ops::FullRange> for Vec<T> {
1245     type Output = [T];
1246     #[inline]
1247     fn index(&self, _index: &ops::FullRange) -> &[T] {
1248         self.as_slice()
1249     }
1250 }
1251
1252 impl<T> ops::IndexMut<ops::Range<uint>> for Vec<T> {
1253     type Output = [T];
1254     #[inline]
1255     fn index_mut(&mut self, index: &ops::Range<uint>) -> &mut [T] {
1256         self.as_mut_slice().index_mut(index)
1257     }
1258 }
1259 impl<T> ops::IndexMut<ops::RangeTo<uint>> for Vec<T> {
1260     type Output = [T];
1261     #[inline]
1262     fn index_mut(&mut self, index: &ops::RangeTo<uint>) -> &mut [T] {
1263         self.as_mut_slice().index_mut(index)
1264     }
1265 }
1266 impl<T> ops::IndexMut<ops::RangeFrom<uint>> for Vec<T> {
1267     type Output = [T];
1268     #[inline]
1269     fn index_mut(&mut self, index: &ops::RangeFrom<uint>) -> &mut [T] {
1270         self.as_mut_slice().index_mut(index)
1271     }
1272 }
1273 impl<T> ops::IndexMut<ops::FullRange> for Vec<T> {
1274     type Output = [T];
1275     #[inline]
1276     fn index_mut(&mut self, _index: &ops::FullRange) -> &mut [T] {
1277         self.as_mut_slice()
1278     }
1279 }
1280
1281
1282 #[stable]
1283 impl<T> ops::Deref for Vec<T> {
1284     type Target = [T];
1285
1286     fn deref<'a>(&'a self) -> &'a [T] { self.as_slice() }
1287 }
1288
1289 #[stable]
1290 impl<T> ops::DerefMut for Vec<T> {
1291     fn deref_mut<'a>(&'a mut self) -> &'a mut [T] { self.as_mut_slice() }
1292 }
1293
1294 #[stable]
1295 impl<T> FromIterator<T> for Vec<T> {
1296     #[inline]
1297     fn from_iter<I:Iterator<Item=T>>(mut iterator: I) -> Vec<T> {
1298         let (lower, _) = iterator.size_hint();
1299         let mut vector = Vec::with_capacity(lower);
1300         for element in iterator {
1301             vector.push(element)
1302         }
1303         vector
1304     }
1305 }
1306
1307 #[unstable = "waiting on Extend stability"]
1308 impl<T> Extend<T> for Vec<T> {
1309     #[inline]
1310     fn extend<I: Iterator<Item=T>>(&mut self, mut iterator: I) {
1311         let (lower, _) = iterator.size_hint();
1312         self.reserve(lower);
1313         for element in iterator {
1314             self.push(element)
1315         }
1316     }
1317 }
1318
1319 impl<A, B> PartialEq<Vec<B>> for Vec<A> where A: PartialEq<B> {
1320     #[inline]
1321     fn eq(&self, other: &Vec<B>) -> bool { PartialEq::eq(&**self, &**other) }
1322     #[inline]
1323     fn ne(&self, other: &Vec<B>) -> bool { PartialEq::ne(&**self, &**other) }
1324 }
1325
1326 macro_rules! impl_eq {
1327     ($lhs:ty, $rhs:ty) => {
1328         impl<'b, A, B> PartialEq<$rhs> for $lhs where A: PartialEq<B> {
1329             #[inline]
1330             fn eq(&self, other: &$rhs) -> bool { PartialEq::eq(&**self, &**other) }
1331             #[inline]
1332             fn ne(&self, other: &$rhs) -> bool { PartialEq::ne(&**self, &**other) }
1333         }
1334
1335         impl<'b, A, B> PartialEq<$lhs> for $rhs where B: PartialEq<A> {
1336             #[inline]
1337             fn eq(&self, other: &$lhs) -> bool { PartialEq::eq(&**self, &**other) }
1338             #[inline]
1339             fn ne(&self, other: &$lhs) -> bool { PartialEq::ne(&**self, &**other) }
1340         }
1341     }
1342 }
1343
1344 impl_eq! { Vec<A>, &'b [B] }
1345 impl_eq! { Vec<A>, &'b mut [B] }
1346
1347 impl<'a, A, B> PartialEq<Vec<B>> for CowVec<'a, A> where A: PartialEq<B> + Clone {
1348     #[inline]
1349     fn eq(&self, other: &Vec<B>) -> bool { PartialEq::eq(&**self, &**other) }
1350     #[inline]
1351     fn ne(&self, other: &Vec<B>) -> bool { PartialEq::ne(&**self, &**other) }
1352 }
1353
1354 impl<'a, A, B> PartialEq<CowVec<'a, A>> for Vec<B> where A: Clone, B: PartialEq<A> {
1355     #[inline]
1356     fn eq(&self, other: &CowVec<'a, A>) -> bool { PartialEq::eq(&**self, &**other) }
1357     #[inline]
1358     fn ne(&self, other: &CowVec<'a, A>) -> bool { PartialEq::ne(&**self, &**other) }
1359 }
1360
1361 macro_rules! impl_eq_for_cowvec {
1362     ($rhs:ty) => {
1363         impl<'a, 'b, A, B> PartialEq<$rhs> for CowVec<'a, A> where A: PartialEq<B> + Clone {
1364             #[inline]
1365             fn eq(&self, other: &$rhs) -> bool { PartialEq::eq(&**self, &**other) }
1366             #[inline]
1367             fn ne(&self, other: &$rhs) -> bool { PartialEq::ne(&**self, &**other) }
1368         }
1369
1370         impl<'a, 'b, A, B> PartialEq<CowVec<'a, A>> for $rhs where A: Clone, B: PartialEq<A> {
1371             #[inline]
1372             fn eq(&self, other: &CowVec<'a, A>) -> bool { PartialEq::eq(&**self, &**other) }
1373             #[inline]
1374             fn ne(&self, other: &CowVec<'a, A>) -> bool { PartialEq::ne(&**self, &**other) }
1375         }
1376     }
1377 }
1378
1379 impl_eq_for_cowvec! { &'b [B] }
1380 impl_eq_for_cowvec! { &'b mut [B] }
1381
1382 #[unstable = "waiting on PartialOrd stability"]
1383 impl<T: PartialOrd> PartialOrd for Vec<T> {
1384     #[inline]
1385     fn partial_cmp(&self, other: &Vec<T>) -> Option<Ordering> {
1386         self.as_slice().partial_cmp(other.as_slice())
1387     }
1388 }
1389
1390 #[unstable = "waiting on Eq stability"]
1391 impl<T: Eq> Eq for Vec<T> {}
1392
1393 #[unstable = "waiting on Ord stability"]
1394 impl<T: Ord> Ord for Vec<T> {
1395     #[inline]
1396     fn cmp(&self, other: &Vec<T>) -> Ordering {
1397         self.as_slice().cmp(other.as_slice())
1398     }
1399 }
1400
1401 impl<T> AsSlice<T> for Vec<T> {
1402     /// Returns a slice into `self`.
1403     ///
1404     /// # Examples
1405     ///
1406     /// ```
1407     /// fn foo(slice: &[int]) {}
1408     ///
1409     /// let vec = vec![1i, 2];
1410     /// foo(vec.as_slice());
1411     /// ```
1412     #[inline]
1413     #[stable]
1414     fn as_slice<'a>(&'a self) -> &'a [T] {
1415         unsafe {
1416             mem::transmute(RawSlice {
1417                 data: *self.ptr as *const T,
1418                 len: self.len
1419             })
1420         }
1421     }
1422 }
1423
1424 #[unstable = "recent addition, needs more experience"]
1425 impl<'a, T: Clone> Add<&'a [T]> for Vec<T> {
1426     type Output = Vec<T>;
1427
1428     #[inline]
1429     fn add(mut self, rhs: &[T]) -> Vec<T> {
1430         self.push_all(rhs);
1431         self
1432     }
1433 }
1434
1435 #[unsafe_destructor]
1436 #[stable]
1437 impl<T> Drop for Vec<T> {
1438     fn drop(&mut self) {
1439         // This is (and should always remain) a no-op if the fields are
1440         // zeroed (when moving out, because of #[unsafe_no_drop_flag]).
1441         if self.cap != 0 {
1442             unsafe {
1443                 for x in self.iter() {
1444                     ptr::read(x);
1445                 }
1446                 dealloc(*self.ptr, self.cap)
1447             }
1448         }
1449     }
1450 }
1451
1452 #[stable]
1453 impl<T> Default for Vec<T> {
1454     #[stable]
1455     fn default() -> Vec<T> {
1456         Vec::new()
1457     }
1458 }
1459
1460 #[unstable = "waiting on Show stability"]
1461 impl<T: fmt::Show> fmt::Show for Vec<T> {
1462     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1463         fmt::Show::fmt(self.as_slice(), f)
1464     }
1465 }
1466
1467 impl<'a> fmt::Writer for Vec<u8> {
1468     fn write_str(&mut self, s: &str) -> fmt::Result {
1469         self.push_all(s.as_bytes());
1470         Ok(())
1471     }
1472 }
1473
1474 ////////////////////////////////////////////////////////////////////////////////
1475 // Clone-on-write
1476 ////////////////////////////////////////////////////////////////////////////////
1477
1478 #[unstable = "unclear how valuable this alias is"]
1479 /// A clone-on-write vector
1480 pub type CowVec<'a, T> = Cow<'a, Vec<T>, [T]>;
1481
1482 #[unstable]
1483 impl<'a, T> FromIterator<T> for CowVec<'a, T> where T: Clone {
1484     fn from_iter<I: Iterator<Item=T>>(it: I) -> CowVec<'a, T> {
1485         Cow::Owned(FromIterator::from_iter(it))
1486     }
1487 }
1488
1489 impl<'a, T: 'a> IntoCow<'a, Vec<T>, [T]> for Vec<T> where T: Clone {
1490     fn into_cow(self) -> CowVec<'a, T> {
1491         Cow::Owned(self)
1492     }
1493 }
1494
1495 impl<'a, T> IntoCow<'a, Vec<T>, [T]> for &'a [T] where T: Clone {
1496     fn into_cow(self) -> CowVec<'a, T> {
1497         Cow::Borrowed(self)
1498     }
1499 }
1500
1501 ////////////////////////////////////////////////////////////////////////////////
1502 // Iterators
1503 ////////////////////////////////////////////////////////////////////////////////
1504
1505 /// An iterator that moves out of a vector.
1506 #[stable]
1507 pub struct IntoIter<T> {
1508     allocation: *mut T, // the block of memory allocated for the vector
1509     cap: uint, // the capacity of the vector
1510     ptr: *const T,
1511     end: *const T
1512 }
1513
1514 unsafe impl<T: Send> Send for IntoIter<T> { }
1515 unsafe impl<T: Sync> Sync for IntoIter<T> { }
1516
1517 impl<T> IntoIter<T> {
1518     #[inline]
1519     /// Drops all items that have not yet been moved and returns the empty vector.
1520     #[unstable]
1521     pub fn into_inner(mut self) -> Vec<T> {
1522         unsafe {
1523             for _x in self { }
1524             let IntoIter { allocation, cap, ptr: _ptr, end: _end } = self;
1525             mem::forget(self);
1526             Vec { ptr: NonZero::new(allocation), cap: cap, len: 0 }
1527         }
1528     }
1529 }
1530
1531 #[stable]
1532 impl<T> Iterator for IntoIter<T> {
1533     type Item = T;
1534
1535     #[inline]
1536     fn next<'a>(&'a mut self) -> Option<T> {
1537         unsafe {
1538             if self.ptr == self.end {
1539                 None
1540             } else {
1541                 if mem::size_of::<T>() == 0 {
1542                     // purposefully don't use 'ptr.offset' because for
1543                     // vectors with 0-size elements this would return the
1544                     // same pointer.
1545                     self.ptr = mem::transmute(self.ptr as uint + 1);
1546
1547                     // Use a non-null pointer value
1548                     Some(ptr::read(mem::transmute(1u)))
1549                 } else {
1550                     let old = self.ptr;
1551                     self.ptr = self.ptr.offset(1);
1552
1553                     Some(ptr::read(old))
1554                 }
1555             }
1556         }
1557     }
1558
1559     #[inline]
1560     fn size_hint(&self) -> (uint, Option<uint>) {
1561         let diff = (self.end as uint) - (self.ptr as uint);
1562         let size = mem::size_of::<T>();
1563         let exact = diff / (if size == 0 {1} else {size});
1564         (exact, Some(exact))
1565     }
1566 }
1567
1568 #[stable]
1569 impl<T> DoubleEndedIterator for IntoIter<T> {
1570     #[inline]
1571     fn next_back<'a>(&'a mut self) -> Option<T> {
1572         unsafe {
1573             if self.end == self.ptr {
1574                 None
1575             } else {
1576                 if mem::size_of::<T>() == 0 {
1577                     // See above for why 'ptr.offset' isn't used
1578                     self.end = mem::transmute(self.end as uint - 1);
1579
1580                     // Use a non-null pointer value
1581                     Some(ptr::read(mem::transmute(1u)))
1582                 } else {
1583                     self.end = self.end.offset(-1);
1584
1585                     Some(ptr::read(mem::transmute(self.end)))
1586                 }
1587             }
1588         }
1589     }
1590 }
1591
1592 #[stable]
1593 impl<T> ExactSizeIterator for IntoIter<T> {}
1594
1595 #[unsafe_destructor]
1596 #[stable]
1597 impl<T> Drop for IntoIter<T> {
1598     fn drop(&mut self) {
1599         // destroy the remaining elements
1600         if self.cap != 0 {
1601             for _x in *self {}
1602             unsafe {
1603                 dealloc(self.allocation, self.cap);
1604             }
1605         }
1606     }
1607 }
1608
1609 /// An iterator that drains a vector.
1610 #[unsafe_no_drop_flag]
1611 #[unstable = "recently added as part of collections reform 2"]
1612 pub struct Drain<'a, T> {
1613     ptr: *const T,
1614     end: *const T,
1615     marker: ContravariantLifetime<'a>,
1616 }
1617
1618 #[stable]
1619 impl<'a, T> Iterator for Drain<'a, T> {
1620     type Item = T;
1621
1622     #[inline]
1623     fn next(&mut self) -> Option<T> {
1624         unsafe {
1625             if self.ptr == self.end {
1626                 None
1627             } else {
1628                 if mem::size_of::<T>() == 0 {
1629                     // purposefully don't use 'ptr.offset' because for
1630                     // vectors with 0-size elements this would return the
1631                     // same pointer.
1632                     self.ptr = mem::transmute(self.ptr as uint + 1);
1633
1634                     // Use a non-null pointer value
1635                     Some(ptr::read(mem::transmute(1u)))
1636                 } else {
1637                     let old = self.ptr;
1638                     self.ptr = self.ptr.offset(1);
1639
1640                     Some(ptr::read(old))
1641                 }
1642             }
1643         }
1644     }
1645
1646     #[inline]
1647     fn size_hint(&self) -> (uint, Option<uint>) {
1648         let diff = (self.end as uint) - (self.ptr as uint);
1649         let size = mem::size_of::<T>();
1650         let exact = diff / (if size == 0 {1} else {size});
1651         (exact, Some(exact))
1652     }
1653 }
1654
1655 #[stable]
1656 impl<'a, T> DoubleEndedIterator for Drain<'a, T> {
1657     #[inline]
1658     fn next_back(&mut self) -> Option<T> {
1659         unsafe {
1660             if self.end == self.ptr {
1661                 None
1662             } else {
1663                 if mem::size_of::<T>() == 0 {
1664                     // See above for why 'ptr.offset' isn't used
1665                     self.end = mem::transmute(self.end as uint - 1);
1666
1667                     // Use a non-null pointer value
1668                     Some(ptr::read(mem::transmute(1u)))
1669                 } else {
1670                     self.end = self.end.offset(-1);
1671
1672                     Some(ptr::read(self.end))
1673                 }
1674             }
1675         }
1676     }
1677 }
1678
1679 #[stable]
1680 impl<'a, T> ExactSizeIterator for Drain<'a, T> {}
1681
1682 #[unsafe_destructor]
1683 #[stable]
1684 impl<'a, T> Drop for Drain<'a, T> {
1685     fn drop(&mut self) {
1686         // self.ptr == self.end == null if drop has already been called,
1687         // so we can use #[unsafe_no_drop_flag].
1688
1689         // destroy the remaining elements
1690         for _x in *self {}
1691     }
1692 }
1693
1694 ////////////////////////////////////////////////////////////////////////////////
1695 // Conversion from &[T] to &Vec<T>
1696 ////////////////////////////////////////////////////////////////////////////////
1697
1698 /// Wrapper type providing a `&Vec<T>` reference via `Deref`.
1699 #[unstable]
1700 pub struct DerefVec<'a, T> {
1701     x: Vec<T>,
1702     l: ContravariantLifetime<'a>
1703 }
1704
1705 #[unstable]
1706 impl<'a, T> Deref for DerefVec<'a, T> {
1707     type Target = Vec<T>;
1708
1709     fn deref<'b>(&'b self) -> &'b Vec<T> {
1710         &self.x
1711     }
1712 }
1713
1714 // Prevent the inner `Vec<T>` from attempting to deallocate memory.
1715 #[unsafe_destructor]
1716 #[stable]
1717 impl<'a, T> Drop for DerefVec<'a, T> {
1718     fn drop(&mut self) {
1719         self.x.len = 0;
1720         self.x.cap = 0;
1721     }
1722 }
1723
1724 /// Convert a slice to a wrapper type providing a `&Vec<T>` reference.
1725 #[unstable]
1726 pub fn as_vec<'a, T>(x: &'a [T]) -> DerefVec<'a, T> {
1727     unsafe {
1728         DerefVec {
1729             x: Vec::from_raw_parts(x.as_ptr() as *mut T, x.len(), x.len()),
1730             l: ContravariantLifetime::<'a>
1731         }
1732     }
1733 }
1734
1735 ////////////////////////////////////////////////////////////////////////////////
1736 // Partial vec, used for map_in_place
1737 ////////////////////////////////////////////////////////////////////////////////
1738
1739 /// An owned, partially type-converted vector of elements with non-zero size.
1740 ///
1741 /// `T` and `U` must have the same, non-zero size. They must also have the same
1742 /// alignment.
1743 ///
1744 /// When the destructor of this struct runs, all `U`s from `start_u` (incl.) to
1745 /// `end_u` (excl.) and all `T`s from `start_t` (incl.) to `end_t` (excl.) are
1746 /// destructed. Additionally the underlying storage of `vec` will be freed.
1747 struct PartialVecNonZeroSized<T,U> {
1748     vec: Vec<T>,
1749
1750     start_u: *mut U,
1751     end_u: *mut U,
1752     start_t: *mut T,
1753     end_t: *mut T,
1754 }
1755
1756 /// An owned, partially type-converted vector of zero-sized elements.
1757 ///
1758 /// When the destructor of this struct runs, all `num_t` `T`s and `num_u` `U`s
1759 /// are destructed.
1760 struct PartialVecZeroSized<T,U> {
1761     num_t: uint,
1762     num_u: uint,
1763     marker_t: InvariantType<T>,
1764     marker_u: InvariantType<U>,
1765 }
1766
1767 #[unsafe_destructor]
1768 impl<T,U> Drop for PartialVecNonZeroSized<T,U> {
1769     fn drop(&mut self) {
1770         unsafe {
1771             // `vec` hasn't been modified until now. As it has a length
1772             // currently, this would run destructors of `T`s which might not be
1773             // there. So at first, set `vec`s length to `0`. This must be done
1774             // at first to remain memory-safe as the destructors of `U` or `T`
1775             // might cause unwinding where `vec`s destructor would be executed.
1776             self.vec.set_len(0);
1777
1778             // We have instances of `U`s and `T`s in `vec`. Destruct them.
1779             while self.start_u != self.end_u {
1780                 let _ = ptr::read(self.start_u as *const U); // Run a `U` destructor.
1781                 self.start_u = self.start_u.offset(1);
1782             }
1783             while self.start_t != self.end_t {
1784                 let _ = ptr::read(self.start_t as *const T); // Run a `T` destructor.
1785                 self.start_t = self.start_t.offset(1);
1786             }
1787             // After this destructor ran, the destructor of `vec` will run,
1788             // deallocating the underlying memory.
1789         }
1790     }
1791 }
1792
1793 #[unsafe_destructor]
1794 impl<T,U> Drop for PartialVecZeroSized<T,U> {
1795     fn drop(&mut self) {
1796         unsafe {
1797             // Destruct the instances of `T` and `U` this struct owns.
1798             while self.num_t != 0 {
1799                 let _: T = mem::uninitialized(); // Run a `T` destructor.
1800                 self.num_t -= 1;
1801             }
1802             while self.num_u != 0 {
1803                 let _: U = mem::uninitialized(); // Run a `U` destructor.
1804                 self.num_u -= 1;
1805             }
1806         }
1807     }
1808 }
1809
1810 #[cfg(test)]
1811 mod tests {
1812     use prelude::*;
1813     use core::mem::size_of;
1814     use core::iter::repeat;
1815     use core::ops::FullRange;
1816     use test::Bencher;
1817     use super::as_vec;
1818
1819     struct DropCounter<'a> {
1820         count: &'a mut int
1821     }
1822
1823     #[unsafe_destructor]
1824     impl<'a> Drop for DropCounter<'a> {
1825         fn drop(&mut self) {
1826             *self.count += 1;
1827         }
1828     }
1829
1830     #[test]
1831     fn test_as_vec() {
1832         let xs = [1u8, 2u8, 3u8];
1833         assert_eq!(as_vec(&xs).as_slice(), xs);
1834     }
1835
1836     #[test]
1837     fn test_as_vec_dtor() {
1838         let (mut count_x, mut count_y) = (0, 0);
1839         {
1840             let xs = &[DropCounter { count: &mut count_x }, DropCounter { count: &mut count_y }];
1841             assert_eq!(as_vec(xs).len(), 2);
1842         }
1843         assert_eq!(count_x, 1);
1844         assert_eq!(count_y, 1);
1845     }
1846
1847     #[test]
1848     fn test_small_vec_struct() {
1849         assert!(size_of::<Vec<u8>>() == size_of::<uint>() * 3);
1850     }
1851
1852     #[test]
1853     fn test_double_drop() {
1854         struct TwoVec<T> {
1855             x: Vec<T>,
1856             y: Vec<T>
1857         }
1858
1859         let (mut count_x, mut count_y) = (0, 0);
1860         {
1861             let mut tv = TwoVec {
1862                 x: Vec::new(),
1863                 y: Vec::new()
1864             };
1865             tv.x.push(DropCounter {count: &mut count_x});
1866             tv.y.push(DropCounter {count: &mut count_y});
1867
1868             // If Vec had a drop flag, here is where it would be zeroed.
1869             // Instead, it should rely on its internal state to prevent
1870             // doing anything significant when dropped multiple times.
1871             drop(tv.x);
1872
1873             // Here tv goes out of scope, tv.y should be dropped, but not tv.x.
1874         }
1875
1876         assert_eq!(count_x, 1);
1877         assert_eq!(count_y, 1);
1878     }
1879
1880     #[test]
1881     fn test_reserve() {
1882         let mut v = Vec::new();
1883         assert_eq!(v.capacity(), 0);
1884
1885         v.reserve(2);
1886         assert!(v.capacity() >= 2);
1887
1888         for i in range(0i, 16) {
1889             v.push(i);
1890         }
1891
1892         assert!(v.capacity() >= 16);
1893         v.reserve(16);
1894         assert!(v.capacity() >= 32);
1895
1896         v.push(16);
1897
1898         v.reserve(16);
1899         assert!(v.capacity() >= 33)
1900     }
1901
1902     #[test]
1903     fn test_extend() {
1904         let mut v = Vec::new();
1905         let mut w = Vec::new();
1906
1907         v.extend(range(0i, 3));
1908         for i in range(0i, 3) { w.push(i) }
1909
1910         assert_eq!(v, w);
1911
1912         v.extend(range(3i, 10));
1913         for i in range(3i, 10) { w.push(i) }
1914
1915         assert_eq!(v, w);
1916     }
1917
1918     #[test]
1919     fn test_slice_from_mut() {
1920         let mut values = vec![1u8,2,3,4,5];
1921         {
1922             let slice = values.slice_from_mut(2);
1923             assert!(slice == [3, 4, 5]);
1924             for p in slice.iter_mut() {
1925                 *p += 2;
1926             }
1927         }
1928
1929         assert!(values == [1, 2, 5, 6, 7]);
1930     }
1931
1932     #[test]
1933     fn test_slice_to_mut() {
1934         let mut values = vec![1u8,2,3,4,5];
1935         {
1936             let slice = values.slice_to_mut(2);
1937             assert!(slice == [1, 2]);
1938             for p in slice.iter_mut() {
1939                 *p += 1;
1940             }
1941         }
1942
1943         assert!(values == [2, 3, 3, 4, 5]);
1944     }
1945
1946     #[test]
1947     fn test_split_at_mut() {
1948         let mut values = vec![1u8,2,3,4,5];
1949         {
1950             let (left, right) = values.split_at_mut(2);
1951             {
1952                 let left: &[_] = left;
1953                 assert!(&left[..left.len()] == &[1, 2][]);
1954             }
1955             for p in left.iter_mut() {
1956                 *p += 1;
1957             }
1958
1959             {
1960                 let right: &[_] = right;
1961                 assert!(&right[..right.len()] == &[3, 4, 5][]);
1962             }
1963             for p in right.iter_mut() {
1964                 *p += 2;
1965             }
1966         }
1967
1968         assert!(values == vec![2u8, 3, 5, 6, 7]);
1969     }
1970
1971     #[test]
1972     fn test_clone() {
1973         let v: Vec<int> = vec!();
1974         let w = vec!(1i, 2, 3);
1975
1976         assert_eq!(v, v.clone());
1977
1978         let z = w.clone();
1979         assert_eq!(w, z);
1980         // they should be disjoint in memory.
1981         assert!(w.as_ptr() != z.as_ptr())
1982     }
1983
1984     #[test]
1985     fn test_clone_from() {
1986         let mut v = vec!();
1987         let three = vec!(box 1i, box 2, box 3);
1988         let two = vec!(box 4i, box 5);
1989         // zero, long
1990         v.clone_from(&three);
1991         assert_eq!(v, three);
1992
1993         // equal
1994         v.clone_from(&three);
1995         assert_eq!(v, three);
1996
1997         // long, short
1998         v.clone_from(&two);
1999         assert_eq!(v, two);
2000
2001         // short, long
2002         v.clone_from(&three);
2003         assert_eq!(v, three)
2004     }
2005
2006     #[test]
2007     fn test_retain() {
2008         let mut vec = vec![1u, 2, 3, 4];
2009         vec.retain(|&x| x % 2 == 0);
2010         assert!(vec == vec![2u, 4]);
2011     }
2012
2013     #[test]
2014     fn zero_sized_values() {
2015         let mut v = Vec::new();
2016         assert_eq!(v.len(), 0);
2017         v.push(());
2018         assert_eq!(v.len(), 1);
2019         v.push(());
2020         assert_eq!(v.len(), 2);
2021         assert_eq!(v.pop(), Some(()));
2022         assert_eq!(v.pop(), Some(()));
2023         assert_eq!(v.pop(), None);
2024
2025         assert_eq!(v.iter().count(), 0);
2026         v.push(());
2027         assert_eq!(v.iter().count(), 1);
2028         v.push(());
2029         assert_eq!(v.iter().count(), 2);
2030
2031         for &() in v.iter() {}
2032
2033         assert_eq!(v.iter_mut().count(), 2);
2034         v.push(());
2035         assert_eq!(v.iter_mut().count(), 3);
2036         v.push(());
2037         assert_eq!(v.iter_mut().count(), 4);
2038
2039         for &mut () in v.iter_mut() {}
2040         unsafe { v.set_len(0); }
2041         assert_eq!(v.iter_mut().count(), 0);
2042     }
2043
2044     #[test]
2045     fn test_partition() {
2046         assert_eq!(vec![].into_iter().partition(|x: &int| *x < 3), (vec![], vec![]));
2047         assert_eq!(vec![1i, 2, 3].into_iter().partition(|x: &int| *x < 4), (vec![1, 2, 3], vec![]));
2048         assert_eq!(vec![1i, 2, 3].into_iter().partition(|x: &int| *x < 2), (vec![1], vec![2, 3]));
2049         assert_eq!(vec![1i, 2, 3].into_iter().partition(|x: &int| *x < 0), (vec![], vec![1, 2, 3]));
2050     }
2051
2052     #[test]
2053     fn test_zip_unzip() {
2054         let z1 = vec![(1i, 4i), (2, 5), (3, 6)];
2055
2056         let (left, right): (Vec<_>, Vec<_>) = z1.iter().map(|&x| x).unzip();
2057
2058         assert_eq!((1, 4), (left[0], right[0]));
2059         assert_eq!((2, 5), (left[1], right[1]));
2060         assert_eq!((3, 6), (left[2], right[2]));
2061     }
2062
2063     #[test]
2064     fn test_unsafe_ptrs() {
2065         unsafe {
2066             // Test on-stack copy-from-buf.
2067             let a = [1i, 2, 3];
2068             let ptr = a.as_ptr();
2069             let b = Vec::from_raw_buf(ptr, 3u);
2070             assert_eq!(b, vec![1, 2, 3]);
2071
2072             // Test on-heap copy-from-buf.
2073             let c = vec![1i, 2, 3, 4, 5];
2074             let ptr = c.as_ptr();
2075             let d = Vec::from_raw_buf(ptr, 5u);
2076             assert_eq!(d, vec![1, 2, 3, 4, 5]);
2077         }
2078     }
2079
2080     #[test]
2081     fn test_vec_truncate_drop() {
2082         static mut drops: uint = 0;
2083         struct Elem(int);
2084         impl Drop for Elem {
2085             fn drop(&mut self) {
2086                 unsafe { drops += 1; }
2087             }
2088         }
2089
2090         let mut v = vec![Elem(1), Elem(2), Elem(3), Elem(4), Elem(5)];
2091         assert_eq!(unsafe { drops }, 0);
2092         v.truncate(3);
2093         assert_eq!(unsafe { drops }, 2);
2094         v.truncate(0);
2095         assert_eq!(unsafe { drops }, 5);
2096     }
2097
2098     #[test]
2099     #[should_fail]
2100     fn test_vec_truncate_fail() {
2101         struct BadElem(int);
2102         impl Drop for BadElem {
2103             fn drop(&mut self) {
2104                 let BadElem(ref mut x) = *self;
2105                 if *x == 0xbadbeef {
2106                     panic!("BadElem panic: 0xbadbeef")
2107                 }
2108             }
2109         }
2110
2111         let mut v = vec![BadElem(1), BadElem(2), BadElem(0xbadbeef), BadElem(4)];
2112         v.truncate(0);
2113     }
2114
2115     #[test]
2116     fn test_index() {
2117         let vec = vec!(1i, 2, 3);
2118         assert!(vec[1] == 2);
2119     }
2120
2121     #[test]
2122     #[should_fail]
2123     fn test_index_out_of_bounds() {
2124         let vec = vec!(1i, 2, 3);
2125         let _ = vec[3];
2126     }
2127
2128     #[test]
2129     #[should_fail]
2130     fn test_slice_out_of_bounds_1() {
2131         let x: Vec<int> = vec![1, 2, 3, 4, 5];
2132         &x[(-1)..];
2133     }
2134
2135     #[test]
2136     #[should_fail]
2137     fn test_slice_out_of_bounds_2() {
2138         let x: Vec<int> = vec![1, 2, 3, 4, 5];
2139         &x[..6];
2140     }
2141
2142     #[test]
2143     #[should_fail]
2144     fn test_slice_out_of_bounds_3() {
2145         let x: Vec<int> = vec![1, 2, 3, 4, 5];
2146         &x[(-1)..4];
2147     }
2148
2149     #[test]
2150     #[should_fail]
2151     fn test_slice_out_of_bounds_4() {
2152         let x: Vec<int> = vec![1, 2, 3, 4, 5];
2153         &x[1..6];
2154     }
2155
2156     #[test]
2157     #[should_fail]
2158     fn test_slice_out_of_bounds_5() {
2159         let x: Vec<int> = vec![1, 2, 3, 4, 5];
2160         &x[3..2];
2161     }
2162
2163     #[test]
2164     #[should_fail]
2165     fn test_swap_remove_empty() {
2166         let mut vec: Vec<uint> = vec!();
2167         vec.swap_remove(0);
2168     }
2169
2170     #[test]
2171     fn test_move_iter_unwrap() {
2172         let mut vec: Vec<uint> = Vec::with_capacity(7);
2173         vec.push(1);
2174         vec.push(2);
2175         let ptr = vec.as_ptr();
2176         vec = vec.into_iter().into_inner();
2177         assert_eq!(vec.as_ptr(), ptr);
2178         assert_eq!(vec.capacity(), 7);
2179         assert_eq!(vec.len(), 0);
2180     }
2181
2182     #[test]
2183     #[should_fail]
2184     fn test_map_in_place_incompatible_types_fail() {
2185         let v = vec![0u, 1, 2];
2186         v.map_in_place(|_| ());
2187     }
2188
2189     #[test]
2190     fn test_map_in_place() {
2191         let v = vec![0u, 1, 2];
2192         assert_eq!(v.map_in_place(|i: uint| i as int - 1), [-1i, 0, 1]);
2193     }
2194
2195     #[test]
2196     fn test_map_in_place_zero_sized() {
2197         let v = vec![(), ()];
2198         #[derive(PartialEq, Show)]
2199         struct ZeroSized;
2200         assert_eq!(v.map_in_place(|_| ZeroSized), [ZeroSized, ZeroSized]);
2201     }
2202
2203     #[test]
2204     fn test_map_in_place_zero_drop_count() {
2205         use std::sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT};
2206
2207         #[derive(Clone, PartialEq, Show)]
2208         struct Nothing;
2209         impl Drop for Nothing { fn drop(&mut self) { } }
2210
2211         #[derive(Clone, PartialEq, Show)]
2212         struct ZeroSized;
2213         impl Drop for ZeroSized {
2214             fn drop(&mut self) {
2215                 DROP_COUNTER.fetch_add(1, Ordering::Relaxed);
2216             }
2217         }
2218         const NUM_ELEMENTS: uint = 2;
2219         static DROP_COUNTER: AtomicUsize = ATOMIC_USIZE_INIT;
2220
2221         let v = repeat(Nothing).take(NUM_ELEMENTS).collect::<Vec<_>>();
2222
2223         DROP_COUNTER.store(0, Ordering::Relaxed);
2224
2225         let v = v.map_in_place(|_| ZeroSized);
2226         assert_eq!(DROP_COUNTER.load(Ordering::Relaxed), 0);
2227         drop(v);
2228         assert_eq!(DROP_COUNTER.load(Ordering::Relaxed), NUM_ELEMENTS);
2229     }
2230
2231     #[test]
2232     fn test_move_items() {
2233         let vec = vec![1, 2, 3];
2234         let mut vec2 : Vec<i32> = vec![];
2235         for i in vec.into_iter() {
2236             vec2.push(i);
2237         }
2238         assert!(vec2 == vec![1, 2, 3]);
2239     }
2240
2241     #[test]
2242     fn test_move_items_reverse() {
2243         let vec = vec![1, 2, 3];
2244         let mut vec2 : Vec<i32> = vec![];
2245         for i in vec.into_iter().rev() {
2246             vec2.push(i);
2247         }
2248         assert!(vec2 == vec![3, 2, 1]);
2249     }
2250
2251     #[test]
2252     fn test_move_items_zero_sized() {
2253         let vec = vec![(), (), ()];
2254         let mut vec2 : Vec<()> = vec![];
2255         for i in vec.into_iter() {
2256             vec2.push(i);
2257         }
2258         assert!(vec2 == vec![(), (), ()]);
2259     }
2260
2261     #[test]
2262     fn test_drain_items() {
2263         let mut vec = vec![1, 2, 3];
2264         let mut vec2: Vec<i32> = vec![];
2265         for i in vec.drain() {
2266             vec2.push(i);
2267         }
2268         assert_eq!(vec, []);
2269         assert_eq!(vec2, [ 1, 2, 3 ]);
2270     }
2271
2272     #[test]
2273     fn test_drain_items_reverse() {
2274         let mut vec = vec![1, 2, 3];
2275         let mut vec2: Vec<i32> = vec![];
2276         for i in vec.drain().rev() {
2277             vec2.push(i);
2278         }
2279         assert_eq!(vec, []);
2280         assert_eq!(vec2, [ 3, 2, 1 ]);
2281     }
2282
2283     #[test]
2284     fn test_drain_items_zero_sized() {
2285         let mut vec = vec![(), (), ()];
2286         let mut vec2: Vec<()> = vec![];
2287         for i in vec.drain() {
2288             vec2.push(i);
2289         }
2290         assert_eq!(vec, []);
2291         assert_eq!(vec2, [(), (), ()]);
2292     }
2293
2294     #[test]
2295     fn test_into_boxed_slice() {
2296         let xs = vec![1u, 2, 3];
2297         let ys = xs.into_boxed_slice();
2298         assert_eq!(ys.as_slice(), [1u, 2, 3]);
2299     }
2300
2301     #[bench]
2302     fn bench_new(b: &mut Bencher) {
2303         b.iter(|| {
2304             let v: Vec<uint> = Vec::new();
2305             assert_eq!(v.len(), 0);
2306             assert_eq!(v.capacity(), 0);
2307         })
2308     }
2309
2310     fn do_bench_with_capacity(b: &mut Bencher, src_len: uint) {
2311         b.bytes = src_len as u64;
2312
2313         b.iter(|| {
2314             let v: Vec<uint> = Vec::with_capacity(src_len);
2315             assert_eq!(v.len(), 0);
2316             assert_eq!(v.capacity(), src_len);
2317         })
2318     }
2319
2320     #[bench]
2321     fn bench_with_capacity_0000(b: &mut Bencher) {
2322         do_bench_with_capacity(b, 0)
2323     }
2324
2325     #[bench]
2326     fn bench_with_capacity_0010(b: &mut Bencher) {
2327         do_bench_with_capacity(b, 10)
2328     }
2329
2330     #[bench]
2331     fn bench_with_capacity_0100(b: &mut Bencher) {
2332         do_bench_with_capacity(b, 100)
2333     }
2334
2335     #[bench]
2336     fn bench_with_capacity_1000(b: &mut Bencher) {
2337         do_bench_with_capacity(b, 1000)
2338     }
2339
2340     fn do_bench_from_fn(b: &mut Bencher, src_len: uint) {
2341         b.bytes = src_len as u64;
2342
2343         b.iter(|| {
2344             let dst = range(0, src_len).collect::<Vec<_>>();
2345             assert_eq!(dst.len(), src_len);
2346             assert!(dst.iter().enumerate().all(|(i, x)| i == *x));
2347         })
2348     }
2349
2350     #[bench]
2351     fn bench_from_fn_0000(b: &mut Bencher) {
2352         do_bench_from_fn(b, 0)
2353     }
2354
2355     #[bench]
2356     fn bench_from_fn_0010(b: &mut Bencher) {
2357         do_bench_from_fn(b, 10)
2358     }
2359
2360     #[bench]
2361     fn bench_from_fn_0100(b: &mut Bencher) {
2362         do_bench_from_fn(b, 100)
2363     }
2364
2365     #[bench]
2366     fn bench_from_fn_1000(b: &mut Bencher) {
2367         do_bench_from_fn(b, 1000)
2368     }
2369
2370     fn do_bench_from_elem(b: &mut Bencher, src_len: uint) {
2371         b.bytes = src_len as u64;
2372
2373         b.iter(|| {
2374             let dst: Vec<uint> = repeat(5).take(src_len).collect();
2375             assert_eq!(dst.len(), src_len);
2376             assert!(dst.iter().all(|x| *x == 5));
2377         })
2378     }
2379
2380     #[bench]
2381     fn bench_from_elem_0000(b: &mut Bencher) {
2382         do_bench_from_elem(b, 0)
2383     }
2384
2385     #[bench]
2386     fn bench_from_elem_0010(b: &mut Bencher) {
2387         do_bench_from_elem(b, 10)
2388     }
2389
2390     #[bench]
2391     fn bench_from_elem_0100(b: &mut Bencher) {
2392         do_bench_from_elem(b, 100)
2393     }
2394
2395     #[bench]
2396     fn bench_from_elem_1000(b: &mut Bencher) {
2397         do_bench_from_elem(b, 1000)
2398     }
2399
2400     fn do_bench_from_slice(b: &mut Bencher, src_len: uint) {
2401         let src: Vec<uint> = FromIterator::from_iter(range(0, src_len));
2402
2403         b.bytes = src_len as u64;
2404
2405         b.iter(|| {
2406             let dst = src.clone()[].to_vec();
2407             assert_eq!(dst.len(), src_len);
2408             assert!(dst.iter().enumerate().all(|(i, x)| i == *x));
2409         });
2410     }
2411
2412     #[bench]
2413     fn bench_from_slice_0000(b: &mut Bencher) {
2414         do_bench_from_slice(b, 0)
2415     }
2416
2417     #[bench]
2418     fn bench_from_slice_0010(b: &mut Bencher) {
2419         do_bench_from_slice(b, 10)
2420     }
2421
2422     #[bench]
2423     fn bench_from_slice_0100(b: &mut Bencher) {
2424         do_bench_from_slice(b, 100)
2425     }
2426
2427     #[bench]
2428     fn bench_from_slice_1000(b: &mut Bencher) {
2429         do_bench_from_slice(b, 1000)
2430     }
2431
2432     fn do_bench_from_iter(b: &mut Bencher, src_len: uint) {
2433         let src: Vec<uint> = FromIterator::from_iter(range(0, src_len));
2434
2435         b.bytes = src_len as u64;
2436
2437         b.iter(|| {
2438             let dst: Vec<uint> = FromIterator::from_iter(src.clone().into_iter());
2439             assert_eq!(dst.len(), src_len);
2440             assert!(dst.iter().enumerate().all(|(i, x)| i == *x));
2441         });
2442     }
2443
2444     #[bench]
2445     fn bench_from_iter_0000(b: &mut Bencher) {
2446         do_bench_from_iter(b, 0)
2447     }
2448
2449     #[bench]
2450     fn bench_from_iter_0010(b: &mut Bencher) {
2451         do_bench_from_iter(b, 10)
2452     }
2453
2454     #[bench]
2455     fn bench_from_iter_0100(b: &mut Bencher) {
2456         do_bench_from_iter(b, 100)
2457     }
2458
2459     #[bench]
2460     fn bench_from_iter_1000(b: &mut Bencher) {
2461         do_bench_from_iter(b, 1000)
2462     }
2463
2464     fn do_bench_extend(b: &mut Bencher, dst_len: uint, src_len: uint) {
2465         let dst: Vec<uint> = FromIterator::from_iter(range(0, dst_len));
2466         let src: Vec<uint> = FromIterator::from_iter(range(dst_len, dst_len + src_len));
2467
2468         b.bytes = src_len as u64;
2469
2470         b.iter(|| {
2471             let mut dst = dst.clone();
2472             dst.extend(src.clone().into_iter());
2473             assert_eq!(dst.len(), dst_len + src_len);
2474             assert!(dst.iter().enumerate().all(|(i, x)| i == *x));
2475         });
2476     }
2477
2478     #[bench]
2479     fn bench_extend_0000_0000(b: &mut Bencher) {
2480         do_bench_extend(b, 0, 0)
2481     }
2482
2483     #[bench]
2484     fn bench_extend_0000_0010(b: &mut Bencher) {
2485         do_bench_extend(b, 0, 10)
2486     }
2487
2488     #[bench]
2489     fn bench_extend_0000_0100(b: &mut Bencher) {
2490         do_bench_extend(b, 0, 100)
2491     }
2492
2493     #[bench]
2494     fn bench_extend_0000_1000(b: &mut Bencher) {
2495         do_bench_extend(b, 0, 1000)
2496     }
2497
2498     #[bench]
2499     fn bench_extend_0010_0010(b: &mut Bencher) {
2500         do_bench_extend(b, 10, 10)
2501     }
2502
2503     #[bench]
2504     fn bench_extend_0100_0100(b: &mut Bencher) {
2505         do_bench_extend(b, 100, 100)
2506     }
2507
2508     #[bench]
2509     fn bench_extend_1000_1000(b: &mut Bencher) {
2510         do_bench_extend(b, 1000, 1000)
2511     }
2512
2513     fn do_bench_push_all(b: &mut Bencher, dst_len: uint, src_len: uint) {
2514         let dst: Vec<uint> = FromIterator::from_iter(range(0, dst_len));
2515         let src: Vec<uint> = FromIterator::from_iter(range(dst_len, dst_len + src_len));
2516
2517         b.bytes = src_len as u64;
2518
2519         b.iter(|| {
2520             let mut dst = dst.clone();
2521             dst.push_all(src.as_slice());
2522             assert_eq!(dst.len(), dst_len + src_len);
2523             assert!(dst.iter().enumerate().all(|(i, x)| i == *x));
2524         });
2525     }
2526
2527     #[bench]
2528     fn bench_push_all_0000_0000(b: &mut Bencher) {
2529         do_bench_push_all(b, 0, 0)
2530     }
2531
2532     #[bench]
2533     fn bench_push_all_0000_0010(b: &mut Bencher) {
2534         do_bench_push_all(b, 0, 10)
2535     }
2536
2537     #[bench]
2538     fn bench_push_all_0000_0100(b: &mut Bencher) {
2539         do_bench_push_all(b, 0, 100)
2540     }
2541
2542     #[bench]
2543     fn bench_push_all_0000_1000(b: &mut Bencher) {
2544         do_bench_push_all(b, 0, 1000)
2545     }
2546
2547     #[bench]
2548     fn bench_push_all_0010_0010(b: &mut Bencher) {
2549         do_bench_push_all(b, 10, 10)
2550     }
2551
2552     #[bench]
2553     fn bench_push_all_0100_0100(b: &mut Bencher) {
2554         do_bench_push_all(b, 100, 100)
2555     }
2556
2557     #[bench]
2558     fn bench_push_all_1000_1000(b: &mut Bencher) {
2559         do_bench_push_all(b, 1000, 1000)
2560     }
2561
2562     fn do_bench_push_all_move(b: &mut Bencher, dst_len: uint, src_len: uint) {
2563         let dst: Vec<uint> = FromIterator::from_iter(range(0u, dst_len));
2564         let src: Vec<uint> = FromIterator::from_iter(range(dst_len, dst_len + src_len));
2565
2566         b.bytes = src_len as u64;
2567
2568         b.iter(|| {
2569             let mut dst = dst.clone();
2570             dst.extend(src.clone().into_iter());
2571             assert_eq!(dst.len(), dst_len + src_len);
2572             assert!(dst.iter().enumerate().all(|(i, x)| i == *x));
2573         });
2574     }
2575
2576     #[bench]
2577     fn bench_push_all_move_0000_0000(b: &mut Bencher) {
2578         do_bench_push_all_move(b, 0, 0)
2579     }
2580
2581     #[bench]
2582     fn bench_push_all_move_0000_0010(b: &mut Bencher) {
2583         do_bench_push_all_move(b, 0, 10)
2584     }
2585
2586     #[bench]
2587     fn bench_push_all_move_0000_0100(b: &mut Bencher) {
2588         do_bench_push_all_move(b, 0, 100)
2589     }
2590
2591     #[bench]
2592     fn bench_push_all_move_0000_1000(b: &mut Bencher) {
2593         do_bench_push_all_move(b, 0, 1000)
2594     }
2595
2596     #[bench]
2597     fn bench_push_all_move_0010_0010(b: &mut Bencher) {
2598         do_bench_push_all_move(b, 10, 10)
2599     }
2600
2601     #[bench]
2602     fn bench_push_all_move_0100_0100(b: &mut Bencher) {
2603         do_bench_push_all_move(b, 100, 100)
2604     }
2605
2606     #[bench]
2607     fn bench_push_all_move_1000_1000(b: &mut Bencher) {
2608         do_bench_push_all_move(b, 1000, 1000)
2609     }
2610
2611     fn do_bench_clone(b: &mut Bencher, src_len: uint) {
2612         let src: Vec<uint> = FromIterator::from_iter(range(0, src_len));
2613
2614         b.bytes = src_len as u64;
2615
2616         b.iter(|| {
2617             let dst = src.clone();
2618             assert_eq!(dst.len(), src_len);
2619             assert!(dst.iter().enumerate().all(|(i, x)| i == *x));
2620         });
2621     }
2622
2623     #[bench]
2624     fn bench_clone_0000(b: &mut Bencher) {
2625         do_bench_clone(b, 0)
2626     }
2627
2628     #[bench]
2629     fn bench_clone_0010(b: &mut Bencher) {
2630         do_bench_clone(b, 10)
2631     }
2632
2633     #[bench]
2634     fn bench_clone_0100(b: &mut Bencher) {
2635         do_bench_clone(b, 100)
2636     }
2637
2638     #[bench]
2639     fn bench_clone_1000(b: &mut Bencher) {
2640         do_bench_clone(b, 1000)
2641     }
2642
2643     fn do_bench_clone_from(b: &mut Bencher, times: uint, dst_len: uint, src_len: uint) {
2644         let dst: Vec<uint> = FromIterator::from_iter(range(0, src_len));
2645         let src: Vec<uint> = FromIterator::from_iter(range(dst_len, dst_len + src_len));
2646
2647         b.bytes = (times * src_len) as u64;
2648
2649         b.iter(|| {
2650             let mut dst = dst.clone();
2651
2652             for _ in range(0, times) {
2653                 dst.clone_from(&src);
2654
2655                 assert_eq!(dst.len(), src_len);
2656                 assert!(dst.iter().enumerate().all(|(i, x)| dst_len + i == *x));
2657             }
2658         });
2659     }
2660
2661     #[bench]
2662     fn bench_clone_from_01_0000_0000(b: &mut Bencher) {
2663         do_bench_clone_from(b, 1, 0, 0)
2664     }
2665
2666     #[bench]
2667     fn bench_clone_from_01_0000_0010(b: &mut Bencher) {
2668         do_bench_clone_from(b, 1, 0, 10)
2669     }
2670
2671     #[bench]
2672     fn bench_clone_from_01_0000_0100(b: &mut Bencher) {
2673         do_bench_clone_from(b, 1, 0, 100)
2674     }
2675
2676     #[bench]
2677     fn bench_clone_from_01_0000_1000(b: &mut Bencher) {
2678         do_bench_clone_from(b, 1, 0, 1000)
2679     }
2680
2681     #[bench]
2682     fn bench_clone_from_01_0010_0010(b: &mut Bencher) {
2683         do_bench_clone_from(b, 1, 10, 10)
2684     }
2685
2686     #[bench]
2687     fn bench_clone_from_01_0100_0100(b: &mut Bencher) {
2688         do_bench_clone_from(b, 1, 100, 100)
2689     }
2690
2691     #[bench]
2692     fn bench_clone_from_01_1000_1000(b: &mut Bencher) {
2693         do_bench_clone_from(b, 1, 1000, 1000)
2694     }
2695
2696     #[bench]
2697     fn bench_clone_from_01_0010_0100(b: &mut Bencher) {
2698         do_bench_clone_from(b, 1, 10, 100)
2699     }
2700
2701     #[bench]
2702     fn bench_clone_from_01_0100_1000(b: &mut Bencher) {
2703         do_bench_clone_from(b, 1, 100, 1000)
2704     }
2705
2706     #[bench]
2707     fn bench_clone_from_01_0010_0000(b: &mut Bencher) {
2708         do_bench_clone_from(b, 1, 10, 0)
2709     }
2710
2711     #[bench]
2712     fn bench_clone_from_01_0100_0010(b: &mut Bencher) {
2713         do_bench_clone_from(b, 1, 100, 10)
2714     }
2715
2716     #[bench]
2717     fn bench_clone_from_01_1000_0100(b: &mut Bencher) {
2718         do_bench_clone_from(b, 1, 1000, 100)
2719     }
2720
2721     #[bench]
2722     fn bench_clone_from_10_0000_0000(b: &mut Bencher) {
2723         do_bench_clone_from(b, 10, 0, 0)
2724     }
2725
2726     #[bench]
2727     fn bench_clone_from_10_0000_0010(b: &mut Bencher) {
2728         do_bench_clone_from(b, 10, 0, 10)
2729     }
2730
2731     #[bench]
2732     fn bench_clone_from_10_0000_0100(b: &mut Bencher) {
2733         do_bench_clone_from(b, 10, 0, 100)
2734     }
2735
2736     #[bench]
2737     fn bench_clone_from_10_0000_1000(b: &mut Bencher) {
2738         do_bench_clone_from(b, 10, 0, 1000)
2739     }
2740
2741     #[bench]
2742     fn bench_clone_from_10_0010_0010(b: &mut Bencher) {
2743         do_bench_clone_from(b, 10, 10, 10)
2744     }
2745
2746     #[bench]
2747     fn bench_clone_from_10_0100_0100(b: &mut Bencher) {
2748         do_bench_clone_from(b, 10, 100, 100)
2749     }
2750
2751     #[bench]
2752     fn bench_clone_from_10_1000_1000(b: &mut Bencher) {
2753         do_bench_clone_from(b, 10, 1000, 1000)
2754     }
2755
2756     #[bench]
2757     fn bench_clone_from_10_0010_0100(b: &mut Bencher) {
2758         do_bench_clone_from(b, 10, 10, 100)
2759     }
2760
2761     #[bench]
2762     fn bench_clone_from_10_0100_1000(b: &mut Bencher) {
2763         do_bench_clone_from(b, 10, 100, 1000)
2764     }
2765
2766     #[bench]
2767     fn bench_clone_from_10_0010_0000(b: &mut Bencher) {
2768         do_bench_clone_from(b, 10, 10, 0)
2769     }
2770
2771     #[bench]
2772     fn bench_clone_from_10_0100_0010(b: &mut Bencher) {
2773         do_bench_clone_from(b, 10, 100, 10)
2774     }
2775
2776     #[bench]
2777     fn bench_clone_from_10_1000_0100(b: &mut Bencher) {
2778         do_bench_clone_from(b, 10, 1000, 100)
2779     }
2780 }