Skip to content

Commit beb8e9e

Browse files
Daniel BloomDaniel-Aaron-Bloom
Daniel Bloom
authored andcommitted
Make slice iterator constructors unstably const
1 parent 3f55023 commit beb8e9e

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
@@ -1043,9 +1043,10 @@ impl<T> [T] {
10431043
/// assert_eq!(iterator.next(), None);
10441044
/// ```
10451045
#[stable(feature = "rust1", since = "1.0.0")]
1046+
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
10461047
#[inline]
10471048
#[rustc_diagnostic_item = "slice_iter"]
1048-
pub fn iter(&self) -> Iter<'_, T> {
1049+
pub const fn iter(&self) -> Iter<'_, T> {
10491050
Iter::new(self)
10501051
}
10511052

@@ -1062,9 +1063,10 @@ impl<T> [T] {
10621063
/// }
10631064
/// assert_eq!(x, &[3, 4, 6]);
10641065
/// ```
1066+
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
10651067
#[stable(feature = "rust1", since = "1.0.0")]
10661068
#[inline]
1067-
pub fn iter_mut(&mut self) -> IterMut<'_, T> {
1069+
pub const fn iter_mut(&mut self) -> IterMut<'_, T> {
10681070
IterMut::new(self)
10691071
}
10701072

@@ -1116,9 +1118,10 @@ impl<T> [T] {
11161118
/// assert_eq!(array, ['s', 't', ' ', '2', '0', '1', '5', 'u', 'R']);
11171119
/// ```
11181120
#[stable(feature = "rust1", since = "1.0.0")]
1121+
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
11191122
#[inline]
11201123
#[track_caller]
1121-
pub fn windows(&self, size: usize) -> Windows<'_, T> {
1124+
pub const fn windows(&self, size: usize) -> Windows<'_, T> {
11221125
let size = NonZero::new(size).expect("window size must be non-zero");
11231126
Windows::new(self, size)
11241127
}
@@ -1151,9 +1154,10 @@ impl<T> [T] {
11511154
/// [`chunks_exact`]: slice::chunks_exact
11521155
/// [`rchunks`]: slice::rchunks
11531156
#[stable(feature = "rust1", since = "1.0.0")]
1157+
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
11541158
#[inline]
11551159
#[track_caller]
1156-
pub fn chunks(&self, chunk_size: usize) -> Chunks<'_, T> {
1160+
pub const fn chunks(&self, chunk_size: usize) -> Chunks<'_, T> {
11571161
assert!(chunk_size != 0, "chunk size must be non-zero");
11581162
Chunks::new(self, chunk_size)
11591163
}
@@ -1190,9 +1194,10 @@ impl<T> [T] {
11901194
/// [`chunks_exact_mut`]: slice::chunks_exact_mut
11911195
/// [`rchunks_mut`]: slice::rchunks_mut
11921196
#[stable(feature = "rust1", since = "1.0.0")]
1197+
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
11931198
#[inline]
11941199
#[track_caller]
1195-
pub fn chunks_mut(&mut self, chunk_size: usize) -> ChunksMut<'_, T> {
1200+
pub const fn chunks_mut(&mut self, chunk_size: usize) -> ChunksMut<'_, T> {
11961201
assert!(chunk_size != 0, "chunk size must be non-zero");
11971202
ChunksMut::new(self, chunk_size)
11981203
}
@@ -1228,9 +1233,10 @@ impl<T> [T] {
12281233
/// [`chunks`]: slice::chunks
12291234
/// [`rchunks_exact`]: slice::rchunks_exact
12301235
#[stable(feature = "chunks_exact", since = "1.31.0")]
1236+
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
12311237
#[inline]
12321238
#[track_caller]
1233-
pub fn chunks_exact(&self, chunk_size: usize) -> ChunksExact<'_, T> {
1239+
pub const fn chunks_exact(&self, chunk_size: usize) -> ChunksExact<'_, T> {
12341240
assert!(chunk_size != 0, "chunk size must be non-zero");
12351241
ChunksExact::new(self, chunk_size)
12361242
}
@@ -1271,9 +1277,10 @@ impl<T> [T] {
12711277
/// [`chunks_mut`]: slice::chunks_mut
12721278
/// [`rchunks_exact_mut`]: slice::rchunks_exact_mut
12731279
#[stable(feature = "chunks_exact", since = "1.31.0")]
1280+
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
12741281
#[inline]
12751282
#[track_caller]
1276-
pub fn chunks_exact_mut(&mut self, chunk_size: usize) -> ChunksExactMut<'_, T> {
1283+
pub const fn chunks_exact_mut(&mut self, chunk_size: usize) -> ChunksExactMut<'_, T> {
12771284
assert!(chunk_size != 0, "chunk size must be non-zero");
12781285
ChunksExactMut::new(self, chunk_size)
12791286
}
@@ -1429,9 +1436,10 @@ impl<T> [T] {
14291436
///
14301437
/// [`chunks_exact`]: slice::chunks_exact
14311438
#[unstable(feature = "array_chunks", issue = "74985")]
1439+
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
14321440
#[inline]
14331441
#[track_caller]
1434-
pub fn array_chunks<const N: usize>(&self) -> ArrayChunks<'_, T, N> {
1442+
pub const fn array_chunks<const N: usize>(&self) -> ArrayChunks<'_, T, N> {
14351443
assert!(N != 0, "chunk size must be non-zero");
14361444
ArrayChunks::new(self)
14371445
}
@@ -1592,9 +1600,10 @@ impl<T> [T] {
15921600
///
15931601
/// [`chunks_exact_mut`]: slice::chunks_exact_mut
15941602
#[unstable(feature = "array_chunks", issue = "74985")]
1603+
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
15951604
#[inline]
15961605
#[track_caller]
1597-
pub fn array_chunks_mut<const N: usize>(&mut self) -> ArrayChunksMut<'_, T, N> {
1606+
pub const fn array_chunks_mut<const N: usize>(&mut self) -> ArrayChunksMut<'_, T, N> {
15981607
assert!(N != 0, "chunk size must be non-zero");
15991608
ArrayChunksMut::new(self)
16001609
}
@@ -1625,9 +1634,10 @@ impl<T> [T] {
16251634
///
16261635
/// [`windows`]: slice::windows
16271636
#[unstable(feature = "array_windows", issue = "75027")]
1637+
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
16281638
#[inline]
16291639
#[track_caller]
1630-
pub fn array_windows<const N: usize>(&self) -> ArrayWindows<'_, T, N> {
1640+
pub const fn array_windows<const N: usize>(&self) -> ArrayWindows<'_, T, N> {
16311641
assert!(N != 0, "window size must be non-zero");
16321642
ArrayWindows::new(self)
16331643
}
@@ -1660,9 +1670,10 @@ impl<T> [T] {
16601670
/// [`rchunks_exact`]: slice::rchunks_exact
16611671
/// [`chunks`]: slice::chunks
16621672
#[stable(feature = "rchunks", since = "1.31.0")]
1673+
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
16631674
#[inline]
16641675
#[track_caller]
1665-
pub fn rchunks(&self, chunk_size: usize) -> RChunks<'_, T> {
1676+
pub const fn rchunks(&self, chunk_size: usize) -> RChunks<'_, T> {
16661677
assert!(chunk_size != 0, "chunk size must be non-zero");
16671678
RChunks::new(self, chunk_size)
16681679
}
@@ -1699,9 +1710,10 @@ impl<T> [T] {
16991710
/// [`rchunks_exact_mut`]: slice::rchunks_exact_mut
17001711
/// [`chunks_mut`]: slice::chunks_mut
17011712
#[stable(feature = "rchunks", since = "1.31.0")]
1713+
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
17021714
#[inline]
17031715
#[track_caller]
1704-
pub fn rchunks_mut(&mut self, chunk_size: usize) -> RChunksMut<'_, T> {
1716+
pub const fn rchunks_mut(&mut self, chunk_size: usize) -> RChunksMut<'_, T> {
17051717
assert!(chunk_size != 0, "chunk size must be non-zero");
17061718
RChunksMut::new(self, chunk_size)
17071719
}
@@ -1739,9 +1751,10 @@ impl<T> [T] {
17391751
/// [`rchunks`]: slice::rchunks
17401752
/// [`chunks_exact`]: slice::chunks_exact
17411753
#[stable(feature = "rchunks", since = "1.31.0")]
1754+
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
17421755
#[inline]
17431756
#[track_caller]
1744-
pub fn rchunks_exact(&self, chunk_size: usize) -> RChunksExact<'_, T> {
1757+
pub const fn rchunks_exact(&self, chunk_size: usize) -> RChunksExact<'_, T> {
17451758
assert!(chunk_size != 0, "chunk size must be non-zero");
17461759
RChunksExact::new(self, chunk_size)
17471760
}
@@ -1783,9 +1796,10 @@ impl<T> [T] {
17831796
/// [`rchunks_mut`]: slice::rchunks_mut
17841797
/// [`chunks_exact_mut`]: slice::chunks_exact_mut
17851798
#[stable(feature = "rchunks", since = "1.31.0")]
1799+
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
17861800
#[inline]
17871801
#[track_caller]
1788-
pub fn rchunks_exact_mut(&mut self, chunk_size: usize) -> RChunksExactMut<'_, T> {
1802+
pub const fn rchunks_exact_mut(&mut self, chunk_size: usize) -> RChunksExactMut<'_, T> {
17891803
assert!(chunk_size != 0, "chunk size must be non-zero");
17901804
RChunksExactMut::new(self, chunk_size)
17911805
}
@@ -1823,8 +1837,9 @@ impl<T> [T] {
18231837
/// assert_eq!(iter.next(), None);
18241838
/// ```
18251839
#[stable(feature = "slice_group_by", since = "1.77.0")]
1840+
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
18261841
#[inline]
1827-
pub fn chunk_by<F>(&self, pred: F) -> ChunkBy<'_, T, F>
1842+
pub const fn chunk_by<F>(&self, pred: F) -> ChunkBy<'_, T, F>
18281843
where
18291844
F: FnMut(&T, &T) -> bool,
18301845
{
@@ -1864,8 +1879,9 @@ impl<T> [T] {
18641879
/// assert_eq!(iter.next(), None);
18651880
/// ```
18661881
#[stable(feature = "slice_group_by", since = "1.77.0")]
1882+
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
18671883
#[inline]
1868-
pub fn chunk_by_mut<F>(&mut self, pred: F) -> ChunkByMut<'_, T, F>
1884+
pub const fn chunk_by_mut<F>(&mut self, pred: F) -> ChunkByMut<'_, T, F>
18691885
where
18701886
F: FnMut(&T, &T) -> bool,
18711887
{

0 commit comments

Comments
 (0)