How to make rayon::ThreadPoolBuilder work with error_chain?


#1

I have this code:

    if config.cpus > 0 { // Inside a function that returns Result<()>
        ThreadPoolBuilder::new().num_threads(config.cpus).build_global()?;
    }

But when I try to compile it I get errors:

error[E0277]: the trait bound `errors::Error: std::convert::From<rayon_core::ThreadPoolBuildError>` is not satisfied          
  --> src/main.rs:33:9                                                                                                        
   |                                                                                                                          
33 |         ThreadPoolBuilder::new().num_threads(config.cpus).build_global()?;                                               
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::convert::From<rayon_core::ThreadPoolBuildError>` is not implemented for `errors::Error`

Currently I have an errors.rs module with this code:

use error_chain::{error_chain, error_chain_processing,
                  impl_error_chain_processed, impl_error_chain_kind,
                  impl_extract_backtrace};

error_chain!{
    foreign_links {
        Int(::std::num::ParseIntError);
        Io(::std::io::Error);
    }
}

I know I need to add the ThreadPoolBuildError to the foreign_links, but I’ve tried various ways and can’t get any to work.


#2

What exactly did you try, and what happens?


#3

For example I tried this:

error_chain!{
    foreign_links {
        Int(::std::num::ParseIntError);
        Io(::std::io::Error);
        Rayon(rayon::core::ThreadPoolBuildError);
    }
}

which produces:

error[E0119]: conflicting implementations of trait `std::convert::From<[type error]>` for type `errors::Error`:               
  --> src/errors.rs:7:1                                                                                                       
   |                                                                                                                          
7  |    error_chain!{                                                                                                         
   |   _^                                                                                                                     
   |  |_|                                                                                                                     
   | ||                                                                                                                       
8  | ||     foreign_links {                                                                                                   
9  | ||         Int(::std::num::ParseIntError);                                                                               
10 | ||         Io(::std::io::Error);                                                                                         
11 | ||         Rayon(rayon::core::ThreadPoolBuildError);                                                                     
12 | ||     }                                                                                                                 
13 | || }                                                                                                                     
   | || ^                                                                                                                     
   | ||_|                                                                                                                     
   | |__first implementation here                                                                                             
   |    conflicting implementation for `errors::Error`

plus several repeats.

Thanks.


#4

Isn’t it rayon::ThreadPoolBuildError? Where did you get the core module from in your reference to it?


#5

I found it during a google search. However, changing it to Rayon(rayon::ThreadPoolBuildError) works; sorry I didn’t think of that myself.
Thanks!


#6

The first error I get on the playground is this:

error[E0433]: failed to resolve: could not find `core` in `rayon`
 --> src/lib.rs:7:22
  |
7 |         Rayon(rayon::core::ThreadPoolBuildError);
  |                      ^^^^ could not find `core` in `rayon`

The “conflicting implementations” errors come after, but I think the compiler is just confused at this point, trying to be helpful but working with incomplete data.

There is the rayon-core crate where the thread pool is actually defined, so perhaps that’s what you were thinking.