You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
use anyhow::{anyhow, Result as AResult};
fn main() -> AResult<()> {
std::thread::spawn( move || { Ok(anyhow!("test error")) }).join()??;
Ok(())
}
Errors with:
error[E0277]: the size for values of type `dyn std::any::Any + std::marker::Send` cannot be known at compilation time
--> src/main.rs:5:70
|
5 | std::thread::spawn( move || { Ok(anyhow!("test error")) }).join()??;
| ^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `dyn std::any::Any + std::marker::Send`
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
= note: required because of the requirements on the impl of `std::error::Error` for `std::boxed::Box<dyn std::any::Any + std::marker::Send>`
= note: required because of the requirements on the impl of `std::convert::From<std::boxed::Box<dyn std::any::Any + std::marker::Send>>` for `anyhow::Error`
= note: required by `std::convert::From::from`
error[E0277]: the trait bound `dyn std::any::Any + std::marker::Send: std::error::Error` is not satisfied
--> src/main.rs:5:70
|
5 | std::thread::spawn( move || { Ok(anyhow!("test error")) }).join()??;
| ^ the trait `std::error::Error` is not implemented for `dyn std::any::Any + std::marker::Send`
|
= note: required because of the requirements on the impl of `std::error::Error` for `std::boxed::Box<dyn std::any::Any + std::marker::Send>`
= note: required because of the requirements on the impl of `std::convert::From<std::boxed::Box<dyn std::any::Any + std::marker::Send>>` for `anyhow::Error`
= note: required by `std::convert::From::from`
error[E0277]: `dyn std::any::Any + std::marker::Send` cannot be shared between threads safely
--> src/main.rs:5:70
|
5 | std::thread::spawn( move || { Ok(anyhow!("test error")) }).join()??;
| ^ `dyn std::any::Any + std::marker::Send` cannot be shared between threads safely
|
= help: the trait `std::marker::Sync` is not implemented for `dyn std::any::Any + std::marker::Send`
= note: required because of the requirements on the impl of `std::marker::Sync` for `std::ptr::Unique<dyn std::any::Any + std::marker::Send>`
= note: required because it appears within the type `std::boxed::Box<dyn std::any::Any + std::marker::Send>`
= note: required because of the requirements on the impl of `std::convert::From<std::boxed::Box<dyn std::any::Any + std::marker::Send>>` for `anyhow::Error`
= note: required by `std::convert::From::from`
error: aborting due to 3 previous errors
Judging from the error messages, the reason is that the error value of std::thread::Result doesn't implement std::error::Error (!). Is there any suggested workarounds? I'd almost expect .map_err(|e| anyhow!(e)) to work for wrapping the error value to anyhow::Error, but it doesn't.
The text was updated successfully, but these errors were encountered:
thread::spawn doesn't return an error, the E in the Result is a box of whatever value your thread panicked with, which probably doesn't implement Error and maybe doesn't implement Display or Debug. You are supposed to propagate it by unwrapping.
I see. In that case better documentation would be warranted in the standard library. At the moment it says just: "Indicates the manner in which a thread exited." No mention about what value is returned. I'll close this.
This example code:
Errors with:
Judging from the error messages, the reason is that the error value of
std::thread::Result
doesn't implementstd::error::Error
(!). Is there any suggested workarounds? I'd almost expect.map_err(|e| anyhow!(e))
to work for wrapping the error value toanyhow::Error
, but it doesn't.The text was updated successfully, but these errors were encountered: