Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cancelling on Result Err #496

Closed
PSeitz opened this issue Dec 21, 2017 · 5 comments
Closed

Cancelling on Result Err #496

PSeitz opened this issue Dec 21, 2017 · 5 comments
Labels

Comments

@PSeitz
Copy link

PSeitz commented Dec 21, 2017

Hi I have a function, which returns an Result<(), Error>. I want to early return on errors.

I saw in result.rs something, that uses while_some may be the right method here, although I don't know how to use that. I couldn't find something in the documentation.

Also a while_ok method would probably be nice.

@PSeitz PSeitz changed the title par_iter cancelling on Result Err Cancelling on Result Err Dec 21, 2017
@cuviper
Copy link
Member

cuviper commented Dec 21, 2017

I saw in result.rs something

You're on the right track. FromParallelIterator is used by ParallelIterator::collect(), just like the standard FromIterator and Iterator::collect(). You'll often need to provide a type hint if it's not clear from context. So you could do something like:

let r: Result<Vec<_>, _> = collection.par_iter().map(fn_to_result).collect()

This will collect the Ok items into a Vec, or short-circuit on Err.

This reminds me, I should add FromParallelIterator<()> for () like rust-lang/rust#45379.

Also a while_ok method would probably be nice.

Well, while_some is basically just a pass-through of the unwrapped Some values, and doesn't need to keep the None that ends it. It's not clear what to do with an Err if we had while_ok. If you want to throw that away and just return early, you can use .map(Result::ok).while_some(). If you want to keep the error, then I think the collect model is better.

@PSeitz
Copy link
Author

PSeitz commented Dec 22, 2017

Thanks, I wasn't aware of the type hint, but seems pretty obvious in hindsight.

@PSeitz PSeitz closed this as completed Dec 22, 2017
bors bot added a commit that referenced this issue Jan 14, 2018
497: impl FromParallelIterator<()> for () r=cuviper a=cuviper

This is more useful when combined with higher-level abstractions, like
collecting to a `Result<(), E>` where you only care about errors.

This is a parallel version of rust-lang/rust#45379.
Cc #496

498: FromParallelIterator and ParallelExtend Cow for String r=cuviper a=cuviper

Parallel version of rust-lang/rust#41449.
bors bot added a commit that referenced this issue Jan 14, 2018
497: impl FromParallelIterator<()> for () r=cuviper a=cuviper

This is more useful when combined with higher-level abstractions, like
collecting to a `Result<(), E>` where you only care about errors.

This is a parallel version of rust-lang/rust#45379.
Cc #496
bors bot added a commit that referenced this issue Jan 14, 2018
497: impl FromParallelIterator<()> for () r=cuviper a=cuviper

This is more useful when combined with higher-level abstractions, like
collecting to a `Result<(), E>` where you only care about errors.

This is a parallel version of rust-lang/rust#45379.
Cc #496
@WilliamVenner
Copy link

while_ok would allow you to have a filter_map that stops depending on a predicate.

while_some does not work with filter_map for obvious reasons 😁

@ghost
Copy link

ghost commented Dec 19, 2022

Hello, I'm doing

fn main() -> anyhow::Result<()> {
    0..123123
        .into_par_iter()
        .map(|num| {
            /* something with lots of foo()? */
            println!("Hi from {num}");
            anyhow::Ok(())
        })
        .collect::<Result<Vec<_>, _>>()?
    ;
    Ok(())
}

And then running with cargo run | head -n 10. So when Bash closes the pipe to head after 10 successful printlns, errors start getting throw for every println with the broken pipe error, as expected. However, this doesn't seem to stop Rayon, as it keeps dumping panic errors on stderr non-stop.

What am I doing wrong please?

Rayon 1.6.1.

@cuviper
Copy link
Member

cuviper commented Dec 21, 2022

@YuriGeinishO the println! macro doesn't return any errors, even though it reports them. Try something like this:

use std::io::{stdout, Write};
// ...
            writeln!(stdout(), "Hi from {num}")?;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants