Skip to content

Conciliating Rust results with a C-idiomatic Out<Error> parameter #113

Discussion options

You must be logged in to vote

Final stage

Now, from the previous stage, the idea is now to try and reach stable Rust. try { … } blocks, especially -> Result-yielding ones, can easily be emulated by a macro that hides an inner closure.

But, given that we're gonna be feeding all this to a error.…(…) API nonetheless, why not make such API directly take such a closure, and skip the macro noise / complexity layer directly?

This thus yields what I think is the final and cleanest solution:

pub
fn api_func (error: Option<Out<'_, Error>>)
  -> Option<usize>
{
    error.try_(|| {
        let x = returns_option().context("returns_option()")?;
        let y = returns_result().context("returns_result()")?;
        Ok(x + y)
    })
}

Replies: 3 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@fjarri
Comment options

Answer selected by danielhenrymantilla
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants