1656 lines
51 KiB
Rust
Raw Normal View History

// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! A growable list type with heap-allocated contents, written `Vec<T>` but
//! pronounced 'vector.'
2014-08-30 17:11:22 -04:00
//!
//! Vectors have `O(1)` indexing, amortized `O(1)` push (to the end) and
//! `O(1)` pop (from the end).
2014-12-16 20:12:30 -05:00
//!
//! # Examples
//!
//! You can explicitly create a `Vec<T>` with `new()`:
2014-12-16 20:12:30 -05:00
//!
//! ```
//! let v: Vec<i32> = Vec::new();
2014-12-16 20:12:30 -05:00
//! ```
//!
//! ...or by using the `vec!` macro:
2014-12-16 20:12:30 -05:00
//!
//! ```
//! let v: Vec<i32> = vec![];
2014-12-16 20:12:30 -05:00
//!
//! let v = vec![1, 2, 3, 4, 5];
//!
//! let v = vec![0; 10]; // ten zeroes
2014-12-16 20:12:30 -05:00
//! ```
//!
//! You can `push` values onto the end of a vector (which will grow the vector
//! as needed):
2014-12-16 20:12:30 -05:00
//!
//! ```
//! let mut v = vec![1, 2];
2014-12-16 20:12:30 -05:00
//!
//! v.push(3);
2014-12-16 20:12:30 -05:00
//! ```
//!
//! Popping values works in much the same way:
2014-12-16 20:12:30 -05:00
//!
//! ```
//! let mut v = vec![1, 2];
2014-12-16 20:12:30 -05:00
//!
//! let two = v.pop();
2014-12-16 20:12:30 -05:00
//! ```
//!
//! Vectors also support indexing (through the `Index` and `IndexMut` traits):
//!
//! ```
//! let mut v = vec![1, 2, 3];
//! let three = v[2];
//! v[1] = v[1] + 5;
//! ```
2015-01-23 21:48:20 -08:00
#![stable(feature = "rust1", since = "1.0.0")]
2015-07-09 21:57:21 -07:00
use alloc::raw_vec::RawVec;
use alloc::boxed::Box;
2015-07-09 21:57:21 -07:00
use alloc::heap::EMPTY;
use core::cmp::Ordering;
std: Recreate a `collections` module As with the previous commit with `librand`, this commit shuffles around some `collections` code. The new state of the world is similar to that of librand: * The libcollections crate now only depends on libcore and liballoc. * The standard library has a new module, `std::collections`. All functionality of libcollections is reexported through this module. I would like to stress that this change is purely cosmetic. There are very few alterations to these primitives. There are a number of notable points about the new organization: * std::{str, slice, string, vec} all moved to libcollections. There is no reason that these primitives shouldn't be necessarily usable in a freestanding context that has allocation. These are all reexported in their usual places in the standard library. * The `hashmap`, and transitively the `lru_cache`, modules no longer reside in `libcollections`, but rather in libstd. The reason for this is because the `HashMap::new` contructor requires access to the OSRng for initially seeding the hash map. Beyond this requirement, there is no reason that the hashmap could not move to libcollections. I do, however, have a plan to move the hash map to the collections module. The `HashMap::new` function could be altered to require that the `H` hasher parameter ascribe to the `Default` trait, allowing the entire `hashmap` module to live in libcollections. The key idea would be that the default hasher would be different in libstd. Something along the lines of: // src/libstd/collections/mod.rs pub type HashMap<K, V, H = RandomizedSipHasher> = core_collections::HashMap<K, V, H>; This is not possible today because you cannot invoke static methods through type aliases. If we modified the compiler, however, to allow invocation of static methods through type aliases, then this type definition would essentially be switching the default hasher from `SipHasher` in libcollections to a libstd-defined `RandomizedSipHasher` type. This type's `Default` implementation would randomly seed the `SipHasher` instance, and otherwise perform the same as `SipHasher`. This future state doesn't seem incredibly far off, but until that time comes, the hashmap module will live in libstd to not compromise on functionality. * In preparation for the hashmap moving to libcollections, the `hash` module has moved from libstd to libcollections. A previously snapshotted commit enables a distinct `Writer` trait to live in the `hash` module which `Hash` implementations are now parameterized over. Due to using a custom trait, the `SipHasher` implementation has lost its specialized methods for writing integers. These can be re-added backwards-compatibly in the future via default methods if necessary, but the FNV hashing should satisfy much of the need for speedier hashing. A list of breaking changes: * HashMap::{get, get_mut} no longer fails with the key formatted into the error message with `{:?}`, instead, a generic message is printed. With backtraces, it should still be not-too-hard to track down errors. * The HashMap, HashSet, and LruCache types are now available through std::collections instead of the collections crate. * Manual implementations of hash should be parameterized over `hash::Writer` instead of just `Writer`. [breaking-change]
2014-05-29 18:50:12 -07:00
use core::fmt;
2015-01-03 22:42:21 -05:00
use core::hash::{self, Hash};
use core::intrinsics::{arith_offset, assume, drop_in_place, needs_drop};
use core::iter::FromIterator;
std: Recreate a `collections` module As with the previous commit with `librand`, this commit shuffles around some `collections` code. The new state of the world is similar to that of librand: * The libcollections crate now only depends on libcore and liballoc. * The standard library has a new module, `std::collections`. All functionality of libcollections is reexported through this module. I would like to stress that this change is purely cosmetic. There are very few alterations to these primitives. There are a number of notable points about the new organization: * std::{str, slice, string, vec} all moved to libcollections. There is no reason that these primitives shouldn't be necessarily usable in a freestanding context that has allocation. These are all reexported in their usual places in the standard library. * The `hashmap`, and transitively the `lru_cache`, modules no longer reside in `libcollections`, but rather in libstd. The reason for this is because the `HashMap::new` contructor requires access to the OSRng for initially seeding the hash map. Beyond this requirement, there is no reason that the hashmap could not move to libcollections. I do, however, have a plan to move the hash map to the collections module. The `HashMap::new` function could be altered to require that the `H` hasher parameter ascribe to the `Default` trait, allowing the entire `hashmap` module to live in libcollections. The key idea would be that the default hasher would be different in libstd. Something along the lines of: // src/libstd/collections/mod.rs pub type HashMap<K, V, H = RandomizedSipHasher> = core_collections::HashMap<K, V, H>; This is not possible today because you cannot invoke static methods through type aliases. If we modified the compiler, however, to allow invocation of static methods through type aliases, then this type definition would essentially be switching the default hasher from `SipHasher` in libcollections to a libstd-defined `RandomizedSipHasher` type. This type's `Default` implementation would randomly seed the `SipHasher` instance, and otherwise perform the same as `SipHasher`. This future state doesn't seem incredibly far off, but until that time comes, the hashmap module will live in libstd to not compromise on functionality. * In preparation for the hashmap moving to libcollections, the `hash` module has moved from libstd to libcollections. A previously snapshotted commit enables a distinct `Writer` trait to live in the `hash` module which `Hash` implementations are now parameterized over. Due to using a custom trait, the `SipHasher` implementation has lost its specialized methods for writing integers. These can be re-added backwards-compatibly in the future via default methods if necessary, but the FNV hashing should satisfy much of the need for speedier hashing. A list of breaking changes: * HashMap::{get, get_mut} no longer fails with the key formatted into the error message with `{:?}`, instead, a generic message is printed. With backtraces, it should still be not-too-hard to track down errors. * The HashMap, HashSet, and LruCache types are now available through std::collections instead of the collections crate. * Manual implementations of hash should be parameterized over `hash::Writer` instead of just `Writer`. [breaking-change]
2014-05-29 18:50:12 -07:00
use core::mem;
use core::ops::{Index, IndexMut, Deref};
use core::ops;
2014-12-11 22:29:24 -05:00
use core::ptr;
use core::slice;
std: Recreate a `collections` module As with the previous commit with `librand`, this commit shuffles around some `collections` code. The new state of the world is similar to that of librand: * The libcollections crate now only depends on libcore and liballoc. * The standard library has a new module, `std::collections`. All functionality of libcollections is reexported through this module. I would like to stress that this change is purely cosmetic. There are very few alterations to these primitives. There are a number of notable points about the new organization: * std::{str, slice, string, vec} all moved to libcollections. There is no reason that these primitives shouldn't be necessarily usable in a freestanding context that has allocation. These are all reexported in their usual places in the standard library. * The `hashmap`, and transitively the `lru_cache`, modules no longer reside in `libcollections`, but rather in libstd. The reason for this is because the `HashMap::new` contructor requires access to the OSRng for initially seeding the hash map. Beyond this requirement, there is no reason that the hashmap could not move to libcollections. I do, however, have a plan to move the hash map to the collections module. The `HashMap::new` function could be altered to require that the `H` hasher parameter ascribe to the `Default` trait, allowing the entire `hashmap` module to live in libcollections. The key idea would be that the default hasher would be different in libstd. Something along the lines of: // src/libstd/collections/mod.rs pub type HashMap<K, V, H = RandomizedSipHasher> = core_collections::HashMap<K, V, H>; This is not possible today because you cannot invoke static methods through type aliases. If we modified the compiler, however, to allow invocation of static methods through type aliases, then this type definition would essentially be switching the default hasher from `SipHasher` in libcollections to a libstd-defined `RandomizedSipHasher` type. This type's `Default` implementation would randomly seed the `SipHasher` instance, and otherwise perform the same as `SipHasher`. This future state doesn't seem incredibly far off, but until that time comes, the hashmap module will live in libstd to not compromise on functionality. * In preparation for the hashmap moving to libcollections, the `hash` module has moved from libstd to libcollections. A previously snapshotted commit enables a distinct `Writer` trait to live in the `hash` module which `Hash` implementations are now parameterized over. Due to using a custom trait, the `SipHasher` implementation has lost its specialized methods for writing integers. These can be re-added backwards-compatibly in the future via default methods if necessary, but the FNV hashing should satisfy much of the need for speedier hashing. A list of breaking changes: * HashMap::{get, get_mut} no longer fails with the key formatted into the error message with `{:?}`, instead, a generic message is printed. With backtraces, it should still be not-too-hard to track down errors. * The HashMap, HashSet, and LruCache types are now available through std::collections instead of the collections crate. * Manual implementations of hash should be parameterized over `hash::Writer` instead of just `Writer`. [breaking-change]
2014-05-29 18:50:12 -07:00
use borrow::{Cow, IntoCow};
use super::range::RangeArgument;
2014-12-16 20:12:30 -05:00
/// A growable list type, written `Vec<T>` but pronounced 'vector.'
///
2014-03-19 23:01:08 -07:00
/// # Examples
///
/// ```
2014-03-19 23:01:08 -07:00
/// let mut vec = Vec::new();
2015-01-25 22:05:03 +01:00
/// vec.push(1);
/// vec.push(2);
2014-03-19 23:01:08 -07:00
///
/// assert_eq!(vec.len(), 2);
2014-07-15 11:37:25 +12:00
/// assert_eq!(vec[0], 1);
2014-03-19 23:01:08 -07:00
///
/// assert_eq!(vec.pop(), Some(2));
/// assert_eq!(vec.len(), 1);
///
2015-01-25 22:05:03 +01:00
/// vec[0] = 7;
/// assert_eq!(vec[0], 7);
///
/// vec.extend([1, 2, 3].iter().cloned());
///
/// for x in &vec {
/// println!("{}", x);
/// }
2015-02-24 21:15:45 +03:00
/// assert_eq!(vec, [7, 1, 2, 3]);
2014-03-19 23:01:08 -07:00
/// ```
///
/// The `vec!` macro is provided to make initialization more convenient:
///
/// ```
2015-01-25 22:05:03 +01:00
/// let mut vec = vec![1, 2, 3];
/// vec.push(4);
2015-02-24 21:15:45 +03:00
/// assert_eq!(vec, [1, 2, 3, 4]);
/// ```
///
/// It can also initialize each element of a `Vec<T>` with a given value:
///
/// ```
/// let vec = vec![0; 5];
/// assert_eq!(vec, [0, 0, 0, 0, 0]);
/// ```
///
2014-12-16 20:12:30 -05:00
/// Use a `Vec<T>` as an efficient stack:
///
/// ```
/// let mut stack = Vec::new();
///
2015-01-25 22:05:03 +01:00
/// stack.push(1);
/// stack.push(2);
/// stack.push(3);
///
/// while let Some(top) = stack.pop() {
/// // Prints 3, 2, 1
/// println!("{}", top);
/// }
/// ```
///
/// # Capacity and reallocation
///
/// The capacity of a vector is the amount of space allocated for any future
/// elements that will be added onto the vector. This is not to be confused with
/// the *length* of a vector, which specifies the number of actual elements
/// within the vector. If a vector's length exceeds its capacity, its capacity
/// will automatically be increased, but its elements will have to be
2014-12-16 20:12:30 -05:00
/// reallocated.
///
/// For example, a vector with capacity 10 and length 0 would be an empty vector
/// with space for 10 more elements. Pushing 10 or fewer elements onto the
/// vector will not change its capacity or cause reallocation to occur. However,
/// if the vector's length is increased to 11, it will have to reallocate, which
/// can be slow. For this reason, it is recommended to use `Vec::with_capacity`
/// whenever possible to specify how big the vector is expected to get.
2015-08-19 10:13:39 -07:00
///
/// # Guarantees
///
/// Due to its incredibly fundamental nature, Vec makes a lot of guarantees
/// about its design. This ensures that it's as low-overhead as possible in
/// the general case, and can be correctly manipulated in primitive ways
/// by unsafe code. Note that these guarantees refer to an unqualified `Vec<T>`.
/// If additional type parameters are added (e.g. to support custom allocators),
/// overriding their defaults may change the behavior.
///
/// Most fundamentally, Vec is and always will be a (pointer, capacity, length)
/// triplet. No more, no less. The order of these fields is completely
/// unspecified, and you should use the appropriate methods to modify these.
/// The pointer will never be null, so this type is null-pointer-optimized.
///
/// However, the pointer may not actually point to allocated memory. In particular,
/// if you construct a Vec with capacity 0 via `Vec::new()`, `vec![]`,
/// `Vec::with_capacity(0)`, or by calling `shrink_to_fit()` on an empty Vec, it
/// will not allocate memory. Similarly, if you store zero-sized types inside
/// a Vec, it will not allocate space for them. *Note that in this case the
/// Vec may not report a `capacity()` of 0*. Vec will allocate if and only
/// if `mem::size_of::<T>() * capacity() > 0`. In general, Vec's allocation
/// details are subtle enough that it is strongly recommended that you only
/// free memory allocated by a Vec by creating a new Vec and dropping it.
///
/// If a Vec *has* allocated memory, then the memory it points to is on the heap
/// (as defined by the allocator Rust is configured to use by default), and its
/// pointer points to `len()` initialized elements in order (what you would see
/// if you coerced it to a slice), followed by `capacity() - len()` logically
/// uninitialized elements.
///
/// Vec will never perform a "small optimization" where elements are actually
/// stored on the stack for two reasons:
///
/// * It would make it more difficult for unsafe code to correctly manipulate
/// a Vec. The contents of a Vec wouldn't have a stable address if it were
/// only moved, and it would be more difficult to determine if a Vec had
/// actually allocated memory.
///
/// * It would penalize the general case, incurring an additional branch
/// on every access.
///
/// Vec will never automatically shrink itself, even if completely empty. This
/// ensures no unnecessary allocations or deallocations occur. Emptying a Vec
/// and then filling it back up to the same `len()` should incur no calls to
/// the allocator. If you wish to free up unused memory, use `shrink_to_fit`.
///
/// `push` and `insert` will never (re)allocate if the reported capacity is
/// sufficient. `push` and `insert` *will* (re)allocate if `len() == capacity()`.
/// That is, the reported capacity is completely accurate, and can be relied on.
/// It can even be used to manually free the memory allocated by a Vec if
2015-08-19 12:36:14 -07:00
/// desired. Bulk insertion methods *may* reallocate, even when not necessary.
2015-08-19 10:13:39 -07:00
///
/// Vec does not guarantee any particular growth strategy when reallocating
/// when full, nor when `reserve` is called. The current strategy is basic
/// and it may prove desirable to use a non-constant growth factor. Whatever
/// strategy is used will of course guarantee `O(1)` amortized `push`.
///
/// `vec![x; n]`, `vec![a, b, c, d]`, and `Vec::with_capacity(n)`, will all
/// produce a Vec with exactly the requested capacity. If `len() == capacity()`,
/// (as is the case for the `vec!` macro), then a `Vec<T>` can be converted
/// to and from a `Box<[T]>` without reallocating or moving the elements.
///
/// Vec will not specifically overwrite any data that is removed from it,
/// but also won't specifically preserve it. Its uninitialized memory is
/// scratch space that it may use however it wants. It will generally just do
/// whatever is most efficient or otherwise easy to implement. Do not rely on
/// removed data to be erased for security purposes. Even if you drop a Vec, its
/// buffer may simply be reused by another Vec. Even if you zero a Vec's memory
/// first, that may not actually happen because the optimizer does not consider
/// this a side-effect that must be preserved.
///
/// Vec does not currently guarantee the order in which elements are dropped
/// (the order has changed in the past, and may change again).
///
#[unsafe_no_drop_flag]
2015-01-23 21:48:20 -08:00
#[stable(feature = "rust1", since = "1.0.0")]
pub struct Vec<T> {
2015-07-09 21:57:21 -07:00
buf: RawVec<T>,
2015-02-04 21:17:19 -05:00
len: usize,
}
////////////////////////////////////////////////////////////////////////////////
// Inherent methods
////////////////////////////////////////////////////////////////////////////////
impl<T> Vec<T> {
2014-12-16 20:12:30 -05:00
/// Constructs a new, empty `Vec<T>`.
///
/// The vector will not allocate until elements are pushed onto it.
///
/// # Examples
///
/// ```
/// let mut vec: Vec<i32> = Vec::new();
/// ```
#[inline]
2015-01-23 21:48:20 -08:00
#[stable(feature = "rust1", since = "1.0.0")]
pub fn new() -> Vec<T> {
2015-07-09 21:57:21 -07:00
Vec { buf: RawVec::new(), len: 0 }
}
2014-12-16 20:12:30 -05:00
/// Constructs a new, empty `Vec<T>` with the specified capacity.
///
/// The vector will be able to hold exactly `capacity` elements without
/// reallocating. If `capacity` is 0, the vector will not allocate.
///
/// It is important to note that this function does not specify the *length*
/// of the returned vector, but only the *capacity*. (For an explanation of
/// the difference between length and capacity, see the main `Vec<T>` docs
/// above, 'Capacity and reallocation'.)
///
/// # Examples
///
/// ```
/// let mut vec = Vec::with_capacity(10);
///
/// // The vector contains no items, even though it has capacity for more
/// assert_eq!(vec.len(), 0);
///
/// // These are all done without reallocating...
2015-01-25 22:05:03 +01:00
/// for i in 0..10 {
/// vec.push(i);
/// }
///
/// // ...but this may make the vector reallocate
/// vec.push(11);
/// ```
std: micro-optimize Vec constructors and add benchmarks Generally speaking, inlining doesn't really help out with constructing vectors, except for when we construct a zero-sized vector. This patch allows llvm to optimize this case away in a lot of cases, which shaves off 4-8ns. It's not much, but it might help in some inner loop somewhere. before: running 12 tests test bench_extend_0 ... bench: 123 ns/iter (+/- 6) test bench_extend_5 ... bench: 323 ns/iter (+/- 11) test bench_from_fn_0 ... bench: 7 ns/iter (+/- 0) test bench_from_fn_5 ... bench: 49 ns/iter (+/- 6) test bench_from_iter_0 ... bench: 11 ns/iter (+/- 0) test bench_from_iter_5 ... bench: 176 ns/iter (+/- 11) test bench_from_slice_0 ... bench: 8 ns/iter (+/- 1) test bench_from_slice_5 ... bench: 73 ns/iter (+/- 5) test bench_new ... bench: 0 ns/iter (+/- 0) test bench_with_capacity_0 ... bench: 6 ns/iter (+/- 1) test bench_with_capacity_100 ... bench: 41 ns/iter (+/- 3) test bench_with_capacity_5 ... bench: 40 ns/iter (+/- 2) after: test bench_extend_0 ... bench: 123 ns/iter (+/- 7) test bench_extend_5 ... bench: 339 ns/iter (+/- 27) test bench_from_fn_0 ... bench: 7 ns/iter (+/- 0) test bench_from_fn_5 ... bench: 54 ns/iter (+/- 4) test bench_from_iter_0 ... bench: 11 ns/iter (+/- 1) test bench_from_iter_5 ... bench: 182 ns/iter (+/- 16) test bench_from_slice_0 ... bench: 4 ns/iter (+/- 0) test bench_from_slice_5 ... bench: 62 ns/iter (+/- 3) test bench_new ... bench: 0 ns/iter (+/- 0) test bench_with_capacity_0 ... bench: 0 ns/iter (+/- 0) test bench_with_capacity_100 ... bench: 41 ns/iter (+/- 1) test bench_with_capacity_5 ... bench: 41 ns/iter (+/- 3)
2014-06-02 21:56:29 -07:00
#[inline]
2015-01-23 21:48:20 -08:00
#[stable(feature = "rust1", since = "1.0.0")]
2015-02-04 21:17:19 -05:00
pub fn with_capacity(capacity: usize) -> Vec<T> {
2015-07-09 21:57:21 -07:00
Vec { buf: RawVec::with_capacity(capacity), len: 0 }
}
/// Creates a `Vec<T>` directly from the raw components of another vector.
///
/// # Unsafety
///
/// This is highly unsafe, due to the number of invariants that aren't
/// checked:
///
/// * `ptr` needs to have been previously allocated via `String`/`Vec<T>`
/// (at least, it's highly likely to be incorrect if it wasn't).
/// * `length` needs to be the length that less than or equal to `capacity`.
/// * `capacity` needs to be the capacity that the pointer was allocated with.
///
/// Violating these may cause problems like corrupting the allocator's
/// internal datastructures.
///
/// # Examples
///
/// ```
/// use std::ptr;
/// use std::mem;
///
/// fn main() {
2015-01-25 22:05:03 +01:00
/// let mut v = vec![1, 2, 3];
///
/// // Pull out the various important pieces of information about `v`
/// let p = v.as_mut_ptr();
/// let len = v.len();
/// let cap = v.capacity();
///
/// unsafe {
/// // Cast `v` into the void: no destructor run, so we are in
/// // complete control of the allocation to which `p` points.
/// mem::forget(v);
///
/// // Overwrite memory with 4, 5, 6
/// for i in 0..len as isize {
/// ptr::write(p.offset(i), 4 + i);
/// }
///
/// // Put everything back together into a Vec
/// let rebuilt = Vec::from_raw_parts(p, len, cap);
2015-02-24 21:15:45 +03:00
/// assert_eq!(rebuilt, [4, 5, 6]);
/// }
/// }
/// ```
2015-01-23 21:48:20 -08:00
#[stable(feature = "rust1", since = "1.0.0")]
2015-02-04 21:17:19 -05:00
pub unsafe fn from_raw_parts(ptr: *mut T, length: usize,
capacity: usize) -> Vec<T> {
Vec {
2015-07-09 21:57:21 -07:00
buf: RawVec::from_raw_parts(ptr, capacity),
len: length,
}
}
/// Returns the number of elements the vector can hold without
/// reallocating.
///
/// # Examples
///
/// ```
/// let vec: Vec<i32> = Vec::with_capacity(10);
/// assert_eq!(vec.capacity(), 10);
/// ```
#[inline]
2015-01-23 21:48:20 -08:00
#[stable(feature = "rust1", since = "1.0.0")]
2015-02-04 21:17:19 -05:00
pub fn capacity(&self) -> usize {
2015-07-09 21:57:21 -07:00
self.buf.cap()
}
/// Reserves capacity for at least `additional` more elements to be inserted
/// in the given `Vec<T>`. The collection may reserve more space to avoid
/// frequent reallocations.
///
/// # Panics
///
2015-02-04 21:17:19 -05:00
/// Panics if the new capacity overflows `usize`.
///
/// # Examples
///
/// ```
/// let mut vec = vec![1];
/// vec.reserve(10);
/// assert!(vec.capacity() >= 11);
/// ```
2015-01-23 21:48:20 -08:00
#[stable(feature = "rust1", since = "1.0.0")]
2015-02-04 21:17:19 -05:00
pub fn reserve(&mut self, additional: usize) {
2015-07-09 21:57:21 -07:00
self.buf.reserve(self.len, additional);
}
/// Reserves the minimum capacity for exactly `additional` more elements to
2014-12-30 10:51:18 -08:00
/// be inserted in the given `Vec<T>`. Does nothing if the capacity is already
/// sufficient.
///
/// Note that the allocator may give the collection more space than it
/// requests. Therefore capacity can not be relied upon to be precisely
/// minimal. Prefer `reserve` if future insertions are expected.
///
/// # Panics
///
2015-02-04 21:17:19 -05:00
/// Panics if the new capacity overflows `usize`.
///
/// # Examples
///
/// ```
/// let mut vec = vec![1];
/// vec.reserve_exact(10);
/// assert!(vec.capacity() >= 11);
/// ```
2015-01-23 21:48:20 -08:00
#[stable(feature = "rust1", since = "1.0.0")]
2015-02-04 21:17:19 -05:00
pub fn reserve_exact(&mut self, additional: usize) {
2015-07-09 21:57:21 -07:00
self.buf.reserve_exact(self.len, additional);
}
2014-12-30 10:51:18 -08:00
/// Shrinks the capacity of the vector as much as possible.
///
2014-12-30 10:51:18 -08:00
/// It will drop down as close as possible to the length but the allocator
/// may still inform the vector that there is space for a few more elements.
///
/// # Examples
///
/// ```
/// let mut vec = Vec::with_capacity(10);
/// vec.extend([1, 2, 3].iter().cloned());
/// assert_eq!(vec.capacity(), 10);
/// vec.shrink_to_fit();
/// assert!(vec.capacity() >= 3);
/// ```
2015-01-23 21:48:20 -08:00
#[stable(feature = "rust1", since = "1.0.0")]
pub fn shrink_to_fit(&mut self) {
2015-07-09 21:57:21 -07:00
self.buf.shrink_to_fit(self.len);
}
/// Converts the vector into Box<[T]>.
///
/// Note that this will drop any excess capacity. Calling this and
/// converting back to a vector with `into_vec()` is equivalent to calling
/// `shrink_to_fit()`.
#[stable(feature = "rust1", since = "1.0.0")]
pub fn into_boxed_slice(mut self) -> Box<[T]> {
unsafe {
2015-07-09 21:57:21 -07:00
self.shrink_to_fit();
let buf = ptr::read(&self.buf);
mem::forget(self);
2015-07-09 21:57:21 -07:00
buf.into_box()
}
}
/// Shorten a vector, dropping excess elements.
///
/// If `len` is greater than the vector's current length, this has no
/// effect.
///
/// # Examples
///
/// ```
2015-01-25 22:05:03 +01:00
/// let mut vec = vec![1, 2, 3, 4];
/// vec.truncate(2);
2015-02-24 21:15:45 +03:00
/// assert_eq!(vec, [1, 2]);
/// ```
2015-01-23 21:48:20 -08:00
#[stable(feature = "rust1", since = "1.0.0")]
2015-02-04 21:17:19 -05:00
pub fn truncate(&mut self, len: usize) {
unsafe {
// drop any extra elements
while len < self.len {
// decrement len before the read(), so a panic on Drop doesn't
// re-drop the just-failed value.
self.len -= 1;
ptr::read(self.get_unchecked(self.len));
}
}
}
/// Extracts a slice containing the entire vector.
///
/// Equivalent to `&s[..]`.
#[inline]
#[unstable(feature = "convert",
reason = "waiting on RFC revision",
issue = "27729")]
pub fn as_slice(&self) -> &[T] {
self
}
/// Extracts a mutable slice of the entire vector.
///
/// Equivalent to `&mut s[..]`.
#[inline]
#[unstable(feature = "convert",
reason = "waiting on RFC revision",
issue = "27729")]
pub fn as_mut_slice(&mut self) -> &mut [T] {
&mut self[..]
}
/// Sets the length of a vector.
///
/// This will explicitly set the size of the vector, without actually
/// modifying its buffers, so it is up to the caller to ensure that the
/// vector is actually the specified size.
///
/// # Examples
///
/// ```
/// let mut v = vec![1, 2, 3, 4];
/// unsafe {
/// v.set_len(1);
/// }
/// ```
2014-07-14 11:03:23 +12:00
#[inline]
2015-01-23 21:48:20 -08:00
#[stable(feature = "rust1", since = "1.0.0")]
2015-02-04 21:17:19 -05:00
pub unsafe fn set_len(&mut self, len: usize) {
self.len = len;
2014-07-14 11:03:23 +12:00
}
/// Removes an element from anywhere in the vector and return it, replacing
/// it with the last element.
2014-12-16 20:12:30 -05:00
///
/// This does not preserve ordering, but is O(1).
///
/// # Panics
///
/// Panics if `index` is out of bounds.
///
/// # Examples
///
/// ```
/// let mut v = vec!["foo", "bar", "baz", "qux"];
///
2014-12-30 10:51:18 -08:00
/// assert_eq!(v.swap_remove(1), "bar");
2015-02-24 21:15:45 +03:00
/// assert_eq!(v, ["foo", "qux", "baz"]);
///
2014-12-30 10:51:18 -08:00
/// assert_eq!(v.swap_remove(0), "foo");
2015-02-24 21:15:45 +03:00
/// assert_eq!(v, ["baz", "qux"]);
/// ```
#[inline]
2015-01-23 21:48:20 -08:00
#[stable(feature = "rust1", since = "1.0.0")]
2015-02-04 21:17:19 -05:00
pub fn swap_remove(&mut self, index: usize) -> T {
let length = self.len();
self.swap(index, length - 1);
self.pop().unwrap()
}
/// Inserts an element at position `index` within the vector, shifting all
/// elements after position `i` one position to the right.
///
/// # Panics
///
/// Panics if `index` is greater than the vector's length.
///
/// # Examples
///
/// ```
2015-01-25 22:05:03 +01:00
/// let mut vec = vec![1, 2, 3];
/// vec.insert(1, 4);
2015-02-24 21:15:45 +03:00
/// assert_eq!(vec, [1, 4, 2, 3]);
/// vec.insert(4, 5);
2015-02-24 21:15:45 +03:00
/// assert_eq!(vec, [1, 4, 2, 3, 5]);
/// ```
2015-01-23 21:48:20 -08:00
#[stable(feature = "rust1", since = "1.0.0")]
2015-02-04 21:17:19 -05:00
pub fn insert(&mut self, index: usize, element: T) {
let len = self.len();
assert!(index <= len);
2015-07-09 21:57:21 -07:00
// space for the new element
2015-07-09 21:57:21 -07:00
if len == self.buf.cap() { self.buf.double(); }
unsafe { // infallible
// The spot to put the new value
{
let p = self.as_mut_ptr().offset(index as isize);
// Shift everything over to make space. (Duplicating the
// `index`th element into two consecutive places.)
2015-07-09 21:57:21 -07:00
ptr::copy(p, p.offset(1), len - index);
// Write it in, overwriting the first copy of the `index`th
// element.
2015-07-09 21:57:21 -07:00
ptr::write(p, element);
}
self.set_len(len + 1);
}
}
/// Removes and returns the element at position `index` within the vector,
/// shifting all elements after position `index` one position to the left.
///
/// # Panics
///
2015-04-27 16:11:46 +02:00
/// Panics if `index` is out of bounds.
///
/// # Examples
///
/// ```
2015-01-25 22:05:03 +01:00
/// let mut v = vec![1, 2, 3];
2014-12-30 10:51:18 -08:00
/// assert_eq!(v.remove(1), 2);
2015-02-24 21:15:45 +03:00
/// assert_eq!(v, [1, 3]);
/// ```
2015-01-23 21:48:20 -08:00
#[stable(feature = "rust1", since = "1.0.0")]
2015-02-04 21:17:19 -05:00
pub fn remove(&mut self, index: usize) -> T {
2014-03-11 00:53:23 -04:00
let len = self.len();
assert!(index < len);
unsafe { // infallible
let ret;
{
// the place we are taking from.
let ptr = self.as_mut_ptr().offset(index as isize);
// copy it out, unsafely having a copy of the value on
// the stack and in the vector at the same time.
ret = ptr::read(ptr);
// Shift everything down to fill in that spot.
2015-07-09 21:57:21 -07:00
ptr::copy(ptr.offset(1), ptr, len - index - 1);
}
self.set_len(len - 1);
ret
}
}
2014-04-02 23:10:36 -04:00
/// Retains only the elements specified by the predicate.
2014-12-16 20:12:30 -05:00
///
/// In other words, remove all elements `e` such that `f(&e)` returns false.
/// This method operates in place and preserves the order of the retained
/// elements.
///
/// # Examples
///
/// ```
2015-01-25 22:05:03 +01:00
/// let mut vec = vec![1, 2, 3, 4];
/// vec.retain(|&x| x%2 == 0);
2015-02-24 21:15:45 +03:00
/// assert_eq!(vec, [2, 4]);
/// ```
2015-01-23 21:48:20 -08:00
#[stable(feature = "rust1", since = "1.0.0")]
pub fn retain<F>(&mut self, mut f: F) where F: FnMut(&T) -> bool {
2014-04-02 23:10:36 -04:00
let len = self.len();
2015-02-04 21:17:19 -05:00
let mut del = 0;
2014-04-02 23:10:36 -04:00
{
let v = &mut **self;
core: Remove the cast module This commit revisits the `cast` module in libcore and libstd, and scrutinizes all functions inside of it. The result was to remove the `cast` module entirely, folding all functionality into the `mem` module. Specifically, this is the fate of each function in the `cast` module. * transmute - This function was moved to `mem`, but it is now marked as #[unstable]. This is due to planned changes to the `transmute` function and how it can be invoked (see the #[unstable] comment). For more information, see RFC 5 and #12898 * transmute_copy - This function was moved to `mem`, with clarification that is is not an error to invoke it with T/U that are different sizes, but rather that it is strongly discouraged. This function is now #[stable] * forget - This function was moved to `mem` and marked #[stable] * bump_box_refcount - This function was removed due to the deprecation of managed boxes as well as its questionable utility. * transmute_mut - This function was previously deprecated, and removed as part of this commit. * transmute_mut_unsafe - This function doesn't serve much of a purpose when it can be achieved with an `as` in safe code, so it was removed. * transmute_lifetime - This function was removed because it is likely a strong indication that code is incorrect in the first place. * transmute_mut_lifetime - This function was removed for the same reasons as `transmute_lifetime` * copy_lifetime - This function was moved to `mem`, but it is marked `#[unstable]` now due to the likelihood of being removed in the future if it is found to not be very useful. * copy_mut_lifetime - This function was also moved to `mem`, but had the same treatment as `copy_lifetime`. * copy_lifetime_vec - This function was removed because it is not used today, and its existence is not necessary with DST (copy_lifetime will suffice). In summary, the cast module was stripped down to these functions, and then the functions were moved to the `mem` module. transmute - #[unstable] transmute_copy - #[stable] forget - #[stable] copy_lifetime - #[unstable] copy_mut_lifetime - #[unstable] [breaking-change]
2014-05-09 10:34:51 -07:00
2015-02-04 21:17:19 -05:00
for i in 0..len {
2014-04-02 23:10:36 -04:00
if !f(&v[i]) {
del += 1;
} else if del > 0 {
v.swap(i-del, i);
2014-05-06 17:01:16 -04:00
}
}
2014-04-02 23:10:36 -04:00
}
if del > 0 {
self.truncate(len - del);
}
}
/// Appends an element to the back of a collection.
///
/// # Panics
///
2015-02-04 21:17:19 -05:00
/// Panics if the number of elements in the vector overflows a `usize`.
///
/// # Examples
///
/// ```
/// let mut vec = vec![1, 2];
/// vec.push(3);
2015-02-24 21:15:45 +03:00
/// assert_eq!(vec, [1, 2, 3]);
/// ```
#[inline]
2015-01-23 21:48:20 -08:00
#[stable(feature = "rust1", since = "1.0.0")]
pub fn push(&mut self, value: T) {
2015-07-09 21:57:21 -07:00
// This will panic or abort if we would allocate > isize::MAX bytes
// or if the length increment would overflow for zero-sized types.
if self.len == self.buf.cap() { self.buf.double(); }
unsafe {
2015-07-09 21:57:21 -07:00
let end = self.as_mut_ptr().offset(self.len as isize);
ptr::write(end, value);
self.len += 1;
}
}
/// Removes the last element from a vector and returns it, or `None` if it
/// is empty.
///
/// # Examples
///
/// ```
2015-01-25 22:05:03 +01:00
/// let mut vec = vec![1, 2, 3];
/// assert_eq!(vec.pop(), Some(3));
2015-02-24 21:15:45 +03:00
/// assert_eq!(vec, [1, 2]);
/// ```
#[inline]
2015-01-23 21:48:20 -08:00
#[stable(feature = "rust1", since = "1.0.0")]
pub fn pop(&mut self) -> Option<T> {
if self.len == 0 {
None
} else {
unsafe {
self.len -= 1;
Some(ptr::read(self.get_unchecked(self.len())))
}
}
}
2015-01-17 14:30:16 -08:00
/// Moves all the elements of `other` into `Self`, leaving `other` empty.
///
/// # Panics
///
2015-02-04 21:17:19 -05:00
/// Panics if the number of elements in the vector overflows a `usize`.
2015-01-17 14:30:16 -08:00
///
/// # Examples
///
/// ```
2015-01-17 14:30:16 -08:00
/// let mut vec = vec![1, 2, 3];
/// let mut vec2 = vec![4, 5, 6];
/// vec.append(&mut vec2);
2015-02-24 21:15:45 +03:00
/// assert_eq!(vec, [1, 2, 3, 4, 5, 6]);
/// assert_eq!(vec2, []);
2015-01-17 14:30:16 -08:00
/// ```
#[inline]
std: Stabilize/deprecate features for 1.4 The FCP is coming to a close and 1.4 is coming out soon, so this brings in the libs team decision for all library features this cycle. Stabilized APIs: * `<Box<str>>::into_string` * `Arc::downgrade` * `Arc::get_mut` * `Arc::make_mut` * `Arc::try_unwrap` * `Box::from_raw` * `Box::into_raw` * `CStr::to_str` * `CStr::to_string_lossy` * `CString::from_raw` * `CString::into_raw` * `IntoRawFd::into_raw_fd` * `IntoRawFd` * `IntoRawHandle::into_raw_handle` * `IntoRawHandle` * `IntoRawSocket::into_raw_socket` * `IntoRawSocket` * `Rc::downgrade` * `Rc::get_mut` * `Rc::make_mut` * `Rc::try_unwrap` * `Result::expect` * `String::into_boxed_slice` * `TcpSocket::read_timeout` * `TcpSocket::set_read_timeout` * `TcpSocket::set_write_timeout` * `TcpSocket::write_timeout` * `UdpSocket::read_timeout` * `UdpSocket::set_read_timeout` * `UdpSocket::set_write_timeout` * `UdpSocket::write_timeout` * `Vec::append` * `Vec::split_off` * `VecDeque::append` * `VecDeque::retain` * `VecDeque::split_off` * `rc::Weak::upgrade` * `rc::Weak` * `slice::Iter::as_slice` * `slice::IterMut::into_slice` * `str::CharIndices::as_str` * `str::Chars::as_str` * `str::split_at_mut` * `str::split_at` * `sync::Weak::upgrade` * `sync::Weak` * `thread::park_timeout` * `thread::sleep` Deprecated APIs * `BTreeMap::with_b` * `BTreeSet::with_b` * `Option::as_mut_slice` * `Option::as_slice` * `Result::as_mut_slice` * `Result::as_slice` * `f32::from_str_radix` * `f64::from_str_radix` Closes #27277 Closes #27718 Closes #27736 Closes #27764 Closes #27765 Closes #27766 Closes #27767 Closes #27768 Closes #27769 Closes #27771 Closes #27773 Closes #27775 Closes #27776 Closes #27785 Closes #27792 Closes #27795 Closes #27797
2015-09-10 13:26:44 -07:00
#[stable(feature = "append", since = "1.4.0")]
2015-01-17 14:30:16 -08:00
pub fn append(&mut self, other: &mut Self) {
self.reserve(other.len());
let len = self.len();
unsafe {
ptr::copy_nonoverlapping(
2015-01-17 14:30:16 -08:00
other.as_ptr(),
self.get_unchecked_mut(len),
2015-01-17 14:30:16 -08:00
other.len());
}
self.len += other.len();
unsafe { other.set_len(0); }
}
/// Create a draining iterator that removes the specified range in the vector
/// and yields the removed items from start to end. The element range is
/// removed even if the iterator is not consumed until the end.
///
/// Note: It is unspecified how many elements are removed from the vector,
/// if the `Drain` value is leaked.
///
/// # Panics
///
/// Panics if the starting point is greater than the end point or if
/// the end point is greater than the length of the vector.
///
/// # Examples
///
/// ```
2015-07-27 10:50:19 -04:00
/// #![feature(drain)]
///
/// // Draining using `..` clears the whole vector.
/// let mut v = vec![1, 2, 3];
/// let u: Vec<_> = v.drain(..).collect();
/// assert_eq!(v, &[]);
/// assert_eq!(u, &[1, 2, 3]);
/// ```
#[unstable(feature = "drain",
reason = "recently added, matches RFC",
issue = "27711")]
pub fn drain<R>(&mut self, range: R) -> Drain<T> where R: RangeArgument<usize> {
// Memory safety
//
// When the Drain is first created, it shortens the length of
// the source vector to make sure no uninitalized or moved-from elements
// are accessible at all if the Drain's destructor never gets to run.
//
// Drain will ptr::read out the values to remove.
// When finished, remaining tail of the vec is copied back to cover
// the hole, and the vector length is restored to the new length.
//
let len = self.len();
let start = *range.start().unwrap_or(&0);
let end = *range.end().unwrap_or(&len);
assert!(start <= end);
assert!(end <= len);
unsafe {
// set self.vec length's to start, to be safe in case Drain is leaked
self.set_len(start);
// Use the borrow in the IterMut to indicate borrowing behavior of the
// whole Drain iterator (like &mut T).
let range_slice = slice::from_raw_parts_mut(
self.as_mut_ptr().offset(start as isize),
end - start);
Drain {
tail_start: end,
tail_len: len - end,
iter: range_slice.iter_mut(),
vec: self as *mut _,
}
}
}
/// Clears the vector, removing all values.
///
/// # Examples
///
/// ```
2015-01-25 22:05:03 +01:00
/// let mut v = vec![1, 2, 3];
2014-12-16 20:12:30 -05:00
///
/// v.clear();
2014-12-16 20:12:30 -05:00
///
/// assert!(v.is_empty());
/// ```
#[inline]
2015-01-23 21:48:20 -08:00
#[stable(feature = "rust1", since = "1.0.0")]
pub fn clear(&mut self) {
self.truncate(0)
}
2014-12-16 20:12:30 -05:00
/// Returns the number of elements in the vector.
///
/// # Examples
///
/// ```
2015-01-25 22:05:03 +01:00
/// let a = vec![1, 2, 3];
/// assert_eq!(a.len(), 3);
/// ```
#[inline]
2015-01-23 21:48:20 -08:00
#[stable(feature = "rust1", since = "1.0.0")]
2015-02-04 21:17:19 -05:00
pub fn len(&self) -> usize { self.len }
2014-12-16 20:12:30 -05:00
/// Returns `true` if the vector contains no elements.
///
/// # Examples
///
/// ```
/// let mut v = Vec::new();
/// assert!(v.is_empty());
2014-12-16 20:12:30 -05:00
///
2015-01-25 22:05:03 +01:00
/// v.push(1);
/// assert!(!v.is_empty());
/// ```
2015-01-23 21:48:20 -08:00
#[stable(feature = "rust1", since = "1.0.0")]
pub fn is_empty(&self) -> bool { self.len() == 0 }
2015-01-24 17:23:26 -08:00
/// Splits the collection into two at the given index.
///
/// Returns a newly allocated `Self`. `self` contains elements `[0, at)`,
/// and the returned `Self` contains elements `[at, len)`.
///
/// Note that the capacity of `self` does not change.
///
/// # Panics
///
/// Panics if `at > len`.
///
2015-01-24 17:23:26 -08:00
/// # Examples
///
/// ```
2015-01-24 17:23:26 -08:00
/// let mut vec = vec![1,2,3];
/// let vec2 = vec.split_off(1);
2015-02-24 21:15:45 +03:00
/// assert_eq!(vec, [1]);
/// assert_eq!(vec2, [2, 3]);
2015-01-24 17:23:26 -08:00
/// ```
#[inline]
std: Stabilize/deprecate features for 1.4 The FCP is coming to a close and 1.4 is coming out soon, so this brings in the libs team decision for all library features this cycle. Stabilized APIs: * `<Box<str>>::into_string` * `Arc::downgrade` * `Arc::get_mut` * `Arc::make_mut` * `Arc::try_unwrap` * `Box::from_raw` * `Box::into_raw` * `CStr::to_str` * `CStr::to_string_lossy` * `CString::from_raw` * `CString::into_raw` * `IntoRawFd::into_raw_fd` * `IntoRawFd` * `IntoRawHandle::into_raw_handle` * `IntoRawHandle` * `IntoRawSocket::into_raw_socket` * `IntoRawSocket` * `Rc::downgrade` * `Rc::get_mut` * `Rc::make_mut` * `Rc::try_unwrap` * `Result::expect` * `String::into_boxed_slice` * `TcpSocket::read_timeout` * `TcpSocket::set_read_timeout` * `TcpSocket::set_write_timeout` * `TcpSocket::write_timeout` * `UdpSocket::read_timeout` * `UdpSocket::set_read_timeout` * `UdpSocket::set_write_timeout` * `UdpSocket::write_timeout` * `Vec::append` * `Vec::split_off` * `VecDeque::append` * `VecDeque::retain` * `VecDeque::split_off` * `rc::Weak::upgrade` * `rc::Weak` * `slice::Iter::as_slice` * `slice::IterMut::into_slice` * `str::CharIndices::as_str` * `str::Chars::as_str` * `str::split_at_mut` * `str::split_at` * `sync::Weak::upgrade` * `sync::Weak` * `thread::park_timeout` * `thread::sleep` Deprecated APIs * `BTreeMap::with_b` * `BTreeSet::with_b` * `Option::as_mut_slice` * `Option::as_slice` * `Result::as_mut_slice` * `Result::as_slice` * `f32::from_str_radix` * `f64::from_str_radix` Closes #27277 Closes #27718 Closes #27736 Closes #27764 Closes #27765 Closes #27766 Closes #27767 Closes #27768 Closes #27769 Closes #27771 Closes #27773 Closes #27775 Closes #27776 Closes #27785 Closes #27792 Closes #27795 Closes #27797
2015-09-10 13:26:44 -07:00
#[stable(feature = "split_off", since = "1.4.0")]
2015-01-24 17:23:26 -08:00
pub fn split_off(&mut self, at: usize) -> Self {
assert!(at <= self.len(), "`at` out of bounds");
2015-01-24 17:23:26 -08:00
let other_len = self.len - at;
let mut other = Vec::with_capacity(other_len);
// Unsafely `set_len` and copy items to `other`.
unsafe {
self.set_len(at);
other.set_len(other_len);
ptr::copy_nonoverlapping(
2015-01-24 17:23:26 -08:00
self.as_ptr().offset(at as isize),
other.as_mut_ptr(),
2015-01-24 17:23:26 -08:00
other.len());
}
other
}
}
2014-04-02 23:10:36 -04:00
impl<T: Clone> Vec<T> {
/// Resizes the `Vec` in-place so that `len()` is equal to `new_len`.
///
/// Calls either `extend()` or `truncate()` depending on whether `new_len`
/// is larger than the current value of `len()` or not.
///
/// # Examples
///
/// ```
2015-07-27 10:50:19 -04:00
/// #![feature(vec_resize)]
///
/// let mut vec = vec!["hello"];
/// vec.resize(3, "world");
2015-02-24 21:15:45 +03:00
/// assert_eq!(vec, ["hello", "world", "world"]);
///
2015-01-25 22:05:03 +01:00
/// let mut vec = vec![1, 2, 3, 4];
/// vec.resize(2, 0);
2015-02-24 21:15:45 +03:00
/// assert_eq!(vec, [1, 2]);
/// ```
#[unstable(feature = "vec_resize",
reason = "matches collection reform specification; waiting for dust to settle",
issue = "27790")]
2015-02-04 21:17:19 -05:00
pub fn resize(&mut self, new_len: usize, value: T) {
let len = self.len();
if new_len > len {
self.extend_with_element(new_len - len, value);
} else {
self.truncate(new_len);
}
}
/// Extend the vector by `n` additional clones of `value`.
fn extend_with_element(&mut self, n: usize, value: T) {
self.reserve(n);
unsafe {
let len = self.len();
let mut ptr = self.as_mut_ptr().offset(len as isize);
// Write all elements except the last one
for i in 1..n {
ptr::write(ptr, value.clone());
ptr = ptr.offset(1);
// Increment the length in every step in case clone() panics
self.set_len(len + i);
}
if n > 0 {
// We can write the last element directly without cloning needlessly
ptr::write(ptr, value);
self.set_len(len + n);
}
}
}
/// Appends all elements in a slice to the `Vec`.
2014-12-16 20:12:30 -05:00
///
/// Iterates over the slice `other`, clones each element, and then appends
/// it to this `Vec`. The `other` vector is traversed in-order.
///
/// # Examples
///
/// ```
2015-07-27 10:50:19 -04:00
/// #![feature(vec_push_all)]
///
2015-01-25 22:05:03 +01:00
/// let mut vec = vec![1];
/// vec.push_all(&[2, 3, 4]);
2015-02-24 21:15:45 +03:00
/// assert_eq!(vec, [1, 2, 3, 4]);
/// ```
#[inline]
#[unstable(feature = "vec_push_all",
reason = "likely to be replaced by a more optimized extend",
issue = "27744")]
pub fn push_all(&mut self, other: &[T]) {
self.reserve(other.len());
for i in 0..other.len() {
let len = self.len();
// Unsafe code so this can be optimised to a memcpy (or something
// similarly fast) when T is Copy. LLVM is easily confused, so any
// extra operations during the loop can prevent this optimisation.
unsafe {
ptr::write(
self.get_unchecked_mut(len),
other.get_unchecked(i).clone());
self.set_len(len + 1);
}
}
}
}
2014-08-04 22:48:39 +12:00
impl<T: PartialEq> Vec<T> {
/// Removes consecutive repeated elements in the vector.
///
/// If the vector is sorted, this removes all duplicates.
///
/// # Examples
///
/// ```
2015-01-25 22:05:03 +01:00
/// let mut vec = vec![1, 2, 2, 3, 2];
2014-12-16 20:12:30 -05:00
///
/// vec.dedup();
2014-12-16 20:12:30 -05:00
///
2015-02-24 21:15:45 +03:00
/// assert_eq!(vec, [1, 2, 3, 2]);
/// ```
2015-01-23 21:48:20 -08:00
#[stable(feature = "rust1", since = "1.0.0")]
pub fn dedup(&mut self) {
unsafe {
// Although we have a mutable reference to `self`, we cannot make
// *arbitrary* changes. The `PartialEq` comparisons could panic, so we
// must ensure that the vector is in a valid state at all time.
//
// The way that we handle this is by using swaps; we iterate
// over all the elements, swapping as we go so that at the end
// the elements we wish to keep are in the front, and those we
// wish to reject are at the back. We can then truncate the
// vector. This operation is still O(n).
//
// Example: We start in this state, where `r` represents "next
// read" and `w` represents "next_write`.
//
// r
// +---+---+---+---+---+---+
// | 0 | 1 | 1 | 2 | 3 | 3 |
// +---+---+---+---+---+---+
// w
//
2014-04-28 15:39:11 +02:00
// Comparing self[r] against self[w-1], this is not a duplicate, so
// we swap self[r] and self[w] (no effect as r==w) and then increment both
// r and w, leaving us with:
//
// r
// +---+---+---+---+---+---+
// | 0 | 1 | 1 | 2 | 3 | 3 |
// +---+---+---+---+---+---+
// w
//
// Comparing self[r] against self[w-1], this value is a duplicate,
// so we increment `r` but leave everything else unchanged:
//
// r
// +---+---+---+---+---+---+
// | 0 | 1 | 1 | 2 | 3 | 3 |
// +---+---+---+---+---+---+
// w
//
// Comparing self[r] against self[w-1], this is not a duplicate,
// so swap self[r] and self[w] and advance r and w:
//
// r
// +---+---+---+---+---+---+
// | 0 | 1 | 2 | 1 | 3 | 3 |
// +---+---+---+---+---+---+
// w
//
// Not a duplicate, repeat:
//
// r
// +---+---+---+---+---+---+
// | 0 | 1 | 2 | 3 | 1 | 3 |
// +---+---+---+---+---+---+
// w
//
// Duplicate, advance r. End of vec. Truncate to w.
let ln = self.len();
if ln <= 1 { return; }
// Avoid bounds checks by using raw pointers.
let p = self.as_mut_ptr();
Add trivial cast lints. This permits all coercions to be performed in casts, but adds lints to warn in those cases. Part of this patch moves cast checking to a later stage of type checking. We acquire obligations to check casts as part of type checking where we previously checked them. Once we have type checked a function or module, then we check any cast obligations which have been acquired. That means we have more type information available to check casts (this was crucial to making coercions work properly in place of some casts), but it means that casts cannot feed input into type inference. [breaking change] * Adds two new lints for trivial casts and trivial numeric casts, these are warn by default, but can cause errors if you build with warnings as errors. Previously, trivial numeric casts and casts to trait objects were allowed. * The unused casts lint has gone. * Interactions between casting and type inference have changed in subtle ways. Two ways this might manifest are: - You may need to 'direct' casts more with extra type information, for example, in some cases where `foo as _ as T` succeeded, you may now need to specify the type for `_` - Casts do not influence inference of integer types. E.g., the following used to type check: ``` let x = 42; let y = &x as *const u32; ``` Because the cast would inform inference that `x` must have type `u32`. This no longer applies and the compiler will fallback to `i32` for `x` and thus there will be a type error in the cast. The solution is to add more type information: ``` let x: u32 = 42; let y = &x as *const u32; ```
2015-03-20 17:15:27 +13:00
let mut r: usize = 1;
let mut w: usize = 1;
while r < ln {
let p_r = p.offset(r as isize);
let p_wm1 = p.offset((w - 1) as isize);
if *p_r != *p_wm1 {
if r != w {
let p_w = p_wm1.offset(1);
mem::swap(&mut *p_r, &mut *p_w);
}
w += 1;
}
r += 1;
}
self.truncate(w);
}
}
}
////////////////////////////////////////////////////////////////////////////////
// Internal methods and functions
////////////////////////////////////////////////////////////////////////////////
#[doc(hidden)]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn from_elem<T: Clone>(elem: T, n: usize) -> Vec<T> {
let mut v = Vec::with_capacity(n);
v.extend_with_element(n, elem);
v
}
////////////////////////////////////////////////////////////////////////////////
// Common trait implementations for Vec
////////////////////////////////////////////////////////////////////////////////
#[stable(feature = "rust1", since = "1.0.0")]
impl<T:Clone> Clone for Vec<T> {
2015-03-11 19:44:02 -05:00
#[cfg(not(test))]
2015-03-10 23:13:29 -05:00
fn clone(&self) -> Vec<T> { <[T]>::to_vec(&**self) }
2015-03-18 09:36:18 -07:00
// HACK(japaric): with cfg(test) the inherent `[T]::to_vec` method, which is
// required for this method definition, is not available. Instead use the
// `slice::to_vec` function which is only available with cfg(test)
// NB see the slice::hack module in slice.rs for more information
2015-03-11 19:44:02 -05:00
#[cfg(test)]
fn clone(&self) -> Vec<T> {
::slice::to_vec(&**self)
}
2015-03-11 19:44:02 -05:00
fn clone_from(&mut self, other: &Vec<T>) {
// drop anything in self that will not be overwritten
Optimize `Vec::clone_from` Before: test dst_bigger::src_100_dst_1000::clone ... bench: 34 ns/iter (+/- 1) test dst_bigger::src_100_dst_1000::clone_from ... bench: 75 ns/iter (+/- 3) test dst_bigger::src_10_dst_100::clone ... bench: 25 ns/iter (+/- 0) test dst_bigger::src_10_dst_100::clone_from ... bench: 9 ns/iter (+/- 1) test eq::src_1000_dst_1000::clone ... bench: 105 ns/iter (+/- 2) test eq::src_1000_dst_1000::clone_from ... bench: 593 ns/iter (+/- 21) test eq::src_100_dst_100::clone ... bench: 34 ns/iter (+/- 1) test eq::src_100_dst_100::clone_from ... bench: 75 ns/iter (+/- 1) test src_bigger::src_1000_dst_100::clone ... bench: 103 ns/iter (+/- 5) test src_bigger::src_1000_dst_100::clone_from ... bench: 148 ns/iter (+/- 5) test src_bigger::src_100_dst_10::clone ... bench: 34 ns/iter (+/- 1) test src_bigger::src_100_dst_10::clone_from ... bench: 20 ns/iter (+/- 0) After: test dst_bigger::src_100_dst_1000::clone ... bench: 34 ns/iter (+/- 2) test dst_bigger::src_100_dst_1000::clone_from ... bench: 15 ns/iter (+/- 1) test dst_bigger::src_10_dst_100::clone ... bench: 26 ns/iter (+/- 1) test dst_bigger::src_10_dst_100::clone_from ... bench: 7 ns/iter (+/- 0) test eq::src_1000_dst_1000::clone ... bench: 103 ns/iter (+/- 1) test eq::src_1000_dst_1000::clone_from ... bench: 85 ns/iter (+/- 4) test eq::src_100_dst_100::clone ... bench: 34 ns/iter (+/- 2) test eq::src_100_dst_100::clone_from ... bench: 15 ns/iter (+/- 1) test src_bigger::src_1000_dst_100::clone ... bench: 103 ns/iter (+/- 4) test src_bigger::src_1000_dst_100::clone_from ... bench: 90 ns/iter (+/- 2) test src_bigger::src_100_dst_10::clone ... bench: 34 ns/iter (+/- 2) test src_bigger::src_100_dst_10::clone_from ... bench: 20 ns/iter (+/- 0) Closes #28601.
2015-09-23 10:33:36 -04:00
self.truncate(other.len());
let len = self.len();
// reuse the contained values' allocations/resources.
Optimize `Vec::clone_from` Before: test dst_bigger::src_100_dst_1000::clone ... bench: 34 ns/iter (+/- 1) test dst_bigger::src_100_dst_1000::clone_from ... bench: 75 ns/iter (+/- 3) test dst_bigger::src_10_dst_100::clone ... bench: 25 ns/iter (+/- 0) test dst_bigger::src_10_dst_100::clone_from ... bench: 9 ns/iter (+/- 1) test eq::src_1000_dst_1000::clone ... bench: 105 ns/iter (+/- 2) test eq::src_1000_dst_1000::clone_from ... bench: 593 ns/iter (+/- 21) test eq::src_100_dst_100::clone ... bench: 34 ns/iter (+/- 1) test eq::src_100_dst_100::clone_from ... bench: 75 ns/iter (+/- 1) test src_bigger::src_1000_dst_100::clone ... bench: 103 ns/iter (+/- 5) test src_bigger::src_1000_dst_100::clone_from ... bench: 148 ns/iter (+/- 5) test src_bigger::src_100_dst_10::clone ... bench: 34 ns/iter (+/- 1) test src_bigger::src_100_dst_10::clone_from ... bench: 20 ns/iter (+/- 0) After: test dst_bigger::src_100_dst_1000::clone ... bench: 34 ns/iter (+/- 2) test dst_bigger::src_100_dst_1000::clone_from ... bench: 15 ns/iter (+/- 1) test dst_bigger::src_10_dst_100::clone ... bench: 26 ns/iter (+/- 1) test dst_bigger::src_10_dst_100::clone_from ... bench: 7 ns/iter (+/- 0) test eq::src_1000_dst_1000::clone ... bench: 103 ns/iter (+/- 1) test eq::src_1000_dst_1000::clone_from ... bench: 85 ns/iter (+/- 4) test eq::src_100_dst_100::clone ... bench: 34 ns/iter (+/- 2) test eq::src_100_dst_100::clone_from ... bench: 15 ns/iter (+/- 1) test src_bigger::src_1000_dst_100::clone ... bench: 103 ns/iter (+/- 4) test src_bigger::src_1000_dst_100::clone_from ... bench: 90 ns/iter (+/- 2) test src_bigger::src_100_dst_10::clone ... bench: 34 ns/iter (+/- 2) test src_bigger::src_100_dst_10::clone_from ... bench: 20 ns/iter (+/- 0) Closes #28601.
2015-09-23 10:33:36 -04:00
self.clone_from_slice(&other[..len]);
// self.len <= other.len due to the truncate above, so the
// slice here is always in-bounds.
Optimize `Vec::clone_from` Before: test dst_bigger::src_100_dst_1000::clone ... bench: 34 ns/iter (+/- 1) test dst_bigger::src_100_dst_1000::clone_from ... bench: 75 ns/iter (+/- 3) test dst_bigger::src_10_dst_100::clone ... bench: 25 ns/iter (+/- 0) test dst_bigger::src_10_dst_100::clone_from ... bench: 9 ns/iter (+/- 1) test eq::src_1000_dst_1000::clone ... bench: 105 ns/iter (+/- 2) test eq::src_1000_dst_1000::clone_from ... bench: 593 ns/iter (+/- 21) test eq::src_100_dst_100::clone ... bench: 34 ns/iter (+/- 1) test eq::src_100_dst_100::clone_from ... bench: 75 ns/iter (+/- 1) test src_bigger::src_1000_dst_100::clone ... bench: 103 ns/iter (+/- 5) test src_bigger::src_1000_dst_100::clone_from ... bench: 148 ns/iter (+/- 5) test src_bigger::src_100_dst_10::clone ... bench: 34 ns/iter (+/- 1) test src_bigger::src_100_dst_10::clone_from ... bench: 20 ns/iter (+/- 0) After: test dst_bigger::src_100_dst_1000::clone ... bench: 34 ns/iter (+/- 2) test dst_bigger::src_100_dst_1000::clone_from ... bench: 15 ns/iter (+/- 1) test dst_bigger::src_10_dst_100::clone ... bench: 26 ns/iter (+/- 1) test dst_bigger::src_10_dst_100::clone_from ... bench: 7 ns/iter (+/- 0) test eq::src_1000_dst_1000::clone ... bench: 103 ns/iter (+/- 1) test eq::src_1000_dst_1000::clone_from ... bench: 85 ns/iter (+/- 4) test eq::src_100_dst_100::clone ... bench: 34 ns/iter (+/- 2) test eq::src_100_dst_100::clone_from ... bench: 15 ns/iter (+/- 1) test src_bigger::src_1000_dst_100::clone ... bench: 103 ns/iter (+/- 4) test src_bigger::src_1000_dst_100::clone_from ... bench: 90 ns/iter (+/- 2) test src_bigger::src_100_dst_10::clone ... bench: 34 ns/iter (+/- 2) test src_bigger::src_100_dst_10::clone_from ... bench: 20 ns/iter (+/- 0) Closes #28601.
2015-09-23 10:33:36 -04:00
self.push_all(&other[len..]);
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T: Hash> Hash for Vec<T> {
#[inline]
fn hash<H: hash::Hasher>(&self, state: &mut H) {
Hash::hash(&**self, state)
}
}
#[stable(feature = "rust1", since = "1.0.0")]
2015-02-04 21:17:19 -05:00
impl<T> Index<usize> for Vec<T> {
2015-01-03 10:40:10 -05:00
type Output = T;
#[inline]
fn index(&self, index: usize) -> &T {
// NB built-in indexing via `&[T]`
&(**self)[index]
}
2015-01-03 10:40:10 -05:00
}
#[stable(feature = "rust1", since = "1.0.0")]
2015-02-04 21:17:19 -05:00
impl<T> IndexMut<usize> for Vec<T> {
#[inline]
fn index_mut(&mut self, index: usize) -> &mut T {
// NB built-in indexing via `&mut [T]`
&mut (**self)[index]
}
2015-01-03 10:40:10 -05:00
}
2015-01-04 17:43:24 +13:00
#[stable(feature = "rust1", since = "1.0.0")]
2015-02-04 21:17:19 -05:00
impl<T> ops::Index<ops::Range<usize>> for Vec<T> {
2015-01-04 17:43:24 +13:00
type Output = [T];
#[inline]
fn index(&self, index: ops::Range<usize>) -> &[T] {
Index::index(&**self, index)
}
2014-12-31 20:20:40 +13:00
}
#[stable(feature = "rust1", since = "1.0.0")]
2015-02-04 21:17:19 -05:00
impl<T> ops::Index<ops::RangeTo<usize>> for Vec<T> {
2015-01-04 17:43:24 +13:00
type Output = [T];
#[inline]
fn index(&self, index: ops::RangeTo<usize>) -> &[T] {
Index::index(&**self, index)
}
2014-12-31 20:20:40 +13:00
}
#[stable(feature = "rust1", since = "1.0.0")]
2015-02-04 21:17:19 -05:00
impl<T> ops::Index<ops::RangeFrom<usize>> for Vec<T> {
2015-01-04 17:43:24 +13:00
type Output = [T];
#[inline]
fn index(&self, index: ops::RangeFrom<usize>) -> &[T] {
Index::index(&**self, index)
}
}
2015-01-28 17:06:46 +13:00
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::Index<ops::RangeFull> for Vec<T> {
type Output = [T];
#[inline]
fn index(&self, _index: ops::RangeFull) -> &[T] {
self
}
2015-01-28 17:06:46 +13:00
}
#[stable(feature = "rust1", since = "1.0.0")]
2015-02-04 21:17:19 -05:00
impl<T> ops::IndexMut<ops::Range<usize>> for Vec<T> {
#[inline]
fn index_mut(&mut self, index: ops::Range<usize>) -> &mut [T] {
IndexMut::index_mut(&mut **self, index)
}
2014-12-31 20:20:40 +13:00
}
#[stable(feature = "rust1", since = "1.0.0")]
2015-02-04 21:17:19 -05:00
impl<T> ops::IndexMut<ops::RangeTo<usize>> for Vec<T> {
#[inline]
fn index_mut(&mut self, index: ops::RangeTo<usize>) -> &mut [T] {
IndexMut::index_mut(&mut **self, index)
}
2014-12-31 20:20:40 +13:00
}
#[stable(feature = "rust1", since = "1.0.0")]
2015-02-04 21:17:19 -05:00
impl<T> ops::IndexMut<ops::RangeFrom<usize>> for Vec<T> {
#[inline]
fn index_mut(&mut self, index: ops::RangeFrom<usize>) -> &mut [T] {
IndexMut::index_mut(&mut **self, index)
}
2014-12-31 20:20:40 +13:00
}
2015-01-28 17:06:46 +13:00
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::IndexMut<ops::RangeFull> for Vec<T> {
#[inline]
fn index_mut(&mut self, _index: ops::RangeFull) -> &mut [T] {
self
}
2015-01-28 17:06:46 +13:00
}
2015-01-23 21:48:20 -08:00
#[stable(feature = "rust1", since = "1.0.0")]
2015-01-01 14:53:20 -05:00
impl<T> ops::Deref for Vec<T> {
type Target = [T];
fn deref(&self) -> &[T] {
unsafe {
2015-07-09 21:57:21 -07:00
let p = self.buf.ptr();
assume(!p.is_null());
slice::from_raw_parts(p, self.len)
}
}
}
2015-01-23 21:48:20 -08:00
#[stable(feature = "rust1", since = "1.0.0")]
2015-01-01 14:53:20 -05:00
impl<T> ops::DerefMut for Vec<T> {
fn deref_mut(&mut self) -> &mut [T] {
unsafe {
2015-07-09 21:57:21 -07:00
let ptr = self.buf.ptr();
assume(!ptr.is_null());
slice::from_raw_parts_mut(ptr, self.len)
}
}
}
2015-01-23 21:48:20 -08:00
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> FromIterator<T> for Vec<T> {
#[inline]
fn from_iter<I: IntoIterator<Item=T>>(iterable: I) -> Vec<T> {
// Unroll the first iteration, as the vector is going to be
// expanded on this iteration in every case when the iterable is not
// empty, but the loop in extend_desugared() is not going to see the
// vector being full in the few subsequent loop iterations.
// So we get better branch prediction and the possibility to
// construct the vector with initial estimated capacity.
let mut iterator = iterable.into_iter();
let mut vector = match iterator.next() {
None => return Vec::new(),
Some(element) => {
let (lower, _) = iterator.size_hint();
let mut vector = Vec::with_capacity(lower.saturating_add(1));
unsafe {
ptr::write(vector.get_unchecked_mut(0), element);
vector.set_len(1);
}
vector
}
};
vector.extend_desugared(iterator);
vector
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> IntoIterator for Vec<T> {
type Item = T;
type IntoIter = IntoIter<T>;
/// Creates a consuming iterator, that is, one that moves each value out of
/// the vector (from start to end). The vector cannot be used after calling
/// this.
///
/// # Examples
///
/// ```
/// let v = vec!["a".to_string(), "b".to_string()];
/// for s in v.into_iter() {
/// // s has type String, not &String
/// println!("{}", s);
/// }
/// ```
#[inline]
2015-07-09 21:57:21 -07:00
fn into_iter(mut self) -> IntoIter<T> {
unsafe {
2015-07-09 21:57:21 -07:00
let ptr = self.as_mut_ptr();
assume(!ptr.is_null());
let begin = ptr as *const T;
let end = if mem::size_of::<T>() == 0 {
arith_offset(ptr as *const i8, self.len() as isize) as *const T
} else {
ptr.offset(self.len() as isize) as *const T
};
2015-07-09 21:57:21 -07:00
let buf = ptr::read(&self.buf);
mem::forget(self);
IntoIter { _buf: buf, ptr: begin, end: end }
}
}
}
#[stable(feature = "rust1", since = "1.0.0")]
2015-01-07 22:01:05 -05:00
impl<'a, T> IntoIterator for &'a Vec<T> {
type Item = &'a T;
type IntoIter = slice::Iter<'a, T>;
2015-01-07 22:01:05 -05:00
fn into_iter(self) -> slice::Iter<'a, T> {
self.iter()
}
}
#[stable(feature = "rust1", since = "1.0.0")]
2015-01-07 22:01:05 -05:00
impl<'a, T> IntoIterator for &'a mut Vec<T> {
type Item = &'a mut T;
type IntoIter = slice::IterMut<'a, T>;
2015-01-07 22:01:05 -05:00
fn into_iter(mut self) -> slice::IterMut<'a, T> {
self.iter_mut()
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> Extend<T> for Vec<T> {
#[inline]
fn extend<I: IntoIterator<Item=T>>(&mut self, iterable: I) {
self.extend_desugared(iterable.into_iter())
}
}
impl<T> Vec<T> {
fn extend_desugared<I: Iterator<Item=T>>(&mut self, mut iterator: I) {
// This function should be the moral equivalent of:
//
// for item in iterator {
// self.push(item);
// }
2015-02-22 23:46:36 +02:00
while let Some(element) = iterator.next() {
let len = self.len();
if len == self.capacity() {
let (lower, _) = iterator.size_hint();
self.reserve(lower.saturating_add(1));
2015-02-22 23:46:36 +02:00
}
unsafe {
ptr::write(self.get_unchecked_mut(len), element);
// NB can't overflow since we would have had to alloc the address space
2015-02-22 23:46:36 +02:00
self.set_len(len + 1);
}
}
}
}
2015-06-03 12:38:42 +02:00
#[stable(feature = "extend_ref", since = "1.2.0")]
impl<'a, T: 'a + Copy> Extend<&'a T> for Vec<T> {
fn extend<I: IntoIterator<Item=&'a T>>(&mut self, iter: I) {
self.extend(iter.into_iter().cloned());
}
}
__impl_slice_eq1! { Vec<A>, Vec<B> }
__impl_slice_eq1! { Vec<A>, &'b [B] }
__impl_slice_eq1! { Vec<A>, &'b mut [B] }
__impl_slice_eq1! { Cow<'a, [A]>, &'b [B], Clone }
__impl_slice_eq1! { Cow<'a, [A]>, &'b mut [B], Clone }
__impl_slice_eq1! { Cow<'a, [A]>, Vec<B>, Clone }
macro_rules! array_impls {
($($N: expr)+) => {
$(
// NOTE: some less important impls are omitted to reduce code bloat
__impl_slice_eq1! { Vec<A>, [B; $N] }
__impl_slice_eq1! { Vec<A>, &'b [B; $N] }
// __impl_slice_eq1! { Vec<A>, &'b mut [B; $N] }
// __impl_slice_eq1! { Cow<'a, [A]>, [B; $N], Clone }
// __impl_slice_eq1! { Cow<'a, [A]>, &'b [B; $N], Clone }
// __impl_slice_eq1! { Cow<'a, [A]>, &'b mut [B; $N], Clone }
)+
}
}
array_impls! {
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
30 31 32
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T: PartialOrd> PartialOrd for Vec<T> {
#[inline]
fn partial_cmp(&self, other: &Vec<T>) -> Option<Ordering> {
PartialOrd::partial_cmp(&**self, &**other)
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T: Eq> Eq for Vec<T> {}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T: Ord> Ord for Vec<T> {
#[inline]
fn cmp(&self, other: &Vec<T>) -> Ordering {
Ord::cmp(&**self, &**other)
}
}
2015-01-23 21:48:20 -08:00
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> Drop for Vec<T> {
fn drop(&mut self) {
2015-07-09 21:57:21 -07:00
// NOTE: this is currently abusing the fact that ZSTs can't impl Drop.
// Or rather, that impl'ing Drop makes them not zero-sized. This is
// OK because exactly when this stops being a valid assumption, we
// don't need unsafe_no_drop_flag shenanigans anymore.
if self.buf.unsafe_no_drop_flag_needs_drop() {
2015-09-20 22:15:39 +03:00
unsafe {
2015-09-20 22:19:30 +03:00
// The branch on needs_drop() is an -O1 performance optimization.
// Without the branch, dropping Vec<u8> takes linear time.
2015-09-20 22:15:39 +03:00
if needs_drop::<T>() {
for x in self.iter_mut() {
drop_in_place(x);
}
}
}
}
2015-07-09 21:57:21 -07:00
// RawVec handles deallocation
}
}
2015-01-23 21:48:20 -08:00
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> Default for Vec<T> {
2015-01-23 21:48:20 -08:00
#[stable(feature = "rust1", since = "1.0.0")]
fn default() -> Vec<T> {
Vec::new()
}
}
#[stable(feature = "rust1", since = "1.0.0")]
std: Rename Show/String to Debug/Display This commit is an implementation of [RFC 565][rfc] which is a stabilization of the `std::fmt` module and the implementations of various formatting traits. Specifically, the following changes were performed: [rfc]: https://github.com/rust-lang/rfcs/blob/master/text/0565-show-string-guidelines.md * The `Show` trait is now deprecated, it was renamed to `Debug` * The `String` trait is now deprecated, it was renamed to `Display` * Many `Debug` and `Display` implementations were audited in accordance with the RFC and audited implementations now have the `#[stable]` attribute * Integers and floats no longer print a suffix * Smart pointers no longer print details that they are a smart pointer * Paths with `Debug` are now quoted and escape characters * The `unwrap` methods on `Result` now require `Display` instead of `Debug` * The `Error` trait no longer has a `detail` method and now requires that `Display` must be implemented. With the loss of `String`, this has moved into libcore. * `impl<E: Error> FromError<E> for Box<Error>` now exists * `derive(Show)` has been renamed to `derive(Debug)`. This is not currently warned about due to warnings being emitted on stage1+ While backwards compatibility is attempted to be maintained with a blanket implementation of `Display` for the old `String` trait (and the same for `Show`/`Debug`) this is still a breaking change due to primitives no longer implementing `String` as well as modifications such as `unwrap` and the `Error` trait. Most code is fairly straightforward to update with a rename or tweaks of method calls. [breaking-change] Closes #21436
2015-01-20 15:45:07 -08:00
impl<T: fmt::Debug> fmt::Debug for Vec<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(&**self, f)
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> AsRef<Vec<T>> for Vec<T> {
fn as_ref(&self) -> &Vec<T> {
self
}
}
2015-09-25 19:54:15 +02:00
#[stable(feature = "vec_as_mut", since = "1.5.0")]
impl<T> AsMut<Vec<T>> for Vec<T> {
fn as_mut(&mut self) -> &mut Vec<T> {
self
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> AsRef<[T]> for Vec<T> {
fn as_ref(&self) -> &[T] {
self
}
}
2015-09-25 17:43:58 +02:00
#[stable(feature = "vec_as_mut", since = "1.5.0")]
impl<T> AsMut<[T]> for Vec<T> {
fn as_mut(&mut self) -> &mut [T] {
self
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T: Clone> From<&'a [T]> for Vec<T> {
#[cfg(not(test))]
fn from(s: &'a [T]) -> Vec<T> {
s.to_vec()
}
#[cfg(test)]
fn from(s: &'a [T]) -> Vec<T> {
::slice::to_vec(s)
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> From<&'a str> for Vec<u8> {
fn from(s: &'a str) -> Vec<u8> {
From::from(s.as_bytes())
}
}
////////////////////////////////////////////////////////////////////////////////
// Clone-on-write
////////////////////////////////////////////////////////////////////////////////
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T> FromIterator<T> for Cow<'a, [T]> where T: Clone {
fn from_iter<I: IntoIterator<Item=T>>(it: I) -> Cow<'a, [T]> {
Cow::Owned(FromIterator::from_iter(it))
}
}
impl<'a, T: 'a> IntoCow<'a, [T]> for Vec<T> where T: Clone {
fn into_cow(self) -> Cow<'a, [T]> {
Cow::Owned(self)
}
}
impl<'a, T> IntoCow<'a, [T]> for &'a [T] where T: Clone {
fn into_cow(self) -> Cow<'a, [T]> {
Cow::Borrowed(self)
}
}
////////////////////////////////////////////////////////////////////////////////
// Iterators
////////////////////////////////////////////////////////////////////////////////
/// An iterator that moves out of a vector.
2015-01-23 21:48:20 -08:00
#[stable(feature = "rust1", since = "1.0.0")]
pub struct IntoIter<T> {
_buf: RawVec<T>,
ptr: *const T,
end: *const T
}
unsafe impl<T: Send> Send for IntoIter<T> { }
unsafe impl<T: Sync> Sync for IntoIter<T> { }
2015-01-23 21:48:20 -08:00
#[stable(feature = "rust1", since = "1.0.0")]
2015-01-01 23:15:35 -05:00
impl<T> Iterator for IntoIter<T> {
type Item = T;
#[inline]
fn next(&mut self) -> Option<T> {
unsafe {
if self.ptr == self.end {
None
} else {
if mem::size_of::<T>() == 0 {
// purposefully don't use 'ptr.offset' because for
// vectors with 0-size elements this would return the
// same pointer.
self.ptr = arith_offset(self.ptr as *const i8, 1) as *const T;
// Use a non-null pointer value
Some(ptr::read(EMPTY as *mut T))
} else {
let old = self.ptr;
self.ptr = self.ptr.offset(1);
Some(ptr::read(old))
}
}
}
}
#[inline]
2015-02-04 21:17:19 -05:00
fn size_hint(&self) -> (usize, Option<usize>) {
let diff = (self.end as usize) - (self.ptr as usize);
let size = mem::size_of::<T>();
let exact = diff / (if size == 0 {1} else {size});
(exact, Some(exact))
}
#[inline]
fn count(self) -> usize {
self.size_hint().0
}
}
2015-01-23 21:48:20 -08:00
#[stable(feature = "rust1", since = "1.0.0")]
2015-01-01 23:15:35 -05:00
impl<T> DoubleEndedIterator for IntoIter<T> {
#[inline]
fn next_back(&mut self) -> Option<T> {
unsafe {
if self.end == self.ptr {
None
} else {
if mem::size_of::<T>() == 0 {
// See above for why 'ptr.offset' isn't used
self.end = arith_offset(self.end as *const i8, -1) as *const T;
// Use a non-null pointer value
Some(ptr::read(EMPTY as *mut T))
} else {
self.end = self.end.offset(-1);
Some(ptr::read(self.end))
}
}
}
}
}
2015-01-23 21:48:20 -08:00
#[stable(feature = "rust1", since = "1.0.0")]
2015-01-01 23:15:35 -05:00
impl<T> ExactSizeIterator for IntoIter<T> {}
2014-09-14 23:25:08 -04:00
2015-01-23 21:48:20 -08:00
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> Drop for IntoIter<T> {
fn drop(&mut self) {
// destroy the remaining elements
for _x in self {}
2015-07-09 21:57:21 -07:00
// RawVec handles deallocation
}
}
/// A draining iterator for `Vec<T>`.
#[unstable(feature = "drain", reason = "recently added", issue = "27711")]
pub struct Drain<'a, T: 'a> {
/// Index of tail to preserve
tail_start: usize,
/// Length of tail
tail_len: usize,
/// Current remaining range to remove
iter: slice::IterMut<'a, T>,
vec: *mut Vec<T>,
}
unsafe impl<'a, T: Sync> Sync for Drain<'a, T> {}
unsafe impl<'a, T: Send> Send for Drain<'a, T> {}
2015-01-23 21:48:20 -08:00
#[stable(feature = "rust1", since = "1.0.0")]
2015-01-01 23:15:35 -05:00
impl<'a, T> Iterator for Drain<'a, T> {
type Item = T;
#[inline]
fn next(&mut self) -> Option<T> {
self.iter.next().map(|elt|
unsafe {
ptr::read(elt as *const _)
}
)
}
2015-02-04 21:17:19 -05:00
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
2015-01-23 21:48:20 -08:00
#[stable(feature = "rust1", since = "1.0.0")]
2015-01-01 23:15:35 -05:00
impl<'a, T> DoubleEndedIterator for Drain<'a, T> {
#[inline]
fn next_back(&mut self) -> Option<T> {
self.iter.next_back().map(|elt|
unsafe {
ptr::read(elt as *const _)
}
)
}
}
2015-01-23 21:48:20 -08:00
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T> Drop for Drain<'a, T> {
fn drop(&mut self) {
// exhaust self first
while let Some(_) = self.next() { }
if self.tail_len > 0 {
unsafe {
let source_vec = &mut *self.vec;
// memmove back untouched tail, update to new length
let start = source_vec.len();
let tail = self.tail_start;
let src = source_vec.as_ptr().offset(tail as isize);
let dst = source_vec.as_mut_ptr().offset(start as isize);
ptr::copy(src, dst, self.tail_len);
source_vec.set_len(start + self.tail_len);
}
}
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T> ExactSizeIterator for Drain<'a, T> {}