From b7c2199e61ee4cec22fdd24864a3dde41a7bab00 Mon Sep 17 00:00:00 2001 From: Scott McMurray Date: Fri, 25 Mar 2022 23:43:54 -0700 Subject: [PATCH] Add `do yeet` expressions to allow experimentation in nightly Using an obviously-placeholder syntax. An RFC would still be needed before this could have any chance at stabilization, and it might be removed at any point. But I'd really like to have it in nightly at least to ensure it works well with try_trait_v2, especially as we refactor the traits. --- core/src/ops/mod.rs | 3 +++ core/src/ops/try_trait.rs | 22 ++++++++++++++++++++++ core/src/option.rs | 8 ++++++++ core/src/result.rs | 8 ++++++++ 4 files changed, 41 insertions(+) diff --git a/core/src/ops/mod.rs b/core/src/ops/mod.rs index 9d1e7e81b..31c1a1d09 100644 --- a/core/src/ops/mod.rs +++ b/core/src/ops/mod.rs @@ -187,6 +187,9 @@ pub use self::range::OneSidedRange; #[unstable(feature = "try_trait_v2", issue = "84277")] pub use self::try_trait::{FromResidual, Try}; +#[unstable(feature = "try_trait_v2_yeet", issue = "96374")] +pub use self::try_trait::Yeet; + #[unstable(feature = "try_trait_v2_residual", issue = "91285")] pub use self::try_trait::Residual; diff --git a/core/src/ops/try_trait.rs b/core/src/ops/try_trait.rs index ba369e7f3..3eaee958b 100644 --- a/core/src/ops/try_trait.rs +++ b/core/src/ops/try_trait.rs @@ -330,6 +330,22 @@ pub trait FromResidual::Residual> { fn from_residual(residual: R) -> Self; } +#[cfg(not(bootstrap))] +#[unstable( + feature = "yeet_desugar_details", + issue = "none", + reason = "just here to simplify the desugaring; will never be stabilized" +)] +#[inline] +#[track_caller] // because `Result::from_residual` has it +#[lang = "from_yeet"] +pub fn from_yeet(yeeted: Y) -> T +where + T: FromResidual>, +{ + FromResidual::from_residual(Yeet(yeeted)) +} + /// Allows retrieving the canonical type implementing [`Try`] that has this type /// as its residual and allows it to hold an `O` as its output. /// @@ -395,3 +411,9 @@ impl FromResidual for NeverShortCircuit { impl Residual for NeverShortCircuitResidual { type TryType = NeverShortCircuit; } + +/// Implement `FromResidual>` on your type to enable +/// `do yeet expr` syntax in functions returning your type. +#[unstable(feature = "try_trait_v2_yeet", issue = "96374")] +#[derive(Debug)] +pub struct Yeet(pub T); diff --git a/core/src/option.rs b/core/src/option.rs index 91e4708f6..f339b076d 100644 --- a/core/src/option.rs +++ b/core/src/option.rs @@ -2287,6 +2287,14 @@ impl const ops::FromResidual for Option { } } +#[unstable(feature = "try_trait_v2_yeet", issue = "96374")] +impl ops::FromResidual> for Option { + #[inline] + fn from_residual(ops::Yeet(()): ops::Yeet<()>) -> Self { + None + } +} + #[unstable(feature = "try_trait_v2_residual", issue = "91285")] impl ops::Residual for Option { type TryType = Option; diff --git a/core/src/result.rs b/core/src/result.rs index b2b132300..5e5f8a5ab 100644 --- a/core/src/result.rs +++ b/core/src/result.rs @@ -2107,6 +2107,14 @@ impl> const ops::FromResidual> ops::FromResidual> for Result { + #[inline] + fn from_residual(ops::Yeet(e): ops::Yeet) -> Self { + Err(From::from(e)) + } +} + #[unstable(feature = "try_trait_v2_residual", issue = "91285")] impl ops::Residual for Result { type TryType = Result;