Skip to content

Commit ad2a80e

Browse files
authored
Rollup merge of #83571 - a1phyr:feature_const_slice_first_last, r=dtolnay
Constantify some slice methods Tracking issue: #83570 This PR constantifies the following functions under feature `const_slice_first_last`: - `slice::first` - `slice::split_first` - `slice::last` - `slice::split_last` Blocking on `#![feature(const_mut_refs)]`: - `slice::first_mut` - `slice::split_first_mut` - `slice::last_mut` - `slice::split_last_mut`
2 parents 9ab5f7d + 6327e46 commit ad2a80e

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

Diff for: library/core/src/slice/mod.rs

+16-8
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,9 @@ impl<T> [T] {
148148
/// assert_eq!(None, w.first());
149149
/// ```
150150
#[stable(feature = "rust1", since = "1.0.0")]
151+
#[rustc_const_unstable(feature = "const_slice_first_last", issue = "83570")]
151152
#[inline]
152-
pub fn first(&self) -> Option<&T> {
153+
pub const fn first(&self) -> Option<&T> {
153154
if let [first, ..] = self { Some(first) } else { None }
154155
}
155156

@@ -166,8 +167,9 @@ impl<T> [T] {
166167
/// assert_eq!(x, &[5, 1, 2]);
167168
/// ```
168169
#[stable(feature = "rust1", since = "1.0.0")]
170+
#[rustc_const_unstable(feature = "const_slice_first_last", issue = "83570")]
169171
#[inline]
170-
pub fn first_mut(&mut self) -> Option<&mut T> {
172+
pub const fn first_mut(&mut self) -> Option<&mut T> {
171173
if let [first, ..] = self { Some(first) } else { None }
172174
}
173175

@@ -184,8 +186,9 @@ impl<T> [T] {
184186
/// }
185187
/// ```
186188
#[stable(feature = "slice_splits", since = "1.5.0")]
189+
#[rustc_const_unstable(feature = "const_slice_first_last", issue = "83570")]
187190
#[inline]
188-
pub fn split_first(&self) -> Option<(&T, &[T])> {
191+
pub const fn split_first(&self) -> Option<(&T, &[T])> {
189192
if let [first, tail @ ..] = self { Some((first, tail)) } else { None }
190193
}
191194

@@ -204,8 +207,9 @@ impl<T> [T] {
204207
/// assert_eq!(x, &[3, 4, 5]);
205208
/// ```
206209
#[stable(feature = "slice_splits", since = "1.5.0")]
210+
#[rustc_const_unstable(feature = "const_slice_first_last", issue = "83570")]
207211
#[inline]
208-
pub fn split_first_mut(&mut self) -> Option<(&mut T, &mut [T])> {
212+
pub const fn split_first_mut(&mut self) -> Option<(&mut T, &mut [T])> {
209213
if let [first, tail @ ..] = self { Some((first, tail)) } else { None }
210214
}
211215

@@ -222,8 +226,9 @@ impl<T> [T] {
222226
/// }
223227
/// ```
224228
#[stable(feature = "slice_splits", since = "1.5.0")]
229+
#[rustc_const_unstable(feature = "const_slice_first_last", issue = "83570")]
225230
#[inline]
226-
pub fn split_last(&self) -> Option<(&T, &[T])> {
231+
pub const fn split_last(&self) -> Option<(&T, &[T])> {
227232
if let [init @ .., last] = self { Some((last, init)) } else { None }
228233
}
229234

@@ -242,8 +247,9 @@ impl<T> [T] {
242247
/// assert_eq!(x, &[4, 5, 3]);
243248
/// ```
244249
#[stable(feature = "slice_splits", since = "1.5.0")]
250+
#[rustc_const_unstable(feature = "const_slice_first_last", issue = "83570")]
245251
#[inline]
246-
pub fn split_last_mut(&mut self) -> Option<(&mut T, &mut [T])> {
252+
pub const fn split_last_mut(&mut self) -> Option<(&mut T, &mut [T])> {
247253
if let [init @ .., last] = self { Some((last, init)) } else { None }
248254
}
249255

@@ -259,8 +265,9 @@ impl<T> [T] {
259265
/// assert_eq!(None, w.last());
260266
/// ```
261267
#[stable(feature = "rust1", since = "1.0.0")]
268+
#[rustc_const_unstable(feature = "const_slice_first_last", issue = "83570")]
262269
#[inline]
263-
pub fn last(&self) -> Option<&T> {
270+
pub const fn last(&self) -> Option<&T> {
264271
if let [.., last] = self { Some(last) } else { None }
265272
}
266273

@@ -277,8 +284,9 @@ impl<T> [T] {
277284
/// assert_eq!(x, &[0, 1, 10]);
278285
/// ```
279286
#[stable(feature = "rust1", since = "1.0.0")]
287+
#[rustc_const_unstable(feature = "const_slice_first_last", issue = "83570")]
280288
#[inline]
281-
pub fn last_mut(&mut self) -> Option<&mut T> {
289+
pub const fn last_mut(&mut self) -> Option<&mut T> {
282290
if let [.., last] = self { Some(last) } else { None }
283291
}
284292

0 commit comments

Comments
 (0)