2014-01-21 09:35:11 -05:00
|
|
|
// 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.
|
2014-04-02 16:54:22 -07:00
|
|
|
|
2015-03-23 15:54:39 -07:00
|
|
|
//! A growable list type with heap-allocated contents, written `Vec<T>` but
|
|
|
|
//! pronounced 'vector.'
|
2014-08-30 17:11:22 -04:00
|
|
|
//!
|
2015-05-22 10:26:18 -07: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
|
|
|
|
//!
|
2015-05-03 23:47:10 -05:00
|
|
|
//! You can explicitly create a `Vec<T>` with `new()`:
|
2014-12-16 20:12:30 -05:00
|
|
|
//!
|
|
|
|
//! ```
|
2015-05-13 10:44:37 -04:00
|
|
|
//! let v: Vec<i32> = Vec::new();
|
2014-12-16 20:12:30 -05:00
|
|
|
//! ```
|
|
|
|
//!
|
2015-05-03 23:47:10 -05:00
|
|
|
//! ...or by using the `vec!` macro:
|
2014-12-16 20:12:30 -05:00
|
|
|
//!
|
|
|
|
//! ```
|
2015-05-13 10:44:37 -04:00
|
|
|
//! let v: Vec<i32> = vec![];
|
2014-12-16 20:12:30 -05:00
|
|
|
//!
|
2015-05-13 10:44:37 -04:00
|
|
|
//! let v = vec![1, 2, 3, 4, 5];
|
|
|
|
//!
|
|
|
|
//! let v = vec![0; 10]; // ten zeroes
|
2014-12-16 20:12:30 -05:00
|
|
|
//! ```
|
|
|
|
//!
|
2015-08-11 17:27:05 -07: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
|
|
|
//!
|
|
|
|
//! ```
|
2015-05-13 10:44:37 -04:00
|
|
|
//! let mut v = vec![1, 2];
|
2014-12-16 20:12:30 -05:00
|
|
|
//!
|
2015-05-13 10:44:37 -04:00
|
|
|
//! v.push(3);
|
2014-12-16 20:12:30 -05:00
|
|
|
//! ```
|
|
|
|
//!
|
2015-05-03 23:47:10 -05:00
|
|
|
//! Popping values works in much the same way:
|
2014-12-16 20:12:30 -05:00
|
|
|
//!
|
|
|
|
//! ```
|
2015-05-13 10:44:37 -04:00
|
|
|
//! let mut v = vec![1, 2];
|
2014-12-16 20:12:30 -05:00
|
|
|
//!
|
2015-05-13 10:44:37 -04:00
|
|
|
//! let two = v.pop();
|
2014-12-16 20:12:30 -05:00
|
|
|
//! ```
|
2015-05-03 23:47:10 -05:00
|
|
|
//!
|
|
|
|
//! Vectors also support indexing (through the `Index` and `IndexMut` traits):
|
|
|
|
//!
|
|
|
|
//! ```
|
2015-05-13 10:44:37 -04:00
|
|
|
//! let mut v = vec![1, 2, 3];
|
|
|
|
//! let three = v[2];
|
|
|
|
//! v[1] = v[1] + 5;
|
2015-05-03 23:47:10 -05:00
|
|
|
//! ```
|
2014-01-21 09:35:11 -05:00
|
|
|
|
2015-01-23 21:48:20 -08:00
|
|
|
#![stable(feature = "rust1", since = "1.0.0")]
|
2015-01-08 00:26:21 -08:00
|
|
|
|
2015-07-09 21:57:21 -07:00
|
|
|
use alloc::raw_vec::RawVec;
|
2014-10-10 05:18:42 -04:00
|
|
|
use alloc::boxed::Box;
|
2015-07-09 21:57:21 -07:00
|
|
|
use alloc::heap::EMPTY;
|
2015-03-28 02:23:20 -07:00
|
|
|
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};
|
2015-07-09 21:57:21 -07:00
|
|
|
use core::intrinsics::{arith_offset, assume, drop_in_place};
|
2015-07-07 13:50:23 +02:00
|
|
|
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;
|
2015-05-06 10:51:10 -07:00
|
|
|
use core::ops::{Index, IndexMut, Deref};
|
2014-09-15 20:48:58 +12:00
|
|
|
use core::ops;
|
2014-12-11 22:29:24 -05:00
|
|
|
use core::ptr;
|
2015-04-21 15:59:14 -07:00
|
|
|
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
|
|
|
|
2015-02-11 23:16:32 -08:00
|
|
|
use borrow::{Cow, IntoCow};
|
|
|
|
|
2015-04-27 19:37:13 +02:00
|
|
|
use super::range::RangeArgument;
|
|
|
|
|
2014-12-16 20:12:30 -05:00
|
|
|
/// A growable list type, written `Vec<T>` but pronounced 'vector.'
|
2014-03-16 16:04:29 -07:00
|
|
|
///
|
2014-03-19 23:01:08 -07:00
|
|
|
/// # Examples
|
2014-03-16 16:04:29 -07:00
|
|
|
///
|
2014-07-22 14:02:55 +02:00
|
|
|
/// ```
|
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);
|
2014-07-22 11:07:49 +02:00
|
|
|
///
|
2015-01-25 22:05:03 +01:00
|
|
|
/// vec[0] = 7;
|
2014-07-22 11:07:49 +02:00
|
|
|
/// assert_eq!(vec[0], 7);
|
|
|
|
///
|
2015-05-19 03:33:17 +02:00
|
|
|
/// vec.extend([1, 2, 3].iter().cloned());
|
2014-07-22 11:07:49 +02:00
|
|
|
///
|
2015-05-19 03:33:17 +02:00
|
|
|
/// for x in &vec {
|
2014-07-22 11:07:49 +02:00
|
|
|
/// 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:
|
2014-03-16 16:04:29 -07:00
|
|
|
///
|
2014-07-22 14:02:55 +02:00
|
|
|
/// ```
|
2015-01-25 22:05:03 +01:00
|
|
|
/// let mut vec = vec![1, 2, 3];
|
2014-03-16 16:04:29 -07:00
|
|
|
/// vec.push(4);
|
2015-02-24 21:15:45 +03:00
|
|
|
/// assert_eq!(vec, [1, 2, 3, 4]);
|
2014-03-16 16:04:29 -07:00
|
|
|
/// ```
|
2014-07-21 20:33:20 +12:00
|
|
|
///
|
2015-07-28 23:53:25 -05:00
|
|
|
/// 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:
|
2014-07-22 11:07:49 +02:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let mut stack = Vec::new();
|
|
|
|
///
|
2015-01-25 22:05:03 +01:00
|
|
|
/// stack.push(1);
|
|
|
|
/// stack.push(2);
|
|
|
|
/// stack.push(3);
|
2014-07-22 11:07:49 +02:00
|
|
|
///
|
2015-04-26 10:10:51 -04:00
|
|
|
/// while let Some(top) = stack.pop() {
|
2014-07-22 11:07:49 +02:00
|
|
|
/// // Prints 3, 2, 1
|
|
|
|
/// println!("{}", top);
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
2014-07-21 20:33:20 +12:00
|
|
|
/// # Capacity and reallocation
|
|
|
|
///
|
2015-03-23 15:54:39 -07:00
|
|
|
/// 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.
|
2014-07-21 20:33:20 +12:00
|
|
|
///
|
2015-03-23 15:54:39 -07:00
|
|
|
/// 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.
|
2014-03-12 22:01:33 +02:00
|
|
|
#[unsafe_no_drop_flag]
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-01-21 09:35:11 -05:00
|
|
|
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,
|
2014-01-21 09:35:11 -05:00
|
|
|
}
|
|
|
|
|
2014-12-16 23:44:43 -08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Inherent methods
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2014-11-21 17:10:42 -05:00
|
|
|
|
2014-01-21 09:35:11 -05:00
|
|
|
impl<T> Vec<T> {
|
2014-12-16 20:12:30 -05:00
|
|
|
/// Constructs a new, empty `Vec<T>`.
|
2014-03-16 16:04:29 -07:00
|
|
|
///
|
|
|
|
/// The vector will not allocate until elements are pushed onto it.
|
|
|
|
///
|
2014-12-08 23:28:07 -06:00
|
|
|
/// # Examples
|
2014-03-16 16:04:29 -07:00
|
|
|
///
|
2014-07-22 14:02:55 +02:00
|
|
|
/// ```
|
2015-02-12 16:45:07 -05:00
|
|
|
/// let mut vec: Vec<i32> = Vec::new();
|
2014-03-16 16:04:29 -07:00
|
|
|
/// ```
|
2014-01-21 09:35:11 -05:00
|
|
|
#[inline]
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-01-21 09:35:11 -05:00
|
|
|
pub fn new() -> Vec<T> {
|
2015-07-09 21:57:21 -07:00
|
|
|
Vec { buf: RawVec::new(), len: 0 }
|
2014-01-21 09:35:11 -05:00
|
|
|
}
|
|
|
|
|
2014-12-16 20:12:30 -05:00
|
|
|
/// Constructs a new, empty `Vec<T>` with the specified capacity.
|
2014-03-16 16:04:29 -07:00
|
|
|
///
|
2015-08-11 17:27:05 -07:00
|
|
|
/// The vector will be able to hold exactly `capacity` elements without
|
|
|
|
/// reallocating. If `capacity` is 0, the vector will not allocate.
|
2014-03-16 16:04:29 -07:00
|
|
|
///
|
2015-08-11 17:27:05 -07:00
|
|
|
/// 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'.)
|
2014-07-21 20:33:20 +12:00
|
|
|
///
|
2014-12-08 23:28:07 -06:00
|
|
|
/// # Examples
|
2014-03-16 16:04:29 -07:00
|
|
|
///
|
2014-07-22 14:02:55 +02:00
|
|
|
/// ```
|
2015-04-03 13:23:58 -04:00
|
|
|
/// let mut vec = Vec::with_capacity(10);
|
2014-07-21 20:33:20 +12:00
|
|
|
///
|
|
|
|
/// // 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 {
|
2014-07-21 20:33:20 +12:00
|
|
|
/// vec.push(i);
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// // ...but this may make the vector reallocate
|
|
|
|
/// vec.push(11);
|
2014-03-16 16:04:29 -07:00
|
|
|
/// ```
|
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 }
|
2014-01-21 09:35:11 -05:00
|
|
|
}
|
|
|
|
|
2014-11-25 11:14:33 -05:00
|
|
|
/// Creates a `Vec<T>` directly from the raw components of another vector.
|
2014-03-20 01:52:37 +11:00
|
|
|
///
|
2015-07-02 13:00:58 -04:00
|
|
|
/// # 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).
|
2015-07-17 15:47:14 +08:00
|
|
|
/// * `length` needs to be the length that less than or equal to `capacity`.
|
2015-07-02 13:00:58 -04:00
|
|
|
/// * `capacity` needs to be the capacity that the pointer was allocated with.
|
|
|
|
///
|
|
|
|
/// Violating these may cause problems like corrupting the allocator's
|
|
|
|
/// internal datastructures.
|
2014-07-22 13:40:38 +02:00
|
|
|
///
|
2014-12-08 23:28:07 -06:00
|
|
|
/// # Examples
|
2014-07-22 13:40:38 +02:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::ptr;
|
|
|
|
/// use std::mem;
|
|
|
|
///
|
|
|
|
/// fn main() {
|
2015-01-25 22:05:03 +01:00
|
|
|
/// let mut v = vec![1, 2, 3];
|
2014-07-22 13:40:38 +02:00
|
|
|
///
|
|
|
|
/// // 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
|
2015-02-05 15:08:33 -05:00
|
|
|
/// for i in 0..len as isize {
|
2014-07-22 13:40:38 +02:00
|
|
|
/// ptr::write(p.offset(i), 4 + i);
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// // Put everything back together into a Vec
|
2014-10-27 09:11:07 -07:00
|
|
|
/// let rebuilt = Vec::from_raw_parts(p, len, cap);
|
2015-02-24 21:15:45 +03:00
|
|
|
/// assert_eq!(rebuilt, [4, 5, 6]);
|
2014-07-22 13:40:38 +02:00
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
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> {
|
2015-01-21 20:02:52 +01:00
|
|
|
Vec {
|
2015-07-09 21:57:21 -07:00
|
|
|
buf: RawVec::from_raw_parts(ptr, capacity),
|
2015-01-21 20:02:52 +01:00
|
|
|
len: length,
|
|
|
|
}
|
2014-03-20 01:52:37 +11:00
|
|
|
}
|
|
|
|
|
2014-12-16 23:44:43 -08:00
|
|
|
/// Returns the number of elements the vector can hold without
|
|
|
|
/// reallocating.
|
2014-03-16 16:04:29 -07:00
|
|
|
///
|
2014-12-08 23:28:07 -06:00
|
|
|
/// # Examples
|
2014-03-16 16:04:29 -07:00
|
|
|
///
|
2014-07-22 14:02:55 +02:00
|
|
|
/// ```
|
2015-02-12 16:45:07 -05:00
|
|
|
/// let vec: Vec<i32> = Vec::with_capacity(10);
|
2014-12-16 23:44:43 -08:00
|
|
|
/// assert_eq!(vec.capacity(), 10);
|
2014-03-16 16:04:29 -07:00
|
|
|
/// ```
|
2014-03-06 23:33:46 +01: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 capacity(&self) -> usize {
|
2015-07-09 21:57:21 -07:00
|
|
|
self.buf.cap()
|
2014-12-16 23:44:43 -08:00
|
|
|
}
|
2014-03-06 23:33:46 +01:00
|
|
|
|
2015-03-27 11:12:28 -07:00
|
|
|
/// 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.
|
2014-03-16 16:04:29 -07:00
|
|
|
///
|
2014-12-16 23:44:43 -08:00
|
|
|
/// # Panics
|
2014-03-16 16:04:29 -07:00
|
|
|
///
|
2015-02-04 21:17:19 -05:00
|
|
|
/// Panics if the new capacity overflows `usize`.
|
2014-03-16 16:04:29 -07:00
|
|
|
///
|
2014-12-08 23:28:07 -06:00
|
|
|
/// # Examples
|
|
|
|
///
|
2014-07-22 14:02:55 +02:00
|
|
|
/// ```
|
2015-02-12 16:45:07 -05:00
|
|
|
/// let mut vec = vec![1];
|
2014-12-16 23:44:43 -08:00
|
|
|
/// vec.reserve(10);
|
|
|
|
/// assert!(vec.capacity() >= 11);
|
2014-03-16 16:04:29 -07:00
|
|
|
/// ```
|
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);
|
2014-01-21 09:35:11 -05:00
|
|
|
}
|
2014-02-18 21:36:51 -08:00
|
|
|
|
2014-12-16 23:44:43 -08:00
|
|
|
/// 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
|
2014-12-16 23:44:43 -08:00
|
|
|
/// sufficient.
|
2014-03-16 16:04:29 -07:00
|
|
|
///
|
2014-12-16 23:44:43 -08:00
|
|
|
/// 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`.
|
2014-03-16 16:04:29 -07:00
|
|
|
///
|
2014-12-08 23:28:07 -06:00
|
|
|
/// # Examples
|
2014-03-16 16:04:29 -07:00
|
|
|
///
|
2014-07-22 14:02:55 +02:00
|
|
|
/// ```
|
2015-02-12 16:45:07 -05:00
|
|
|
/// let mut vec = vec![1];
|
2014-12-16 23:44:43 -08:00
|
|
|
/// vec.reserve_exact(10);
|
|
|
|
/// assert!(vec.capacity() >= 11);
|
2014-03-16 16:04:29 -07:00
|
|
|
/// ```
|
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-02-18 21:36:51 -08:00
|
|
|
}
|
2014-02-28 12:54:01 -08:00
|
|
|
|
2014-12-30 10:51:18 -08:00
|
|
|
/// Shrinks the capacity of the vector as much as possible.
|
2014-03-16 16:04:29 -07:00
|
|
|
///
|
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.
|
2014-03-16 16:04:29 -07:00
|
|
|
///
|
2014-12-08 23:28:07 -06:00
|
|
|
/// # Examples
|
2014-03-16 16:04:29 -07:00
|
|
|
///
|
2014-07-22 14:02:55 +02:00
|
|
|
/// ```
|
2015-02-12 16:45:07 -05:00
|
|
|
/// let mut vec = Vec::with_capacity(10);
|
2015-05-19 03:33:17 +02:00
|
|
|
/// vec.extend([1, 2, 3].iter().cloned());
|
2014-12-16 23:44:43 -08:00
|
|
|
/// assert_eq!(vec.capacity(), 10);
|
|
|
|
/// vec.shrink_to_fit();
|
|
|
|
/// assert!(vec.capacity() >= 3);
|
2014-03-16 16:04:29 -07:00
|
|
|
/// ```
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-16 23:44:43 -08:00
|
|
|
pub fn shrink_to_fit(&mut self) {
|
2015-07-09 21:57:21 -07:00
|
|
|
self.buf.shrink_to_fit(self.len);
|
2014-02-28 12:54:01 -08:00
|
|
|
}
|
|
|
|
|
2015-04-13 10:21:32 -04:00
|
|
|
/// Converts the vector into Box<[T]>.
|
2014-12-19 14:30:51 -06:00
|
|
|
///
|
2014-12-16 23:44:43 -08:00
|
|
|
/// 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()`.
|
2015-03-31 13:47:57 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-16 23:44:43 -08:00
|
|
|
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);
|
2014-12-16 23:44:43 -08:00
|
|
|
mem::forget(self);
|
2015-07-09 21:57:21 -07:00
|
|
|
buf.into_box()
|
2014-02-28 12:54:01 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-16 23:44:43 -08:00
|
|
|
/// Shorten a vector, dropping excess elements.
|
2014-12-19 14:30:51 -06:00
|
|
|
///
|
2014-12-16 23:44:43 -08:00
|
|
|
/// If `len` is greater than the vector's current length, this has no
|
|
|
|
/// effect.
|
2014-12-19 14:30:51 -06:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2015-01-25 22:05:03 +01:00
|
|
|
/// let mut vec = vec![1, 2, 3, 4];
|
2014-12-16 23:44:43 -08:00
|
|
|
/// vec.truncate(2);
|
2015-02-24 21:15:45 +03:00
|
|
|
/// assert_eq!(vec, [1, 2]);
|
2014-12-19 14:30:51 -06:00
|
|
|
/// ```
|
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) {
|
2014-12-16 23:44:43 -08:00
|
|
|
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));
|
|
|
|
}
|
2014-12-19 14:30:51 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-13 10:21:32 -04:00
|
|
|
/// Extracts a slice containing the entire vector.
|
2015-05-27 12:37:29 -07:00
|
|
|
///
|
|
|
|
/// Equivalent to `&s[..]`.
|
2015-03-30 15:15:27 -07:00
|
|
|
#[inline]
|
|
|
|
#[unstable(feature = "convert",
|
2015-08-12 22:19:51 -07:00
|
|
|
reason = "waiting on RFC revision",
|
|
|
|
issue = "27729")]
|
2015-03-30 15:15:27 -07:00
|
|
|
pub fn as_slice(&self) -> &[T] {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2015-05-27 12:37:29 -07:00
|
|
|
/// Extracts a mutable slice of the entire vector.
|
|
|
|
///
|
|
|
|
/// Equivalent to `&mut s[..]`.
|
2014-12-16 23:44:43 -08:00
|
|
|
#[inline]
|
2015-03-30 15:15:27 -07:00
|
|
|
#[unstable(feature = "convert",
|
2015-08-12 22:19:51 -07:00
|
|
|
reason = "waiting on RFC revision",
|
|
|
|
issue = "27729")]
|
2015-02-05 13:48:20 -05:00
|
|
|
pub fn as_mut_slice(&mut self) -> &mut [T] {
|
2015-03-26 07:29:06 -07:00
|
|
|
&mut self[..]
|
2014-02-27 23:49:25 -08:00
|
|
|
}
|
2014-04-03 22:28:45 +11:00
|
|
|
|
2014-12-16 23:44:43 -08:00
|
|
|
/// Sets the length of a vector.
|
|
|
|
///
|
2014-12-30 18:51:51 -08:00
|
|
|
/// 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.
|
2014-12-16 23:44:43 -08:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2015-02-05 15:08:33 -05:00
|
|
|
/// let mut v = vec![1, 2, 3, 4];
|
2014-12-16 23:44:43 -08:00
|
|
|
/// 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) {
|
2014-12-16 23:44:43 -08:00
|
|
|
self.len = len;
|
2014-07-14 11:03:23 +12:00
|
|
|
}
|
|
|
|
|
2014-12-30 18:51:51 -08: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).
|
2014-03-16 16:04:29 -07:00
|
|
|
///
|
2014-12-16 23:44:43 -08:00
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Panics if `index` is out of bounds.
|
2014-03-16 16:04:29 -07:00
|
|
|
///
|
2014-12-08 23:28:07 -06:00
|
|
|
/// # Examples
|
2014-03-16 16:04:29 -07:00
|
|
|
///
|
2014-07-22 14:02:55 +02:00
|
|
|
/// ```
|
2014-11-07 17:34:32 +05:30
|
|
|
/// let mut v = vec!["foo", "bar", "baz", "qux"];
|
2014-03-16 16:04:29 -07:00
|
|
|
///
|
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-03-16 16:04:29 -07:00
|
|
|
///
|
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"]);
|
2014-03-16 16:04:29 -07:00
|
|
|
/// ```
|
2014-01-21 09:35:11 -05: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 swap_remove(&mut self, index: usize) -> T {
|
2014-02-18 21:36:51 -08:00
|
|
|
let length = self.len();
|
2014-12-16 23:44:43 -08:00
|
|
|
self.swap(index, length - 1);
|
|
|
|
self.pop().unwrap()
|
2014-03-04 07:46:24 +11:00
|
|
|
}
|
|
|
|
|
2014-12-30 18:51:51 -08:00
|
|
|
/// Inserts an element at position `index` within the vector, shifting all
|
|
|
|
/// elements after position `i` one position to the right.
|
2014-03-16 16:04:29 -07:00
|
|
|
///
|
2014-11-06 12:24:47 -05:00
|
|
|
/// # Panics
|
2014-03-16 16:04:29 -07:00
|
|
|
///
|
2015-04-10 11:33:21 -04:00
|
|
|
/// Panics if `index` is greater than the vector's length.
|
2014-03-16 16:04:29 -07:00
|
|
|
///
|
2014-12-08 23:28:07 -06:00
|
|
|
/// # Examples
|
2014-03-16 16:04:29 -07:00
|
|
|
///
|
2014-07-22 14:02:55 +02:00
|
|
|
/// ```
|
2015-01-25 22:05:03 +01:00
|
|
|
/// let mut vec = vec![1, 2, 3];
|
2014-03-16 16:04:29 -07:00
|
|
|
/// vec.insert(1, 4);
|
2015-02-24 21:15:45 +03:00
|
|
|
/// assert_eq!(vec, [1, 4, 2, 3]);
|
2014-06-27 10:51:56 +02:00
|
|
|
/// vec.insert(4, 5);
|
2015-02-24 21:15:45 +03:00
|
|
|
/// assert_eq!(vec, [1, 4, 2, 3, 5]);
|
2014-03-16 16:04:29 -07:00
|
|
|
/// ```
|
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) {
|
2014-02-18 21:36:51 -08:00
|
|
|
let len = self.len();
|
|
|
|
assert!(index <= len);
|
2015-07-09 21:57:21 -07:00
|
|
|
|
2014-02-18 21:36:51 -08:00
|
|
|
// space for the new element
|
2015-07-09 21:57:21 -07:00
|
|
|
if len == self.buf.cap() { self.buf.double(); }
|
2014-02-18 21:36:51 -08:00
|
|
|
|
|
|
|
unsafe { // infallible
|
|
|
|
// The spot to put the new value
|
|
|
|
{
|
2015-02-05 15:08:33 -05:00
|
|
|
let p = self.as_mut_ptr().offset(index as isize);
|
2014-02-18 21:36:51 -08:00
|
|
|
// 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);
|
2014-02-18 21:36:51 -08:00
|
|
|
// Write it in, overwriting the first copy of the `index`th
|
|
|
|
// element.
|
2015-07-09 21:57:21 -07:00
|
|
|
ptr::write(p, element);
|
2014-02-18 21:36:51 -08:00
|
|
|
}
|
|
|
|
self.set_len(len + 1);
|
2014-02-23 10:59:23 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-16 23:44:43 -08:00
|
|
|
/// Removes and returns the element at position `index` within the vector,
|
|
|
|
/// shifting all elements after position `index` one position to the left.
|
2014-11-06 12:24:47 -05:00
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
2015-04-27 16:11:46 +02:00
|
|
|
/// Panics if `index` is out of bounds.
|
2014-03-16 16:04:29 -07:00
|
|
|
///
|
2014-12-08 23:28:07 -06:00
|
|
|
/// # Examples
|
2014-03-16 16:04:29 -07:00
|
|
|
///
|
2014-07-22 14:02:55 +02:00
|
|
|
/// ```
|
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]);
|
2014-03-16 16:04:29 -07:00
|
|
|
/// ```
|
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();
|
2014-12-16 23:44:43 -08:00
|
|
|
assert!(index < len);
|
|
|
|
unsafe { // infallible
|
|
|
|
let ret;
|
|
|
|
{
|
|
|
|
// the place we are taking from.
|
2015-02-05 15:08:33 -05:00
|
|
|
let ptr = self.as_mut_ptr().offset(index as isize);
|
2014-12-16 23:44:43 -08:00
|
|
|
// copy it out, unsafely having a copy of the value on
|
|
|
|
// the stack and in the vector at the same time.
|
2015-01-17 07:34:10 +03:00
|
|
|
ret = ptr::read(ptr);
|
2014-12-16 23:44:43 -08:00
|
|
|
|
|
|
|
// 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);
|
2014-01-21 09:35:11 -05:00
|
|
|
}
|
2014-12-16 23:44:43 -08:00
|
|
|
self.set_len(len - 1);
|
|
|
|
ret
|
2014-01-21 09:35:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-02 23:10:36 -04:00
|
|
|
/// Retains only the elements specified by the predicate.
|
2014-12-16 20:12:30 -05:00
|
|
|
///
|
2014-12-30 18:51:51 -08: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.
|
2014-03-16 16:04:29 -07:00
|
|
|
///
|
2014-12-08 23:28:07 -06:00
|
|
|
/// # Examples
|
2014-03-16 16:04:29 -07:00
|
|
|
///
|
2014-07-22 14:02:55 +02:00
|
|
|
/// ```
|
2015-01-25 22:05:03 +01:00
|
|
|
/// let mut vec = vec![1, 2, 3, 4];
|
2014-10-31 05:40:15 -04:00
|
|
|
/// vec.retain(|&x| x%2 == 0);
|
2015-02-24 21:15:45 +03:00
|
|
|
/// assert_eq!(vec, [2, 4]);
|
2014-03-16 16:04:29 -07:00
|
|
|
/// ```
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-05 16:54:09 -05:00
|
|
|
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
|
|
|
{
|
2015-02-01 21:53:25 -05: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);
|
2014-01-21 09:35:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-30 13:43:24 -07:00
|
|
|
/// Appends an element to the back of a collection.
|
2014-03-16 16:04:29 -07:00
|
|
|
///
|
2014-11-12 03:36:09 +09:00
|
|
|
/// # Panics
|
2014-10-30 13:43:24 -07:00
|
|
|
///
|
2015-02-04 21:17:19 -05:00
|
|
|
/// Panics if the number of elements in the vector overflows a `usize`.
|
2014-03-16 16:04:29 -07:00
|
|
|
///
|
2014-12-08 23:28:07 -06:00
|
|
|
/// # Examples
|
2014-03-16 16:04:29 -07:00
|
|
|
///
|
2015-03-12 22:42:38 -04:00
|
|
|
/// ```
|
2015-07-28 23:55:12 -05:00
|
|
|
/// let mut vec = vec![1, 2];
|
2014-10-30 13:43:24 -07:00
|
|
|
/// vec.push(3);
|
2015-02-24 21:15:45 +03:00
|
|
|
/// assert_eq!(vec, [1, 2, 3]);
|
2014-07-22 14:02:55 +02:00
|
|
|
/// ```
|
2014-10-30 13:43:24 -07:00
|
|
|
#[inline]
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-10-30 13:43:24 -07:00
|
|
|
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(); }
|
2014-10-30 13:43:24 -07:00
|
|
|
unsafe {
|
2015-07-09 21:57:21 -07:00
|
|
|
let end = self.as_mut_ptr().offset(self.len as isize);
|
|
|
|
ptr::write(end, value);
|
2014-10-30 13:43:24 -07:00
|
|
|
self.len += 1;
|
2014-01-21 09:35:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-11 17:27:05 -07:00
|
|
|
/// Removes the last element from a vector and returns it, or `None` if it
|
|
|
|
/// is empty.
|
2014-03-16 16:04:29 -07:00
|
|
|
///
|
2014-12-08 23:28:07 -06:00
|
|
|
/// # Examples
|
2014-03-16 16:04:29 -07:00
|
|
|
///
|
2015-03-12 22:42:38 -04:00
|
|
|
/// ```
|
2015-01-25 22:05:03 +01:00
|
|
|
/// let mut vec = vec![1, 2, 3];
|
2014-10-30 13:43:24 -07:00
|
|
|
/// assert_eq!(vec.pop(), Some(3));
|
2015-02-24 21:15:45 +03:00
|
|
|
/// assert_eq!(vec, [1, 2]);
|
2014-03-16 16:04:29 -07:00
|
|
|
/// ```
|
2014-01-21 09:35:11 -05:00
|
|
|
#[inline]
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-10-30 13:43:24 -07:00
|
|
|
pub fn pop(&mut self) -> Option<T> {
|
|
|
|
if self.len == 0 {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
unsafe {
|
|
|
|
self.len -= 1;
|
2014-12-16 23:44:43 -08:00
|
|
|
Some(ptr::read(self.get_unchecked(self.len())))
|
2014-10-30 13:43:24 -07:00
|
|
|
}
|
2014-04-17 15:28:14 -07:00
|
|
|
}
|
2014-01-21 09:35:11 -05:00
|
|
|
}
|
|
|
|
|
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-02-07 14:58:58 -05:00
|
|
|
///
|
|
|
|
/// ```
|
2015-07-27 10:50:19 -04:00
|
|
|
/// #![feature(append)]
|
|
|
|
///
|
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]
|
2015-06-09 14:39:23 -07:00
|
|
|
#[unstable(feature = "append",
|
2015-08-12 22:19:51 -07:00
|
|
|
reason = "new API, waiting for dust to settle",
|
|
|
|
issue = "27765")]
|
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 {
|
2015-02-23 11:39:16 -08:00
|
|
|
ptr::copy_nonoverlapping(
|
2015-01-17 14:30:16 -08:00
|
|
|
other.as_ptr(),
|
2015-03-27 11:12:28 -07:00
|
|
|
self.get_unchecked_mut(len),
|
2015-01-17 14:30:16 -08:00
|
|
|
other.len());
|
|
|
|
}
|
|
|
|
|
|
|
|
self.len += other.len();
|
|
|
|
unsafe { other.set_len(0); }
|
|
|
|
}
|
|
|
|
|
2015-04-27 19:37:13 +02:00
|
|
|
/// 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.
|
2014-03-16 16:04:29 -07:00
|
|
|
///
|
2014-12-08 23:28:07 -06:00
|
|
|
/// # Examples
|
2014-03-16 16:04:29 -07:00
|
|
|
///
|
2014-07-22 14:02:55 +02:00
|
|
|
/// ```
|
2015-07-27 10:50:19 -04:00
|
|
|
/// #![feature(drain)]
|
2015-04-27 19:37:13 +02:00
|
|
|
///
|
|
|
|
/// // 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]);
|
2014-03-16 16:04:29 -07:00
|
|
|
/// ```
|
2015-06-09 14:39:23 -07:00
|
|
|
#[unstable(feature = "drain",
|
2015-08-12 22:19:51 -07:00
|
|
|
reason = "recently added, matches RFC",
|
|
|
|
issue = "27711")]
|
2015-04-27 19:37:13 +02:00
|
|
|
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);
|
|
|
|
|
2014-01-21 09:35:11 -05:00
|
|
|
unsafe {
|
2015-04-27 19:37:13 +02:00
|
|
|
// 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);
|
2014-12-16 17:45:03 -05:00
|
|
|
Drain {
|
2015-04-27 19:37:13 +02:00
|
|
|
tail_start: end,
|
|
|
|
tail_len: len - end,
|
|
|
|
iter: range_slice.iter_mut(),
|
|
|
|
vec: self as *mut _,
|
2014-12-16 17:45:03 -05:00
|
|
|
}
|
2014-01-21 09:35:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-30 13:43:24 -07:00
|
|
|
/// Clears the vector, removing all values.
|
2014-07-22 11:07:49 +02:00
|
|
|
///
|
2014-12-08 23:28:07 -06:00
|
|
|
/// # Examples
|
2014-07-22 11:07:49 +02:00
|
|
|
///
|
|
|
|
/// ```
|
2015-01-25 22:05:03 +01:00
|
|
|
/// let mut v = vec![1, 2, 3];
|
2014-12-16 20:12:30 -05:00
|
|
|
///
|
2014-10-30 13:43:24 -07:00
|
|
|
/// v.clear();
|
2014-12-16 20:12:30 -05:00
|
|
|
///
|
2014-10-30 13:43:24 -07:00
|
|
|
/// assert!(v.is_empty());
|
2014-07-22 11:07:49 +02:00
|
|
|
/// ```
|
2014-01-21 09:35:11 -05:00
|
|
|
#[inline]
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-10-30 13:43:24 -07:00
|
|
|
pub fn clear(&mut self) {
|
2014-03-04 10:39:49 -08:00
|
|
|
self.truncate(0)
|
2014-01-21 09:35:11 -05:00
|
|
|
}
|
2014-02-18 21:36:51 -08:00
|
|
|
|
2014-12-16 20:12:30 -05:00
|
|
|
/// Returns the number of elements in the vector.
|
2014-03-16 16:04:29 -07:00
|
|
|
///
|
2014-12-08 23:28:07 -06:00
|
|
|
/// # Examples
|
|
|
|
///
|
2014-07-22 14:02:55 +02:00
|
|
|
/// ```
|
2015-01-25 22:05:03 +01:00
|
|
|
/// let a = vec![1, 2, 3];
|
2014-10-30 13:43:24 -07:00
|
|
|
/// assert_eq!(a.len(), 3);
|
2014-03-16 16:04:29 -07:00
|
|
|
/// ```
|
2014-02-18 21:36:51 -08: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 len(&self) -> usize { self.len }
|
2014-02-18 21:36:51 -08:00
|
|
|
|
2014-12-16 20:12:30 -05:00
|
|
|
/// Returns `true` if the vector contains no elements.
|
2014-03-16 16:04:29 -07:00
|
|
|
///
|
2014-12-08 23:28:07 -06:00
|
|
|
/// # Examples
|
2014-03-16 16:04:29 -07:00
|
|
|
///
|
2014-07-22 14:02:55 +02:00
|
|
|
/// ```
|
2014-10-30 13:43:24 -07:00
|
|
|
/// 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);
|
2014-10-30 13:43:24 -07:00
|
|
|
/// assert!(!v.is_empty());
|
2014-03-16 16:04:29 -07:00
|
|
|
/// ```
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-10-30 13:43:24 -07:00
|
|
|
pub fn is_empty(&self) -> bool { self.len() == 0 }
|
2014-02-18 21:36:51 -08:00
|
|
|
|
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.
|
|
|
|
///
|
2015-02-07 14:58:58 -05:00
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Panics if `at > len`.
|
|
|
|
///
|
2015-01-24 17:23:26 -08:00
|
|
|
/// # Examples
|
2015-02-07 14:58:58 -05:00
|
|
|
///
|
|
|
|
/// ```
|
2015-07-27 10:50:19 -04:00
|
|
|
/// #![feature(split_off)]
|
|
|
|
///
|
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]
|
2015-06-09 14:39:23 -07:00
|
|
|
#[unstable(feature = "split_off",
|
2015-08-12 22:19:51 -07:00
|
|
|
reason = "new API, waiting for dust to settle",
|
|
|
|
issue = "27766")]
|
2015-01-24 17:23:26 -08:00
|
|
|
pub fn split_off(&mut self, at: usize) -> Self {
|
2015-02-07 14:58:58 -05:00
|
|
|
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);
|
|
|
|
|
2015-02-23 11:39:16 -08:00
|
|
|
ptr::copy_nonoverlapping(
|
2015-01-24 17:23:26 -08:00
|
|
|
self.as_ptr().offset(at as isize),
|
2015-03-27 11:12:28 -07:00
|
|
|
other.as_mut_ptr(),
|
2015-01-24 17:23:26 -08:00
|
|
|
other.len());
|
|
|
|
}
|
|
|
|
other
|
|
|
|
}
|
|
|
|
|
2014-12-16 23:44:43 -08:00
|
|
|
}
|
2014-04-02 23:10:36 -04:00
|
|
|
|
2014-12-16 23:44:43 -08:00
|
|
|
impl<T: Clone> Vec<T> {
|
|
|
|
/// Resizes the `Vec` in-place so that `len()` is equal to `new_len`.
|
2014-10-30 13:43:24 -07:00
|
|
|
///
|
2014-12-16 23:44:43 -08:00
|
|
|
/// Calls either `extend()` or `truncate()` depending on whether `new_len`
|
|
|
|
/// is larger than the current value of `len()` or not.
|
2014-10-30 13:43:24 -07:00
|
|
|
///
|
2014-12-08 23:28:07 -06:00
|
|
|
/// # Examples
|
2014-10-30 13:43:24 -07:00
|
|
|
///
|
|
|
|
/// ```
|
2015-07-27 10:50:19 -04:00
|
|
|
/// #![feature(vec_resize)]
|
|
|
|
///
|
2014-12-16 23:44:43 -08:00
|
|
|
/// let mut vec = vec!["hello"];
|
|
|
|
/// vec.resize(3, "world");
|
2015-02-24 21:15:45 +03:00
|
|
|
/// assert_eq!(vec, ["hello", "world", "world"]);
|
2014-10-30 13:43:24 -07:00
|
|
|
///
|
2015-01-25 22:05:03 +01:00
|
|
|
/// let mut vec = vec![1, 2, 3, 4];
|
2014-12-16 23:44:43 -08:00
|
|
|
/// vec.resize(2, 0);
|
2015-02-24 21:15:45 +03:00
|
|
|
/// assert_eq!(vec, [1, 2]);
|
2014-10-30 13:43:24 -07:00
|
|
|
/// ```
|
2015-06-09 14:39:23 -07:00
|
|
|
#[unstable(feature = "vec_resize",
|
2015-08-12 22:19:51 -07:00
|
|
|
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) {
|
2014-12-16 23:44:43 -08:00
|
|
|
let len = self.len();
|
2014-10-30 13:43:24 -07:00
|
|
|
|
2014-12-16 23:44:43 -08:00
|
|
|
if new_len > len {
|
2015-07-07 13:50:23 +02:00
|
|
|
self.extend_with_element(new_len - len, value);
|
2014-12-16 23:44:43 -08:00
|
|
|
} else {
|
|
|
|
self.truncate(new_len);
|
2014-12-16 17:45:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-07 13:50:23 +02:00
|
|
|
/// 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-16 23:44:43 -08:00
|
|
|
/// Appends all elements in a slice to the `Vec`.
|
2014-12-16 20:12:30 -05:00
|
|
|
///
|
2014-12-16 23:44:43 -08:00
|
|
|
/// Iterates over the slice `other`, clones each element, and then appends
|
|
|
|
/// it to this `Vec`. The `other` vector is traversed in-order.
|
2014-10-30 13:43:24 -07:00
|
|
|
///
|
2014-12-08 23:28:07 -06:00
|
|
|
/// # Examples
|
2014-10-30 13:43:24 -07:00
|
|
|
///
|
|
|
|
/// ```
|
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]);
|
2014-10-30 13:43:24 -07:00
|
|
|
/// ```
|
|
|
|
#[inline]
|
2015-06-09 14:39:23 -07:00
|
|
|
#[unstable(feature = "vec_push_all",
|
2015-08-12 22:19:51 -07:00
|
|
|
reason = "likely to be replaced by a more optimized extend",
|
|
|
|
issue = "27744")]
|
2014-12-16 23:44:43 -08:00
|
|
|
pub fn push_all(&mut self, other: &[T]) {
|
|
|
|
self.reserve(other.len());
|
2014-11-06 12:24:47 -05:00
|
|
|
|
2015-01-26 16:05:07 -05:00
|
|
|
for i in 0..other.len() {
|
2014-12-16 23:44:43 -08:00
|
|
|
let len = self.len();
|
2014-11-06 12:24:47 -05:00
|
|
|
|
2015-08-11 17:27:05 -07:00
|
|
|
// 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.
|
2014-11-06 12:24:47 -05:00
|
|
|
unsafe {
|
2014-12-16 23:44:43 -08:00
|
|
|
ptr::write(
|
|
|
|
self.get_unchecked_mut(len),
|
|
|
|
other.get_unchecked(i).clone());
|
|
|
|
self.set_len(len + 1);
|
2014-11-06 12:24:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-02-28 12:55:51 -08:00
|
|
|
}
|
|
|
|
|
2014-08-04 22:48:39 +12:00
|
|
|
impl<T: PartialEq> Vec<T> {
|
|
|
|
/// Removes consecutive repeated elements in the vector.
|
2014-03-16 16:04:29 -07:00
|
|
|
///
|
|
|
|
/// If the vector is sorted, this removes all duplicates.
|
|
|
|
///
|
2014-12-08 23:28:07 -06:00
|
|
|
/// # Examples
|
2014-03-16 16:04:29 -07:00
|
|
|
///
|
2014-07-22 14:02:55 +02:00
|
|
|
/// ```
|
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
|
|
|
///
|
2014-03-16 16:04:29 -07: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]);
|
2014-03-16 16:04:29 -07:00
|
|
|
/// ```
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-03-04 10:39:49 -08:00
|
|
|
pub fn dedup(&mut self) {
|
|
|
|
unsafe {
|
|
|
|
// Although we have a mutable reference to `self`, we cannot make
|
2014-10-09 15:17:22 -04:00
|
|
|
// *arbitrary* changes. The `PartialEq` comparisons could panic, so we
|
2014-03-04 10:39:49 -08:00
|
|
|
// 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
|
2014-03-04 10:39:49 -08:00
|
|
|
// 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();
|
2015-06-10 10:51:48 +08:00
|
|
|
if ln <= 1 { return; }
|
2014-03-04 10:39:49 -08:00
|
|
|
|
2015-06-09 16:49:24 -04:00
|
|
|
// Avoid bounds checks by using raw pointers.
|
2014-11-27 16:44:57 -05:00
|
|
|
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;
|
2014-03-04 10:39:49 -08:00
|
|
|
|
|
|
|
while r < ln {
|
2015-02-05 15:08:33 -05:00
|
|
|
let p_r = p.offset(r as isize);
|
|
|
|
let p_wm1 = p.offset((w - 1) as isize);
|
2014-03-04 10:39:49 -08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-12-16 23:44:43 -08:00
|
|
|
self.truncate(w);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Internal methods and functions
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-02-17 17:44:46 +01:00
|
|
|
#[doc(hidden)]
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
pub fn from_elem<T: Clone>(elem: T, n: usize) -> Vec<T> {
|
2015-07-07 13:50:23 +02:00
|
|
|
let mut v = Vec::with_capacity(n);
|
|
|
|
v.extend_with_element(n, elem);
|
|
|
|
v
|
2015-02-17 17:44:46 +01:00
|
|
|
}
|
|
|
|
|
2014-12-16 23:44:43 -08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Common trait implementations for Vec
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-04-30 15:24:39 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-16 23:44:43 -08:00
|
|
|
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)
|
2015-03-13 19:44:02 -05:00
|
|
|
// NB see the slice::hack module in slice.rs for more information
|
2015-03-11 19:44:02 -05:00
|
|
|
#[cfg(test)]
|
2015-03-13 19:44:02 -05:00
|
|
|
fn clone(&self) -> Vec<T> {
|
|
|
|
::slice::to_vec(&**self)
|
|
|
|
}
|
2015-03-11 19:44:02 -05:00
|
|
|
|
2014-12-16 23:44:43 -08:00
|
|
|
fn clone_from(&mut self, other: &Vec<T>) {
|
|
|
|
// drop anything in self that will not be overwritten
|
|
|
|
if self.len() > other.len() {
|
|
|
|
self.truncate(other.len())
|
|
|
|
}
|
|
|
|
|
|
|
|
// reuse the contained values' allocations/resources.
|
2015-06-10 17:22:20 +01:00
|
|
|
for (place, thing) in self.iter_mut().zip(other) {
|
2014-12-16 23:44:43 -08:00
|
|
|
place.clone_from(thing)
|
|
|
|
}
|
|
|
|
|
|
|
|
// self.len <= other.len due to the truncate above, so the
|
|
|
|
// slice here is always in-bounds.
|
2015-01-07 11:58:31 -05:00
|
|
|
let slice = &other[self.len()..];
|
2014-12-16 23:44:43 -08:00
|
|
|
self.push_all(slice);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-17 20:48:07 -08:00
|
|
|
#[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)
|
|
|
|
}
|
|
|
|
}
|
2014-12-16 23:44:43 -08:00
|
|
|
|
2015-01-24 09:15:42 -08:00
|
|
|
#[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;
|
|
|
|
|
2015-03-21 19:33:27 -04:00
|
|
|
#[inline]
|
|
|
|
fn index(&self, index: usize) -> &T {
|
|
|
|
// NB built-in indexing via `&[T]`
|
|
|
|
&(**self)[index]
|
|
|
|
}
|
2015-01-03 10:40:10 -05:00
|
|
|
}
|
|
|
|
|
2015-01-24 09:15:42 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-04 21:17:19 -05:00
|
|
|
impl<T> IndexMut<usize> for Vec<T> {
|
2015-03-21 19:33:27 -04:00
|
|
|
#[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
|
|
|
|
2015-01-24 09:15:42 -08: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];
|
2015-03-21 19:33:27 -04:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn index(&self, index: ops::Range<usize>) -> &[T] {
|
|
|
|
Index::index(&**self, index)
|
|
|
|
}
|
2014-12-31 20:20:40 +13:00
|
|
|
}
|
2015-01-24 09:15:42 -08: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];
|
2015-03-21 19:33:27 -04:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn index(&self, index: ops::RangeTo<usize>) -> &[T] {
|
|
|
|
Index::index(&**self, index)
|
|
|
|
}
|
2014-12-31 20:20:40 +13:00
|
|
|
}
|
2015-01-24 09:15:42 -08: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];
|
2015-03-21 19:33:27 -04:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn index(&self, index: ops::RangeFrom<usize>) -> &[T] {
|
|
|
|
Index::index(&**self, index)
|
|
|
|
}
|
2014-12-16 23:44:43 -08:00
|
|
|
}
|
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];
|
2015-03-21 19:33:27 -04:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn index(&self, _index: ops::RangeFull) -> &[T] {
|
2015-03-23 15:54:39 -07:00
|
|
|
self
|
2015-03-21 19:33:27 -04:00
|
|
|
}
|
2015-01-28 17:06:46 +13:00
|
|
|
}
|
2014-12-16 23:44:43 -08:00
|
|
|
|
2015-01-24 09:15:42 -08: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> {
|
2015-03-21 19:33:27 -04:00
|
|
|
|
|
|
|
#[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
|
|
|
}
|
2015-01-24 09:15:42 -08: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> {
|
2015-03-21 19:33:27 -04:00
|
|
|
|
|
|
|
#[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
|
|
|
}
|
2015-01-24 09:15:42 -08: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> {
|
2015-03-21 19:33:27 -04:00
|
|
|
|
|
|
|
#[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> {
|
2015-03-21 19:33:27 -04:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn index_mut(&mut self, _index: ops::RangeFull) -> &mut [T] {
|
2015-03-26 07:29:06 -07:00
|
|
|
self
|
2015-03-21 19:33:27 -04:00
|
|
|
}
|
2015-01-28 17:06:46 +13:00
|
|
|
}
|
2014-12-16 23:44:43 -08: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];
|
|
|
|
|
2015-03-18 09:14:54 -07:00
|
|
|
fn deref(&self) -> &[T] {
|
|
|
|
unsafe {
|
2015-07-09 21:57:21 -07:00
|
|
|
let p = self.buf.ptr();
|
2015-09-03 09:49:50 +03:00
|
|
|
assume(!p.is_null());
|
2015-03-18 09:14:54 -07:00
|
|
|
slice::from_raw_parts(p, self.len)
|
|
|
|
}
|
|
|
|
}
|
2014-12-16 23:44:43 -08: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::DerefMut for Vec<T> {
|
2015-03-26 07:29:06 -07:00
|
|
|
fn deref_mut(&mut self) -> &mut [T] {
|
|
|
|
unsafe {
|
2015-07-09 21:57:21 -07:00
|
|
|
let ptr = self.buf.ptr();
|
2015-03-26 07:29:06 -07:00
|
|
|
assume(!ptr.is_null());
|
|
|
|
slice::from_raw_parts_mut(ptr, self.len)
|
|
|
|
}
|
|
|
|
}
|
2014-12-16 23:44:43 -08:00
|
|
|
}
|
|
|
|
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-16 23:44:43 -08:00
|
|
|
impl<T> FromIterator<T> for Vec<T> {
|
|
|
|
#[inline]
|
2015-02-18 13:06:21 -05:00
|
|
|
fn from_iter<I: IntoIterator<Item=T>>(iterable: I) -> Vec<T> {
|
2015-02-22 19:20:18 +02:00
|
|
|
// 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.
|
2015-02-18 13:06:21 -05:00
|
|
|
let mut iterator = iterable.into_iter();
|
2015-02-22 19:20:18 +02:00
|
|
|
let mut vector = match iterator.next() {
|
|
|
|
None => return Vec::new(),
|
|
|
|
Some(element) => {
|
|
|
|
let (lower, _) = iterator.size_hint();
|
2015-06-24 22:47:56 -07:00
|
|
|
let mut vector = Vec::with_capacity(lower.saturating_add(1));
|
2015-02-22 19:20:18 +02:00
|
|
|
unsafe {
|
|
|
|
ptr::write(vector.get_unchecked_mut(0), element);
|
|
|
|
vector.set_len(1);
|
|
|
|
}
|
|
|
|
vector
|
2015-02-15 21:04:20 -08:00
|
|
|
}
|
2015-02-22 19:20:18 +02:00
|
|
|
};
|
2015-02-22 19:22:50 +02:00
|
|
|
vector.extend_desugared(iterator);
|
2014-12-16 23:44:43 -08:00
|
|
|
vector
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-17 10:06:24 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-13 17:55:10 -05:00
|
|
|
impl<T> IntoIterator for Vec<T> {
|
|
|
|
type Item = T;
|
|
|
|
type IntoIter = IntoIter<T>;
|
|
|
|
|
2015-04-17 14:31:30 -07:00
|
|
|
/// 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> {
|
2015-05-15 15:20:42 +02:00
|
|
|
unsafe {
|
2015-07-09 21:57:21 -07:00
|
|
|
let ptr = self.as_mut_ptr();
|
2015-05-15 15:20:42 +02:00
|
|
|
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);
|
2015-05-15 15:20:42 +02:00
|
|
|
mem::forget(self);
|
2015-08-11 17:27:05 -07:00
|
|
|
IntoIter { _buf: buf, ptr: begin, end: end }
|
2015-05-15 15:20:42 +02:00
|
|
|
}
|
|
|
|
}
|
2015-02-13 17:55:10 -05:00
|
|
|
}
|
|
|
|
|
2015-02-17 10:06:24 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-07 22:01:05 -05:00
|
|
|
impl<'a, T> IntoIterator for &'a Vec<T> {
|
2015-02-13 17:55:10 -05:00
|
|
|
type Item = &'a T;
|
2015-02-06 14:47:55 -08:00
|
|
|
type IntoIter = slice::Iter<'a, T>;
|
2015-01-07 22:01:05 -05:00
|
|
|
|
|
|
|
fn into_iter(self) -> slice::Iter<'a, T> {
|
|
|
|
self.iter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-17 10:06:24 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-07 22:01:05 -05:00
|
|
|
impl<'a, T> IntoIterator for &'a mut Vec<T> {
|
2015-02-13 17:55:10 -05:00
|
|
|
type Item = &'a mut T;
|
2015-02-06 14:47:55 -08:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-30 15:24:39 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-16 23:44:43 -08:00
|
|
|
impl<T> Extend<T> for Vec<T> {
|
|
|
|
#[inline]
|
2015-02-18 10:04:30 -05:00
|
|
|
fn extend<I: IntoIterator<Item=T>>(&mut self, iterable: I) {
|
2015-02-22 19:22:50 +02:00
|
|
|
self.extend_desugared(iterable.into_iter())
|
|
|
|
}
|
|
|
|
}
|
2015-02-22 17:22:48 +02:00
|
|
|
|
2015-02-22 19:22:50 +02:00
|
|
|
impl<T> Vec<T> {
|
|
|
|
fn extend_desugared<I: Iterator<Item=T>>(&mut self, mut iterator: I) {
|
2015-02-22 17:22:48 +02:00
|
|
|
// 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();
|
2015-06-24 22:47:56 -07:00
|
|
|
self.reserve(lower.saturating_add(1));
|
2015-02-22 23:46:36 +02:00
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
ptr::write(self.get_unchecked_mut(len), element);
|
2015-06-24 22:47:56 -07:00
|
|
|
// 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-02-22 17:22:48 +02:00
|
|
|
}
|
2014-12-16 23:44:43 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 20:32:55 +03:00
|
|
|
__impl_slice_eq1! { Vec<A>, Vec<B> }
|
2015-03-10 17:59:23 -07:00
|
|
|
__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 }
|
2015-02-20 20:32:55 +03:00
|
|
|
|
|
|
|
macro_rules! array_impls {
|
|
|
|
($($N: expr)+) => {
|
|
|
|
$(
|
|
|
|
// NOTE: some less important impls are omitted to reduce code bloat
|
2015-03-10 17:59:23 -07:00
|
|
|
__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 }
|
2015-02-20 20:32:55 +03:00
|
|
|
)+
|
2014-12-16 23:44:43 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 20:32:55 +03:00
|
|
|
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
|
2014-12-16 23:44:43 -08:00
|
|
|
}
|
|
|
|
|
2015-02-11 23:16:32 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-16 23:44:43 -08:00
|
|
|
impl<T: PartialOrd> PartialOrd for Vec<T> {
|
|
|
|
#[inline]
|
|
|
|
fn partial_cmp(&self, other: &Vec<T>) -> Option<Ordering> {
|
2015-02-01 21:53:25 -05:00
|
|
|
PartialOrd::partial_cmp(&**self, &**other)
|
2014-12-16 23:44:43 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-11 23:16:32 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-16 23:44:43 -08:00
|
|
|
impl<T: Eq> Eq for Vec<T> {}
|
|
|
|
|
2015-02-11 23:16:32 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-16 23:44:43 -08:00
|
|
|
impl<T: Ord> Ord for Vec<T> {
|
|
|
|
#[inline]
|
|
|
|
fn cmp(&self, other: &Vec<T>) -> Ordering {
|
2015-02-01 21:53:25 -05:00
|
|
|
Ord::cmp(&**self, &**other)
|
2014-03-04 10:39:49 -08:00
|
|
|
}
|
2014-01-21 09:35:11 -05:00
|
|
|
}
|
|
|
|
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-01-21 09:35:11 -05:00
|
|
|
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() {
|
|
|
|
for x in self.iter_mut() {
|
|
|
|
unsafe { drop_in_place(x); }
|
2014-01-21 09:35:11 -05:00
|
|
|
}
|
|
|
|
}
|
2015-07-09 21:57:21 -07:00
|
|
|
// RawVec handles deallocation
|
2014-01-21 09:35:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-02-28 12:55:51 -08:00
|
|
|
impl<T> Default for Vec<T> {
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-02-28 12:55:51 -08:00
|
|
|
fn default() -> Vec<T> {
|
|
|
|
Vec::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-24 09:15:42 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-20 15:45:07 -08:00
|
|
|
impl<T: fmt::Debug> fmt::Debug for Vec<T> {
|
2014-02-28 12:54:01 -08:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2015-02-01 21:53:25 -05:00
|
|
|
fmt::Debug::fmt(&**self, f)
|
2014-12-20 00:09:35 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-18 09:14:54 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
impl<T> AsRef<Vec<T>> for Vec<T> {
|
|
|
|
fn as_ref(&self) -> &Vec<T> {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
impl<T> AsRef<[T]> for Vec<T> {
|
|
|
|
fn as_ref(&self) -> &[T] {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
impl<'a, T: Clone> From<&'a [T]> for Vec<T> {
|
2015-03-23 15:54:39 -07:00
|
|
|
#[cfg(not(test))]
|
2015-03-18 09:14:54 -07:00
|
|
|
fn from(s: &'a [T]) -> Vec<T> {
|
|
|
|
s.to_vec()
|
|
|
|
}
|
2015-03-23 15:54:39 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
fn from(s: &'a [T]) -> Vec<T> {
|
|
|
|
::slice::to_vec(s)
|
|
|
|
}
|
2015-03-18 09:14:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
impl<'a> From<&'a str> for Vec<u8> {
|
|
|
|
fn from(s: &'a str) -> Vec<u8> {
|
2015-03-23 15:54:39 -07:00
|
|
|
From::from(s.as_bytes())
|
2015-03-18 09:14:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-16 23:44:43 -08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Clone-on-write
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-04-30 15:24:39 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-11 23:16:32 -08:00
|
|
|
impl<'a, T> FromIterator<T> for Cow<'a, [T]> where T: Clone {
|
2015-02-18 15:34:32 -08:00
|
|
|
fn from_iter<I: IntoIterator<Item=T>>(it: I) -> Cow<'a, [T]> {
|
2014-12-16 23:44:43 -08:00
|
|
|
Cow::Owned(FromIterator::from_iter(it))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-11 23:16:32 -08:00
|
|
|
impl<'a, T: 'a> IntoCow<'a, [T]> for Vec<T> where T: Clone {
|
|
|
|
fn into_cow(self) -> Cow<'a, [T]> {
|
2014-12-16 23:44:43 -08:00
|
|
|
Cow::Owned(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-11 23:16:32 -08:00
|
|
|
impl<'a, T> IntoCow<'a, [T]> for &'a [T] where T: Clone {
|
|
|
|
fn into_cow(self) -> Cow<'a, [T]> {
|
2014-12-16 23:44:43 -08:00
|
|
|
Cow::Borrowed(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Iterators
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-03-16 16:04:29 -07:00
|
|
|
/// An iterator that moves out of a vector.
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-19 21:52:10 +01:00
|
|
|
pub struct IntoIter<T> {
|
2015-08-11 17:27:05 -07:00
|
|
|
_buf: RawVec<T>,
|
2014-09-04 15:25:23 -04:00
|
|
|
ptr: *const T,
|
|
|
|
end: *const T
|
2014-01-21 09:35:11 -05:00
|
|
|
}
|
|
|
|
|
2015-01-12 17:12:20 -02:00
|
|
|
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;
|
|
|
|
|
2014-01-21 09:35:11 -05:00
|
|
|
#[inline]
|
2015-05-15 15:20:42 +02:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-21 09:35:11 -05:00
|
|
|
#[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);
|
2014-09-04 15:25:23 -04:00
|
|
|
let size = mem::size_of::<T>();
|
|
|
|
let exact = diff / (if size == 0 {1} else {size});
|
|
|
|
(exact, Some(exact))
|
2014-01-21 09:35:11 -05:00
|
|
|
}
|
2015-05-02 14:01:38 +09:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn count(self) -> usize {
|
|
|
|
self.size_hint().0
|
|
|
|
}
|
2014-01-21 09:35:11 -05:00
|
|
|
}
|
|
|
|
|
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> {
|
2014-01-21 09:35:11 -05:00
|
|
|
#[inline]
|
2015-05-15 15:20:42 +02:00
|
|
|
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);
|
|
|
|
|
2015-07-24 03:04:55 +02:00
|
|
|
Some(ptr::read(self.end))
|
2015-05-15 15:20:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-01-21 09:35:11 -05:00
|
|
|
}
|
|
|
|
|
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")]
|
2014-12-19 21:52:10 +01:00
|
|
|
impl<T> Drop for IntoIter<T> {
|
2014-01-21 09:35:11 -05:00
|
|
|
fn drop(&mut self) {
|
|
|
|
// destroy the remaining elements
|
2015-07-09 21:57:21 -07:00
|
|
|
for _x in self.by_ref() {}
|
|
|
|
|
|
|
|
// RawVec handles deallocation
|
2014-01-21 09:35:11 -05:00
|
|
|
}
|
|
|
|
}
|
2014-03-04 07:46:24 +11:00
|
|
|
|
2015-04-27 19:37:13 +02:00
|
|
|
/// A draining iterator for `Vec<T>`.
|
2015-08-12 22:19:51 -07:00
|
|
|
#[unstable(feature = "drain", reason = "recently added", issue = "27711")]
|
2015-04-27 19:37:13 +02:00
|
|
|
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>,
|
2014-12-16 17:45:03 -05:00
|
|
|
}
|
|
|
|
|
2015-02-25 20:52:18 +08:00
|
|
|
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;
|
|
|
|
|
2014-12-16 17:45:03 -05:00
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<T> {
|
2015-04-27 19:37:13 +02:00
|
|
|
self.iter.next().map(|elt|
|
|
|
|
unsafe {
|
|
|
|
ptr::read(elt as *const _)
|
2014-12-16 17:45:03 -05:00
|
|
|
}
|
2015-04-27 19:37:13 +02:00
|
|
|
)
|
2014-12-16 17:45:03 -05:00
|
|
|
}
|
|
|
|
|
2015-02-04 21:17:19 -05:00
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
2015-04-27 19:37:13 +02:00
|
|
|
self.iter.size_hint()
|
2014-12-16 17:45:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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> {
|
2014-12-16 17:45:03 -05:00
|
|
|
#[inline]
|
|
|
|
fn next_back(&mut self) -> Option<T> {
|
2015-04-27 19:37:13 +02:00
|
|
|
self.iter.next_back().map(|elt|
|
|
|
|
unsafe {
|
|
|
|
ptr::read(elt as *const _)
|
2014-12-16 17:45:03 -05:00
|
|
|
}
|
2015-04-27 19:37:13 +02:00
|
|
|
)
|
2014-12-16 17:45:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-16 17:45:03 -05:00
|
|
|
impl<'a, T> Drop for Drain<'a, T> {
|
|
|
|
fn drop(&mut self) {
|
2015-04-27 19:37:13 +02:00
|
|
|
// exhaust self first
|
|
|
|
while let Some(_) = self.next() { }
|
2014-12-16 17:45:03 -05:00
|
|
|
|
2015-04-27 19:37:13 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2014-12-16 17:45:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-27 19:37:13 +02:00
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
impl<'a, T> ExactSizeIterator for Drain<'a, T> {}
|