iced-rs / iced

A cross-platform GUI library for Rust, inspired by Elm
https://iced.rs
MIT License
24.93k stars 1.18k forks source link

Make iced::Result Send + Sync #516

Closed Tarnadas closed 3 years ago

Tarnadas commented 4 years ago

With the recent addition of error handling, I tried to wrap an iced::Result with an anyhow::Result, but it's not working because I get this error:

error[E0277]: `(dyn StdError + 'static)` cannot be sent between threads safely                                                                                                                                                                                               
  --> src\main.rs:51:23
   |
51 |     App::run(settings)?;
   |                       ^ `(dyn StdError + 'static)` cannot be sent between threads safely
   |
   = help: the trait `std::marker::Send` is not implemented for `(dyn StdError + 'static)`
   = note: required because of the requirements on the impl of `std::marker::Send` for `Unique<(dyn StdError + 'static)>`
   = note: required because it appears within the type `Box<(dyn StdError + 'static)>`
   = note: required because it appears within the type `iced::Error`
   = note: required because of the requirements on the impl of `From<iced::Error>` for `anyhow::Error`
   = note: required by `std::convert::From::from`

error[E0277]: `(dyn StdError + 'static)` cannot be shared between threads safely
  --> src\main.rs:51:23
   |
51 |     App::run(settings)?;
   |                       ^ `(dyn StdError + 'static)` cannot be shared between threads safely
   |
   = help: the trait `Sync` is not implemented for `(dyn StdError + 'static)`
   = note: required because of the requirements on the impl of `Sync` for `Unique<(dyn StdError + 'static)>`
   = note: required because it appears within the type `Box<(dyn StdError + 'static)>`
   = note: required because it appears within the type `iced::Error`
   = note: required because of the requirements on the impl of `From<iced::Error>` for `anyhow::Error`
   = note: required by `std::convert::From::from`

Example code:

use anyhow::Result;

fn main() -> Result<()> {
    use iced::{Application, Settings};

    App::run(Settings::default())?;
    Ok(())
}
brendanzab commented 4 years ago

I'm running into this too. I think it might be because WindowCreationFailed(Box<dyn std::error::Error>) needs a more restrictive trait object?

brendanzab commented 4 years ago

A workaround is to do something like:

// FIXME: `iced::Error` is not `Send + Sync`, and so is incompatible with `anyhow::Result`.
// See this issue for more information: https://github.com/hecrj/iced/issues/516
pikelet_editor::run().map_err(|err| anyhow!("{}", err))