]> git.lizzy.rs Git - rust.git/blobdiff - library/core/src/ops/control_flow.rs
Rollup merge of #85315 - satylogin:master, r=yaahc
[rust.git] / library / core / src / ops / control_flow.rs
index b5ac590b59d7eabe233578c5b9ed3ba4aa11db29..dbb51540bd475eaef3c005a9126f1ad216cc404b 100644 (file)
 #[derive(Debug, Clone, Copy, PartialEq)]
 pub enum ControlFlow<B, C = ()> {
     /// Move on to the next phase of the operation as normal.
+    #[cfg_attr(not(bootstrap), lang = "Continue")]
     Continue(C),
     /// Exit the operation without running subsequent phases.
+    #[cfg_attr(not(bootstrap), lang = "Break")]
     Break(B),
     // Yes, the order of the variants doesn't match the type parameters.
     // They're in this order so that `ControlFlow<A, B>` <-> `Result<B, A>`
@@ -62,10 +64,10 @@ pub enum ControlFlow<B, C = ()> {
 
 #[unstable(feature = "control_flow_enum", reason = "new API", issue = "75744")]
 impl<B, C> ops::TryV1 for ControlFlow<B, C> {
-    type Ok = C;
+    type Output = C;
     type Error = B;
     #[inline]
-    fn into_result(self) -> Result<Self::Ok, Self::Error> {
+    fn into_result(self) -> Result<Self::Output, Self::Error> {
         match self {
             ControlFlow::Continue(y) => Ok(y),
             ControlFlow::Break(x) => Err(x),
@@ -76,7 +78,7 @@ fn from_error(v: Self::Error) -> Self {
         ControlFlow::Break(v)
     }
     #[inline]
-    fn from_ok(v: Self::Ok) -> Self {
+    fn from_ok(v: Self::Output) -> Self {
         ControlFlow::Continue(v)
     }
 }
@@ -181,7 +183,8 @@ pub fn map_break<T, F>(self, f: F) -> ControlFlow<T, C>
     }
 }
 
-impl<R: ops::TryV1> ControlFlow<R, R::Ok> {
+#[cfg(bootstrap)]
+impl<R: ops::TryV1> ControlFlow<R, R::Output> {
     /// Create a `ControlFlow` from any type implementing `Try`.
     #[unstable(feature = "control_flow_enum", reason = "new API", issue = "75744")]
     #[inline]
@@ -203,6 +206,29 @@ pub fn into_try(self) -> R {
     }
 }
 
+#[cfg(not(bootstrap))]
+impl<R: ops::TryV2> ControlFlow<R, R::Output> {
+    /// Create a `ControlFlow` from any type implementing `Try`.
+    #[unstable(feature = "control_flow_enum", reason = "new API", issue = "75744")]
+    #[inline]
+    pub fn from_try(r: R) -> Self {
+        match R::branch(r) {
+            ControlFlow::Continue(v) => ControlFlow::Continue(v),
+            ControlFlow::Break(v) => ControlFlow::Break(R::from_residual(v)),
+        }
+    }
+
+    /// Convert a `ControlFlow` into any type implementing `Try`;
+    #[unstable(feature = "control_flow_enum", reason = "new API", issue = "75744")]
+    #[inline]
+    pub fn into_try(self) -> R {
+        match self {
+            ControlFlow::Continue(v) => R::from_output(v),
+            ControlFlow::Break(v) => v,
+        }
+    }
+}
+
 impl<B> ControlFlow<B, ()> {
     /// It's frequently the case that there's no value needed with `Continue`,
     /// so this provides a way to avoid typing `(())`, if you prefer it.