Hi everyone.
I have an async method that can return a result containing an error. But since the method is used inside tokio threads, the error type that it returns should impl Send trait, Like this:
But the problem is, after adding Send to trait bounds, I can't return normal errors using question mark ? anymore, and this for example leads to trait bound error:
Error conversion using ? is not a hardcoded magic, but invokes From::from(). You can search "dyn Error" on the implementors list of From. We have ones for Box<dyn Error> and Box<dyn Error + Send + Sync>, but not Box<dyn Error + Send>.
I can't use ? for normal Result<(), Box<dyn Error>>anymore. Also I can't edit functions like do_some() since they are imported from other crates.
It results in following errors.
error[E0277]: the size for values of type `dyn std::error::Error` cannot be known at compilation time
--> src/main.rs:32:24
|
32 | do_some().await?;
| ^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `dyn std::error::Error`
= 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::error::Error>`
= note: required because of the requirements on the impl of `std::convert::From<std::boxed::Box<dyn std::error::Error>>` for `std::boxed::Box<dyn std::error::Error + std::marker::Send + std::marker::Sync>`
= note: required by `std::convert::From::from`
error[E0277]: `dyn std::error::Error` cannot be sent between threads safely
--> src/main.rs:32:24
|
32 | do_some().await?;
| ^ `dyn std::error::Error` cannot be sent between threads safely
|
= help: the trait `std::marker::Send` is not implemented for `dyn std::error::Error`
= note: required because of the requirements on the impl of `std::marker::Send` for `std::ptr::Unique<dyn std::error::Error>`
= note: required because it appears within the type `std::boxed::Box<dyn std::error::Error>`
= note: required because of the requirements on the impl of `std::convert::From<std::boxed::Box<dyn std::error::Error>>` for `std::boxed::Box<dyn std::error::Error + std::marker::Send + std::marker::Sync>`
= note: required by `std::convert::From::from`
error[E0277]: `dyn std::error::Error` cannot be shared between threads safely
--> src/main.rs:32:24
|
32 | do_some().await?;
| ^ `dyn std::error::Error` cannot be shared between threads safely
|
= help: the trait `std::marker::Sync` is not implemented for `dyn std::error::Error`
= note: required because of the requirements on the impl of `std::marker::Sync` for `std::ptr::Unique<dyn std::error::Error>`
= note: required because it appears within the type `std::boxed::Box<dyn std::error::Error>`
= note: required because of the requirements on the impl of `std::convert::From<std::boxed::Box<dyn std::error::Error>>` for `std::boxed::Box<dyn std::error::Error + std::marker::Send + std::marker::Sync>`
= note: required by `std::convert::From::from`