mod.rs (2612e3bbc0386368a850140a6c9b990cd496a5ec) mod.rs (89eed1ab1161e7d60595917e3b982e03dfcc0f8d)
1// SPDX-License-Identifier: Apache-2.0 OR MIT
2
3//! A contiguous growable array type with heap-allocated contents, written
4//! `Vec<T>`.
5//!
6//! Vectors have *O*(1) indexing, amortized *O*(1) push (to the end) and
7//! *O*(1) pop (from the end).
8//!

--- 44 unchanged lines hidden (view full) ---

53//!
54//! [`push`]: Vec::push
55
56#![stable(feature = "rust1", since = "1.0.0")]
57
58#[cfg(not(no_global_oom_handling))]
59use core::cmp;
60use core::cmp::Ordering;
1// SPDX-License-Identifier: Apache-2.0 OR MIT
2
3//! A contiguous growable array type with heap-allocated contents, written
4//! `Vec<T>`.
5//!
6//! Vectors have *O*(1) indexing, amortized *O*(1) push (to the end) and
7//! *O*(1) pop (from the end).
8//!

--- 44 unchanged lines hidden (view full) ---

53//!
54//! [`push`]: Vec::push
55
56#![stable(feature = "rust1", since = "1.0.0")]
57
58#[cfg(not(no_global_oom_handling))]
59use core::cmp;
60use core::cmp::Ordering;
61use core::convert::TryFrom;
62use core::fmt;
63use core::hash::{Hash, Hasher};
61use core::fmt;
62use core::hash::{Hash, Hasher};
64use core::intrinsics::assume;
65use core::iter;
63use core::iter;
66#[cfg(not(no_global_oom_handling))]
67use core::iter::FromIterator;
68use core::marker::PhantomData;
69use core::mem::{self, ManuallyDrop, MaybeUninit, SizedTypeProperties};
70use core::ops::{self, Index, IndexMut, Range, RangeBounds};
71use core::ptr::{self, NonNull};
72use core::slice::{self, SliceIndex};
73
74use crate::alloc::{Allocator, Global};
75#[cfg(not(no_borrow))]

--- 300 unchanged lines hidden (view full) ---

376/// first, that might not actually happen because the optimizer does not consider
377/// this a side-effect that must be preserved. There is one case which we will
378/// not break, however: using `unsafe` code to write to the excess capacity,
379/// and then increasing the length to match, is always valid.
380///
381/// Currently, `Vec` does not guarantee the order in which elements are dropped.
382/// The order has changed in the past and may change again.
383///
64use core::marker::PhantomData;
65use core::mem::{self, ManuallyDrop, MaybeUninit, SizedTypeProperties};
66use core::ops::{self, Index, IndexMut, Range, RangeBounds};
67use core::ptr::{self, NonNull};
68use core::slice::{self, SliceIndex};
69
70use crate::alloc::{Allocator, Global};
71#[cfg(not(no_borrow))]

--- 300 unchanged lines hidden (view full) ---

372/// first, that might not actually happen because the optimizer does not consider
373/// this a side-effect that must be preserved. There is one case which we will
374/// not break, however: using `unsafe` code to write to the excess capacity,
375/// and then increasing the length to match, is always valid.
376///
377/// Currently, `Vec` does not guarantee the order in which elements are dropped.
378/// The order has changed in the past and may change again.
379///
384/// [`get`]: ../../std/vec/struct.Vec.html#method.get
385/// [`get_mut`]: ../../std/vec/struct.Vec.html#method.get_mut
380/// [`get`]: slice::get
381/// [`get_mut`]: slice::get_mut
386/// [`String`]: crate::string::String
387/// [`&str`]: type@str
388/// [`shrink_to_fit`]: Vec::shrink_to_fit
389/// [`shrink_to`]: Vec::shrink_to
390/// [capacity]: Vec::capacity
391/// [`capacity`]: Vec::capacity
392/// [mem::size_of::\<T>]: core::mem::size_of
393/// [len]: Vec::len

--- 309 unchanged lines hidden (view full) ---

703 /// #![feature(allocator_api)]
704 ///
705 /// use std::alloc::System;
706 ///
707 /// let mut vec = Vec::with_capacity_in(10, System);
708 ///
709 /// // The vector contains no items, even though it has capacity for more
710 /// assert_eq!(vec.len(), 0);
382/// [`String`]: crate::string::String
383/// [`&str`]: type@str
384/// [`shrink_to_fit`]: Vec::shrink_to_fit
385/// [`shrink_to`]: Vec::shrink_to
386/// [capacity]: Vec::capacity
387/// [`capacity`]: Vec::capacity
388/// [mem::size_of::\<T>]: core::mem::size_of
389/// [len]: Vec::len

--- 309 unchanged lines hidden (view full) ---

699 /// #![feature(allocator_api)]
700 ///
701 /// use std::alloc::System;
702 ///
703 /// let mut vec = Vec::with_capacity_in(10, System);
704 ///
705 /// // The vector contains no items, even though it has capacity for more
706 /// assert_eq!(vec.len(), 0);
711 /// assert_eq!(vec.capacity(), 10);
707 /// assert!(vec.capacity() >= 10);
712 ///
713 /// // These are all done without reallocating...
714 /// for i in 0..10 {
715 /// vec.push(i);
716 /// }
717 /// assert_eq!(vec.len(), 10);
708 ///
709 /// // These are all done without reallocating...
710 /// for i in 0..10 {
711 /// vec.push(i);
712 /// }
713 /// assert_eq!(vec.len(), 10);
718 /// assert_eq!(vec.capacity(), 10);
714 /// assert!(vec.capacity() >= 10);
719 ///
720 /// // ...but this may make the vector reallocate
721 /// vec.push(11);
722 /// assert_eq!(vec.len(), 11);
723 /// assert!(vec.capacity() >= 11);
724 ///
725 /// // A vector of a zero-sized type will always over-allocate, since no
726 /// // allocation is necessary

--- 34 unchanged lines hidden (view full) ---

761 /// #![feature(allocator_api)]
762 ///
763 /// use std::alloc::System;
764 ///
765 /// let mut vec = Vec::try_with_capacity_in(10, System).unwrap();
766 ///
767 /// // The vector contains no items, even though it has capacity for more
768 /// assert_eq!(vec.len(), 0);
715 ///
716 /// // ...but this may make the vector reallocate
717 /// vec.push(11);
718 /// assert_eq!(vec.len(), 11);
719 /// assert!(vec.capacity() >= 11);
720 ///
721 /// // A vector of a zero-sized type will always over-allocate, since no
722 /// // allocation is necessary

--- 34 unchanged lines hidden (view full) ---

757 /// #![feature(allocator_api)]
758 ///
759 /// use std::alloc::System;
760 ///
761 /// let mut vec = Vec::try_with_capacity_in(10, System).unwrap();
762 ///
763 /// // The vector contains no items, even though it has capacity for more
764 /// assert_eq!(vec.len(), 0);
769 /// assert_eq!(vec.capacity(), 10);
765 /// assert!(vec.capacity() >= 10);
770 ///
771 /// // These are all done without reallocating...
772 /// for i in 0..10 {
773 /// vec.push(i);
774 /// }
775 /// assert_eq!(vec.len(), 10);
766 ///
767 /// // These are all done without reallocating...
768 /// for i in 0..10 {
769 /// vec.push(i);
770 /// }
771 /// assert_eq!(vec.len(), 10);
776 /// assert_eq!(vec.capacity(), 10);
772 /// assert!(vec.capacity() >= 10);
777 ///
778 /// // ...but this may make the vector reallocate
779 /// vec.push(11);
780 /// assert_eq!(vec.len(), 11);
781 /// assert!(vec.capacity() >= 11);
782 ///
783 /// let mut result = Vec::try_with_capacity_in(usize::MAX, System);
784 /// assert!(result.is_err());

--- 209 unchanged lines hidden (view full) ---

994 /// Returns the total number of elements the vector can hold without
995 /// reallocating.
996 ///
997 /// # Examples
998 ///
999 /// ```
1000 /// let mut vec: Vec<i32> = Vec::with_capacity(10);
1001 /// vec.push(42);
773 ///
774 /// // ...but this may make the vector reallocate
775 /// vec.push(11);
776 /// assert_eq!(vec.len(), 11);
777 /// assert!(vec.capacity() >= 11);
778 ///
779 /// let mut result = Vec::try_with_capacity_in(usize::MAX, System);
780 /// assert!(result.is_err());

--- 209 unchanged lines hidden (view full) ---

990 /// Returns the total number of elements the vector can hold without
991 /// reallocating.
992 ///
993 /// # Examples
994 ///
995 /// ```
996 /// let mut vec: Vec<i32> = Vec::with_capacity(10);
997 /// vec.push(42);
1002 /// assert_eq!(vec.capacity(), 10);
998 /// assert!(vec.capacity() >= 10);
1003 /// ```
1004 #[inline]
1005 #[stable(feature = "rust1", since = "1.0.0")]
1006 pub fn capacity(&self) -> usize {
1007 self.buf.capacity()
1008 }
1009
1010 /// Reserves capacity for at least `additional` more elements to be inserted

--- 134 unchanged lines hidden (view full) ---

1145 /// It will drop down as close as possible to the length but the allocator
1146 /// may still inform the vector that there is space for a few more elements.
1147 ///
1148 /// # Examples
1149 ///
1150 /// ```
1151 /// let mut vec = Vec::with_capacity(10);
1152 /// vec.extend([1, 2, 3]);
999 /// ```
1000 #[inline]
1001 #[stable(feature = "rust1", since = "1.0.0")]
1002 pub fn capacity(&self) -> usize {
1003 self.buf.capacity()
1004 }
1005
1006 /// Reserves capacity for at least `additional` more elements to be inserted

--- 134 unchanged lines hidden (view full) ---

1141 /// It will drop down as close as possible to the length but the allocator
1142 /// may still inform the vector that there is space for a few more elements.
1143 ///
1144 /// # Examples
1145 ///
1146 /// ```
1147 /// let mut vec = Vec::with_capacity(10);
1148 /// vec.extend([1, 2, 3]);
1153 /// assert_eq!(vec.capacity(), 10);
1149 /// assert!(vec.capacity() >= 10);
1154 /// vec.shrink_to_fit();
1155 /// assert!(vec.capacity() >= 3);
1156 /// ```
1157 #[cfg(not(no_global_oom_handling))]
1158 #[stable(feature = "rust1", since = "1.0.0")]
1159 pub fn shrink_to_fit(&mut self) {
1160 // The capacity is never less than the length, and there's nothing to do when
1161 // they are equal, so we can avoid the panic case in `RawVec::shrink_to_fit`

--- 10 unchanged lines hidden (view full) ---

1172 ///
1173 /// If the current capacity is less than the lower limit, this is a no-op.
1174 ///
1175 /// # Examples
1176 ///
1177 /// ```
1178 /// let mut vec = Vec::with_capacity(10);
1179 /// vec.extend([1, 2, 3]);
1150 /// vec.shrink_to_fit();
1151 /// assert!(vec.capacity() >= 3);
1152 /// ```
1153 #[cfg(not(no_global_oom_handling))]
1154 #[stable(feature = "rust1", since = "1.0.0")]
1155 pub fn shrink_to_fit(&mut self) {
1156 // The capacity is never less than the length, and there's nothing to do when
1157 // they are equal, so we can avoid the panic case in `RawVec::shrink_to_fit`

--- 10 unchanged lines hidden (view full) ---

1168 ///
1169 /// If the current capacity is less than the lower limit, this is a no-op.
1170 ///
1171 /// # Examples
1172 ///
1173 /// ```
1174 /// let mut vec = Vec::with_capacity(10);
1175 /// vec.extend([1, 2, 3]);
1180 /// assert_eq!(vec.capacity(), 10);
1176 /// assert!(vec.capacity() >= 10);
1181 /// vec.shrink_to(4);
1182 /// assert!(vec.capacity() >= 4);
1183 /// vec.shrink_to(0);
1184 /// assert!(vec.capacity() >= 3);
1185 /// ```
1186 #[cfg(not(no_global_oom_handling))]
1187 #[stable(feature = "shrink_to", since = "1.56.0")]
1188 pub fn shrink_to(&mut self, min_capacity: usize) {

--- 18 unchanged lines hidden (view full) ---

1207 /// ```
1208 ///
1209 /// Any excess capacity is removed:
1210 ///
1211 /// ```
1212 /// let mut vec = Vec::with_capacity(10);
1213 /// vec.extend([1, 2, 3]);
1214 ///
1177 /// vec.shrink_to(4);
1178 /// assert!(vec.capacity() >= 4);
1179 /// vec.shrink_to(0);
1180 /// assert!(vec.capacity() >= 3);
1181 /// ```
1182 #[cfg(not(no_global_oom_handling))]
1183 #[stable(feature = "shrink_to", since = "1.56.0")]
1184 pub fn shrink_to(&mut self, min_capacity: usize) {

--- 18 unchanged lines hidden (view full) ---

1203 /// ```
1204 ///
1205 /// Any excess capacity is removed:
1206 ///
1207 /// ```
1208 /// let mut vec = Vec::with_capacity(10);
1209 /// vec.extend([1, 2, 3]);
1210 ///
1215 /// assert_eq!(vec.capacity(), 10);
1211 /// assert!(vec.capacity() >= 10);
1216 /// let slice = vec.into_boxed_slice();
1217 /// assert_eq!(slice.into_vec().capacity(), 3);
1218 /// ```
1219 #[cfg(not(no_global_oom_handling))]
1220 #[stable(feature = "rust1", since = "1.0.0")]
1221 pub fn into_boxed_slice(mut self) -> Box<[T], A> {
1222 unsafe {
1223 self.shrink_to_fit();

--- 129 unchanged lines hidden (view full) ---

1353 /// ```
1354 ///
1355 /// [`as_mut_ptr`]: Vec::as_mut_ptr
1356 #[stable(feature = "vec_as_ptr", since = "1.37.0")]
1357 #[inline]
1358 pub fn as_ptr(&self) -> *const T {
1359 // We shadow the slice method of the same name to avoid going through
1360 // `deref`, which creates an intermediate reference.
1212 /// let slice = vec.into_boxed_slice();
1213 /// assert_eq!(slice.into_vec().capacity(), 3);
1214 /// ```
1215 #[cfg(not(no_global_oom_handling))]
1216 #[stable(feature = "rust1", since = "1.0.0")]
1217 pub fn into_boxed_slice(mut self) -> Box<[T], A> {
1218 unsafe {
1219 self.shrink_to_fit();

--- 129 unchanged lines hidden (view full) ---

1349 /// ```
1350 ///
1351 /// [`as_mut_ptr`]: Vec::as_mut_ptr
1352 #[stable(feature = "vec_as_ptr", since = "1.37.0")]
1353 #[inline]
1354 pub fn as_ptr(&self) -> *const T {
1355 // We shadow the slice method of the same name to avoid going through
1356 // `deref`, which creates an intermediate reference.
1361 let ptr = self.buf.ptr();
1362 unsafe {
1363 assume(!ptr.is_null());
1364 }
1365 ptr
1357 self.buf.ptr()
1366 }
1367
1368 /// Returns an unsafe mutable pointer to the vector's buffer, or a dangling
1369 /// raw pointer valid for zero sized reads if the vector didn't allocate.
1370 ///
1371 /// The caller must ensure that the vector outlives the pointer this
1372 /// function returns, or else it will end up pointing to garbage.
1373 /// Modifying the vector may cause its buffer to be reallocated,

--- 16 unchanged lines hidden (view full) ---

1390 /// }
1391 /// assert_eq!(&*x, &[0, 1, 2, 3]);
1392 /// ```
1393 #[stable(feature = "vec_as_ptr", since = "1.37.0")]
1394 #[inline]
1395 pub fn as_mut_ptr(&mut self) -> *mut T {
1396 // We shadow the slice method of the same name to avoid going through
1397 // `deref_mut`, which creates an intermediate reference.
1358 }
1359
1360 /// Returns an unsafe mutable pointer to the vector's buffer, or a dangling
1361 /// raw pointer valid for zero sized reads if the vector didn't allocate.
1362 ///
1363 /// The caller must ensure that the vector outlives the pointer this
1364 /// function returns, or else it will end up pointing to garbage.
1365 /// Modifying the vector may cause its buffer to be reallocated,

--- 16 unchanged lines hidden (view full) ---

1382 /// }
1383 /// assert_eq!(&*x, &[0, 1, 2, 3]);
1384 /// ```
1385 #[stable(feature = "vec_as_ptr", since = "1.37.0")]
1386 #[inline]
1387 pub fn as_mut_ptr(&mut self) -> *mut T {
1388 // We shadow the slice method of the same name to avoid going through
1389 // `deref_mut`, which creates an intermediate reference.
1398 let ptr = self.buf.ptr();
1399 unsafe {
1400 assume(!ptr.is_null());
1401 }
1402 ptr
1390 self.buf.ptr()
1403 }
1404
1405 /// Returns a reference to the underlying allocator.
1406 #[unstable(feature = "allocator_api", issue = "32838")]
1407 #[inline]
1408 pub fn allocator(&self) -> &A {
1409 self.buf.allocator()
1410 }

--- 1476 unchanged lines hidden (view full) ---

2887impl<T, A: Allocator> ops::DerefMut for Vec<T, A> {
2888 #[inline]
2889 fn deref_mut(&mut self) -> &mut [T] {
2890 unsafe { slice::from_raw_parts_mut(self.as_mut_ptr(), self.len) }
2891 }
2892}
2893
2894#[cfg(not(no_global_oom_handling))]
1391 }
1392
1393 /// Returns a reference to the underlying allocator.
1394 #[unstable(feature = "allocator_api", issue = "32838")]
1395 #[inline]
1396 pub fn allocator(&self) -> &A {
1397 self.buf.allocator()
1398 }

--- 1476 unchanged lines hidden (view full) ---

2875impl<T, A: Allocator> ops::DerefMut for Vec<T, A> {
2876 #[inline]
2877 fn deref_mut(&mut self) -> &mut [T] {
2878 unsafe { slice::from_raw_parts_mut(self.as_mut_ptr(), self.len) }
2879 }
2880}
2881
2882#[cfg(not(no_global_oom_handling))]
2895trait SpecCloneFrom {
2896 fn clone_from(this: &mut Self, other: &Self);
2897}
2898
2899#[cfg(not(no_global_oom_handling))]
2900impl<T: Clone, A: Allocator> SpecCloneFrom for Vec<T, A> {
2901 default fn clone_from(this: &mut Self, other: &Self) {
2902 // drop anything that will not be overwritten
2903 this.truncate(other.len());
2904
2905 // self.len <= other.len due to the truncate above, so the
2906 // slices here are always in-bounds.
2907 let (init, tail) = other.split_at(this.len());
2908
2909 // reuse the contained values' allocations/resources.
2910 this.clone_from_slice(init);
2911 this.extend_from_slice(tail);
2912 }
2913}
2914
2915#[cfg(not(no_global_oom_handling))]
2916impl<T: Copy, A: Allocator> SpecCloneFrom for Vec<T, A> {
2917 fn clone_from(this: &mut Self, other: &Self) {
2918 this.clear();
2919 this.extend_from_slice(other);
2920 }
2921}
2922
2923#[cfg(not(no_global_oom_handling))]
2924#[stable(feature = "rust1", since = "1.0.0")]
2925impl<T: Clone, A: Allocator + Clone> Clone for Vec<T, A> {
2926 #[cfg(not(test))]
2927 fn clone(&self) -> Self {
2928 let alloc = self.allocator().clone();
2929 <[T]>::to_vec_in(&**self, alloc)
2930 }
2931
2932 // HACK(japaric): with cfg(test) the inherent `[T]::to_vec` method, which is
2933 // required for this method definition, is not available. Instead use the
2934 // `slice::to_vec` function which is only available with cfg(test)
2935 // NB see the slice::hack module in slice.rs for more information
2936 #[cfg(test)]
2937 fn clone(&self) -> Self {
2938 let alloc = self.allocator().clone();
2939 crate::slice::to_vec(&**self, alloc)
2940 }
2941
2942 fn clone_from(&mut self, other: &Self) {
2883#[stable(feature = "rust1", since = "1.0.0")]
2884impl<T: Clone, A: Allocator + Clone> Clone for Vec<T, A> {
2885 #[cfg(not(test))]
2886 fn clone(&self) -> Self {
2887 let alloc = self.allocator().clone();
2888 <[T]>::to_vec_in(&**self, alloc)
2889 }
2890
2891 // HACK(japaric): with cfg(test) the inherent `[T]::to_vec` method, which is
2892 // required for this method definition, is not available. Instead use the
2893 // `slice::to_vec` function which is only available with cfg(test)
2894 // NB see the slice::hack module in slice.rs for more information
2895 #[cfg(test)]
2896 fn clone(&self) -> Self {
2897 let alloc = self.allocator().clone();
2898 crate::slice::to_vec(&**self, alloc)
2899 }
2900
2901 fn clone_from(&mut self, other: &Self) {
2943 SpecCloneFrom::clone_from(self, other)
2902 crate::slice::SpecCloneIntoVec::clone_into(other.as_slice(), self);
2944 }
2945}
2946
2947/// The hash of a vector is the same as that of the corresponding slice,
2948/// as required by the `core::borrow::Borrow` implementation.
2949///
2950/// ```
2903 }
2904}
2905
2906/// The hash of a vector is the same as that of the corresponding slice,
2907/// as required by the `core::borrow::Borrow` implementation.
2908///
2909/// ```
2951/// #![feature(build_hasher_simple_hash_one)]
2952/// use std::hash::BuildHasher;
2953///
2954/// let b = std::collections::hash_map::RandomState::new();
2955/// let v: Vec<u8> = vec![0xa8, 0x3c, 0x09];
2956/// let s: &[u8] = &[0xa8, 0x3c, 0x09];
2957/// assert_eq!(b.hash_one(v), b.hash_one(s));
2958/// ```
2959#[stable(feature = "rust1", since = "1.0.0")]

--- 365 unchanged lines hidden (view full) ---

3325 }
3326
3327 #[inline]
3328 fn extend_reserve(&mut self, additional: usize) {
3329 self.reserve(additional);
3330 }
3331}
3332
2910/// use std::hash::BuildHasher;
2911///
2912/// let b = std::collections::hash_map::RandomState::new();
2913/// let v: Vec<u8> = vec![0xa8, 0x3c, 0x09];
2914/// let s: &[u8] = &[0xa8, 0x3c, 0x09];
2915/// assert_eq!(b.hash_one(v), b.hash_one(s));
2916/// ```
2917#[stable(feature = "rust1", since = "1.0.0")]

--- 365 unchanged lines hidden (view full) ---

3283 }
3284
3285 #[inline]
3286 fn extend_reserve(&mut self, additional: usize) {
3287 self.reserve(additional);
3288 }
3289}
3290
3333/// Implements comparison of vectors, [lexicographically](core::cmp::Ord#lexicographical-comparison).
3291/// Implements comparison of vectors, [lexicographically](Ord#lexicographical-comparison).
3334#[stable(feature = "rust1", since = "1.0.0")]
3335impl<T: PartialOrd, A: Allocator> PartialOrd for Vec<T, A> {
3336 #[inline]
3337 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
3338 PartialOrd::partial_cmp(&**self, &**other)
3339 }
3340}
3341
3342#[stable(feature = "rust1", since = "1.0.0")]
3343impl<T: Eq, A: Allocator> Eq for Vec<T, A> {}
3344
3292#[stable(feature = "rust1", since = "1.0.0")]
3293impl<T: PartialOrd, A: Allocator> PartialOrd for Vec<T, A> {
3294 #[inline]
3295 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
3296 PartialOrd::partial_cmp(&**self, &**other)
3297 }
3298}
3299
3300#[stable(feature = "rust1", since = "1.0.0")]
3301impl<T: Eq, A: Allocator> Eq for Vec<T, A> {}
3302
3345/// Implements ordering of vectors, [lexicographically](core::cmp::Ord#lexicographical-comparison).
3303/// Implements ordering of vectors, [lexicographically](Ord#lexicographical-comparison).
3346#[stable(feature = "rust1", since = "1.0.0")]
3347impl<T: Ord, A: Allocator> Ord for Vec<T, A> {
3348 #[inline]
3349 fn cmp(&self, other: &Self) -> Ordering {
3350 Ord::cmp(&**self, &**other)
3351 }
3352}
3353

--- 6 unchanged lines hidden (view full) ---

3360 // could avoid questions of validity in certain cases
3361 ptr::drop_in_place(ptr::slice_from_raw_parts_mut(self.as_mut_ptr(), self.len))
3362 }
3363 // RawVec handles deallocation
3364 }
3365}
3366
3367#[stable(feature = "rust1", since = "1.0.0")]
3304#[stable(feature = "rust1", since = "1.0.0")]
3305impl<T: Ord, A: Allocator> Ord for Vec<T, A> {
3306 #[inline]
3307 fn cmp(&self, other: &Self) -> Ordering {
3308 Ord::cmp(&**self, &**other)
3309 }
3310}
3311

--- 6 unchanged lines hidden (view full) ---

3318 // could avoid questions of validity in certain cases
3319 ptr::drop_in_place(ptr::slice_from_raw_parts_mut(self.as_mut_ptr(), self.len))
3320 }
3321 // RawVec handles deallocation
3322 }
3323}
3324
3325#[stable(feature = "rust1", since = "1.0.0")]
3368#[rustc_const_unstable(feature = "const_default_impls", issue = "87864")]
3369impl<T> const Default for Vec<T> {
3326impl<T> Default for Vec<T> {
3370 /// Creates an empty `Vec<T>`.
3371 ///
3372 /// The vector will not allocate until elements are pushed onto it.
3373 fn default() -> Vec<T> {
3374 Vec::new()
3375 }
3376}
3377

--- 79 unchanged lines hidden (view full) ---

3457 ///
3458 /// # Examples
3459 ///
3460 /// ```
3461 /// assert_eq!(Vec::from([1, 2, 3]), vec![1, 2, 3]);
3462 /// ```
3463 #[cfg(not(test))]
3464 fn from(s: [T; N]) -> Vec<T> {
3327 /// Creates an empty `Vec<T>`.
3328 ///
3329 /// The vector will not allocate until elements are pushed onto it.
3330 fn default() -> Vec<T> {
3331 Vec::new()
3332 }
3333}
3334

--- 79 unchanged lines hidden (view full) ---

3414 ///
3415 /// # Examples
3416 ///
3417 /// ```
3418 /// assert_eq!(Vec::from([1, 2, 3]), vec![1, 2, 3]);
3419 /// ```
3420 #[cfg(not(test))]
3421 fn from(s: [T; N]) -> Vec<T> {
3465 <[T]>::into_vec(
3466 #[rustc_box]
3467 Box::new(s),
3468 )
3422 <[T]>::into_vec(Box::new(s))
3469 }
3470
3471 #[cfg(test)]
3472 fn from(s: [T; N]) -> Vec<T> {
3473 crate::slice::into_vec(Box::new(s))
3474 }
3475}
3476

--- 8 unchanged lines hidden (view full) ---

3485 /// If `s` already owns a `Vec<T>`, it will be returned directly.
3486 /// If `s` is borrowing a slice, a new `Vec<T>` will be allocated and
3487 /// filled by cloning `s`'s items into it.
3488 ///
3489 /// # Examples
3490 ///
3491 /// ```
3492 /// # use std::borrow::Cow;
3423 }
3424
3425 #[cfg(test)]
3426 fn from(s: [T; N]) -> Vec<T> {
3427 crate::slice::into_vec(Box::new(s))
3428 }
3429}
3430

--- 8 unchanged lines hidden (view full) ---

3439 /// If `s` already owns a `Vec<T>`, it will be returned directly.
3440 /// If `s` is borrowing a slice, a new `Vec<T>` will be allocated and
3441 /// filled by cloning `s`'s items into it.
3442 ///
3443 /// # Examples
3444 ///
3445 /// ```
3446 /// # use std::borrow::Cow;
3493 /// let o: Cow<[i32]> = Cow::Owned(vec![1, 2, 3]);
3494 /// let b: Cow<[i32]> = Cow::Borrowed(&[1, 2, 3]);
3447 /// let o: Cow<'_, [i32]> = Cow::Owned(vec![1, 2, 3]);
3448 /// let b: Cow<'_, [i32]> = Cow::Borrowed(&[1, 2, 3]);
3495 /// assert_eq!(Vec::from(o), Vec::from(b));
3496 /// ```
3497 fn from(s: Cow<'a, [T]>) -> Vec<T> {
3498 s.into_owned()
3499 }
3500}
3501
3502// note: test pulls in std, which causes errors here

--- 107 unchanged lines hidden ---
3449 /// assert_eq!(Vec::from(o), Vec::from(b));
3450 /// ```
3451 fn from(s: Cow<'a, [T]>) -> Vec<T> {
3452 s.into_owned()
3453 }
3454}
3455
3456// note: test pulls in std, which causes errors here

--- 107 unchanged lines hidden ---