Skip to content

Commit 06339bc

Browse files
committed
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.
1 parent cf5d745 commit 06339bc

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

core/src/ops/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ pub use self::range::OneSidedRange;
187187
#[unstable(feature = "try_trait_v2", issue = "84277")]
188188
pub use self::try_trait::{FromResidual, Try};
189189

190+
#[unstable(feature = "try_trait_v2_yeet", issue = "96374")]
191+
pub use self::try_trait::Yeet;
192+
190193
#[unstable(feature = "try_trait_v2_residual", issue = "91285")]
191194
pub use self::try_trait::Residual;
192195

core/src/ops/try_trait.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,22 @@ pub trait FromResidual<R = <Self as Try>::Residual> {
330330
fn from_residual(residual: R) -> Self;
331331
}
332332

333+
#[cfg(not(bootstrap))]
334+
#[unstable(
335+
feature = "yeet_desugar_details",
336+
issue = "none",
337+
reason = "just here to simplify the desugaring; will never be stabilized"
338+
)]
339+
#[inline]
340+
#[track_caller] // because `Result::from_residual` has it
341+
#[lang = "from_yeet"]
342+
pub fn from_yeet<T, Y>(yeeted: Y) -> T
343+
where
344+
T: FromResidual<Yeet<Y>>,
345+
{
346+
FromResidual::from_residual(Yeet(yeeted))
347+
}
348+
333349
/// Allows retrieving the canonical type implementing [`Try`] that has this type
334350
/// as its residual and allows it to hold an `O` as its output.
335351
///
@@ -395,3 +411,9 @@ impl<T> FromResidual for NeverShortCircuit<T> {
395411
impl<T> Residual<T> for NeverShortCircuitResidual {
396412
type TryType = NeverShortCircuit<T>;
397413
}
414+
415+
/// Implement `FromResidual<Yeet<T>>` on your type to enable
416+
/// `do yeet expr` syntax in functions returning your type.
417+
#[unstable(feature = "try_trait_v2_yeet", issue = "96374")]
418+
#[derive(Debug)]
419+
pub struct Yeet<T>(pub T);

core/src/option.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2287,6 +2287,14 @@ impl<T> const ops::FromResidual for Option<T> {
22872287
}
22882288
}
22892289

2290+
#[unstable(feature = "try_trait_v2_yeet", issue = "96374")]
2291+
impl<T> ops::FromResidual<ops::Yeet<()>> for Option<T> {
2292+
#[inline]
2293+
fn from_residual(ops::Yeet(()): ops::Yeet<()>) -> Self {
2294+
None
2295+
}
2296+
}
2297+
22902298
#[unstable(feature = "try_trait_v2_residual", issue = "91285")]
22912299
impl<T> ops::Residual<T> for Option<convert::Infallible> {
22922300
type TryType = Option<T>;

core/src/result.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2107,6 +2107,14 @@ impl<T, E, F: ~const From<E>> const ops::FromResidual<Result<convert::Infallible
21072107
}
21082108
}
21092109

2110+
#[unstable(feature = "try_trait_v2_yeet", issue = "96374")]
2111+
impl<T, E, F: From<E>> ops::FromResidual<ops::Yeet<E>> for Result<T, F> {
2112+
#[inline]
2113+
fn from_residual(ops::Yeet(e): ops::Yeet<E>) -> Self {
2114+
Err(From::from(e))
2115+
}
2116+
}
2117+
21102118
#[unstable(feature = "try_trait_v2_residual", issue = "91285")]
21112119
impl<T, E> ops::Residual<T> for Result<convert::Infallible, E> {
21122120
type TryType = Result<T, E>;

0 commit comments

Comments
 (0)