Constify Iterator-related methods and functions

This commit is contained in:
Evgenii Zheltonozhskii
2026-05-10 13:45:51 +03:00
parent e95e73209f
commit 0fae37518a
8 changed files with 45 additions and 14 deletions
+6 -3
View File
@@ -23,11 +23,13 @@ pub struct IntoIter<T, const N: usize> {
impl<T, const N: usize> IntoIter<T, N> {
#[inline]
fn unsize(&self) -> &InnerUnsized<T> {
#[rustc_const_unstable(feature = "const_iter", issue = "92476")]
const fn unsize(&self) -> &InnerUnsized<T> {
self.inner.deref()
}
#[inline]
fn unsize_mut(&mut self) -> &mut InnerUnsized<T> {
#[rustc_const_unstable(feature = "const_iter", issue = "92476")]
const fn unsize_mut(&mut self) -> &mut InnerUnsized<T> {
self.inner.deref_mut()
}
}
@@ -219,7 +221,8 @@ pub fn as_slice(&self) -> &[T] {
/// Returns a mutable slice of all elements that have not been yielded yet.
#[stable(feature = "array_value_iter", since = "1.51.0")]
#[inline]
pub fn as_mut_slice(&mut self) -> &mut [T] {
#[rustc_const_unstable(feature = "const_iter", issue = "92476")]
pub const fn as_mut_slice(&mut self) -> &mut [T] {
self.unsize_mut().as_mut_slice()
}
}
+2 -1
View File
@@ -134,7 +134,8 @@ pub(super) fn as_slice(&self) -> &[T] {
}
#[inline]
pub(super) fn as_mut_slice(&mut self) -> &mut [T] {
#[rustc_const_unstable(feature = "const_iter", issue = "92476")]
pub(super) const fn as_mut_slice(&mut self) -> &mut [T] {
// SAFETY: We know that all elements within `alive` are properly initialized.
unsafe {
let slice = self.data.get_unchecked_mut(self.alive.clone());
+3 -1
View File
@@ -63,9 +63,11 @@ impl<I: FusedIterator + ?Sized> FusedIterator for &mut I {}
/// of this trait must inspect [`Iterator::size_hint()`]s upper bound.
#[unstable(feature = "trusted_len", issue = "37572")]
#[rustc_unsafe_specialization_marker]
pub unsafe trait TrustedLen: Iterator {}
#[rustc_const_unstable(feature = "const_iter", issue = "92476")]
pub const unsafe trait TrustedLen: [const] Iterator {}
#[unstable(feature = "trusted_len", issue = "37572")]
#[rustc_const_unstable(feature = "const_iter", issue = "92476")]
unsafe impl<I: TrustedLen + ?Sized> TrustedLen for &mut I {}
/// An iterator that when yielding an item will have taken at least one element
+10 -2
View File
@@ -427,7 +427,11 @@ pub const fn into_value(self) -> T {
impl<R: ops::Try> ControlFlow<R, R::Output> {
/// Creates a `ControlFlow` from any type implementing `Try`.
#[inline]
pub(crate) fn from_try(r: R) -> Self {
#[rustc_const_unstable(feature = "const_control_flow", issue = "148739")]
pub(crate) const fn from_try(r: R) -> Self
where
R: [const] ops::Try,
{
match R::branch(r) {
ControlFlow::Continue(v) => ControlFlow::Continue(v),
ControlFlow::Break(v) => ControlFlow::Break(R::from_residual(v)),
@@ -436,7 +440,11 @@ pub(crate) fn from_try(r: R) -> Self {
/// Converts a `ControlFlow` into any type implementing `Try`.
#[inline]
pub(crate) fn into_try(self) -> R {
#[rustc_const_unstable(feature = "const_control_flow", issue = "148739")]
pub(crate) const fn into_try(self) -> R
where
R: [const] ops::Try,
{
match self {
ControlFlow::Continue(v) => R::from_output(v),
ControlFlow::Break(v) => v,
+8 -2
View File
@@ -416,8 +416,14 @@ pub(crate) const fn wrap_mut_1<A, F>(
}
#[inline]
pub(crate) fn wrap_mut_2<A, B>(mut f: impl FnMut(A, B) -> T) -> impl FnMut(A, B) -> Self {
move |a, b| NeverShortCircuit(f(a, b))
#[rustc_const_unstable(feature = "const_array", issue = "147606")]
pub(crate) const fn wrap_mut_2<A, B, F>(
mut f: F,
) -> impl [const] FnMut(A, B) -> Self + [const] Destruct
where
F: [const] FnMut(A, B) -> T + [const] Destruct,
{
const move |a, b| NeverShortCircuit(f(a, b))
}
}
+8 -3
View File
@@ -1748,8 +1748,12 @@ pub const fn insert(&mut self, value: T) -> &mut T
/// ```
#[inline]
#[stable(feature = "option_entry", since = "1.20.0")]
pub fn get_or_insert(&mut self, value: T) -> &mut T {
self.get_or_insert_with(|| value)
#[rustc_const_unstable(feature = "const_option_ops", issue = "143956")]
pub const fn get_or_insert(&mut self, value: T) -> &mut T
where
T: [const] Destruct,
{
self.get_or_insert_with(const || value)
}
/// Inserts the default value into the option if it is [`None`], then
@@ -2649,7 +2653,8 @@ impl<A> ExactSizeIterator for IntoIter<A> {}
impl<A> FusedIterator for IntoIter<A> {}
#[unstable(feature = "trusted_len", issue = "37572")]
unsafe impl<A> TrustedLen for IntoIter<A> {}
#[rustc_const_unstable(feature = "const_iter", issue = "92476")]
unsafe impl<A> const TrustedLen for IntoIter<A> {}
/// The iterator produced by [`Option::into_flat_iter`]. See its documentation for more.
#[derive(Clone, Debug)]
+6 -1
View File
@@ -1682,7 +1682,12 @@ pub const fn unwrap_or_else<F>(self, op: F) -> T
#[inline]
#[track_caller]
#[stable(feature = "option_result_unwrap_unchecked", since = "1.58.0")]
pub unsafe fn unwrap_err_unchecked(self) -> E {
#[rustc_const_unstable(feature = "const_result_unwrap_unchecked", issue = "148714")]
pub const unsafe fn unwrap_err_unchecked(self) -> E
where
T: [const] Destruct,
E: [const] Destruct,
{
match self {
// SAFETY: the safety contract must be upheld by the caller.
Ok(_) => unsafe { hint::unreachable_unchecked() },
+2 -1
View File
@@ -120,7 +120,8 @@ fn cmp(&self, other: &($($T,)+)) -> Ordering {
maybe_tuple_doc! {
$($T)+ @
#[stable(feature = "rust1", since = "1.0.0")]
impl<$($T: Default),+> Default for ($($T,)+) {
#[rustc_const_unstable(feature = "const_default", issue = "143894")]
impl<$($T: [const] Default),+> const Default for ($($T,)+) {
#[inline]
fn default() -> ($($T,)+) {
($({ let x: $T = Default::default(); x},)+)