Returning early from a function I'm in, from inside a closure

Hi folks, I'm facing difficulties returning early from my function, while inside a closure, passed to a library function I call.

Here is the library function docs: invoke_from_event_loop in slint - Rust,
this is the signature:

pub fn invoke_from_event_loop(
    func: impl FnOnce() + Send + 'static,
) -> Result<(), EventLoopError> 

Here an example usage:

slint::invoke_from_event_loop(move || handle_copy.unwrap().set_foo(foo)); ()

My Q is, given the example above, how to propagate the error from handle_copy, instead of .unwrap()ing it? The closure shall return EventLoopError in case of failure, however that's not a suitable error type for my situation, where I parse stuff.
Thank you!

let copy = handle_copy?;
slint::invoke_from_event_loop(move || copy.set_foo(foo));
Ok(())
2 Likes

You cannot do what you're asking for. In fact, even when the surrounding function returns, your closure might not have yet executed. Slint stores the closure for execution at some point, which might happen after the surrounding function returns.

1 Like

Thank you for the help! I felt like I try to do something impossible, thanks for confirming there isn't another way. In fact my lambda is more complicated than a single function call, and the parsing which might fail is done only when some conditions say so. I'll leave it for now the way it is, with doing operations which might fail first and passing their results to the lambda.