]> git.lizzy.rs Git - rust.git/blobdiff - crates/test-utils/src/minicore.rs
Diagnose incorrect usages of the question mark operator
[rust.git] / crates / test-utils / src / minicore.rs
index 69d2e62b2567357541b81ab43bb83ea2ff1ac7f3..59b1c147d7f174e08339de3225c5e3541b2af1c9 100644 (file)
@@ -27,6 +27,7 @@
 //!     generator: pin
 //!     hash:
 //!     index: sized
+//!     infallible:
 //!     iterator: option
 //!     iterators: iterator, fn
 //!     option:
@@ -36,7 +37,7 @@
 //!     result:
 //!     sized:
 //!     slice:
-//!     try:
+//!     try: infallible
 //!     unsize: sized
 
 pub mod marker {
@@ -150,6 +151,9 @@ pub trait AsRef<T: ?Sized> {
         fn as_ref(&self) -> &T;
     }
     // endregion:as_ref
+    // region:infallible
+    pub enum Infallible {}
+    // endregion:infallible
 }
 
 pub mod ops {
@@ -326,7 +330,7 @@ pub enum ControlFlow<B, C = ()> {
             Continue(C),
             Break(B),
         }
-        pub trait FromResidual<R = Self::Residual> {
+        pub trait FromResidual<R = <Self as Try>::Residual> {
             #[lang = "from_residual"]
             fn from_residual(residual: R) -> Self;
         }
@@ -342,13 +346,13 @@ pub trait Try: FromResidual<Self::Residual> {
 
         impl<B, C> Try for ControlFlow<B, C> {
             type Output = C;
-            type Residual = ControlFlow<B, convert::Infallible>;
+            type Residual = ControlFlow<B, crate::convert::Infallible>;
             fn from_output(output: Self::Output) -> Self {}
             fn branch(self) -> ControlFlow<Self::Residual, Self::Output> {}
         }
 
         impl<B, C> FromResidual for ControlFlow<B, C> {
-            fn from_residual(residual: ControlFlow<B, convert::Infallible>) -> Self {}
+            fn from_residual(residual: ControlFlow<B, crate::convert::Infallible>) -> Self {}
         }
     }
     pub use self::try_::{ControlFlow, FromResidual, Try};
@@ -469,6 +473,33 @@ pub const fn unwrap(self) -> T {
             }
         }
     }
+    // region:try
+    impl<T> crate::ops::Try for Option<T> {
+        type Output = T;
+        type Residual = Option<crate::convert::Infallible>;
+
+        #[inline]
+        fn from_output(output: Self::Output) -> Self {
+            Some(output)
+        }
+
+        #[inline]
+        fn branch(self) -> crate::ops::ControlFlow<Self::Residual, Self::Output> {
+            match self {
+                Some(v) => crate::ops::ControlFlow::Continue(v),
+                None => crate::ops::ControlFlow::Break(None),
+            }
+        }
+    }
+    impl<T> crate::ops::FromResidual for Option<T> {
+        #[inline]
+        fn from_residual(residual: Option<crate::convert::Infallible>) -> Self {
+            match residual {
+                None => None,
+            }
+        }
+    }
+    // endregion:try
 }
 // endregion:option