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

std::thread::Result has problems with ?ing to anyhow::Error #39

Closed
golddranks opened this issue Nov 5, 2019 · 2 comments
Closed

std::thread::Result has problems with ?ing to anyhow::Error #39

golddranks opened this issue Nov 5, 2019 · 2 comments

Comments

@golddranks
Copy link

This example code:

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.

@dtolnay
Copy link
Owner

dtolnay commented Nov 5, 2019

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.

@golddranks
Copy link
Author

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.

Repository owner locked and limited conversation to collaborators Feb 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants