Skip to content

Commit d8a38b0

Browse files
committed
Auto merge of rust-lang#119127 - joboet:array_repeat, r=scottmcm
Implement `array::repeat` See rust-lang/libs-team#310. I've decided to make the function use the input value as last element instead of cloning it to every position and dropping it, and to make this part of the API so that callers are not surprised by this behaviour. TODO: open a tracking issue. I'll wait for the ACP to be accepted, first. `@rustbot` label +T-libs-api +T-libs r? libs
2 parents 5c8459f + 0aa3310 commit d8a38b0

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

library/core/src/array/mod.rs

+28-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::convert::Infallible;
1010
use crate::error::Error;
1111
use crate::fmt;
1212
use crate::hash::{self, Hash};
13-
use crate::iter::UncheckedIterator;
13+
use crate::iter::{repeat_n, UncheckedIterator};
1414
use crate::mem::{self, MaybeUninit};
1515
use crate::ops::{
1616
ChangeOutputType, ControlFlow, FromResidual, Index, IndexMut, NeverShortCircuit, Residual, Try,
@@ -27,6 +27,33 @@ pub(crate) use drain::drain_array_with;
2727
#[stable(feature = "array_value_iter", since = "1.51.0")]
2828
pub use iter::IntoIter;
2929

30+
/// Creates an array of type `[T; N]` by repeatedly cloning a value.
31+
///
32+
/// This is the same as `[val; N]`, but it also works for types that do not
33+
/// implement [`Copy`].
34+
///
35+
/// The provided value will be used as an element of the resulting array and
36+
/// will be cloned N - 1 times to fill up the rest. If N is zero, the value
37+
/// will be dropped.
38+
///
39+
/// # Example
40+
///
41+
/// Creating muliple copies of a `String`:
42+
/// ```rust
43+
/// #![feature(array_repeat)]
44+
///
45+
/// use std::array;
46+
///
47+
/// let string = "Hello there!".to_string();
48+
/// let strings = array::repeat(string);
49+
/// assert_eq!(strings, ["Hello there!", "Hello there!"]);
50+
/// ```
51+
#[inline]
52+
#[unstable(feature = "array_repeat", issue = "126695")]
53+
pub fn repeat<T: Clone, const N: usize>(val: T) -> [T; N] {
54+
from_trusted_iterator(repeat_n(val, N))
55+
}
56+
3057
/// Creates an array of type [T; N], where each element `T` is the returned value from `cb`
3158
/// using that element's index.
3259
///

library/core/src/iter/sources/repeat_n.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::iter::{FusedIterator, TrustedLen};
1+
use crate::iter::{FusedIterator, TrustedLen, UncheckedIterator};
22
use crate::mem::ManuallyDrop;
33
use crate::num::NonZero;
44

@@ -193,3 +193,5 @@ impl<A: Clone> FusedIterator for RepeatN<A> {}
193193

194194
#[unstable(feature = "trusted_len", issue = "37572")]
195195
unsafe impl<A: Clone> TrustedLen for RepeatN<A> {}
196+
#[unstable(feature = "trusted_len_next_unchecked", issue = "37572")]
197+
impl<A: Clone> UncheckedIterator for RepeatN<A> {}

tests/codegen/array-repeat.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//@ compile-flags: -O
2+
3+
#![crate_type = "lib"]
4+
#![feature(array_repeat)]
5+
6+
use std::array::repeat;
7+
8+
// CHECK-LABEL: @byte_repeat
9+
#[no_mangle]
10+
fn byte_repeat(b: u8) -> [u8; 1024] {
11+
// CHECK-NOT: alloca
12+
// CHECK-NOT: store
13+
// CHECK: memset
14+
repeat(b)
15+
}

0 commit comments

Comments
 (0)