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

Whatever is not async compatible #322

Closed
bryanlarsen opened this issue Jan 15, 2022 · 6 comments · Fixed by #331
Closed

Whatever is not async compatible #322

bryanlarsen opened this issue Jan 15, 2022 · 6 comments · Fixed by #331
Labels
found in the field A user of SNAFU found this when trying to use it good first issue Good for newcomers help wanted Extra attention is needed

Comments

@bryanlarsen
Copy link

Following the instructions on the front page, I have

#[derive(Debug, Snafu)]
pub enum Error {
    #[snafu(whatever, display("{message}"))]
    Whatever {
        message: String,
        #[snafu(source(from(Box<dyn std::error::Error>, Some)))]
        source: Option<Box<dyn std::error::Error>>,
    },

This results in the error:

error: future cannot be sent between threads safely
  --> src/main.rs:45:61
   |
45 |       async fn get(&self, key: &str) -> Result<String, Error> {
   |  _____________________________________________________________^

...  |
   | |_____^ future created by async block is not `Send`
   |
   = help: the trait `Send` is not implemented for `(dyn snafu::Error + 'static)`
note: future is not `Send` as this value is used across an await
  --> src/main.rs:46:12

Removing the Whatever block from my error enum fixes the message. I wasn't using whatever! in my async function but it was annoying stripping it from the rest of my code.

I'm a rust newbie, so problem is likely PBKAC, but in case it might affect others I raised an issue. I've now purged whatever usage, so I'm fine with you closing the issue if that's appropriate.

@shepmaster
Copy link
Owner

When reporting an issue, it’s useful to produce an MRE.

When next I have time to investigate this issue, I’ll spend it on reproducing the problem. Then the next time i have, I can work on addressing the problem.

@shepmaster shepmaster added found in the field A user of SNAFU found this when trying to use it good first issue Good for newcomers help wanted Extra attention is needed labels Jan 15, 2022
@shepmaster
Copy link
Owner

Should work if you adjust the trait object to be Send / Sync:

use snafu::{prelude::*, whatever};

#[derive(Debug, Snafu)]
pub enum Error {
    #[snafu(whatever, display("{message}"))]
    Whatever {
        message: String,
        #[snafu(source(from(Box<dyn std::error::Error + Send + Sync>, Some)))]
        source: Option<Box<dyn std::error::Error + Send + Sync>>,
    },
}

fn create() -> Result<(), Error> {
    whatever!("Broken")
}

fn main() {
    fn is_send(_: impl Send) {}
    is_send(create());
}

See also Sending trait objects between threads in Rust

@bryanlarsen
Copy link
Author

bryanlarsen commented Jan 15, 2022

Yes, that works. I don't know if it should be added to the documentation or not.

Thanks.

@hellow554
Copy link
Contributor

I stumbled across this today as well.

It should definitly added to the guide at

snafu/src/lib.rs

Lines 167 to 185 in ba92e75

//! You can combine the power of the [`whatever!`][] macro with an
//! enum error type. This is great if you started out with
//! [`Whatever`][] and are moving to a custom error type:
//!
//! ```rust
//! use snafu::prelude::*;
//!
//! #[derive(Debug, Snafu)]
//! enum Error {
//! #[snafu(display("ID may not be less than 10, but it was {id}"))]
//! InvalidId { id: u16 },
//!
//! #[snafu(whatever, display("{message}"))]
//! Whatever {
//! message: String,
//! #[snafu(source(from(Box<dyn std::error::Error>, Some)))]
//! source: Option<Box<dyn std::error::Error>>,
//! },
//! }
because that's where I (and everyone else?) copied the code from to use whatever along with other errors.

@shepmaster
Copy link
Owner

Feedback welcome on #331

@shepmaster
Copy link
Owner

Published in https://crates.io/crates/snafu/0.7.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
found in the field A user of SNAFU found this when trying to use it good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants