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
|
|
|
|
2016-02-14 17:00:46 -08:00
|
|
|
//! A contiguous growable array 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
|
|
|
|
//!
|
2017-03-12 14:04:52 -04: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
|
|
|
//! ```
|
|
|
|
//!
|
2016-10-08 17:58:53 +02: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
|
|
|
//! ```
|
|
|
|
//!
|
2016-10-08 17:58:53 +02:00
|
|
|
//! You can [`push`] values onto the end of a vector (which will grow the vector
|
2015-08-11 17:27:05 -07:00
|
|
|
//! 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
|
|
|
//!
|
2016-10-08 17:58:53 +02:00
|
|
|
//! Vectors also support indexing (through the [`Index`] and [`IndexMut`] traits):
|
2015-05-03 23:47:10 -05:00
|
|
|
//!
|
|
|
|
//! ```
|
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
|
|
|
//! ```
|
2016-10-08 17:58:53 +02:00
|
|
|
//!
|
|
|
|
//! [`Vec<T>`]: ../../std/vec/struct.Vec.html
|
2017-03-12 14:04:52 -04:00
|
|
|
//! [`new`]: ../../std/vec/struct.Vec.html#method.new
|
2016-10-08 17:58:53 +02:00
|
|
|
//! [`push`]: ../../std/vec/struct.Vec.html#method.push
|
|
|
|
//! [`Index`]: ../../std/ops/trait.Index.html
|
|
|
|
//! [`IndexMut`]: ../../std/ops/trait.IndexMut.html
|
|
|
|
//! [`vec!`]: ../../std/macro.vec.html
|
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
|
|
|
|
2014-10-10 05:18:42 -04:00
|
|
|
use alloc::boxed::Box;
|
2015-07-09 21:57:21 -07:00
|
|
|
use alloc::heap::EMPTY;
|
2016-02-16 16:00:38 +01:00
|
|
|
use alloc::raw_vec::RawVec;
|
|
|
|
use borrow::ToOwned;
|
2016-03-07 15:42:29 -08:00
|
|
|
use borrow::Cow;
|
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};
|
2016-03-02 17:46:54 +01:00
|
|
|
use core::intrinsics::{arith_offset, assume};
|
2016-10-20 14:07:31 +02:00
|
|
|
use core::iter::{FromIterator, FusedIterator, TrustedLen};
|
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;
|
2017-03-10 08:19:42 -08:00
|
|
|
#[cfg(not(test))]
|
|
|
|
use core::num::Float;
|
2016-12-22 17:15:13 +00:00
|
|
|
use core::ops::{InPlace, Index, IndexMut, Place, Placer};
|
2014-09-15 20:48:58 +12:00
|
|
|
use core::ops;
|
2014-12-11 22:29:24 -05:00
|
|
|
use core::ptr;
|
2016-07-21 20:55:19 -07:00
|
|
|
use core::ptr::Shared;
|
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-04-27 19:37:13 +02:00
|
|
|
use super::range::RangeArgument;
|
2016-12-23 19:15:56 +00:00
|
|
|
use Bound::{Excluded, Included, Unbounded};
|
2015-04-27 19:37:13 +02:00
|
|
|
|
2016-10-08 17:58:53 +02:00
|
|
|
/// A contiguous growable array 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
|
|
|
/// ```
|
|
|
|
///
|
2016-10-08 17:58:53 +02: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);
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
2016-02-21 03:00:50 +01:00
|
|
|
/// # Indexing
|
|
|
|
///
|
2016-10-08 17:58:53 +02:00
|
|
|
/// The `Vec` type allows to access values by index, because it implements the
|
|
|
|
/// [`Index`] trait. An example will be more explicit:
|
2016-02-21 03:00:50 +01:00
|
|
|
///
|
|
|
|
/// ```
|
2016-10-29 22:54:04 +01:00
|
|
|
/// let v = vec![0, 2, 4, 6];
|
2016-02-21 03:00:50 +01:00
|
|
|
/// println!("{}", v[1]); // it will display '2'
|
|
|
|
/// ```
|
|
|
|
///
|
2016-10-08 17:58:53 +02:00
|
|
|
/// However be careful: if you try to access an index which isn't in the `Vec`,
|
2016-02-21 03:00:50 +01:00
|
|
|
/// your software will panic! You cannot do this:
|
|
|
|
///
|
|
|
|
/// ```ignore
|
2016-10-29 22:54:04 +01:00
|
|
|
/// let v = vec![0, 2, 4, 6];
|
2016-02-21 03:00:50 +01:00
|
|
|
/// println!("{}", v[6]); // it will panic!
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// In conclusion: always check if the index you want to get really exists
|
|
|
|
/// before doing it.
|
|
|
|
///
|
|
|
|
/// # Slicing
|
|
|
|
///
|
2016-10-08 17:58:53 +02:00
|
|
|
/// A `Vec` can be mutable. Slices, on the other hand, are read-only objects.
|
2016-10-25 14:22:54 +13:00
|
|
|
/// To get a slice, use `&`. Example:
|
2016-02-21 03:00:50 +01:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// fn read_slice(slice: &[usize]) {
|
|
|
|
/// // ...
|
|
|
|
/// }
|
|
|
|
///
|
2016-10-29 22:54:04 +01:00
|
|
|
/// let v = vec![0, 1];
|
2016-02-21 03:00:50 +01:00
|
|
|
/// read_slice(&v);
|
|
|
|
///
|
|
|
|
/// // ... and that's all!
|
|
|
|
/// // you can also do it like this:
|
|
|
|
/// let x : &[usize] = &v;
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// In Rust, it's more common to pass slices as arguments rather than vectors
|
2016-10-08 17:58:53 +02:00
|
|
|
/// when you just want to provide a read access. The same goes for [`String`] and
|
|
|
|
/// [`&str`].
|
2016-02-21 03:00:50 +01:00
|
|
|
///
|
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
|
2016-10-08 17:58:53 +02:00
|
|
|
/// can be slow. For this reason, it is recommended to use [`Vec::with_capacity`]
|
2015-03-23 15:54:39 -07:00
|
|
|
/// whenever possible to specify how big the vector is expected to get.
|
2015-08-19 10:13:39 -07:00
|
|
|
///
|
|
|
|
/// # Guarantees
|
|
|
|
///
|
2016-10-25 14:22:54 +13:00
|
|
|
/// Due to its incredibly fundamental nature, `Vec` makes a lot of guarantees
|
2015-08-19 10:13:39 -07:00
|
|
|
/// 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.
|
|
|
|
///
|
2016-10-25 14:22:54 +13:00
|
|
|
/// Most fundamentally, `Vec` is and always will be a (pointer, capacity, length)
|
2015-08-19 10:13:39 -07:00
|
|
|
/// 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,
|
2017-03-12 14:04:52 -04:00
|
|
|
/// if you construct a `Vec` with capacity 0 via [`Vec::new`], [`vec![]`][`vec!`],
|
|
|
|
/// [`Vec::with_capacity(0)`][`Vec::with_capacity`], or by calling [`shrink_to_fit`]
|
2016-10-08 17:58:53 +02:00
|
|
|
/// 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
|
2017-03-12 14:04:52 -04:00
|
|
|
/// 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
|
2015-08-19 10:13:39 -07:00
|
|
|
/// details are subtle enough that it is strongly recommended that you only
|
2016-10-25 14:22:54 +13:00
|
|
|
/// free memory allocated by a `Vec` by creating a new `Vec` and dropping it.
|
2015-08-19 10:13:39 -07:00
|
|
|
///
|
2016-10-08 17:58:53 +02:00
|
|
|
/// If a `Vec` *has* allocated memory, then the memory it points to is on the heap
|
2015-08-19 10:13:39 -07:00
|
|
|
/// (as defined by the allocator Rust is configured to use by default), and its
|
2017-03-12 14:04:52 -04:00
|
|
|
/// pointer points to [`len`] initialized elements in order (what you would see
|
|
|
|
/// if you coerced it to a slice), followed by [`capacity`]` - `[`len`]
|
2016-10-25 14:22:54 +13:00
|
|
|
/// logically uninitialized elements.
|
2015-08-19 10:13:39 -07:00
|
|
|
///
|
2016-10-08 17:58:53 +02:00
|
|
|
/// `Vec` will never perform a "small optimization" where elements are actually
|
2015-08-19 10:13:39 -07:00
|
|
|
/// stored on the stack for two reasons:
|
|
|
|
///
|
|
|
|
/// * It would make it more difficult for unsafe code to correctly manipulate
|
2016-10-08 17:58:53 +02:00
|
|
|
/// 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
|
2015-08-19 10:13:39 -07:00
|
|
|
/// actually allocated memory.
|
|
|
|
///
|
|
|
|
/// * It would penalize the general case, incurring an additional branch
|
|
|
|
/// on every access.
|
|
|
|
///
|
2016-10-08 17:58:53 +02:00
|
|
|
/// `Vec` will never automatically shrink itself, even if completely empty. This
|
|
|
|
/// ensures no unnecessary allocations or deallocations occur. Emptying a `Vec`
|
2017-03-12 14:04:52 -04:00
|
|
|
/// and then filling it back up to the same [`len`] should incur no calls to
|
2016-10-08 17:58:53 +02:00
|
|
|
/// the allocator. If you wish to free up unused memory, use
|
2017-03-12 14:04:52 -04:00
|
|
|
/// [`shrink_to_fit`][`shrink_to_fit`].
|
2016-10-08 17:58:53 +02:00
|
|
|
///
|
|
|
|
/// [`push`] and [`insert`] will never (re)allocate if the reported capacity is
|
2016-10-25 14:22:54 +13:00
|
|
|
/// sufficient. [`push`] and [`insert`] *will* (re)allocate if
|
2017-03-12 14:04:52 -04:00
|
|
|
/// [`len`]` == `[`capacity`]. That is, the reported capacity is completely
|
2016-10-08 17:58:53 +02:00
|
|
|
/// accurate, and can be relied on. It can even be used to manually free the memory
|
|
|
|
/// allocated by a `Vec` if desired. Bulk insertion methods *may* reallocate, even
|
|
|
|
/// when not necessary.
|
|
|
|
///
|
|
|
|
/// `Vec` does not guarantee any particular growth strategy when reallocating
|
|
|
|
/// when full, nor when [`reserve`] is called. The current strategy is basic
|
2015-08-19 10:13:39 -07:00
|
|
|
/// and it may prove desirable to use a non-constant growth factor. Whatever
|
2016-10-08 17:58:53 +02:00
|
|
|
/// strategy is used will of course guarantee `O(1)` amortized [`push`].
|
2015-08-19 10:13:39 -07:00
|
|
|
///
|
2016-10-08 17:58:53 +02:00
|
|
|
/// `vec![x; n]`, `vec![a, b, c, d]`, and
|
2016-10-25 14:22:54 +13:00
|
|
|
/// [`Vec::with_capacity(n)`][`Vec::with_capacity`], will all produce a `Vec`
|
2017-03-12 14:04:52 -04:00
|
|
|
/// with exactly the requested capacity. If [`len`]` == `[`capacity`],
|
2016-10-25 14:22:54 +13:00
|
|
|
/// (as is the case for the [`vec!`] macro), then a `Vec<T>` can be converted to
|
|
|
|
/// and from a [`Box<[T]>`][owned slice] without reallocating or moving the elements.
|
2015-08-19 10:13:39 -07:00
|
|
|
///
|
2016-10-08 17:58:53 +02:00
|
|
|
/// `Vec` will not specifically overwrite any data that is removed from it,
|
2015-08-19 10:13:39 -07:00
|
|
|
/// 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
|
2016-10-08 17:58:53 +02:00
|
|
|
/// 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
|
2015-08-19 10:13:39 -07:00
|
|
|
/// first, that may not actually happen because the optimizer does not consider
|
|
|
|
/// this a side-effect that must be preserved.
|
|
|
|
///
|
2016-10-08 17:58:53 +02:00
|
|
|
/// `Vec` does not currently guarantee the order in which elements are dropped
|
2015-08-19 10:13:39 -07:00
|
|
|
/// (the order has changed in the past, and may change again).
|
|
|
|
///
|
2016-10-08 17:58:53 +02:00
|
|
|
/// [`vec!`]: ../../std/macro.vec.html
|
|
|
|
/// [`Index`]: ../../std/ops/trait.Index.html
|
|
|
|
/// [`String`]: ../../std/string/struct.String.html
|
|
|
|
/// [`&str`]: ../../std/primitive.str.html
|
|
|
|
/// [`Vec::with_capacity`]: ../../std/vec/struct.Vec.html#method.with_capacity
|
2017-03-12 14:04:52 -04:00
|
|
|
/// [`Vec::new`]: ../../std/vec/struct.Vec.html#method.new
|
|
|
|
/// [`shrink_to_fit`]: ../../std/vec/struct.Vec.html#method.shrink_to_fit
|
|
|
|
/// [`capacity`]: ../../std/vec/struct.Vec.html#method.capacity
|
|
|
|
/// [`mem::size_of::<T>`]: ../../std/mem/fn.size_of.html
|
|
|
|
/// [`len`]: ../../std/vec/struct.Vec.html#method.len
|
2016-10-08 17:58:53 +02:00
|
|
|
/// [`push`]: ../../std/vec/struct.Vec.html#method.push
|
|
|
|
/// [`insert`]: ../../std/vec/struct.Vec.html#method.insert
|
|
|
|
/// [`reserve`]: ../../std/vec/struct.Vec.html#method.reserve
|
2016-10-25 14:22:54 +13:00
|
|
|
/// [owned slice]: ../../std/boxed/struct.Box.html
|
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-11-03 14:49:33 +00:00
|
|
|
/// # #![allow(unused_mut)]
|
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-11-24 11:23:48 +13: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*
|
2016-10-25 14:22:54 +13:00
|
|
|
/// of the returned vector, but only the *capacity*. For an explanation of
|
|
|
|
/// the difference between length and capacity, see *[Capacity and reallocation]*.
|
|
|
|
///
|
|
|
|
/// [Capacity and reallocation]: #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-11-24 11:23:48 +13: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-10-23 11:42:14 -04:00
|
|
|
/// # Safety
|
2015-07-02 13:00:58 -04:00
|
|
|
///
|
|
|
|
/// This is highly unsafe, due to the number of invariants that aren't
|
|
|
|
/// checked:
|
|
|
|
///
|
2016-10-08 17:58:53 +02:00
|
|
|
/// * `ptr` needs to have been previously allocated via [`String`]/`Vec<T>`
|
2015-07-02 13:00:58 -04:00
|
|
|
/// (at least, it's highly likely to be incorrect if it wasn't).
|
2016-07-17 10:55:00 -04:00
|
|
|
/// * `length` needs to be 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
|
2017-01-06 15:58:35 +00:00
|
|
|
/// internal datastructures. For example it is **not** safe
|
2017-01-06 18:17:18 +00:00
|
|
|
/// to build a `Vec<u8>` from a pointer to a C `char` array and a `size_t`.
|
2014-07-22 13:40:38 +02:00
|
|
|
///
|
2016-07-17 10:52:59 -04:00
|
|
|
/// The ownership of `ptr` is effectively transferred to the
|
|
|
|
/// `Vec<T>` which may then deallocate, reallocate or change the
|
|
|
|
/// contents of memory pointed to by the pointer at will. Ensure
|
|
|
|
/// that nothing else uses the pointer after calling this
|
|
|
|
/// function.
|
|
|
|
///
|
2016-10-08 17:58:53 +02:00
|
|
|
/// [`String`]: ../../std/string/struct.String.html
|
|
|
|
///
|
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-11-24 11:23:48 +13: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
|
2017-02-09 13:58:48 -05:00
|
|
|
/// frequent reallocations. After calling `reserve`, capacity will be
|
|
|
|
/// greater than or equal to `self.len() + additional`. Does nothing if
|
|
|
|
/// capacity is already sufficient.
|
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
|
2017-02-09 13:58:48 -05:00
|
|
|
/// be inserted in the given `Vec<T>`. After calling `reserve_exact`,
|
|
|
|
/// capacity will be greater than or equal to `self.len() + additional`.
|
|
|
|
/// Does nothing if the capacity is already 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
|
|
|
}
|
|
|
|
|
2016-10-25 14:22:54 +13:00
|
|
|
/// Converts the vector into [`Box<[T]>`][owned slice].
|
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
|
2017-03-12 14:04:52 -04:00
|
|
|
/// converting back to a vector with [`into_vec`] is equivalent to calling
|
|
|
|
/// [`shrink_to_fit`].
|
2016-10-08 17:58:53 +02:00
|
|
|
///
|
2016-10-25 14:22:54 +13:00
|
|
|
/// [owned slice]: ../../std/boxed/struct.Box.html
|
2017-03-12 14:04:52 -04:00
|
|
|
/// [`into_vec`]: ../../std/primitive.slice.html#method.into_vec
|
|
|
|
/// [`shrink_to_fit`]: #method.shrink_to_fit
|
2016-08-02 03:28:13 +02:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let v = vec![1, 2, 3];
|
|
|
|
///
|
|
|
|
/// let slice = v.into_boxed_slice();
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Any excess capacity is removed:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let mut vec = Vec::with_capacity(10);
|
|
|
|
/// vec.extend([1, 2, 3].iter().cloned());
|
|
|
|
///
|
|
|
|
/// assert_eq!(vec.capacity(), 10);
|
|
|
|
/// let slice = vec.into_boxed_slice();
|
|
|
|
/// assert_eq!(slice.into_vec().capacity(), 3);
|
|
|
|
/// ```
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-16 11:20:17 -04:00
|
|
|
/// Shortens the vector, keeping the first `len` elements and dropping
|
|
|
|
/// the rest.
|
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
|
|
|
///
|
2016-07-16 11:20:17 -04:00
|
|
|
/// The [`drain`] method can emulate `truncate`, but causes the excess
|
|
|
|
/// elements to be returned instead of dropped.
|
|
|
|
///
|
2017-02-11 20:29:29 +11:00
|
|
|
/// Note that this method has no effect on the allocated capacity
|
|
|
|
/// of the vector.
|
|
|
|
///
|
2014-12-19 14:30:51 -06:00
|
|
|
/// # Examples
|
|
|
|
///
|
2016-07-16 11:20:17 -04:00
|
|
|
/// Truncating a five element vector to two elements:
|
|
|
|
///
|
2014-12-19 14:30:51 -06:00
|
|
|
/// ```
|
2015-10-28 21:38:46 -04:00
|
|
|
/// let mut vec = vec![1, 2, 3, 4, 5];
|
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
|
|
|
/// ```
|
2016-07-16 11:20:17 -04:00
|
|
|
///
|
|
|
|
/// No truncation occurs when `len` is greater than the vector's current
|
|
|
|
/// length:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let mut vec = vec![1, 2, 3];
|
|
|
|
/// vec.truncate(8);
|
|
|
|
/// assert_eq!(vec, [1, 2, 3]);
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Truncating when `len == 0` is equivalent to calling the [`clear`]
|
|
|
|
/// method.
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let mut vec = vec![1, 2, 3];
|
|
|
|
/// vec.truncate(0);
|
|
|
|
/// assert_eq!(vec, []);
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// [`clear`]: #method.clear
|
|
|
|
/// [`drain`]: #method.drain
|
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 {
|
2016-03-02 17:48:50 +01:00
|
|
|
// decrement len before the drop_in_place(), so a panic on Drop
|
|
|
|
// doesn't re-drop the just-failed value.
|
2014-12-16 23:44:43 -08:00
|
|
|
self.len -= 1;
|
2016-03-02 17:48:50 +01:00
|
|
|
let len = self.len;
|
|
|
|
ptr::drop_in_place(self.get_unchecked_mut(len));
|
2014-12-16 23:44:43 -08:00
|
|
|
}
|
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[..]`.
|
2016-07-19 20:38:35 -04:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::io::{self, Write};
|
|
|
|
/// let buffer = vec![1, 2, 3, 5, 8];
|
|
|
|
/// io::sink().write(buffer.as_slice()).unwrap();
|
|
|
|
/// ```
|
2015-03-30 15:15:27 -07:00
|
|
|
#[inline]
|
2016-01-15 10:07:52 -08:00
|
|
|
#[stable(feature = "vec_as_slice", since = "1.7.0")]
|
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[..]`.
|
2016-07-19 20:38:35 -04:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::io::{self, Read};
|
|
|
|
/// let mut buffer = vec![0; 3];
|
|
|
|
/// io::repeat(0b101).read_exact(buffer.as_mut_slice()).unwrap();
|
|
|
|
/// ```
|
2014-12-16 23:44:43 -08:00
|
|
|
#[inline]
|
2016-01-15 10:07:52 -08:00
|
|
|
#[stable(feature = "vec_as_slice", since = "1.7.0")]
|
2015-02-05 13:48:20 -05:00
|
|
|
pub fn as_mut_slice(&mut self) -> &mut [T] {
|
2016-07-16 22:09:55 -04:00
|
|
|
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
|
|
|
|
///
|
|
|
|
/// ```
|
2016-07-18 22:29:05 -04:00
|
|
|
/// use std::ptr;
|
|
|
|
///
|
|
|
|
/// let mut vec = vec!['r', 'u', 's', 't'];
|
|
|
|
///
|
|
|
|
/// unsafe {
|
|
|
|
/// ptr::drop_in_place(&mut vec[3]);
|
|
|
|
/// vec.set_len(3);
|
|
|
|
/// }
|
|
|
|
/// assert_eq!(vec, ['r', 'u', 's']);
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// In this example, there is a memory leak since the memory locations
|
2016-07-22 21:43:59 -04:00
|
|
|
/// owned by the inner vectors were not freed prior to the `set_len` call:
|
2016-07-18 22:29:05 -04:00
|
|
|
///
|
|
|
|
/// ```
|
2016-07-22 21:43:59 -04:00
|
|
|
/// let mut vec = vec![vec![1, 0, 0],
|
|
|
|
/// vec![0, 1, 0],
|
|
|
|
/// vec![0, 0, 1]];
|
2016-07-18 22:29:05 -04:00
|
|
|
/// unsafe {
|
|
|
|
/// vec.set_len(0);
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// In this example, the vector gets expanded from zero to four items
|
|
|
|
/// without any memory allocations occurring, resulting in vector
|
|
|
|
/// values of unallocated memory:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let mut vec: Vec<char> = Vec::new();
|
|
|
|
///
|
2014-12-16 23:44:43 -08:00
|
|
|
/// unsafe {
|
2016-07-18 22:29:05 -04:00
|
|
|
/// vec.set_len(4);
|
2014-12-16 23:44:43 -08:00
|
|
|
/// }
|
|
|
|
/// ```
|
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
|
|
|
}
|
|
|
|
|
2017-04-03 09:51:34 -05:00
|
|
|
/// Removes an element from the vector and returns it.
|
2017-04-03 15:48:12 -05:00
|
|
|
///
|
2017-04-03 09:51:34 -05:00
|
|
|
/// The removed element is replaced by the last element of the vector.
|
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
|
2016-02-11 23:35:30 +02:00
|
|
|
/// elements after it 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
|
|
|
///
|
2016-10-08 17:58:53 +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 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-11-24 11:23:48 +13:00
|
|
|
if len == self.buf.cap() {
|
|
|
|
self.buf.double();
|
|
|
|
}
|
2014-02-18 21:36:51 -08:00
|
|
|
|
2015-11-24 11:23:48 +13:00
|
|
|
unsafe {
|
|
|
|
// infallible
|
2014-02-18 21:36:51 -08:00
|
|
|
// 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,
|
2016-02-11 23:35:30 +02:00
|
|
|
/// shifting all elements after it 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);
|
2015-11-24 11:23:48 +13:00
|
|
|
unsafe {
|
|
|
|
// infallible
|
2014-12-16 23:44:43 -08:00
|
|
|
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
|
|
|
///
|
2016-10-25 14:22:54 +13:00
|
|
|
/// In other words, remove all elements `e` such that `f(&e)` returns `false`.
|
2014-12-30 18:51:51 -08:00
|
|
|
/// 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")]
|
2015-11-24 11:23:48 +13: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 {
|
2015-11-24 11:23:48 +13:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-26 18:28:00 +02:00
|
|
|
/// Removes consecutive elements in the vector that resolve to the same key.
|
|
|
|
///
|
|
|
|
/// If the vector is sorted, this removes all duplicates.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let mut vec = vec![10, 20, 21, 30, 20];
|
|
|
|
///
|
|
|
|
/// vec.dedup_by_key(|i| *i / 10);
|
|
|
|
///
|
|
|
|
/// assert_eq!(vec, [10, 20, 30, 20]);
|
|
|
|
/// ```
|
2017-01-25 15:37:20 -08:00
|
|
|
#[stable(feature = "dedup_by", since = "1.16.0")]
|
2016-09-26 18:28:00 +02:00
|
|
|
#[inline]
|
|
|
|
pub fn dedup_by_key<F, K>(&mut self, mut key: F) where F: FnMut(&mut T) -> K, K: PartialEq {
|
|
|
|
self.dedup_by(|a, b| key(a) == key(b))
|
|
|
|
}
|
|
|
|
|
2017-03-14 20:51:00 -07:00
|
|
|
/// Removes consecutive elements in the vector according to a predicate.
|
|
|
|
///
|
|
|
|
/// The `same_bucket` function is passed references to two elements from the vector, and
|
|
|
|
/// returns `true` if the elements compare equal, or `false` if they do not. Only the first
|
|
|
|
/// of adjacent equal items is kept.
|
2016-09-26 18:28:00 +02:00
|
|
|
///
|
|
|
|
/// If the vector is sorted, this removes all duplicates.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::ascii::AsciiExt;
|
|
|
|
///
|
|
|
|
/// let mut vec = vec!["foo", "bar", "Bar", "baz", "bar"];
|
|
|
|
///
|
|
|
|
/// vec.dedup_by(|a, b| a.eq_ignore_ascii_case(b));
|
|
|
|
///
|
|
|
|
/// assert_eq!(vec, ["foo", "bar", "baz", "bar"]);
|
|
|
|
/// ```
|
2017-01-25 15:37:20 -08:00
|
|
|
#[stable(feature = "dedup_by", since = "1.16.0")]
|
2016-09-26 18:28:00 +02:00
|
|
|
pub fn dedup_by<F>(&mut self, mut same_bucket: F) where F: FnMut(&mut T, &mut T) -> bool {
|
|
|
|
unsafe {
|
|
|
|
// Although we have a mutable reference to `self`, we cannot make
|
2016-10-16 14:40:56 +02:00
|
|
|
// *arbitrary* changes. The `same_bucket` calls could panic, so we
|
2016-09-26 18:28:00 +02: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
|
|
|
|
//
|
|
|
|
// 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();
|
|
|
|
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 !same_bucket(&mut *p_r, &mut *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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2015-11-24 11:23:48 +13:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-29 23:01:34 +03:00
|
|
|
/// Returns a place for insertion at the back of the `Vec`.
|
|
|
|
///
|
|
|
|
/// Using this method with placement syntax is equivalent to [`push`](#method.push),
|
|
|
|
/// but may be more efficient.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// #![feature(collection_placement)]
|
|
|
|
/// #![feature(placement_in_syntax)]
|
|
|
|
///
|
|
|
|
/// let mut vec = vec![1, 2];
|
|
|
|
/// vec.place_back() <- 3;
|
|
|
|
/// vec.place_back() <- 4;
|
|
|
|
/// assert_eq!(&vec, &[1, 2, 3, 4]);
|
|
|
|
/// ```
|
|
|
|
#[unstable(feature = "collection_placement",
|
|
|
|
reason = "placement protocol is subject to change",
|
|
|
|
issue = "30172")]
|
|
|
|
pub fn place_back(&mut self) -> PlaceBack<T> {
|
|
|
|
PlaceBack { vec: self }
|
|
|
|
}
|
|
|
|
|
2016-10-08 17:58:53 +02:00
|
|
|
/// Removes the last element from a vector and returns it, or [`None`] if it
|
2015-08-11 17:27:05 -07:00
|
|
|
/// is empty.
|
2014-03-16 16:04:29 -07:00
|
|
|
///
|
2016-10-08 17:58:53 +02:00
|
|
|
/// [`None`]: ../../std/option/enum.Option.html#variant.None
|
|
|
|
///
|
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-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-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) {
|
2015-11-24 11:23:48 +13:00
|
|
|
unsafe {
|
2017-04-19 11:57:29 -07:00
|
|
|
self.append_elements(other.as_slice() as _);
|
2015-11-24 11:23:48 +13:00
|
|
|
other.set_len(0);
|
|
|
|
}
|
2015-01-17 14:30:16 -08:00
|
|
|
}
|
|
|
|
|
2017-04-19 11:57:29 -07:00
|
|
|
/// Appends elements to `Self` from other buffer.
|
|
|
|
#[inline]
|
|
|
|
unsafe fn append_elements(&mut self, other: *const [T]) {
|
|
|
|
let count = (*other).len();
|
|
|
|
self.reserve(count);
|
|
|
|
let len = self.len();
|
|
|
|
ptr::copy_nonoverlapping(other as *const T, self.get_unchecked_mut(len), count);
|
|
|
|
self.len += count;
|
|
|
|
}
|
|
|
|
|
2015-04-27 19:37:13 +02:00
|
|
|
/// Create a draining iterator that removes the specified range in the vector
|
2015-12-09 00:04:54 +02:00
|
|
|
/// and yields the removed items.
|
2015-04-27 19:37:13 +02:00
|
|
|
///
|
2017-01-17 14:39:16 -05:00
|
|
|
/// Note 1: The element range is removed even if the iterator is only
|
|
|
|
/// partially consumed or not consumed at all.
|
2015-12-09 00:04:54 +02:00
|
|
|
///
|
|
|
|
/// Note 2: It is unspecified how many elements are removed from the vector,
|
2015-04-27 19:37:13 +02:00
|
|
|
/// 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-04-27 19:37:13 +02:00
|
|
|
/// let mut v = vec![1, 2, 3];
|
2015-12-09 00:04:54 +02:00
|
|
|
/// let u: Vec<_> = v.drain(1..).collect();
|
|
|
|
/// assert_eq!(v, &[1]);
|
|
|
|
/// assert_eq!(u, &[2, 3]);
|
|
|
|
///
|
|
|
|
/// // A full range clears the vector
|
|
|
|
/// v.drain(..);
|
2015-04-27 19:37:13 +02:00
|
|
|
/// assert_eq!(v, &[]);
|
2014-03-16 16:04:29 -07:00
|
|
|
/// ```
|
2015-12-02 17:31:49 -08:00
|
|
|
#[stable(feature = "drain", since = "1.6.0")]
|
2015-11-24 11:23:48 +13:00
|
|
|
pub fn drain<R>(&mut self, range: R) -> Drain<T>
|
|
|
|
where R: RangeArgument<usize>
|
|
|
|
{
|
2015-04-27 19:37:13 +02:00
|
|
|
// 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();
|
2016-12-23 19:15:56 +00:00
|
|
|
let start = match range.start() {
|
|
|
|
Included(&n) => n,
|
|
|
|
Excluded(&n) => n + 1,
|
|
|
|
Unbounded => 0,
|
|
|
|
};
|
|
|
|
let end = match range.end() {
|
|
|
|
Included(&n) => n + 1,
|
|
|
|
Excluded(&n) => n,
|
|
|
|
Unbounded => len,
|
|
|
|
};
|
2015-04-27 19:37:13 +02:00
|
|
|
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).
|
2015-11-24 11:23:48 +13:00
|
|
|
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,
|
2016-07-21 20:55:19 -07:00
|
|
|
iter: range_slice.iter(),
|
|
|
|
vec: Shared::new(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
|
|
|
///
|
2017-02-11 20:29:29 +11:00
|
|
|
/// Note that this method has no effect on the allocated capacity
|
|
|
|
/// of the vector.
|
|
|
|
///
|
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-11-24 11:23:48 +13: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")]
|
2015-11-24 11:23:48 +13: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-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-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 {
|
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-11-24 11:23:48 +13:00
|
|
|
ptr::copy_nonoverlapping(self.as_ptr().offset(at as isize),
|
|
|
|
other.as_mut_ptr(),
|
|
|
|
other.len());
|
2015-01-24 17:23:26 -08:00
|
|
|
}
|
|
|
|
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
|
|
|
///
|
2015-10-29 21:39:05 +02:00
|
|
|
/// If `new_len` is greater than `len()`, the `Vec` is extended by the
|
|
|
|
/// difference, with each additional slot filled with `value`.
|
|
|
|
/// If `new_len` is less than `len()`, the `Vec` is simply truncated.
|
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
|
|
|
///
|
|
|
|
/// ```
|
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
|
|
|
/// ```
|
std: Stabilize library APIs for 1.5
This commit stabilizes and deprecates library APIs whose FCP has closed in the
last cycle, specifically:
Stabilized APIs:
* `fs::canonicalize`
* `Path::{metadata, symlink_metadata, canonicalize, read_link, read_dir, exists,
is_file, is_dir}` - all moved to inherent methods from the `PathExt` trait.
* `Formatter::fill`
* `Formatter::width`
* `Formatter::precision`
* `Formatter::sign_plus`
* `Formatter::sign_minus`
* `Formatter::alternate`
* `Formatter::sign_aware_zero_pad`
* `string::ParseError`
* `Utf8Error::valid_up_to`
* `Iterator::{cmp, partial_cmp, eq, ne, lt, le, gt, ge}`
* `<[T]>::split_{first,last}{,_mut}`
* `Condvar::wait_timeout` - note that `wait_timeout_ms` is not yet deprecated
but will be once 1.5 is released.
* `str::{R,}MatchIndices`
* `str::{r,}match_indices`
* `char::from_u32_unchecked`
* `VecDeque::insert`
* `VecDeque::shrink_to_fit`
* `VecDeque::as_slices`
* `VecDeque::as_mut_slices`
* `VecDeque::swap_remove_front` - (renamed from `swap_front_remove`)
* `VecDeque::swap_remove_back` - (renamed from `swap_back_remove`)
* `Vec::resize`
* `str::slice_mut_unchecked`
* `FileTypeExt`
* `FileTypeExt::{is_block_device, is_char_device, is_fifo, is_socket}`
* `BinaryHeap::from` - `from_vec` deprecated in favor of this
* `BinaryHeap::into_vec` - plus a `Into` impl
* `BinaryHeap::into_sorted_vec`
Deprecated APIs
* `slice::ref_slice`
* `slice::mut_ref_slice`
* `iter::{range_inclusive, RangeInclusive}`
* `std::dynamic_lib`
Closes #27706
Closes #27725
cc #27726 (align not stabilized yet)
Closes #27734
Closes #27737
Closes #27742
Closes #27743
Closes #27772
Closes #27774
Closes #27777
Closes #27781
cc #27788 (a few remaining methods though)
Closes #27790
Closes #27793
Closes #27796
Closes #27810
cc #28147 (not all parts stabilized)
2015-10-22 16:28:45 -07:00
|
|
|
#[stable(feature = "vec_resize", since = "1.5.0")]
|
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 {
|
2016-09-08 23:48:08 +02:00
|
|
|
let mut ptr = self.as_mut_ptr().offset(self.len() as isize);
|
|
|
|
// Use SetLenOnDrop to work around bug where compiler
|
2017-02-13 18:41:45 +08:00
|
|
|
// may not realize the store through `ptr` through self.set_len()
|
2016-09-08 23:48:08 +02:00
|
|
|
// don't alias.
|
|
|
|
let mut local_len = SetLenOnDrop::new(&mut self.len);
|
|
|
|
|
2015-07-07 13:50:23 +02:00
|
|
|
// Write all elements except the last one
|
2016-09-08 23:48:08 +02:00
|
|
|
for _ in 1..n {
|
2015-07-07 13:50:23 +02:00
|
|
|
ptr::write(ptr, value.clone());
|
|
|
|
ptr = ptr.offset(1);
|
|
|
|
// Increment the length in every step in case clone() panics
|
2016-09-08 23:48:08 +02:00
|
|
|
local_len.increment_len(1);
|
2015-07-07 13:50:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if n > 0 {
|
|
|
|
// We can write the last element directly without cloning needlessly
|
|
|
|
ptr::write(ptr, value);
|
2016-09-08 23:48:08 +02:00
|
|
|
local_len.increment_len(1);
|
2015-07-07 13:50:23 +02:00
|
|
|
}
|
2016-09-08 23:48:08 +02:00
|
|
|
|
|
|
|
// len set by scope guard
|
2015-07-07 13:50:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-25 10:09:20 +08:00
|
|
|
/// Clones and appends all elements in a slice to the `Vec`.
|
2015-12-02 17:31:49 -08:00
|
|
|
///
|
|
|
|
/// Iterates over the slice `other`, clones each element, and then appends
|
|
|
|
/// it to this `Vec`. The `other` vector is traversed in-order.
|
|
|
|
///
|
|
|
|
/// Note that this function is same as `extend` except that it is
|
|
|
|
/// specialized to work with slices instead. If and when Rust gets
|
|
|
|
/// specialization this function will likely be deprecated (but still
|
|
|
|
/// available).
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let mut vec = vec![1];
|
|
|
|
/// vec.extend_from_slice(&[2, 3, 4]);
|
|
|
|
/// assert_eq!(vec, [1, 2, 3, 4]);
|
|
|
|
/// ```
|
|
|
|
#[stable(feature = "vec_extend_from_slice", since = "1.6.0")]
|
|
|
|
pub fn extend_from_slice(&mut self, other: &[T]) {
|
2016-12-05 21:48:53 +01:00
|
|
|
self.spec_extend(other.iter())
|
2014-11-06 12:24:47 -05:00
|
|
|
}
|
2014-02-28 12:55:51 -08:00
|
|
|
}
|
|
|
|
|
2016-09-08 23:48:08 +02:00
|
|
|
// Set the length of the vec when the `SetLenOnDrop` value goes out of scope.
|
|
|
|
//
|
|
|
|
// The idea is: The length field in SetLenOnDrop is a local variable
|
|
|
|
// that the optimizer will see does not alias with any stores through the Vec's data
|
|
|
|
// pointer. This is a workaround for alias analysis issue #32155
|
|
|
|
struct SetLenOnDrop<'a> {
|
|
|
|
len: &'a mut usize,
|
|
|
|
local_len: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> SetLenOnDrop<'a> {
|
|
|
|
#[inline]
|
|
|
|
fn new(len: &'a mut usize) -> Self {
|
|
|
|
SetLenOnDrop { local_len: *len, len: len }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn increment_len(&mut self, increment: usize) {
|
|
|
|
self.local_len += increment;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Drop for SetLenOnDrop<'a> {
|
|
|
|
#[inline]
|
|
|
|
fn drop(&mut self) {
|
|
|
|
*self.len = self.local_len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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")]
|
2016-09-26 18:26:49 +02:00
|
|
|
#[inline]
|
2014-03-04 10:39:49 -08:00
|
|
|
pub fn dedup(&mut self) {
|
2016-09-26 18:26:49 +02:00
|
|
|
self.dedup_by(|a, b| a == b)
|
2014-12-16 23:44:43 -08:00
|
|
|
}
|
2016-12-03 12:47:27 -04:00
|
|
|
|
|
|
|
/// Removes the first instance of `item` from the vector if the item exists.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2017-04-06 12:57:40 +01:00
|
|
|
/// # #![feature(vec_remove_item)]
|
2016-12-03 12:47:27 -04:00
|
|
|
/// let mut vec = vec![1, 2, 3, 1];
|
|
|
|
///
|
|
|
|
/// vec.remove_item(&1);
|
|
|
|
///
|
|
|
|
/// assert_eq!(vec, vec![2, 3, 1]);
|
|
|
|
/// ```
|
|
|
|
#[unstable(feature = "vec_remove_item", reason = "recently added", issue = "40062")]
|
|
|
|
pub fn remove_item(&mut self, item: &T) -> Option<T> {
|
|
|
|
let pos = match self.iter().position(|x| *x == *item) {
|
|
|
|
Some(x) => x,
|
|
|
|
None => return None,
|
|
|
|
};
|
|
|
|
Some(self.remove(pos))
|
|
|
|
}
|
2014-12-16 23:44:43 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// 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> {
|
2017-03-09 17:53:01 -08:00
|
|
|
<T as SpecFromElem>::from_elem(elem, n)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Specialization trait used for Vec::from_elem
|
|
|
|
trait SpecFromElem: Sized {
|
|
|
|
fn from_elem(elem: Self, n: usize) -> Vec<Self>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Clone> SpecFromElem for T {
|
|
|
|
default fn from_elem(elem: Self, n: usize) -> Vec<Self> {
|
|
|
|
let mut v = Vec::with_capacity(n);
|
|
|
|
v.extend_with_element(n, elem);
|
|
|
|
v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SpecFromElem for u8 {
|
|
|
|
#[inline]
|
|
|
|
fn from_elem(elem: u8, n: usize) -> Vec<u8> {
|
|
|
|
if elem == 0 {
|
|
|
|
return Vec {
|
|
|
|
buf: RawVec::with_capacity_zeroed(n),
|
|
|
|
len: n,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
let mut v = Vec::with_capacity(n);
|
|
|
|
ptr::write_bytes(v.as_mut_ptr(), elem, n);
|
|
|
|
v.set_len(n);
|
|
|
|
v
|
|
|
|
}
|
|
|
|
}
|
2015-02-17 17:44:46 +01:00
|
|
|
}
|
|
|
|
|
2017-03-10 08:19:42 -08:00
|
|
|
macro_rules! impl_spec_from_elem {
|
|
|
|
($t: ty, $is_zero: expr) => {
|
|
|
|
impl SpecFromElem for $t {
|
|
|
|
#[inline]
|
|
|
|
fn from_elem(elem: $t, n: usize) -> Vec<$t> {
|
|
|
|
if $is_zero(elem) {
|
|
|
|
return Vec {
|
|
|
|
buf: RawVec::with_capacity_zeroed(n),
|
|
|
|
len: n,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let mut v = Vec::with_capacity(n);
|
|
|
|
v.extend_with_element(n, elem);
|
|
|
|
v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_spec_from_elem!(i8, |x| x == 0);
|
|
|
|
impl_spec_from_elem!(i16, |x| x == 0);
|
|
|
|
impl_spec_from_elem!(i32, |x| x == 0);
|
|
|
|
impl_spec_from_elem!(i64, |x| x == 0);
|
|
|
|
impl_spec_from_elem!(i128, |x| x == 0);
|
|
|
|
impl_spec_from_elem!(isize, |x| x == 0);
|
|
|
|
|
|
|
|
impl_spec_from_elem!(u16, |x| x == 0);
|
|
|
|
impl_spec_from_elem!(u32, |x| x == 0);
|
|
|
|
impl_spec_from_elem!(u64, |x| x == 0);
|
|
|
|
impl_spec_from_elem!(u128, |x| x == 0);
|
|
|
|
impl_spec_from_elem!(usize, |x| x == 0);
|
|
|
|
|
|
|
|
impl_spec_from_elem!(f32, |x: f32| x == 0. && x.is_sign_positive());
|
|
|
|
impl_spec_from_elem!(f64, |x: f64| x == 0. && x.is_sign_positive());
|
|
|
|
|
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")]
|
2015-11-24 11:23:48 +13:00
|
|
|
impl<T: Clone> Clone for Vec<T> {
|
2015-03-11 19:44:02 -05:00
|
|
|
#[cfg(not(test))]
|
2015-11-24 11:23:48 +13:00
|
|
|
fn clone(&self) -> Vec<T> {
|
|
|
|
<[T]>::to_vec(&**self)
|
|
|
|
}
|
2015-03-10 23:13:29 -05:00
|
|
|
|
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>) {
|
2017-04-01 19:33:45 -07:00
|
|
|
other.as_slice().clone_into(self);
|
2014-12-16 23:44:43 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2016-01-28 11:20:48 -05:00
|
|
|
#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
|
|
|
|
impl<T> ops::Index<ops::RangeInclusive<usize>> for Vec<T> {
|
|
|
|
type Output = [T];
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn index(&self, index: ops::RangeInclusive<usize>) -> &[T] {
|
|
|
|
Index::index(&**self, index)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
|
|
|
|
impl<T> ops::Index<ops::RangeToInclusive<usize>> for Vec<T> {
|
|
|
|
type Output = [T];
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn index(&self, index: ops::RangeToInclusive<usize>) -> &[T] {
|
|
|
|
Index::index(&**self, index)
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
2016-01-28 11:20:48 -05:00
|
|
|
#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
|
|
|
|
impl<T> ops::IndexMut<ops::RangeInclusive<usize>> for Vec<T> {
|
|
|
|
#[inline]
|
|
|
|
fn index_mut(&mut self, index: ops::RangeInclusive<usize>) -> &mut [T] {
|
|
|
|
IndexMut::index_mut(&mut **self, index)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
|
|
|
|
impl<T> ops::IndexMut<ops::RangeToInclusive<usize>> for Vec<T> {
|
|
|
|
#[inline]
|
|
|
|
fn index_mut(&mut self, index: ops::RangeToInclusive<usize>) -> &mut [T] {
|
|
|
|
IndexMut::index_mut(&mut **self, index)
|
|
|
|
}
|
|
|
|
}
|
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]
|
2016-03-28 13:55:47 -04:00
|
|
|
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Vec<T> {
|
2017-03-17 22:36:21 -07:00
|
|
|
<Self as SpecExtend<T, I::IntoIter>>::from_iter(iter.into_iter())
|
2014-12-16 23:44:43 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2016-08-07 10:08:40 -04:00
|
|
|
let begin = self.as_mut_ptr();
|
|
|
|
assume(!begin.is_null());
|
2015-05-15 15:20:42 +02:00
|
|
|
let end = if mem::size_of::<T>() == 0 {
|
2016-08-07 10:08:40 -04:00
|
|
|
arith_offset(begin as *const i8, self.len() as isize) as *const T
|
2015-05-15 15:20:42 +02:00
|
|
|
} else {
|
2016-08-07 10:08:40 -04:00
|
|
|
begin.offset(self.len() as isize) as *const T
|
2015-05-15 15:20:42 +02:00
|
|
|
};
|
2016-08-16 20:45:07 -04:00
|
|
|
let cap = self.buf.cap();
|
2015-05-15 15:20:42 +02:00
|
|
|
mem::forget(self);
|
2015-11-24 11:23:48 +13:00
|
|
|
IntoIter {
|
2016-08-16 20:45:07 -04:00
|
|
|
buf: Shared::new(begin),
|
|
|
|
cap: cap,
|
2015-11-24 11:23:48 +13:00
|
|
|
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]
|
2016-03-28 13:55:47 -04:00
|
|
|
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
|
2017-03-17 22:36:21 -07:00
|
|
|
<Self as SpecExtend<T, I::IntoIter>>::spec_extend(self, iter.into_iter())
|
2015-02-22 19:22:50 +02:00
|
|
|
}
|
|
|
|
}
|
2015-02-22 17:22:48 +02:00
|
|
|
|
2016-11-13 01:09:27 +01:00
|
|
|
// Specialization trait used for Vec::from_iter and Vec::extend
|
2016-12-05 21:48:53 +01:00
|
|
|
trait SpecExtend<T, I> {
|
2016-11-13 01:09:27 +01:00
|
|
|
fn from_iter(iter: I) -> Self;
|
2016-11-11 12:54:10 +01:00
|
|
|
fn spec_extend(&mut self, iter: I);
|
2016-10-20 14:07:31 +02:00
|
|
|
}
|
|
|
|
|
2016-12-05 21:48:53 +01:00
|
|
|
impl<T, I> SpecExtend<T, I> for Vec<T>
|
2016-11-11 12:54:10 +01:00
|
|
|
where I: Iterator<Item=T>,
|
2016-10-20 14:07:31 +02:00
|
|
|
{
|
2016-11-13 01:09:27 +01:00
|
|
|
default fn from_iter(mut iterator: I) -> Self {
|
|
|
|
// 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.
|
|
|
|
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
|
|
|
|
}
|
|
|
|
};
|
2017-03-17 22:36:21 -07:00
|
|
|
<Vec<T> as SpecExtend<T, I>>::spec_extend(&mut vector, iterator);
|
2016-11-13 01:09:27 +01:00
|
|
|
vector
|
|
|
|
}
|
|
|
|
|
2016-11-11 12:54:10 +01:00
|
|
|
default fn spec_extend(&mut self, iter: I) {
|
|
|
|
self.extend_desugared(iter)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-05 21:48:53 +01:00
|
|
|
impl<T, I> SpecExtend<T, I> for Vec<T>
|
2016-11-11 12:54:10 +01:00
|
|
|
where I: TrustedLen<Item=T>,
|
|
|
|
{
|
2017-03-17 22:36:21 -07:00
|
|
|
default fn from_iter(iterator: I) -> Self {
|
2016-11-13 01:09:27 +01:00
|
|
|
let mut vector = Vec::new();
|
|
|
|
vector.spec_extend(iterator);
|
|
|
|
vector
|
|
|
|
}
|
|
|
|
|
2017-04-19 11:57:29 -07:00
|
|
|
default fn spec_extend(&mut self, iterator: I) {
|
2016-11-11 12:54:10 +01:00
|
|
|
// This is the case for a TrustedLen iterator.
|
|
|
|
let (low, high) = iterator.size_hint();
|
2016-10-21 19:18:08 +02:00
|
|
|
if let Some(high_value) = high {
|
|
|
|
debug_assert_eq!(low, high_value,
|
|
|
|
"TrustedLen iterator's size hint is not exact: {:?}",
|
|
|
|
(low, high));
|
|
|
|
}
|
2016-11-11 12:54:10 +01:00
|
|
|
if let Some(additional) = high {
|
2016-10-20 14:07:31 +02:00
|
|
|
self.reserve(additional);
|
2015-02-22 23:46:36 +02:00
|
|
|
unsafe {
|
2016-10-20 14:07:31 +02:00
|
|
|
let mut ptr = self.as_mut_ptr().offset(self.len() as isize);
|
|
|
|
let mut local_len = SetLenOnDrop::new(&mut self.len);
|
|
|
|
for element in iterator {
|
|
|
|
ptr::write(ptr, element);
|
|
|
|
ptr = ptr.offset(1);
|
|
|
|
// NB can't overflow since we would have had to alloc the address space
|
|
|
|
local_len.increment_len(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2016-11-11 12:54:10 +01:00
|
|
|
self.extend_desugared(iterator)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-17 22:36:21 -07:00
|
|
|
impl<T> SpecExtend<T, IntoIter<T>> for Vec<T> {
|
|
|
|
fn from_iter(iterator: IntoIter<T>) -> Self {
|
|
|
|
// A common case is passing a vector into a function which immediately
|
|
|
|
// re-collects into a vector. We can short circuit this if the IntoIter
|
|
|
|
// has not been advanced at all.
|
|
|
|
if *iterator.buf == iterator.ptr as *mut T {
|
|
|
|
unsafe {
|
|
|
|
let vec = Vec::from_raw_parts(*iterator.buf as *mut T,
|
|
|
|
iterator.len(),
|
|
|
|
iterator.cap);
|
|
|
|
mem::forget(iterator);
|
|
|
|
vec
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let mut vector = Vec::new();
|
|
|
|
vector.spec_extend(iterator);
|
|
|
|
vector
|
|
|
|
}
|
|
|
|
}
|
2017-04-19 11:57:29 -07:00
|
|
|
|
|
|
|
fn spec_extend(&mut self, mut iterator: IntoIter<T>) {
|
|
|
|
unsafe {
|
|
|
|
self.append_elements(iterator.as_slice() as _);
|
|
|
|
}
|
|
|
|
iterator.ptr = iterator.end;
|
|
|
|
}
|
2017-03-17 22:36:21 -07:00
|
|
|
}
|
|
|
|
|
2016-12-05 21:48:53 +01:00
|
|
|
impl<'a, T: 'a, I> SpecExtend<&'a T, I> for Vec<T>
|
|
|
|
where I: Iterator<Item=&'a T>,
|
|
|
|
T: Clone,
|
|
|
|
{
|
|
|
|
default fn from_iter(iterator: I) -> Self {
|
|
|
|
SpecExtend::from_iter(iterator.cloned())
|
|
|
|
}
|
|
|
|
|
|
|
|
default fn spec_extend(&mut self, iterator: I) {
|
|
|
|
self.spec_extend(iterator.cloned())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T: 'a> SpecExtend<&'a T, slice::Iter<'a, T>> for Vec<T>
|
|
|
|
where T: Copy,
|
|
|
|
{
|
|
|
|
fn spec_extend(&mut self, iterator: slice::Iter<'a, T>) {
|
|
|
|
let slice = iterator.as_slice();
|
|
|
|
self.reserve(slice.len());
|
|
|
|
unsafe {
|
|
|
|
let len = self.len();
|
|
|
|
self.set_len(len + slice.len());
|
|
|
|
self.get_unchecked_mut(len..).copy_from_slice(slice);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-11 12:54:10 +01:00
|
|
|
impl<T> Vec<T> {
|
|
|
|
fn extend_desugared<I: Iterator<Item = T>>(&mut self, mut iterator: I) {
|
|
|
|
// This is the case for a general iterator.
|
|
|
|
//
|
|
|
|
// This function should be the moral equivalent of:
|
|
|
|
//
|
|
|
|
// for item in iterator {
|
|
|
|
// self.push(item);
|
|
|
|
// }
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
ptr::write(self.get_unchecked_mut(len), element);
|
|
|
|
// NB can't overflow since we would have had to alloc the address space
|
|
|
|
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> {
|
2015-11-24 11:23:48 +13:00
|
|
|
fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
|
2016-12-05 21:48:53 +01:00
|
|
|
self.spec_extend(iter.into_iter())
|
2015-06-03 12:38:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-02 17:31:49 -08:00
|
|
|
macro_rules! __impl_slice_eq1 {
|
|
|
|
($Lhs: ty, $Rhs: ty) => {
|
|
|
|
__impl_slice_eq1! { $Lhs, $Rhs, Sized }
|
|
|
|
};
|
|
|
|
($Lhs: ty, $Rhs: ty, $Bound: ident) => {
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
impl<'a, 'b, A: $Bound, B> PartialEq<$Rhs> for $Lhs where A: PartialEq<B> {
|
|
|
|
#[inline]
|
|
|
|
fn eq(&self, other: &$Rhs) -> bool { self[..] == other[..] }
|
|
|
|
#[inline]
|
|
|
|
fn ne(&self, other: &$Rhs) -> bool { self[..] != other[..] }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-02-16 09:18:18 -08:00
|
|
|
/// Implements comparison of vectors, lexicographically.
|
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> {}
|
|
|
|
|
2017-02-16 09:18:18 -08:00
|
|
|
/// Implements ordering of vectors, lexicographically.
|
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")]
|
2016-12-28 17:47:10 -05:00
|
|
|
unsafe impl<#[may_dangle] T> Drop for Vec<T> {
|
2014-01-21 09:35:11 -05:00
|
|
|
fn drop(&mut self) {
|
2016-08-23 10:39:30 +03:00
|
|
|
unsafe {
|
|
|
|
// use drop for [T]
|
|
|
|
ptr::drop_in_place(&mut self[..]);
|
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> {
|
2016-09-11 17:00:09 +05:30
|
|
|
/// Creates an empty `Vec<T>`.
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-18 09:14:54 -07:00
|
|
|
#[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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-18 09:14:54 -07:00
|
|
|
#[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
|
|
|
}
|
|
|
|
|
Implement `From<Cow<str>> for String` and `From<Cow<[T]>> for Vec<T>`.
Motivation: the `selectors` crate is generic over a string type,
in order to support all of `String`, `string_cache::Atom`, and
`gecko_string_cache::Atom`. Multiple trait bounds are used
for the various operations done with these strings.
One of these operations is creating a string (as efficiently as possible,
re-using an existing memory allocation if possible) from `Cow<str>`.
The `std::convert::From` trait seems natural for this, but
the relevant implementation was missing before this PR.
To work around this I’ve added a `FromCowStr` trait in `selectors`,
but with trait coherence that means one of `selectors` or `string_cache`
needs to depend on the other to implement this trait.
Using a trait from `std` would solve this.
The `Vec<T>` implementation is just added for consistency.
I also tried a more general
`impl<'a, O, B: ?Sized + ToOwned<Owned=O>> From<Cow<'a, B>> for O`,
but (the compiler thinks?) it conflicts with `From<T> for T` the impl
(after moving all of `collections::borrow` into `core::borrow`
to work around trait coherence).
2016-10-21 16:51:59 +02:00
|
|
|
#[stable(feature = "vec_from_cow_slice", since = "1.14.0")]
|
|
|
|
impl<'a, T> From<Cow<'a, [T]>> for Vec<T> where [T]: ToOwned<Owned=Vec<T>> {
|
|
|
|
fn from(s: Cow<'a, [T]>) -> Vec<T> {
|
|
|
|
s.into_owned()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-13 20:37:42 -05:00
|
|
|
// note: test pulls in libstd, which causes errors here
|
|
|
|
#[cfg(not(test))]
|
|
|
|
#[stable(feature = "vec_from_box", since = "1.17.0")]
|
|
|
|
impl<T> From<Box<[T]>> for Vec<T> {
|
|
|
|
fn from(s: Box<[T]>) -> Vec<T> {
|
|
|
|
s.into_vec()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "box_from_vec", since = "1.17.0")]
|
|
|
|
impl<T> Into<Box<[T]>> for Vec<T> {
|
|
|
|
fn into(self) -> Box<[T]> {
|
|
|
|
self.into_boxed_slice()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-02-03 13:57:02 +01:00
|
|
|
#[stable(feature = "cow_from_vec", since = "1.7.0")]
|
|
|
|
impl<'a, T: Clone> From<&'a [T]> for Cow<'a, [T]> {
|
|
|
|
fn from(s: &'a [T]) -> Cow<'a, [T]> {
|
|
|
|
Cow::Borrowed(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "cow_from_vec", since = "1.7.0")]
|
|
|
|
impl<'a, T: Clone> From<Vec<T>> for Cow<'a, [T]> {
|
|
|
|
fn from(v: Vec<T>) -> Cow<'a, [T]> {
|
|
|
|
Cow::Owned(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-11-24 11:23:48 +13: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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Iterators
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-03-16 16:04:29 -07:00
|
|
|
/// An iterator that moves out of a vector.
|
2016-07-14 09:34:43 -04:00
|
|
|
///
|
|
|
|
/// This `struct` is created by the `into_iter` method on [`Vec`][`Vec`] (provided
|
|
|
|
/// by the [`IntoIterator`] trait).
|
|
|
|
///
|
|
|
|
/// [`Vec`]: struct.Vec.html
|
|
|
|
/// [`IntoIterator`]: ../../std/iter/trait.IntoIterator.html
|
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> {
|
2016-08-16 20:45:07 -04:00
|
|
|
buf: Shared<T>,
|
|
|
|
cap: usize,
|
|
|
|
ptr: *const T,
|
2015-11-24 11:23:48 +13:00
|
|
|
end: *const T,
|
2014-01-21 09:35:11 -05:00
|
|
|
}
|
|
|
|
|
2016-09-28 11:28:42 +01:00
|
|
|
#[stable(feature = "vec_intoiter_debug", since = "1.13.0")]
|
2016-08-15 23:45:12 -04:00
|
|
|
impl<T: fmt::Debug> fmt::Debug for IntoIter<T> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
f.debug_tuple("IntoIter")
|
|
|
|
.field(&self.as_slice())
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-07 00:30:17 -04:00
|
|
|
impl<T> IntoIter<T> {
|
|
|
|
/// Returns the remaining items of this iterator as a slice.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2016-09-21 17:07:45 +02:00
|
|
|
/// ```
|
2016-08-07 00:30:17 -04:00
|
|
|
/// let vec = vec!['a', 'b', 'c'];
|
|
|
|
/// let mut into_iter = vec.into_iter();
|
|
|
|
/// assert_eq!(into_iter.as_slice(), &['a', 'b', 'c']);
|
|
|
|
/// let _ = into_iter.next().unwrap();
|
|
|
|
/// assert_eq!(into_iter.as_slice(), &['b', 'c']);
|
|
|
|
/// ```
|
2016-12-14 12:15:22 -08:00
|
|
|
#[stable(feature = "vec_into_iter_as_slice", since = "1.15.0")]
|
2016-08-07 00:30:17 -04:00
|
|
|
pub fn as_slice(&self) -> &[T] {
|
|
|
|
unsafe {
|
|
|
|
slice::from_raw_parts(self.ptr, self.len())
|
|
|
|
}
|
|
|
|
}
|
2016-08-07 10:08:40 -04:00
|
|
|
|
|
|
|
/// Returns the remaining items of this iterator as a mutable slice.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2016-09-21 17:07:45 +02:00
|
|
|
/// ```
|
2016-08-07 10:08:40 -04:00
|
|
|
/// let vec = vec!['a', 'b', 'c'];
|
|
|
|
/// let mut into_iter = vec.into_iter();
|
|
|
|
/// assert_eq!(into_iter.as_slice(), &['a', 'b', 'c']);
|
|
|
|
/// into_iter.as_mut_slice()[2] = 'z';
|
|
|
|
/// assert_eq!(into_iter.next().unwrap(), 'a');
|
|
|
|
/// assert_eq!(into_iter.next().unwrap(), 'b');
|
|
|
|
/// assert_eq!(into_iter.next().unwrap(), 'z');
|
|
|
|
/// ```
|
2016-12-14 12:15:22 -08:00
|
|
|
#[stable(feature = "vec_into_iter_as_slice", since = "1.15.0")]
|
2017-02-02 11:26:44 -08:00
|
|
|
pub fn as_mut_slice(&mut self) -> &mut [T] {
|
2016-08-07 10:08:40 -04:00
|
|
|
unsafe {
|
2016-08-16 20:45:07 -04:00
|
|
|
slice::from_raw_parts_mut(self.ptr as *mut T, self.len())
|
2016-08-07 10:08:40 -04:00
|
|
|
}
|
|
|
|
}
|
2016-08-07 00:30:17 -04:00
|
|
|
}
|
|
|
|
|
2015-11-16 19:54:28 +03:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-11-24 11:23:48 +13:00
|
|
|
unsafe impl<T: Send> Send for IntoIter<T> {}
|
2015-11-16 19:54:28 +03:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-11-24 11:23:48 +13:00
|
|
|
unsafe impl<T: Sync> Sync for IntoIter<T> {}
|
2015-01-12 17:12:20 -02: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> 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 {
|
2016-08-07 10:08:40 -04:00
|
|
|
if self.ptr as *const _ == self.end {
|
2015-05-15 15:20:42 +02:00
|
|
|
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.
|
2016-08-07 10:08:40 -04:00
|
|
|
self.ptr = arith_offset(self.ptr as *const i8, 1) as *mut T;
|
2015-05-15 15:20:42 +02:00
|
|
|
|
|
|
|
// 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>) {
|
2017-03-31 13:52:46 +01:00
|
|
|
let exact = match self.ptr.offset_to(self.end) {
|
|
|
|
Some(x) => x as usize,
|
|
|
|
None => (self.end as usize).wrapping_sub(self.ptr as usize),
|
|
|
|
};
|
2014-09-04 15:25:23 -04:00
|
|
|
(exact, Some(exact))
|
2014-01-21 09:35:11 -05:00
|
|
|
}
|
2015-05-02 14:01:38 +09:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn count(self) -> usize {
|
2016-06-23 12:26:15 +02:00
|
|
|
self.len()
|
2015-05-02 14:01:38 +09: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> 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
|
2016-08-07 10:08:40 -04:00
|
|
|
self.end = arith_offset(self.end as *const i8, -1) as *mut T;
|
2015-05-15 15:20:42 +02:00
|
|
|
|
|
|
|
// 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")]
|
2016-11-22 23:31:31 +01:00
|
|
|
impl<T> ExactSizeIterator for IntoIter<T> {
|
|
|
|
fn is_empty(&self) -> bool {
|
|
|
|
self.ptr == self.end
|
|
|
|
}
|
|
|
|
}
|
2014-09-14 23:25:08 -04:00
|
|
|
|
2016-08-13 14:42:36 -04:00
|
|
|
#[unstable(feature = "fused", issue = "35602")]
|
|
|
|
impl<T> FusedIterator for IntoIter<T> {}
|
|
|
|
|
2016-11-04 00:24:59 +01:00
|
|
|
#[unstable(feature = "trusted_len", issue = "37572")]
|
2016-10-27 00:18:13 +02:00
|
|
|
unsafe impl<T> TrustedLen for IntoIter<T> {}
|
|
|
|
|
2016-02-16 16:00:38 +01:00
|
|
|
#[stable(feature = "vec_into_iter_clone", since = "1.8.0")]
|
|
|
|
impl<T: Clone> Clone for IntoIter<T> {
|
|
|
|
fn clone(&self) -> IntoIter<T> {
|
2016-08-07 00:30:17 -04:00
|
|
|
self.as_slice().to_owned().into_iter()
|
2016-02-16 16:00:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2016-12-28 17:47:10 -05:00
|
|
|
unsafe impl<#[may_dangle] T> Drop for IntoIter<T> {
|
2014-01-21 09:35:11 -05:00
|
|
|
fn drop(&mut self) {
|
|
|
|
// destroy the remaining elements
|
2016-08-16 20:45:07 -04:00
|
|
|
for _x in self.by_ref() {}
|
2015-07-09 21:57:21 -07:00
|
|
|
|
|
|
|
// RawVec handles deallocation
|
2017-03-14 21:10:02 -07:00
|
|
|
let _ = unsafe { RawVec::from_raw_parts(self.buf.as_mut_ptr(), self.cap) };
|
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>`.
|
2016-07-14 09:34:43 -04:00
|
|
|
///
|
|
|
|
/// This `struct` is created by the [`drain`] method on [`Vec`].
|
|
|
|
///
|
|
|
|
/// [`drain`]: struct.Vec.html#method.drain
|
|
|
|
/// [`Vec`]: struct.Vec.html
|
2015-12-02 17:31:49 -08:00
|
|
|
#[stable(feature = "drain", since = "1.6.0")]
|
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
|
2016-07-21 20:55:19 -07:00
|
|
|
iter: slice::Iter<'a, T>,
|
|
|
|
vec: Shared<Vec<T>>,
|
2014-12-16 17:45:03 -05:00
|
|
|
}
|
|
|
|
|
2017-01-21 00:33:38 +01:00
|
|
|
#[stable(feature = "collection_debug", since = "1.17.0")]
|
2017-01-11 23:12:49 +01:00
|
|
|
impl<'a, T: 'a + fmt::Debug> fmt::Debug for Drain<'a, T> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2017-01-21 00:33:38 +01:00
|
|
|
f.debug_tuple("Drain")
|
2017-01-11 23:12:49 +01:00
|
|
|
.field(&self.iter.as_slice())
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-02 17:31:49 -08:00
|
|
|
#[stable(feature = "drain", since = "1.6.0")]
|
2015-02-25 20:52:18 +08:00
|
|
|
unsafe impl<'a, T: Sync> Sync for Drain<'a, T> {}
|
2015-12-02 17:31:49 -08:00
|
|
|
#[stable(feature = "drain", since = "1.6.0")]
|
2015-02-25 20:52:18 +08:00
|
|
|
unsafe impl<'a, T: Send> Send for Drain<'a, T> {}
|
|
|
|
|
2016-09-28 11:28:42 +01:00
|
|
|
#[stable(feature = "drain", since = "1.6.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-11-24 11:23:48 +13:00
|
|
|
self.iter.next().map(|elt| unsafe { ptr::read(elt as *const _) })
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-28 11:28:42 +01:00
|
|
|
#[stable(feature = "drain", since = "1.6.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-11-24 11:23:48 +13:00
|
|
|
self.iter.next_back().map(|elt| unsafe { ptr::read(elt as *const _) })
|
2014-12-16 17:45:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-28 11:28:42 +01:00
|
|
|
#[stable(feature = "drain", since = "1.6.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
|
2015-11-24 11:23:48 +13:00
|
|
|
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 {
|
2017-03-14 21:10:02 -07:00
|
|
|
let source_vec = &mut *self.vec.as_mut_ptr();
|
2015-04-27 19:37:13 +02:00
|
|
|
// 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
|
|
|
|
2016-09-28 11:28:42 +01:00
|
|
|
#[stable(feature = "drain", since = "1.6.0")]
|
2016-11-22 23:31:31 +01:00
|
|
|
impl<'a, T> ExactSizeIterator for Drain<'a, T> {
|
|
|
|
fn is_empty(&self) -> bool {
|
|
|
|
self.iter.is_empty()
|
|
|
|
}
|
|
|
|
}
|
2016-08-13 14:42:36 -04:00
|
|
|
|
|
|
|
#[unstable(feature = "fused", issue = "35602")]
|
|
|
|
impl<'a, T> FusedIterator for Drain<'a, T> {}
|
2016-12-22 17:15:13 +00:00
|
|
|
|
|
|
|
/// A place for insertion at the back of a `Vec`.
|
|
|
|
///
|
|
|
|
/// See [`Vec::place_back`](struct.Vec.html#method.place_back) for details.
|
|
|
|
#[must_use = "places do nothing unless written to with `<-` syntax"]
|
|
|
|
#[unstable(feature = "collection_placement",
|
|
|
|
reason = "struct name and placement protocol are subject to change",
|
|
|
|
issue = "30172")]
|
2017-01-21 00:33:38 +01:00
|
|
|
#[derive(Debug)]
|
2016-12-22 17:15:13 +00:00
|
|
|
pub struct PlaceBack<'a, T: 'a> {
|
|
|
|
vec: &'a mut Vec<T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[unstable(feature = "collection_placement",
|
|
|
|
reason = "placement protocol is subject to change",
|
|
|
|
issue = "30172")]
|
|
|
|
impl<'a, T> Placer<T> for PlaceBack<'a, T> {
|
|
|
|
type Place = PlaceBack<'a, T>;
|
|
|
|
|
|
|
|
fn make_place(self) -> Self {
|
|
|
|
// 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.vec.len == self.vec.buf.cap() {
|
|
|
|
self.vec.buf.double();
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[unstable(feature = "collection_placement",
|
|
|
|
reason = "placement protocol is subject to change",
|
|
|
|
issue = "30172")]
|
|
|
|
impl<'a, T> Place<T> for PlaceBack<'a, T> {
|
|
|
|
fn pointer(&mut self) -> *mut T {
|
|
|
|
unsafe { self.vec.as_mut_ptr().offset(self.vec.len as isize) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[unstable(feature = "collection_placement",
|
|
|
|
reason = "placement protocol is subject to change",
|
|
|
|
issue = "30172")]
|
|
|
|
impl<'a, T> InPlace<T> for PlaceBack<'a, T> {
|
|
|
|
type Owner = &'a mut T;
|
|
|
|
|
|
|
|
unsafe fn finalize(mut self) -> &'a mut T {
|
|
|
|
let ptr = self.pointer();
|
|
|
|
self.vec.len += 1;
|
|
|
|
&mut *ptr
|
|
|
|
}
|
|
|
|
}
|