Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 59 additions & 8 deletions library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3601,7 +3601,61 @@ where
{
}

impl<T: Clone> Vec<T, Global> {
/// Create a new `Vec` that contains `elem` `n` times.
///
/// This method requires `T` to implement [`Clone`],
/// in order to be able to clone the passed value.
///
/// If `n` is zero, the vector will not allocate.
///
/// # Panics
///
/// Panics if `size_of::<T>() * n` exceeds `isize::MAX` bytes.
///
/// # Examples
///
/// ```
/// #![feature(vec_repeat)]
/// let vec = Vec::repeat(0, 3);
/// assert_eq!(vec, [0; 3]);
/// ```
#[cfg(not(no_global_oom_handling))]
#[unstable(feature = "vec_repeat", issue = "164215")]
#[inline]
pub fn repeat(elem: T, n: usize) -> Self {
<T as SpecFromElem>::from_elem(elem, n, Global)
}
}

impl<T: Clone, A: Allocator> Vec<T, A> {
/// Create a new `Vec` with the provided allocator that contains `elem` `n` times.
///
/// This method requires `T` to implement [`Clone`],
/// in order to be able to clone the passed value.
///
/// If `n` is zero, the vector will not allocate.
///
/// # Panics
///
/// Panics if `size_of::<T>() * n` exceeds `isize::MAX` bytes.
///
/// # Examples
///
/// ```
/// #![feature(vec_repeat)]
/// use std::alloc::System;
///
/// let vec = Vec::repeat_in(0, 3, System);
/// assert_eq!(vec, [0; 3]);
/// ```
#[cfg(not(no_global_oom_handling))]
#[unstable(feature = "vec_repeat", issue = "164215")]
#[inline]
pub fn repeat_in(elem: T, n: usize, alloc: A) -> Self {
<T as SpecFromElem>::from_elem(elem, n, alloc)
}

/// Resizes the `Vec` in-place so that `len` is equal to `new_len`.
///
/// If `new_len` is greater than `len`, the `Vec` is extended by the
Expand Down Expand Up @@ -3956,19 +4010,16 @@ pub fn from_elem<T: Clone>(elem: T, n: usize) -> Vec<T> {
vec![elem; n]
}

// This function is required because it ensures the `vec_from_elem` diagnostic
// item is emitted, which is consumed by Clippy to detect calls to
// `vec![elem; n]`. We cannot add the item to `Vec::repeat` because then we
// would end up confusing Clippy.
#[doc(hidden)]
#[cfg(not(no_global_oom_handling))]
#[unstable(feature = "liballoc_internals", issue = "none")]
#[rustc_diagnostic_item = "vec_from_elem"]
pub fn from_elem_global<T: Clone>(elem: T, n: usize) -> Vec<T> {
<T as SpecFromElem>::from_elem(elem, n, Global)
}

#[doc(hidden)]
#[cfg(not(no_global_oom_handling))]
#[unstable(feature = "allocator_ext", issue = "163177", implied_by = "allocator_api")]
pub fn from_elem_in<T: Clone, A: Allocator>(elem: T, n: usize, alloc: A) -> Vec<T, A> {
<T as SpecFromElem>::from_elem(elem, n, alloc)
Vec::repeat(elem, n)
}

#[cfg(not(no_global_oom_handling))]
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/suggestions/deref-path-method.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ note: if you're trying to build a new `Vec<_, _>` consider using one of the foll
Vec::<T>::with_capacity
Vec::<T>::try_with_capacity
Vec::<T>::from_raw_parts
and 7 others
and 9 others
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
help: the function `contains` is implemented on `[_]`
|
Expand Down
Loading