tauri-apps / tray-icon

Tray icons for Desktop Applications.
Apache License 2.0
215 stars 32 forks source link

[0.15.x] tray_icon::Icon not Sync on Windows #184

Open catgirlconspiracy opened 1 month ago

catgirlconspiracy commented 1 month ago

Since 0.15, tray_icon::Icon is not Sync anymore on Windows. This is easily visible in the docs for 0.15 vs 0.14. At least on Linux Icon is still Sync which makes for an awkward portability gotcha.

Test program:

fn check_sync<T: Sync>(x: T) -> T {
    x
}

fn main() {
    let icon = tray_icon::Icon::from_rgba(vec![], 0, 0).unwrap();
    check_sync(icon);
}

Compiler error (x86_64-pc-windows-gnu):

error[E0277]: `*mut c_void` cannot be shared between threads safely
   --> src/main.rs:7:16
    |
7   |     check_sync(icon);
    |     ---------- ^^^^ `*mut c_void` cannot be shared between threads safely
    |     |
    |     required by a bound introduced by this call
    |
    = help: within `tray_icon::platform_impl::platform::icon::RaiiIcon`, the trait `Sync` is not implemented for `*mut c_void`, which is required by `tray_icon::Icon: Sync`
note: required because it appears within the type `tray_icon::platform_impl::platform::icon::RaiiIcon`
   --> /home/mara/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tray-icon-0.15.1/src/platform_impl/windows/icon.rs:58:8
    |
58  | struct RaiiIcon {
    |        ^^^^^^^^
    = note: required for `Arc<tray_icon::platform_impl::platform::icon::RaiiIcon>` to implement `Sync`
note: required because it appears within the type `tray_icon::platform_impl::platform::icon::WinIcon`
   --> /home/mara/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tray-icon-0.15.1/src/platform_impl/windows/icon.rs:63:19
    |
63  | pub(crate) struct WinIcon {
    |                   ^^^^^^^
note: required because it appears within the type `tray_icon::Icon`
   --> /home/mara/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tray-icon-0.15.1/src/icon.rs:117:12
    |
117 | pub struct Icon {
    |            ^^^^
note: required by a bound in `check_sync`
   --> src/main.rs:1:18
    |
1   | fn check_sync<T: Sync>(x: T) -> T {
    |                  ^^^^ required by this bound in `check_sync`

error[E0277]: `*mut c_void` cannot be sent between threads safely
   --> src/main.rs:7:16
    |
7   |     check_sync(icon);
    |     ---------- ^^^^ `*mut c_void` cannot be sent between threads safely
    |     |
    |     required by a bound introduced by this call
    |
    = help: within `tray_icon::platform_impl::platform::icon::RaiiIcon`, the trait `Send` is not implemented for `*mut c_void`, which is required by `tray_icon::Icon: Sync`
note: required because it appears within the type `tray_icon::platform_impl::platform::icon::RaiiIcon`
   --> /home/mara/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tray-icon-0.15.1/src/platform_impl/windows/icon.rs:58:8
    |
58  | struct RaiiIcon {
    |        ^^^^^^^^
    = note: required for `Arc<tray_icon::platform_impl::platform::icon::RaiiIcon>` to implement `Sync`
note: required because it appears within the type `tray_icon::platform_impl::platform::icon::WinIcon`
   --> /home/mara/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tray-icon-0.15.1/src/platform_impl/windows/icon.rs:63:19
    |
63  | pub(crate) struct WinIcon {
    |                   ^^^^^^^
note: required because it appears within the type `tray_icon::Icon`
   --> /home/mara/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tray-icon-0.15.1/src/icon.rs:117:12
    |
117 | pub struct Icon {
    |            ^^^^
note: required by a bound in `check_sync`
   --> src/main.rs:1:18
    |
1   | fn check_sync<T: Sync>(x: T) -> T {
    |                  ^^^^ required by this bound in `check_sync`
amrbashir commented 1 month ago

This happened because the latest windows-sys crate update changed the HICON type from isize to *mut c_void. I think I'd prefer to keep them non sync and require them to be used in the same place as they would be used.