device_query icon indicating copy to clipboard operation
device_query copied to clipboard

DeviceState is not `Send` (linux)

Open adriannsemb opened this issue 10 months ago • 0 comments

I get the following error when trying to use DeviceState in an async function.

    let device_state = Arc::new(DeviceState::new());
    |         ------------ has type `std::sync::Arc<device_query::DeviceState>` which is not `Send`

This is somewhat what I have:

static TRACKING_ENABLED: AtomicBool = AtomicBool::new(false);

pub async fn track_mouse_position() {
  //...
  tokio::spawn(async move {
    let device_state = Arc::new(DeviceState::new());
    let stop_notify = Arc::new(tokio::sync::Notify::new());

    let _move_guard = device_state.on_mouse_move({
      let stop_notify_clone = stop_notify.clone();
      let window = window.clone();

      move |position| {
        if !TRACKING_ENABLED.load(Ordering::Relaxed) {
          stop_notify_clone.notify_one();
          return;
        }
        // ...
      }
    });

    stop_notify.notified().await;
    // drops guard
  });
}

I could probably spawn a normal thread with a channel, but was wondering if it is possible to change the Linux implementation to use an Arc instead of Rc (not sure if there is a specific reason to use Rc).

adriannsemb avatar Feb 20 '25 22:02 adriannsemb