Skip to content

Commit 4ec741a

Browse files
Daniel BloomDaniel-Aaron-Bloom
Daniel Bloom
authored andcommitted
Make slice iterator constructors unstably const
1 parent 617aad8 commit 4ec741a

File tree

2 files changed

+57
-36
lines changed

2 files changed

+57
-36
lines changed

library/core/src/slice/iter.rs

+25-20
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ unsafe impl<T: Sync> Send for Iter<'_, T> {}
9393

9494
impl<'a, T> Iter<'a, T> {
9595
#[inline]
96-
pub(super) fn new(slice: &'a [T]) -> Self {
96+
pub(super) const fn new(slice: &'a [T]) -> Self {
9797
let len = slice.len();
98-
let ptr: NonNull<T> = NonNull::from(slice).cast();
98+
let ptr: NonNull<T> = NonNull::from_ref(slice).cast();
9999
// SAFETY: Similar to `IterMut::new`.
100100
unsafe {
101101
let end_or_len =
@@ -218,9 +218,9 @@ unsafe impl<T: Send> Send for IterMut<'_, T> {}
218218

219219
impl<'a, T> IterMut<'a, T> {
220220
#[inline]
221-
pub(super) fn new(slice: &'a mut [T]) -> Self {
221+
pub(super) const fn new(slice: &'a mut [T]) -> Self {
222222
let len = slice.len();
223-
let ptr: NonNull<T> = NonNull::from(slice).cast();
223+
let ptr: NonNull<T> = NonNull::from_mut(slice).cast();
224224
// SAFETY: There are several things here:
225225
//
226226
// `ptr` has been obtained by `slice.as_ptr()` where `slice` is a valid
@@ -1335,7 +1335,7 @@ pub struct Windows<'a, T: 'a> {
13351335

13361336
impl<'a, T: 'a> Windows<'a, T> {
13371337
#[inline]
1338-
pub(super) fn new(slice: &'a [T], size: NonZero<usize>) -> Self {
1338+
pub(super) const fn new(slice: &'a [T], size: NonZero<usize>) -> Self {
13391339
Self { v: slice, size }
13401340
}
13411341
}
@@ -1487,7 +1487,7 @@ pub struct Chunks<'a, T: 'a> {
14871487

14881488
impl<'a, T: 'a> Chunks<'a, T> {
14891489
#[inline]
1490-
pub(super) fn new(slice: &'a [T], size: usize) -> Self {
1490+
pub(super) const fn new(slice: &'a [T], size: usize) -> Self {
14911491
Self { v: slice, chunk_size: size }
14921492
}
14931493
}
@@ -1677,7 +1677,7 @@ pub struct ChunksMut<'a, T: 'a> {
16771677

16781678
impl<'a, T: 'a> ChunksMut<'a, T> {
16791679
#[inline]
1680-
pub(super) fn new(slice: &'a mut [T], size: usize) -> Self {
1680+
pub(super) const fn new(slice: &'a mut [T], size: usize) -> Self {
16811681
Self { v: slice, chunk_size: size, _marker: PhantomData }
16821682
}
16831683
}
@@ -1863,7 +1863,7 @@ pub struct ChunksExact<'a, T: 'a> {
18631863

18641864
impl<'a, T> ChunksExact<'a, T> {
18651865
#[inline]
1866-
pub(super) fn new(slice: &'a [T], chunk_size: usize) -> Self {
1866+
pub(super) const fn new(slice: &'a [T], chunk_size: usize) -> Self {
18671867
let rem = slice.len() % chunk_size;
18681868
let fst_len = slice.len() - rem;
18691869
// SAFETY: 0 <= fst_len <= slice.len() by construction above
@@ -2043,7 +2043,7 @@ pub struct ChunksExactMut<'a, T: 'a> {
20432043

20442044
impl<'a, T> ChunksExactMut<'a, T> {
20452045
#[inline]
2046-
pub(super) fn new(slice: &'a mut [T], chunk_size: usize) -> Self {
2046+
pub(super) const fn new(slice: &'a mut [T], chunk_size: usize) -> Self {
20472047
let rem = slice.len() % chunk_size;
20482048
let fst_len = slice.len() - rem;
20492049
// SAFETY: 0 <= fst_len <= slice.len() by construction above
@@ -2210,7 +2210,7 @@ pub struct ArrayWindows<'a, T: 'a, const N: usize> {
22102210

22112211
impl<'a, T: 'a, const N: usize> ArrayWindows<'a, T, N> {
22122212
#[inline]
2213-
pub(super) fn new(slice: &'a [T]) -> Self {
2213+
pub(super) const fn new(slice: &'a [T]) -> Self {
22142214
let num_windows = slice.len().saturating_sub(N - 1);
22152215
Self { slice_head: slice.as_ptr(), num: num_windows, marker: PhantomData }
22162216
}
@@ -2334,8 +2334,10 @@ pub struct ArrayChunks<'a, T: 'a, const N: usize> {
23342334
}
23352335

23362336
impl<'a, T, const N: usize> ArrayChunks<'a, T, N> {
2337+
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
2338+
// #[rustc_const_unstable(feature = "slice_as_chunks", issue = "74985")]
23372339
#[inline]
2338-
pub(super) fn new(slice: &'a [T]) -> Self {
2340+
pub(super) const fn new(slice: &'a [T]) -> Self {
23392341
let (array_slice, rem) = slice.as_chunks();
23402342
Self { iter: array_slice.iter(), rem }
23412343
}
@@ -2460,8 +2462,9 @@ pub struct ArrayChunksMut<'a, T: 'a, const N: usize> {
24602462
}
24612463

24622464
impl<'a, T, const N: usize> ArrayChunksMut<'a, T, N> {
2465+
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
24632466
#[inline]
2464-
pub(super) fn new(slice: &'a mut [T]) -> Self {
2467+
pub(super) const fn new(slice: &'a mut [T]) -> Self {
24652468
let (array_slice, rem) = slice.as_chunks_mut();
24662469
Self { iter: array_slice.iter_mut(), rem }
24672470
}
@@ -2579,7 +2582,7 @@ pub struct RChunks<'a, T: 'a> {
25792582

25802583
impl<'a, T: 'a> RChunks<'a, T> {
25812584
#[inline]
2582-
pub(super) fn new(slice: &'a [T], size: usize) -> Self {
2585+
pub(super) const fn new(slice: &'a [T], size: usize) -> Self {
25832586
Self { v: slice, chunk_size: size }
25842587
}
25852588
}
@@ -2759,7 +2762,7 @@ pub struct RChunksMut<'a, T: 'a> {
27592762

27602763
impl<'a, T: 'a> RChunksMut<'a, T> {
27612764
#[inline]
2762-
pub(super) fn new(slice: &'a mut [T], size: usize) -> Self {
2765+
pub(super) const fn new(slice: &'a mut [T], size: usize) -> Self {
27632766
Self { v: slice, chunk_size: size, _marker: PhantomData }
27642767
}
27652768
}
@@ -2950,7 +2953,7 @@ pub struct RChunksExact<'a, T: 'a> {
29502953

29512954
impl<'a, T> RChunksExact<'a, T> {
29522955
#[inline]
2953-
pub(super) fn new(slice: &'a [T], chunk_size: usize) -> Self {
2956+
pub(super) const fn new(slice: &'a [T], chunk_size: usize) -> Self {
29542957
let rem = slice.len() % chunk_size;
29552958
// SAFETY: 0 <= rem <= slice.len() by construction above
29562959
let (fst, snd) = unsafe { slice.split_at_unchecked(rem) };
@@ -2976,7 +2979,8 @@ impl<'a, T> RChunksExact<'a, T> {
29762979
/// ```
29772980
#[must_use]
29782981
#[stable(feature = "rchunks", since = "1.31.0")]
2979-
pub fn remainder(&self) -> &'a [T] {
2982+
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
2983+
pub const fn remainder(&self) -> &'a [T] {
29802984
self.rem
29812985
}
29822986
}
@@ -3132,7 +3136,7 @@ pub struct RChunksExactMut<'a, T: 'a> {
31323136

31333137
impl<'a, T> RChunksExactMut<'a, T> {
31343138
#[inline]
3135-
pub(super) fn new(slice: &'a mut [T], chunk_size: usize) -> Self {
3139+
pub(super) const fn new(slice: &'a mut [T], chunk_size: usize) -> Self {
31363140
let rem = slice.len() % chunk_size;
31373141
// SAFETY: 0 <= rem <= slice.len() by construction above
31383142
let (fst, snd) = unsafe { slice.split_at_mut_unchecked(rem) };
@@ -3144,7 +3148,8 @@ impl<'a, T> RChunksExactMut<'a, T> {
31443148
/// elements.
31453149
#[must_use = "`self` will be dropped if the result is not used"]
31463150
#[stable(feature = "rchunks", since = "1.31.0")]
3147-
pub fn into_remainder(self) -> &'a mut [T] {
3151+
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
3152+
pub const fn into_remainder(self) -> &'a mut [T] {
31483153
self.rem
31493154
}
31503155
}
@@ -3308,7 +3313,7 @@ pub struct ChunkBy<'a, T: 'a, P> {
33083313

33093314
#[stable(feature = "slice_group_by", since = "1.77.0")]
33103315
impl<'a, T: 'a, P> ChunkBy<'a, T, P> {
3311-
pub(super) fn new(slice: &'a [T], predicate: P) -> Self {
3316+
pub(super) const fn new(slice: &'a [T], predicate: P) -> Self {
33123317
ChunkBy { slice, predicate }
33133318
}
33143319
}
@@ -3395,7 +3400,7 @@ pub struct ChunkByMut<'a, T: 'a, P> {
33953400

33963401
#[stable(feature = "slice_group_by", since = "1.77.0")]
33973402
impl<'a, T: 'a, P> ChunkByMut<'a, T, P> {
3398-
pub(super) fn new(slice: &'a mut [T], predicate: P) -> Self {
3403+
pub(super) const fn new(slice: &'a mut [T], predicate: P) -> Self {
33993404
ChunkByMut { slice, predicate }
34003405
}
34013406
}

library/core/src/slice/mod.rs

+32-16
Original file line numberDiff line numberDiff line change
@@ -1045,9 +1045,10 @@ impl<T> [T] {
10451045
/// assert_eq!(iterator.next(), None);
10461046
/// ```
10471047
#[stable(feature = "rust1", since = "1.0.0")]
1048+
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
10481049
#[inline]
10491050
#[cfg_attr(not(test), rustc_diagnostic_item = "slice_iter")]
1050-
pub fn iter(&self) -> Iter<'_, T> {
1051+
pub const fn iter(&self) -> Iter<'_, T> {
10511052
Iter::new(self)
10521053
}
10531054

@@ -1064,9 +1065,10 @@ impl<T> [T] {
10641065
/// }
10651066
/// assert_eq!(x, &[3, 4, 6]);
10661067
/// ```
1068+
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
10671069
#[stable(feature = "rust1", since = "1.0.0")]
10681070
#[inline]
1069-
pub fn iter_mut(&mut self) -> IterMut<'_, T> {
1071+
pub const fn iter_mut(&mut self) -> IterMut<'_, T> {
10701072
IterMut::new(self)
10711073
}
10721074

@@ -1118,9 +1120,10 @@ impl<T> [T] {
11181120
/// assert_eq!(array, ['s', 't', ' ', '2', '0', '1', '5', 'u', 'R']);
11191121
/// ```
11201122
#[stable(feature = "rust1", since = "1.0.0")]
1123+
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
11211124
#[inline]
11221125
#[track_caller]
1123-
pub fn windows(&self, size: usize) -> Windows<'_, T> {
1126+
pub const fn windows(&self, size: usize) -> Windows<'_, T> {
11241127
let size = NonZero::new(size).expect("window size must be non-zero");
11251128
Windows::new(self, size)
11261129
}
@@ -1153,9 +1156,10 @@ impl<T> [T] {
11531156
/// [`chunks_exact`]: slice::chunks_exact
11541157
/// [`rchunks`]: slice::rchunks
11551158
#[stable(feature = "rust1", since = "1.0.0")]
1159+
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
11561160
#[inline]
11571161
#[track_caller]
1158-
pub fn chunks(&self, chunk_size: usize) -> Chunks<'_, T> {
1162+
pub const fn chunks(&self, chunk_size: usize) -> Chunks<'_, T> {
11591163
assert!(chunk_size != 0, "chunk size must be non-zero");
11601164
Chunks::new(self, chunk_size)
11611165
}
@@ -1192,9 +1196,10 @@ impl<T> [T] {
11921196
/// [`chunks_exact_mut`]: slice::chunks_exact_mut
11931197
/// [`rchunks_mut`]: slice::rchunks_mut
11941198
#[stable(feature = "rust1", since = "1.0.0")]
1199+
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
11951200
#[inline]
11961201
#[track_caller]
1197-
pub fn chunks_mut(&mut self, chunk_size: usize) -> ChunksMut<'_, T> {
1202+
pub const fn chunks_mut(&mut self, chunk_size: usize) -> ChunksMut<'_, T> {
11981203
assert!(chunk_size != 0, "chunk size must be non-zero");
11991204
ChunksMut::new(self, chunk_size)
12001205
}
@@ -1230,9 +1235,10 @@ impl<T> [T] {
12301235
/// [`chunks`]: slice::chunks
12311236
/// [`rchunks_exact`]: slice::rchunks_exact
12321237
#[stable(feature = "chunks_exact", since = "1.31.0")]
1238+
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
12331239
#[inline]
12341240
#[track_caller]
1235-
pub fn chunks_exact(&self, chunk_size: usize) -> ChunksExact<'_, T> {
1241+
pub const fn chunks_exact(&self, chunk_size: usize) -> ChunksExact<'_, T> {
12361242
assert!(chunk_size != 0, "chunk size must be non-zero");
12371243
ChunksExact::new(self, chunk_size)
12381244
}
@@ -1273,9 +1279,10 @@ impl<T> [T] {
12731279
/// [`chunks_mut`]: slice::chunks_mut
12741280
/// [`rchunks_exact_mut`]: slice::rchunks_exact_mut
12751281
#[stable(feature = "chunks_exact", since = "1.31.0")]
1282+
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
12761283
#[inline]
12771284
#[track_caller]
1278-
pub fn chunks_exact_mut(&mut self, chunk_size: usize) -> ChunksExactMut<'_, T> {
1285+
pub const fn chunks_exact_mut(&mut self, chunk_size: usize) -> ChunksExactMut<'_, T> {
12791286
assert!(chunk_size != 0, "chunk size must be non-zero");
12801287
ChunksExactMut::new(self, chunk_size)
12811288
}
@@ -1431,9 +1438,10 @@ impl<T> [T] {
14311438
///
14321439
/// [`chunks_exact`]: slice::chunks_exact
14331440
#[unstable(feature = "array_chunks", issue = "74985")]
1441+
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
14341442
#[inline]
14351443
#[track_caller]
1436-
pub fn array_chunks<const N: usize>(&self) -> ArrayChunks<'_, T, N> {
1444+
pub const fn array_chunks<const N: usize>(&self) -> ArrayChunks<'_, T, N> {
14371445
assert!(N != 0, "chunk size must be non-zero");
14381446
ArrayChunks::new(self)
14391447
}
@@ -1594,9 +1602,10 @@ impl<T> [T] {
15941602
///
15951603
/// [`chunks_exact_mut`]: slice::chunks_exact_mut
15961604
#[unstable(feature = "array_chunks", issue = "74985")]
1605+
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
15971606
#[inline]
15981607
#[track_caller]
1599-
pub fn array_chunks_mut<const N: usize>(&mut self) -> ArrayChunksMut<'_, T, N> {
1608+
pub const fn array_chunks_mut<const N: usize>(&mut self) -> ArrayChunksMut<'_, T, N> {
16001609
assert!(N != 0, "chunk size must be non-zero");
16011610
ArrayChunksMut::new(self)
16021611
}
@@ -1627,9 +1636,10 @@ impl<T> [T] {
16271636
///
16281637
/// [`windows`]: slice::windows
16291638
#[unstable(feature = "array_windows", issue = "75027")]
1639+
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
16301640
#[inline]
16311641
#[track_caller]
1632-
pub fn array_windows<const N: usize>(&self) -> ArrayWindows<'_, T, N> {
1642+
pub const fn array_windows<const N: usize>(&self) -> ArrayWindows<'_, T, N> {
16331643
assert!(N != 0, "window size must be non-zero");
16341644
ArrayWindows::new(self)
16351645
}
@@ -1662,9 +1672,10 @@ impl<T> [T] {
16621672
/// [`rchunks_exact`]: slice::rchunks_exact
16631673
/// [`chunks`]: slice::chunks
16641674
#[stable(feature = "rchunks", since = "1.31.0")]
1675+
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
16651676
#[inline]
16661677
#[track_caller]
1667-
pub fn rchunks(&self, chunk_size: usize) -> RChunks<'_, T> {
1678+
pub const fn rchunks(&self, chunk_size: usize) -> RChunks<'_, T> {
16681679
assert!(chunk_size != 0, "chunk size must be non-zero");
16691680
RChunks::new(self, chunk_size)
16701681
}
@@ -1701,9 +1712,10 @@ impl<T> [T] {
17011712
/// [`rchunks_exact_mut`]: slice::rchunks_exact_mut
17021713
/// [`chunks_mut`]: slice::chunks_mut
17031714
#[stable(feature = "rchunks", since = "1.31.0")]
1715+
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
17041716
#[inline]
17051717
#[track_caller]
1706-
pub fn rchunks_mut(&mut self, chunk_size: usize) -> RChunksMut<'_, T> {
1718+
pub const fn rchunks_mut(&mut self, chunk_size: usize) -> RChunksMut<'_, T> {
17071719
assert!(chunk_size != 0, "chunk size must be non-zero");
17081720
RChunksMut::new(self, chunk_size)
17091721
}
@@ -1741,9 +1753,10 @@ impl<T> [T] {
17411753
/// [`rchunks`]: slice::rchunks
17421754
/// [`chunks_exact`]: slice::chunks_exact
17431755
#[stable(feature = "rchunks", since = "1.31.0")]
1756+
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
17441757
#[inline]
17451758
#[track_caller]
1746-
pub fn rchunks_exact(&self, chunk_size: usize) -> RChunksExact<'_, T> {
1759+
pub const fn rchunks_exact(&self, chunk_size: usize) -> RChunksExact<'_, T> {
17471760
assert!(chunk_size != 0, "chunk size must be non-zero");
17481761
RChunksExact::new(self, chunk_size)
17491762
}
@@ -1785,9 +1798,10 @@ impl<T> [T] {
17851798
/// [`rchunks_mut`]: slice::rchunks_mut
17861799
/// [`chunks_exact_mut`]: slice::chunks_exact_mut
17871800
#[stable(feature = "rchunks", since = "1.31.0")]
1801+
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
17881802
#[inline]
17891803
#[track_caller]
1790-
pub fn rchunks_exact_mut(&mut self, chunk_size: usize) -> RChunksExactMut<'_, T> {
1804+
pub const fn rchunks_exact_mut(&mut self, chunk_size: usize) -> RChunksExactMut<'_, T> {
17911805
assert!(chunk_size != 0, "chunk size must be non-zero");
17921806
RChunksExactMut::new(self, chunk_size)
17931807
}
@@ -1825,8 +1839,9 @@ impl<T> [T] {
18251839
/// assert_eq!(iter.next(), None);
18261840
/// ```
18271841
#[stable(feature = "slice_group_by", since = "1.77.0")]
1842+
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
18281843
#[inline]
1829-
pub fn chunk_by<F>(&self, pred: F) -> ChunkBy<'_, T, F>
1844+
pub const fn chunk_by<F>(&self, pred: F) -> ChunkBy<'_, T, F>
18301845
where
18311846
F: FnMut(&T, &T) -> bool,
18321847
{
@@ -1866,8 +1881,9 @@ impl<T> [T] {
18661881
/// assert_eq!(iter.next(), None);
18671882
/// ```
18681883
#[stable(feature = "slice_group_by", since = "1.77.0")]
1884+
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
18691885
#[inline]
1870-
pub fn chunk_by_mut<F>(&mut self, pred: F) -> ChunkByMut<'_, T, F>
1886+
pub const fn chunk_by_mut<F>(&mut self, pred: F) -> ChunkByMut<'_, T, F>
18711887
where
18721888
F: FnMut(&T, &T) -> bool,
18731889
{

0 commit comments

Comments
 (0)