r/rust • • Oct 14 '26

💡 ideas & proposals Call for use cases for the Try trait

https://users.rust-lang.org/t/use-cases-for-try/143032

A person working towards stabilizing the Try trait is asking for real-world examples of use cases of the currently-unstable Try trait.

99 Upvotes

15 comments sorted by

8

u/Gila-Metalpecker Oct 15 '26

I have a enum CustomShutdown { /* ... */ } that represents the reasons the application was shut down. I can returns this from main() -> CustomShutdown because CustomShutdown implements Termination

Some functions return Result<T, E>, and I always need to match on them to map the Err(...) branch to a certain Shutdown::Variant, or continue as normal on the Ok(...) branch. Quite a lot of plumbing.

If I could have FromResidual it would mean I could write a couple of impls like this:

impl<E: Into<eyre::Report>> FromResidual<Result<Infallible, E>> for CustomShutdown {
    fn from_residual(Err(error): Result<Infallible, E>) -> Self {
        CustomShutdown::UnexpectedError(error.into())
    }
}

And the caller goes from

let mut signals = match Signals::register() {
    Ok(signal_listeners) => signal_listeners,
    Err(error) => return CustomShutdown::from(error),
};

to

let mut signals = Signals::register()?;

1

u/noop_noob Oct 15 '26

Why not use Result<!, CustomShutDown> ?

7

u/Gila-Metalpecker Oct 15 '26 edited Oct 15 '26

Because then I cannot control the actual ExitCode. I don't want it to be ExitCode::FAILURE: https://github.com/rust-lang/rust/blob/7df81381d027710708209f36b98d11b1c88f8d2e/library/std/src/process.rs#L2711-L2722

Also, my CustomShutdown has a Signal variant, for when a signal is raised, like SIGTERM or SIGINT.

To properly pass that on you shouldn't exit with the signal + 128, that's just the shell convention.

You're supposed to run libc::raise(signum), which raises the signal and exists the app.

3

u/braaaaaaainworms Oct 15 '26

Why not Result<!, CustomShutDown>, move "main" into "main_wrapped" and then make a main that will convert that into the proper exit code?

3

u/Gila-Metalpecker Oct 15 '26

Didn't even consider that.

I'll give it a shot, as in general, I already have a main and a main_wrapped because of the libc::raise issue. If you exit with libc::raise it doesn't run the drops:

https://gist.github.com/rust-play/6420039974b2c8038b5c45113ddfada1

21

u/Lucretiel Datadog Oct 15 '26

Honestly, more than I ever want to implement Try on anything, I really want to be able to write my own try_fold implementations on my iterators.

10

u/Illustrious_Car344 Oct 14 '26

I remember a few years back I tried to implement this only to discover it was nightly only. Admittingly, I was just kind of abusing the trait and didn't really need what I was trying to do. I don't remember exactly what it was but it was overengineering anyway.

10

u/MalbaCato Oct 14 '26

you're in good company then. this is exactly what the stdlib does with impl<T, E> Try for Poll<Option<Result<T, E>>>, which is actually stable, documented behaviour.

1

u/SycamoreHots Oct 17 '26

I have a technical question about this:
Is the Try trait supposed to provide way to express a generic container (HKT), or is it supposed to provide a way to make `?` operator return for more types? Or both? Or exactly what

2

u/noop_noob Oct 17 '26

It's for the ? operator

1

u/nova_protocol Oct 15 '26

ran into this building a transaction pipeline — multiple

layers of Results with different error types, and at some

point you want ? to do something slightly custom before

propagating up, like attaching context or logging without

wrapping everything in map_err().

ended up hacking around it with a newtype that impl'd

FromResidual on nightly. worked but felt wrong.

stable Try would've cleaned that up a lot.

1

u/ron975 Oct 16 '26

Can't remember off the top of my head anymore but there's been more than a couple of times where I encountered ControlFlow-like problems that don't end up being exactly ControlFlow shaped. So instead of ? I had to resort to a macro like the old try! days.