about summary refs log tree commit diff
diff options
context:
space:
mode:
authoronestacked <chrisi.schrefl@gmail.com>2022-09-23 13:43:34 +0200
committeronestacked <chrisi.schrefl@gmail.com>2022-09-23 13:43:34 +0200
commit8e0ea60a04c463bf52fc81dbbf182f4739525681 (patch)
tree5d97939e718c6256585a67115ef6fdafb90651f0
parent0b2f717dfaf4835aa644d925a12c93db8f15dd61 (diff)
downloadrust-8e0ea60a04c463bf52fc81dbbf182f4739525681.tar.gz
rust-8e0ea60a04c463bf52fc81dbbf182f4739525681.zip
Constifed Try trait
-rw-r--r--library/core/src/iter/adapters/array_chunks.rs9
-rw-r--r--library/core/src/iter/adapters/by_ref_sized.rs19
-rw-r--r--library/core/src/iter/adapters/mod.rs5
-rw-r--r--library/core/src/ops/control_flow.rs2
-rw-r--r--library/core/src/ops/try_trait.rs18
5 files changed, 34 insertions, 19 deletions
diff --git a/library/core/src/iter/adapters/array_chunks.rs b/library/core/src/iter/adapters/array_chunks.rs
index 9b479a9f8ad..489fb13c0dc 100644
--- a/library/core/src/iter/adapters/array_chunks.rs
+++ b/library/core/src/iter/adapters/array_chunks.rs
@@ -1,4 +1,5 @@
 use crate::array;
+use crate::const_closure::ConstFnMutClosure;
 use crate::iter::{ByRefSized, FusedIterator, Iterator};
 use crate::ops::{ControlFlow, NeverShortCircuit, Try};
 
@@ -82,12 +83,12 @@ where
         }
     }
 
-    fn fold<B, F>(mut self, init: B, f: F) -> B
+    fn fold<B, F>(mut self, init: B, mut f: F) -> B
     where
         Self: Sized,
         F: FnMut(B, Self::Item) -> B,
     {
-        self.try_fold(init, NeverShortCircuit::wrap_mut_2(f)).0
+        self.try_fold(init, ConstFnMutClosure::new(&mut f, NeverShortCircuit::wrap_mut_2_imp)).0
     }
 }
 
@@ -126,12 +127,12 @@ where
         try { acc }
     }
 
-    fn rfold<B, F>(mut self, init: B, f: F) -> B
+    fn rfold<B, F>(mut self, init: B, mut f: F) -> B
     where
         Self: Sized,
         F: FnMut(B, Self::Item) -> B,
     {
-        self.try_rfold(init, NeverShortCircuit::wrap_mut_2(f)).0
+        self.try_rfold(init, ConstFnMutClosure::new(&mut f, NeverShortCircuit::wrap_mut_2_imp)).0
     }
 }
 
diff --git a/library/core/src/iter/adapters/by_ref_sized.rs b/library/core/src/iter/adapters/by_ref_sized.rs
index 477e7117c3e..1945e402ff5 100644
--- a/library/core/src/iter/adapters/by_ref_sized.rs
+++ b/library/core/src/iter/adapters/by_ref_sized.rs
@@ -1,4 +1,7 @@
-use crate::ops::{NeverShortCircuit, Try};
+use crate::{
+    const_closure::ConstFnMutClosure,
+    ops::{NeverShortCircuit, Try},
+};
 
 /// Like `Iterator::by_ref`, but requiring `Sized` so it can forward generics.
 ///
@@ -36,12 +39,13 @@ impl<I: Iterator> Iterator for ByRefSized<'_, I> {
     }
 
     #[inline]
-    fn fold<B, F>(self, init: B, f: F) -> B
+    fn fold<B, F>(self, init: B, mut f: F) -> B
     where
         F: FnMut(B, Self::Item) -> B,
     {
         // `fold` needs ownership, so this can't forward directly.
-        I::try_fold(self.0, init, NeverShortCircuit::wrap_mut_2(f)).0
+        I::try_fold(self.0, init, ConstFnMutClosure::new(&mut f, NeverShortCircuit::wrap_mut_2_imp))
+            .0
     }
 
     #[inline]
@@ -72,12 +76,17 @@ impl<I: DoubleEndedIterator> DoubleEndedIterator for ByRefSized<'_, I> {
     }
 
     #[inline]
-    fn rfold<B, F>(self, init: B, f: F) -> B
+    fn rfold<B, F>(self, init: B, mut f: F) -> B
     where
         F: FnMut(B, Self::Item) -> B,
     {
         // `rfold` needs ownership, so this can't forward directly.
-        I::try_rfold(self.0, init, NeverShortCircuit::wrap_mut_2(f)).0
+        I::try_rfold(
+            self.0,
+            init,
+            ConstFnMutClosure::new(&mut f, NeverShortCircuit::wrap_mut_2_imp),
+        )
+        .0
     }
 
     #[inline]
diff --git a/library/core/src/iter/adapters/mod.rs b/library/core/src/iter/adapters/mod.rs
index bf4fabad32a..de3a534f81b 100644
--- a/library/core/src/iter/adapters/mod.rs
+++ b/library/core/src/iter/adapters/mod.rs
@@ -1,3 +1,4 @@
+use crate::const_closure::ConstFnMutClosure;
 use crate::iter::{InPlaceIterable, Iterator};
 use crate::ops::{ChangeOutputType, ControlFlow, FromResidual, NeverShortCircuit, Residual, Try};
 
@@ -203,12 +204,12 @@ where
             .into_try()
     }
 
-    fn fold<B, F>(mut self, init: B, fold: F) -> B
+    fn fold<B, F>(mut self, init: B, mut fold: F) -> B
     where
         Self: Sized,
         F: FnMut(B, Self::Item) -> B,
     {
-        self.try_fold(init, NeverShortCircuit::wrap_mut_2(fold)).0
+        self.try_fold(init, ConstFnMutClosure::new(&mut fold, NeverShortCircuit::wrap_mut_2_imp)).0
     }
 }
 
diff --git a/library/core/src/ops/control_flow.rs b/library/core/src/ops/control_flow.rs
index fd567a8c684..8d236a9feca 100644
--- a/library/core/src/ops/control_flow.rs
+++ b/library/core/src/ops/control_flow.rs
@@ -126,7 +126,7 @@ impl<B, C> const ops::FromResidual for ControlFlow<B, C> {
 }
 
 #[unstable(feature = "try_trait_v2_residual", issue = "91285")]
-impl<B, C> ops::Residual<C> for ControlFlow<B, convert::Infallible> {
+impl<B, C> const ops::Residual<C> for ControlFlow<B, convert::Infallible> {
     type TryType = ControlFlow<B, C>;
 }
 
diff --git a/library/core/src/ops/try_trait.rs b/library/core/src/ops/try_trait.rs
index dfde0b37acf..4d0d4e12adb 100644
--- a/library/core/src/ops/try_trait.rs
+++ b/library/core/src/ops/try_trait.rs
@@ -129,7 +129,7 @@ use crate::ops::ControlFlow;
 #[doc(alias = "?")]
 #[lang = "Try"]
 #[const_trait]
-pub trait Try: FromResidual {
+pub trait Try: ~const FromResidual {
     /// The type of the value produced by `?` when *not* short-circuiting.
     #[unstable(feature = "try_trait_v2", issue = "84277")]
     type Output;
@@ -438,10 +438,11 @@ where
 /// and in the other direction,
 /// `<Result<Infallible, E> as Residual<T>>::TryType = Result<T, E>`.
 #[unstable(feature = "try_trait_v2_residual", issue = "91285")]
+#[const_trait]
 pub trait Residual<O> {
     /// The "return" type of this meta-function.
     #[unstable(feature = "try_trait_v2_residual", issue = "91285")]
-    type TryType: Try<Output = O, Residual = Self>;
+    type TryType: ~const Try<Output = O, Residual = Self>;
 }
 
 #[unstable(feature = "pub_crate_should_not_need_unstable_attr", issue = "none")]
@@ -460,14 +461,17 @@ pub(crate) struct NeverShortCircuit<T>(pub T);
 impl<T> NeverShortCircuit<T> {
     /// Wrap a binary `FnMut` to return its result wrapped in a `NeverShortCircuit`.
     #[inline]
-    pub 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))
+    pub const fn wrap_mut_2_imp<A, B, F: ~const FnMut(A, B) -> T>(
+        f: &mut F,
+        (a, b): (A, B),
+    ) -> NeverShortCircuit<T> {
+        NeverShortCircuit(f(a, b))
     }
 }
 
 pub(crate) enum NeverShortCircuitResidual {}
 
-impl<T> Try for NeverShortCircuit<T> {
+impl<T> const Try for NeverShortCircuit<T> {
     type Output = T;
     type Residual = NeverShortCircuitResidual;
 
@@ -482,14 +486,14 @@ impl<T> Try for NeverShortCircuit<T> {
     }
 }
 
-impl<T> FromResidual for NeverShortCircuit<T> {
+impl<T> const FromResidual for NeverShortCircuit<T> {
     #[inline]
     fn from_residual(never: NeverShortCircuitResidual) -> Self {
         match never {}
     }
 }
 
-impl<T> Residual<T> for NeverShortCircuitResidual {
+impl<T> const Residual<T> for NeverShortCircuitResidual {
     type TryType = NeverShortCircuit<T>;
 }