Skip to content

split try trait in twain#154715

Open
bend-n wants to merge 2 commits intorust-lang:mainfrom
bend-n:the-try.-split-it
Open

split try trait in twain#154715
bend-n wants to merge 2 commits intorust-lang:mainfrom
bend-n:the-try.-split-it

Conversation

@bend-n
Copy link
Copy Markdown
Contributor

@bend-n bend-n commented Apr 2, 2026

https://rust-lang.zulipchat.com/#narrow/channel/219381-t-libs/topic/on.20.60Try.60.20for.20.60.26Option.60/with/582974682

currently Try for &Option is impossible, because from_output would need to return &Option from &Self::Output. but i think there should be some way to do Option::as_ref in a generic setting, and i think this is a good cause to split the traits up a bit, like so:

pub trait Branch {
    type Output;
    type Residual;
    fn branch(self) -> ControlFlow<Self::Residual, Self::Output>;
}
pub trait FromOutput<O = <Self as Branch>::Output>: Branch {
    fn from_output(output: O) -> Self;
}
pub trait FromResidual<R = <Self as Branch>::Residual>: Branch {
    fn from_residual(residual: R) -> Self;
}
pub trait Try = Branch + FromResidual + FromOutput;
pub trait Residual<O>: Sized {
    type TryType: Try<Output = O, Residual = Self>;
}

which then allows (this is not implemented in this PR.)

impl<'a, T, E> Branch for &'a Result<T, E> {
    type Output = &'a T;
    type Residual = Result<Infallible, &'a E>;

    fn branch(self) -> ControlFlow<Self::Residual, Self::Output> {}
}

which then allows

fn as_ref<R>(x: R) -> <R::Residual as Residual<<R as Branch>::Output>>::TryType
where
    R: Branch<Residual: Residual<<R as Branch>::Output>>
 {}

which then allows

let x: Result<&i32, &()> = as_ref(&Ok::<i32, ()>(1));

which i think is a great boon to Try.

@rust-log-analyzer

This comment has been minimized.

@rustbot rustbot added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. T-rust-analyzer Relevant to the rust-analyzer team, which will review and decide on the PR/issue. labels Apr 4, 2026
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-bors

This comment has been minimized.

@bend-n bend-n force-pushed the the-try.-split-it branch from 709df2d to cb71243 Compare April 18, 2026 16:37
@rust-log-analyzer

This comment has been minimized.

@rustbot rustbot added the T-clippy Relevant to the Clippy team. label Apr 19, 2026
@bend-n bend-n marked this pull request as ready for review April 19, 2026 14:11
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 19, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 19, 2026

rust-analyzer is developed in its own repository. If possible, consider making this change to rust-lang/rust-analyzer instead.

cc @rust-lang/rust-analyzer

Some changes occurred to the CTFE / Miri interpreter

cc @rust-lang/miri, @RalfJung, @oli-obk, @lcnr

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 19, 2026

r? @jhpratt

rustbot has assigned @jhpratt.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @scottmcm, libs
  • @scottmcm, libs expanded to 7 candidates
  • Random selection from Mark-Simulacrum, jhpratt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. T-rust-analyzer Relevant to the rust-analyzer team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants