5
我有一個使用scoped_threadpool有點像這樣一些代碼:如何從scoped_threadpool線程返回錯誤?
extern crate scoped_threadpool;
use scoped_threadpool::Pool;
use std::error::Error;
fn main() {
inner_main().unwrap();
}
fn inner_main() -> Result<(), Box<Error>> {
let mut pool = Pool::new(2);
pool.scoped(|scope| {
scope.execute(move || {
// This changed to become fallible
fallible_code();
});
});
Ok(())
}
fn fallible_code() -> Result<(), Box<Error + Send + Sync>> {
Err(From::from("Failing"))
}
的fallible_code
功能最近改爲返回Result
,我想傳播pool.scoped
塊之外的錯誤。然而,Scope::execute
簽名不允許返回值:
fn execute<F>(&self, f: F)
where F: FnOnce() + Send + 'scope
我使用scoped_threadpool 0.1.7。
這個想法聽起來不錯,但不應該''同步'? –
@MatthieuM。這就是爲什麼'ret.map_err(| x | x作爲方框)'來匹配'inner_main'的返回類型的原因。 –
Veedrac