From ddc36b75b65dbf18203e517f3d21b28a0b075e58 Mon Sep 17 00:00:00 2001 From: Demmie <2e3s19@gmail.com> Date: Mon, 29 Jan 2024 13:41:56 -0500 Subject: [PATCH] Remove async_trait crate --- Cargo.lock | 1 - watchers/Cargo.toml | 1 - watchers/src/watchers.rs | 13 ++++------ watchers/src/watchers/gnome_idle.rs | 24 +++++++++++-------- watchers/src/watchers/gnome_window.rs | 10 ++++---- watchers/src/watchers/idle.rs | 2 -- watchers/src/watchers/kwin_window.rs | 8 +++---- watchers/src/watchers/wl_ext_idle_notify.rs | 8 +++---- watchers/src/watchers/wl_foreign_toplevel.rs | 9 ++++--- watchers/src/watchers/wl_kwin_idle.rs | 9 +++---- watchers/src/watchers/x11_screensaver_idle.rs | 10 ++++---- watchers/src/watchers/x11_window.rs | 9 +++---- 12 files changed, 49 insertions(+), 55 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 762ec3a..9b13c62 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3814,7 +3814,6 @@ name = "watchers" version = "0.2.4" dependencies = [ "anyhow", - "async-trait", "aw-client-rust", "chrono", "dirs 5.0.1", diff --git a/watchers/Cargo.toml b/watchers/Cargo.toml index 410222c..b1a8727 100644 --- a/watchers/Cargo.toml +++ b/watchers/Cargo.toml @@ -31,7 +31,6 @@ regex = "1.10.2" gethostname = "0.4.3" log = { workspace = true } anyhow = { workspace = true } -async-trait = "0.1.75" tokio = { workspace = true, features = ["time", "sync"] } [features] diff --git a/watchers/src/watchers.rs b/watchers/src/watchers.rs index 42c7a0a..6c8e1b9 100644 --- a/watchers/src/watchers.rs +++ b/watchers/src/watchers.rs @@ -16,8 +16,7 @@ mod x11_screensaver_idle; mod x11_window; use crate::{config::Config, report_client::ReportClient}; -use async_trait::async_trait; -use std::{fmt::Display, sync::Arc}; +use std::{fmt::Display, sync::Arc, pin::Pin, future::Future}; use tokio::time; pub enum WatcherType { @@ -43,13 +42,11 @@ impl Display for WatcherType { } } -#[async_trait] pub trait Watcher: Send { - async fn new(client: &Arc) -> anyhow::Result - where - Self: Sized; - - async fn run_iteration(&mut self, client: &Arc) -> anyhow::Result<()>; + fn run_iteration<'a>( + &'a mut self, + client: &'a Arc, + ) -> Pin> + Send + 'a>>; } macro_rules! watch { diff --git a/watchers/src/watchers/gnome_idle.rs b/watchers/src/watchers/gnome_idle.rs index cf6df35..670dbea 100644 --- a/watchers/src/watchers/gnome_idle.rs +++ b/watchers/src/watchers/gnome_idle.rs @@ -1,8 +1,7 @@ use super::{gnome_wayland::load_watcher, gnome_wayland::GnomeWatcher, idle, Watcher}; use crate::report_client::ReportClient; use anyhow::Context; -use async_trait::async_trait; -use std::sync::Arc; +use std::{sync::Arc, pin::Pin, future::Future}; use zbus::Connection; pub struct IdleWatcher { @@ -10,7 +9,6 @@ pub struct IdleWatcher { is_idle: bool, } -#[async_trait] impl idle::SinceLastInput for IdleWatcher { async fn seconds_since_input(&mut self) -> anyhow::Result { let ms = self @@ -40,15 +38,21 @@ impl GnomeWatcher for IdleWatcher { } } -#[async_trait] -impl Watcher for IdleWatcher { - async fn new(_: &Arc) -> anyhow::Result { +impl IdleWatcher { + pub async fn new(_: &Arc) -> anyhow::Result { load_watcher().await } +} - async fn run_iteration(&mut self, client: &Arc) -> anyhow::Result<()> { - self.is_idle = idle::ping_since_last_input(self, self.is_idle, client).await?; - - Ok(()) +impl Watcher for IdleWatcher { + fn run_iteration<'a>( + &'a mut self, + client: &'a Arc, + ) -> Pin> + Send + 'a>> { + Box::pin(async move { + self.is_idle = idle::ping_since_last_input(self, self.is_idle, client).await?; + + Ok(()) + }) } } diff --git a/watchers/src/watchers/gnome_window.rs b/watchers/src/watchers/gnome_window.rs index 4f5bebf..841fb2a 100644 --- a/watchers/src/watchers/gnome_window.rs +++ b/watchers/src/watchers/gnome_window.rs @@ -1,6 +1,5 @@ use crate::report_client::ReportClient; use anyhow::Context; -use async_trait::async_trait; use serde::Deserialize; use std::sync::Arc; use zbus::Connection; @@ -20,6 +19,10 @@ struct WindowData { } impl WindowWatcher { + pub async fn new(_: &Arc) -> anyhow::Result { + load_watcher().await + } + async fn get_window_data(&self) -> anyhow::Result { let call_response = self .dbus_connection @@ -92,12 +95,7 @@ impl GnomeWatcher for WindowWatcher { } } -#[async_trait] impl Watcher for WindowWatcher { - async fn new(_: &Arc) -> anyhow::Result { - load_watcher().await - } - async fn run_iteration(&mut self, client: &Arc) -> anyhow::Result<()> { self.send_active_window(client).await } diff --git a/watchers/src/watchers/idle.rs b/watchers/src/watchers/idle.rs index 911ce65..895daf7 100644 --- a/watchers/src/watchers/idle.rs +++ b/watchers/src/watchers/idle.rs @@ -1,9 +1,7 @@ use crate::report_client::ReportClient; -use async_trait::async_trait; use chrono::{Duration, Utc}; use std::sync::Arc; -#[async_trait] pub trait SinceLastInput { async fn seconds_since_input(&mut self) -> anyhow::Result; } diff --git a/watchers/src/watchers/kwin_window.rs b/watchers/src/watchers/kwin_window.rs index cfef167..54339a0 100644 --- a/watchers/src/watchers/kwin_window.rs +++ b/watchers/src/watchers/kwin_window.rs @@ -6,7 +6,6 @@ use super::Watcher; use crate::report_client::ReportClient; use anyhow::{anyhow, Context}; -use async_trait::async_trait; use std::env::{self, temp_dir}; use std::path::Path; use std::sync::{mpsc::channel, Arc}; @@ -167,9 +166,8 @@ pub struct WindowWatcher { _kwin_script: KWinScript, } -#[async_trait] -impl Watcher for WindowWatcher { - async fn new(_: &Arc) -> anyhow::Result { +impl WindowWatcher { + pub async fn new(_: &Arc) -> anyhow::Result { let mut kwin_script = KWinScript::new(Connection::session().await?); if kwin_script.is_loaded().await? { debug!("KWin script is already loaded, unloading"); @@ -230,7 +228,9 @@ impl Watcher for WindowWatcher { _kwin_script: kwin_script, }) } +} +impl Watcher for WindowWatcher { async fn run_iteration(&mut self, client: &Arc) -> anyhow::Result<()> { send_active_window(client, &self.active_window).await } diff --git a/watchers/src/watchers/wl_ext_idle_notify.rs b/watchers/src/watchers/wl_ext_idle_notify.rs index af31068..2abd5ee 100644 --- a/watchers/src/watchers/wl_ext_idle_notify.rs +++ b/watchers/src/watchers/wl_ext_idle_notify.rs @@ -2,7 +2,6 @@ use super::wl_connection::{subscribe_state, WlEventConnection}; use super::Watcher; use crate::report_client::ReportClient; use anyhow::anyhow; -use async_trait::async_trait; use chrono::{DateTime, Duration, Utc}; use std::sync::Arc; use wayland_client::{ @@ -127,9 +126,8 @@ pub struct IdleWatcher { idle_state: IdleState, } -#[async_trait] -impl Watcher for IdleWatcher { - async fn new(client: &Arc) -> anyhow::Result { +impl IdleWatcher { + pub async fn new(client: &Arc) -> anyhow::Result { let mut connection: WlEventConnection = WlEventConnection::connect()?; connection.get_ext_idle()?; @@ -146,7 +144,9 @@ impl Watcher for IdleWatcher { idle_state, }) } +} +impl Watcher for IdleWatcher { async fn run_iteration(&mut self, client: &Arc) -> anyhow::Result<()> { self.connection .event_queue diff --git a/watchers/src/watchers/wl_foreign_toplevel.rs b/watchers/src/watchers/wl_foreign_toplevel.rs index b10c4d8..adf834f 100644 --- a/watchers/src/watchers/wl_foreign_toplevel.rs +++ b/watchers/src/watchers/wl_foreign_toplevel.rs @@ -2,7 +2,6 @@ use super::wl_connection::WlEventConnection; use super::{wl_connection::subscribe_state, Watcher}; use crate::report_client::ReportClient; use anyhow::{anyhow, Context}; -use async_trait::async_trait; use std::collections::HashMap; use std::sync::Arc; use wayland_client::{ @@ -138,11 +137,8 @@ impl WindowWatcher { .await .with_context(|| "Failed to send heartbeat for active window") } -} -#[async_trait] -impl Watcher for WindowWatcher { - async fn new(_: &Arc) -> anyhow::Result { + pub async fn new(_: &Arc) -> anyhow::Result { let mut connection: WlEventConnection = WlEventConnection::connect()?; connection.get_foreign_toplevel_manager()?; @@ -159,6 +155,9 @@ impl Watcher for WindowWatcher { }) } +} + +impl Watcher for WindowWatcher { async fn run_iteration(&mut self, client: &Arc) -> anyhow::Result<()> { self.connection .event_queue diff --git a/watchers/src/watchers/wl_kwin_idle.rs b/watchers/src/watchers/wl_kwin_idle.rs index 6575b4d..e8f3bc2 100644 --- a/watchers/src/watchers/wl_kwin_idle.rs +++ b/watchers/src/watchers/wl_kwin_idle.rs @@ -2,7 +2,6 @@ use super::wl_connection::{subscribe_state, WlEventConnection}; use super::Watcher; use crate::report_client::ReportClient; use anyhow::anyhow; -use async_trait::async_trait; use chrono::{DateTime, Duration, Utc}; use std::sync::Arc; use wayland_client::{ @@ -128,9 +127,8 @@ pub struct IdleWatcher { idle_state: IdleState, } -#[async_trait] -impl Watcher for IdleWatcher { - async fn new(client: &Arc) -> anyhow::Result { +impl IdleWatcher { + pub async fn new(client: &Arc) -> anyhow::Result { let mut connection: WlEventConnection = WlEventConnection::connect()?; connection.get_kwin_idle()?; @@ -145,6 +143,9 @@ impl Watcher for IdleWatcher { }) } +} + +impl Watcher for IdleWatcher { async fn run_iteration(&mut self, client: &Arc) -> anyhow::Result<()> { self.connection .event_queue diff --git a/watchers/src/watchers/x11_screensaver_idle.rs b/watchers/src/watchers/x11_screensaver_idle.rs index f11f831..1333c07 100644 --- a/watchers/src/watchers/x11_screensaver_idle.rs +++ b/watchers/src/watchers/x11_screensaver_idle.rs @@ -1,5 +1,3 @@ -use async_trait::async_trait; - use super::{idle, x11_connection::X11Client, Watcher}; use crate::report_client::ReportClient; use std::sync::Arc; @@ -9,16 +7,14 @@ pub struct IdleWatcher { is_idle: bool, } -#[async_trait] impl idle::SinceLastInput for IdleWatcher { async fn seconds_since_input(&mut self) -> anyhow::Result { self.client.seconds_since_last_input() } } -#[async_trait] -impl Watcher for IdleWatcher { - async fn new(_: &Arc) -> anyhow::Result { +impl IdleWatcher { + pub async fn new(_: &Arc) -> anyhow::Result { let mut client = X11Client::new()?; // Check if screensaver extension is supported @@ -29,7 +25,9 @@ impl Watcher for IdleWatcher { is_idle: false, }) } +} +impl Watcher for IdleWatcher { async fn run_iteration(&mut self, client: &Arc) -> anyhow::Result<()> { self.is_idle = idle::ping_since_last_input(self, self.is_idle, client).await?; diff --git a/watchers/src/watchers/x11_window.rs b/watchers/src/watchers/x11_window.rs index 3776a72..30a066f 100644 --- a/watchers/src/watchers/x11_window.rs +++ b/watchers/src/watchers/x11_window.rs @@ -1,7 +1,6 @@ use super::{x11_connection::X11Client, Watcher}; use crate::report_client::ReportClient; use anyhow::Context; -use async_trait::async_trait; use std::sync::Arc; pub struct WindowWatcher { @@ -30,9 +29,8 @@ impl WindowWatcher { } } -#[async_trait] -impl Watcher for WindowWatcher { - async fn new(_: &Arc) -> anyhow::Result { +impl WindowWatcher { + pub async fn new(_: &Arc) -> anyhow::Result { let mut client = X11Client::new()?; client.active_window_data()?; @@ -43,6 +41,9 @@ impl Watcher for WindowWatcher { }) } +} + +impl Watcher for WindowWatcher { async fn run_iteration(&mut self, client: &Arc) -> anyhow::Result<()> { self.send_active_window(client).await }