snafu icon indicating copy to clipboard operation
snafu copied to clipboard

Make Whatever thread-safe by default (add markers `Send + Sync`)

Open zardini123 opened this issue 1 year ago • 6 comments

Allows Whatever's to be sent between threads by making them thread-safe by marking the source std::error::Error with Send + Sync.

For more discussion on why, see issue #446.

Resolves #446.

zardini123 avatar Apr 02 '24 21:04 zardini123

Deploy Preview for shepmaster-snafu ready!

Name Link
Latest commit 0d7792e5c282ab0c117a09ebd1a750f1d972e188
Latest deploy log https://app.netlify.com/sites/shepmaster-snafu/deploys/660c73e8382fe40008b5f9e0
Deploy Preview https://deploy-preview-448--shepmaster-snafu.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

netlify[bot] avatar Apr 02 '24 21:04 netlify[bot]

News?

failable avatar Feb 28 '25 14:02 failable

Well, since my comment on https://github.com/shepmaster/snafu/issues/446#issuecomment-2033809122, no one has expressed concern over this change, but this also doesn't mean that someone out there is depending on Whatever not having to be Send or Sync.

At some point I hope we can move forward with this, since it does seem desirable and makes the error type more usable. It will imply a semver bump in any case.

Enet4 avatar Feb 28 '25 15:02 Enet4

Still think sync is not needed

Stargateur avatar Mar 01 '25 11:03 Stargateur

So can you share the best practices when using snafu in async Rust? Thanks!

failable avatar Mar 01 '25 16:03 failable

Is it recommended to use Whatever in an async context or multi-threaded environment?

Yevgnen avatar Mar 29 '25 13:03 Yevgnen

would an additional WhateverAsync be possible so no semver bump would be needed?

JakubKoralewski avatar Nov 19 '25 18:11 JakubKoralewski

an additional WhateverAsync be possible

Yep! You can do this today by copy-pasting the Whatever type into your code and modifying it. Stealing from this PR:

// You pick if you want `+ Send`, `+ Sync`, both, or neither! Change it in all 3 places.

#[derive(Debug, Snafu)]
#[snafu(whatever)]
#[snafu(display("{message}"))]
#[snafu(provide(opt, ref, chain, dyn std::error::Error + Send + Sync => source.as_deref()))]
pub struct WhateverAsync {
    #[snafu(source(from(Box<dyn std::error::Error + Send + Sync>, Some)))]
    #[snafu(provide(false))]
    source: Option<Box<dyn std::error::Error + Send + Sync>>,
    message: String,
    backtrace: Backtrace,
}

shepmaster avatar Nov 19 '25 19:11 shepmaster