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
63 changes: 55 additions & 8 deletions library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3564,7 +3564,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_from_elem)]
/// let vec = Vec::from_elem(0, 3);
/// assert_eq!(vec, [0; 3]);
/// ```
#[cfg(not(no_global_oom_handling))]
#[unstable(feature = "vec_from_elem", issue = "164215")]
#[inline]
pub fn from_elem(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_from_elem)]
/// use std::alloc::System;
///
/// let vec = Vec::from_elem_in(0, 3, System);
/// assert_eq!(vec, [0; 3]);
/// ```
#[cfg(not(no_global_oom_handling))]
#[unstable(feature = "vec_from_elem", issue = "164215")]
#[inline]
pub fn from_elem_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 @@ -3801,14 +3855,7 @@ impl<T: PartialEq, A: Allocator> Vec<T, A> {
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_diagnostic_item = "vec_from_elem"]
pub fn from_elem<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::from_elem(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