]> git.lizzy.rs Git - rust.git/blobdiff - library/core/src/ops/control_flow.rs
Rollup merge of #84540 - 12101111:enable-sanitizers, r=Mark-Simulacrum
[rust.git] / library / core / src / ops / control_flow.rs
index 2f78ba8f28e29189697d5c78d375a792d13c3854..ecaff053bd5c0ebe49471c176c3ade6b909fdc09 100644 (file)
@@ -1,4 +1,5 @@
-use crate::ops::Try;
+use crate::convert;
+use crate::ops::{self, Try};
 
 /// Used to tell an operation whether it should exit early or go on as usual.
 ///
@@ -81,6 +82,35 @@ fn from_ok(v: Self::Ok) -> Self {
     }
 }
 
+#[unstable(feature = "try_trait_v2", issue = "84277")]
+impl<B, C> ops::TryV2 for ControlFlow<B, C> {
+    type Output = C;
+    type Residual = ControlFlow<B, convert::Infallible>;
+
+    #[inline]
+    fn from_output(output: Self::Output) -> Self {
+        ControlFlow::Continue(output)
+    }
+
+    #[inline]
+    fn branch(self) -> ControlFlow<Self::Residual, Self::Output> {
+        match self {
+            ControlFlow::Continue(c) => ControlFlow::Continue(c),
+            ControlFlow::Break(b) => ControlFlow::Break(ControlFlow::Break(b)),
+        }
+    }
+}
+
+#[unstable(feature = "try_trait_v2", issue = "84277")]
+impl<B, C> ops::FromResidual for ControlFlow<B, C> {
+    #[inline]
+    fn from_residual(residual: ControlFlow<B, convert::Infallible>) -> Self {
+        match residual {
+            ControlFlow::Break(b) => ControlFlow::Break(b),
+        }
+    }
+}
+
 impl<B, C> ControlFlow<B, C> {
     /// Returns `true` if this is a `Break` variant.
     ///