From a41517101e338d6a909b5e4adab0f09d99b7cc49 Mon Sep 17 00:00:00 2001 From: Ottatop Date: Thu, 25 Jan 2024 20:14:20 -0600 Subject: [PATCH] Replace all `futures::executor::block_on` with tokio equivalent --- api/rust/src/input.rs | 12 ++++++------ api/rust/src/lib.rs | 11 ++++++++++- api/rust/src/output.rs | 18 ++++++++---------- api/rust/src/pinnacle.rs | 5 +++-- api/rust/src/process.rs | 8 ++++---- api/rust/src/tag.rs | 25 ++++++++++++++----------- api/rust/src/window.rs | 39 +++++++++++++++++++-------------------- 7 files changed, 64 insertions(+), 54 deletions(-) diff --git a/api/rust/src/input.rs b/api/rust/src/input.rs index 972c0d1..2605a36 100644 --- a/api/rust/src/input.rs +++ b/api/rust/src/input.rs @@ -8,9 +8,7 @@ //! methods for setting key- and mousebinds, changing xkeyboard settings, and more. //! View the struct's documentation for more information. -use futures::{ - channel::mpsc::UnboundedSender, executor::block_on, future::BoxFuture, FutureExt, StreamExt, -}; +use futures::{channel::mpsc::UnboundedSender, future::BoxFuture, FutureExt, StreamExt}; use num_enum::TryFromPrimitive; use pinnacle_api_defs::pinnacle::input::{ self, @@ -24,6 +22,8 @@ use pinnacle_api_defs::pinnacle::input::{ use tonic::transport::Channel; use xkbcommon::xkb::Keysym; +use crate::block_on_tokio; + use self::libinput::LibinputSetting; pub mod libinput; @@ -259,7 +259,7 @@ impl Input { pub fn set_xkb_config(&self, xkb_config: XkbConfig) { let mut client = self.create_input_client(); - block_on(client.set_xkb_config(SetXkbConfigRequest { + block_on_tokio(client.set_xkb_config(SetXkbConfigRequest { rules: xkb_config.rules.map(String::from), variant: xkb_config.variant.map(String::from), layout: xkb_config.layout.map(String::from), @@ -286,7 +286,7 @@ impl Input { pub fn set_repeat_rate(&self, rate: i32, delay: i32) { let mut client = self.create_input_client(); - block_on(client.set_repeat_rate(SetRepeatRateRequest { + block_on_tokio(client.set_repeat_rate(SetRepeatRateRequest { rate: Some(rate), delay: Some(delay), })) @@ -343,7 +343,7 @@ impl Input { LibinputSetting::Tap(enable) => Setting::Tap(enable), }; - block_on(client.set_libinput_setting(SetLibinputSettingRequest { + block_on_tokio(client.set_libinput_setting(SetLibinputSettingRequest { setting: Some(setting), })) .unwrap(); diff --git a/api/rust/src/lib.rs b/api/rust/src/lib.rs index 45a5f3a..68a2edf 100644 --- a/api/rust/src/lib.rs +++ b/api/rust/src/lib.rs @@ -82,7 +82,8 @@ use std::sync::OnceLock; use futures::{ - channel::mpsc::UnboundedReceiver, future::BoxFuture, stream::FuturesUnordered, StreamExt, + channel::mpsc::UnboundedReceiver, future::BoxFuture, stream::FuturesUnordered, Future, + StreamExt, }; use input::Input; use output::Output; @@ -202,3 +203,11 @@ pub async fn listen(fut_recv: UnboundedReceiver>) { } } } + +/// Block on a future using the current Tokio runtime. +pub(crate) fn block_on_tokio(future: F) -> F::Output { + tokio::task::block_in_place(|| { + let handle = tokio::runtime::Handle::current(); + handle.block_on(future) + }) +} diff --git a/api/rust/src/output.rs b/api/rust/src/output.rs index 20adb12..d26f7c6 100644 --- a/api/rust/src/output.rs +++ b/api/rust/src/output.rs @@ -9,9 +9,7 @@ //! This module provides [`Output`], which allows you to get [`OutputHandle`]s for different //! connected monitors and set them up. -use futures::{ - channel::mpsc::UnboundedSender, executor::block_on, future::BoxFuture, FutureExt, StreamExt, -}; +use futures::{channel::mpsc::UnboundedSender, future::BoxFuture, FutureExt, StreamExt}; use pinnacle_api_defs::pinnacle::{ output::{ self, @@ -23,7 +21,7 @@ use pinnacle_api_defs::pinnacle::{ }; use tonic::transport::Channel; -use crate::tag::TagHandle; +use crate::{block_on_tokio, tag::TagHandle}; /// A struct that allows you to get handles to connected outputs and set them up. /// @@ -63,7 +61,7 @@ impl Output { pub fn get_all(&self) -> impl Iterator { let mut client = self.create_output_client(); let tag_client = self.create_tag_client(); - block_on(client.get(output::v0alpha1::GetRequest {})) + block_on_tokio(client.get(output::v0alpha1::GetRequest {})) .unwrap() .into_inner() .output_names @@ -248,7 +246,7 @@ impl OutputHandle { /// ``` pub fn set_location(&self, x: impl Into>, y: impl Into>) { let mut client = self.client.clone(); - block_on(client.set_location(SetLocationRequest { + block_on_tokio(client.set_location(SetLocationRequest { output_name: Some(self.name.clone()), x: x.into(), y: y.into(), @@ -380,11 +378,11 @@ impl OutputHandle { /// ``` pub fn props(&self) -> OutputProperties { let mut client = self.client.clone(); - let response = block_on( - client.get_properties(output::v0alpha1::GetPropertiesRequest { + let response = block_on_tokio(client.get_properties( + output::v0alpha1::GetPropertiesRequest { output_name: Some(self.name.clone()), - }), - ) + }, + )) .unwrap() .into_inner(); diff --git a/api/rust/src/pinnacle.rs b/api/rust/src/pinnacle.rs index a4f7769..bbbf88b 100644 --- a/api/rust/src/pinnacle.rs +++ b/api/rust/src/pinnacle.rs @@ -6,12 +6,13 @@ //! //! This module provides [`Pinnacle`], which allows you to quit the compositor. -use futures::executor::block_on; use pinnacle_api_defs::pinnacle::v0alpha1::{ pinnacle_service_client::PinnacleServiceClient, QuitRequest, }; use tonic::transport::Channel; +use crate::block_on_tokio; + /// A struct that allows you to quit the compositor. #[derive(Debug, Clone)] pub struct Pinnacle { @@ -37,6 +38,6 @@ impl Pinnacle { /// ``` pub fn quit(&self) { let mut client = self.create_pinnacle_client(); - block_on(client.quit(QuitRequest {})).unwrap(); + block_on_tokio(client.quit(QuitRequest {})).unwrap(); } } diff --git a/api/rust/src/process.rs b/api/rust/src/process.rs index ba1d660..f5b1e9b 100644 --- a/api/rust/src/process.rs +++ b/api/rust/src/process.rs @@ -7,14 +7,14 @@ //! This module provides [`Process`], which allows you to spawn processes and set environment //! variables. -use futures::{ - channel::mpsc::UnboundedSender, executor::block_on, future::BoxFuture, FutureExt, StreamExt, -}; +use futures::{channel::mpsc::UnboundedSender, future::BoxFuture, FutureExt, StreamExt}; use pinnacle_api_defs::pinnacle::process::v0alpha1::{ process_service_client::ProcessServiceClient, SetEnvRequest, SpawnRequest, }; use tonic::transport::Channel; +use crate::block_on_tokio; + /// A struct containing methods to spawn processes with optional callbacks and set environment /// variables. #[derive(Debug, Clone)] @@ -173,7 +173,7 @@ impl Process { let mut client = self.create_process_client(); - block_on(client.set_env(SetEnvRequest { + block_on_tokio(client.set_env(SetEnvRequest { key: Some(key), value: Some(value), })) diff --git a/api/rust/src/tag.rs b/api/rust/src/tag.rs index 1523144..ce90ff0 100644 --- a/api/rust/src/tag.rs +++ b/api/rust/src/tag.rs @@ -34,7 +34,7 @@ use std::{ sync::{Arc, Mutex}, }; -use futures::{channel::mpsc::UnboundedSender, executor::block_on, future::BoxFuture}; +use futures::{channel::mpsc::UnboundedSender, future::BoxFuture}; use num_enum::TryFromPrimitive; use pinnacle_api_defs::pinnacle::{ output::v0alpha1::output_service_client::OutputServiceClient, @@ -48,7 +48,10 @@ use pinnacle_api_defs::pinnacle::{ }; use tonic::transport::Channel; -use crate::output::{Output, OutputHandle}; +use crate::{ + block_on_tokio, + output::{Output, OutputHandle}, +}; /// A struct that allows you to add and remove tags and get [`TagHandle`]s. #[derive(Clone, Debug)] @@ -99,7 +102,7 @@ impl Tag { let tag_names = tag_names.into_iter().map(Into::into).collect(); - let response = block_on(client.add(AddRequest { + let response = block_on_tokio(client.add(AddRequest { output_name: Some(output.name.clone()), tag_names, })) @@ -124,7 +127,7 @@ impl Tag { let mut client = self.create_tag_client(); let output_client = self.create_output_client(); - let response = block_on(client.get(tag::v0alpha1::GetRequest {})) + let response = block_on_tokio(client.get(tag::v0alpha1::GetRequest {})) .unwrap() .into_inner(); @@ -201,7 +204,7 @@ impl Tag { let mut client = self.create_tag_client(); - block_on(client.remove(RemoveRequest { tag_ids })).unwrap(); + block_on_tokio(client.remove(RemoveRequest { tag_ids })).unwrap(); } /// Create a [`LayoutCycler`] to cycle layouts on outputs. @@ -386,7 +389,7 @@ impl TagHandle { /// ``` pub fn switch_to(&self) { let mut client = self.client.clone(); - block_on(client.switch_to(SwitchToRequest { + block_on_tokio(client.switch_to(SwitchToRequest { tag_id: Some(self.id), })) .unwrap(); @@ -412,7 +415,7 @@ impl TagHandle { /// ``` pub fn set_active(&self, set: bool) { let mut client = self.client.clone(); - block_on(client.set_active(SetActiveRequest { + block_on_tokio(client.set_active(SetActiveRequest { tag_id: Some(self.id), set_or_toggle: Some(tag::v0alpha1::set_active_request::SetOrToggle::Set(set)), })) @@ -440,7 +443,7 @@ impl TagHandle { /// ``` pub fn toggle_active(&self) { let mut client = self.client.clone(); - block_on(client.set_active(SetActiveRequest { + block_on_tokio(client.set_active(SetActiveRequest { tag_id: Some(self.id), set_or_toggle: Some(tag::v0alpha1::set_active_request::SetOrToggle::Toggle(())), })) @@ -461,7 +464,7 @@ impl TagHandle { /// // "DP-1" now only has tags "1" and "Buckle" /// ``` pub fn remove(mut self) { - block_on(self.client.remove(RemoveRequest { + block_on_tokio(self.client.remove(RemoveRequest { tag_ids: vec![self.id], })) .unwrap(); @@ -485,7 +488,7 @@ impl TagHandle { /// ``` pub fn set_layout(&self, layout: Layout) { let mut client = self.client.clone(); - block_on(client.set_layout(SetLayoutRequest { + block_on_tokio(client.set_layout(SetLayoutRequest { tag_id: Some(self.id), layout: Some(layout as i32), })) @@ -509,7 +512,7 @@ impl TagHandle { let mut client = self.client.clone(); let output_client = self.output_client.clone(); - let response = block_on(client.get_properties(tag::v0alpha1::GetPropertiesRequest { + let response = block_on_tokio(client.get_properties(tag::v0alpha1::GetPropertiesRequest { tag_id: Some(self.id), })) .unwrap() diff --git a/api/rust/src/window.rs b/api/rust/src/window.rs index 25e29c7..7bf78c3 100644 --- a/api/rust/src/window.rs +++ b/api/rust/src/window.rs @@ -12,7 +12,6 @@ //! //! This module also allows you to set window rules; see the [rules] module for more information. -use futures::executor::block_on; use num_enum::TryFromPrimitive; use pinnacle_api_defs::pinnacle::{ output::v0alpha1::output_service_client::OutputServiceClient, @@ -31,7 +30,7 @@ use pinnacle_api_defs::pinnacle::{ }; use tonic::transport::Channel; -use crate::{input::MouseButton, tag::TagHandle, util::Geometry}; +use crate::{block_on_tokio, input::MouseButton, tag::TagHandle, util::Geometry}; use self::rules::{WindowRule, WindowRuleCondition}; @@ -81,7 +80,7 @@ impl Window { /// ``` pub fn begin_move(&self, button: MouseButton) { let mut client = self.create_window_client(); - block_on(client.move_grab(MoveGrabRequest { + block_on_tokio(client.move_grab(MoveGrabRequest { button: Some(button as u32), })) .unwrap(); @@ -106,7 +105,7 @@ impl Window { /// ``` pub fn begin_resize(&self, button: MouseButton) { let mut client = self.create_window_client(); - block_on(client.resize_grab(ResizeGrabRequest { + block_on_tokio(client.resize_grab(ResizeGrabRequest { button: Some(button as u32), })) .unwrap(); @@ -123,7 +122,7 @@ impl Window { let mut client = self.create_window_client(); let tag_client = self.create_tag_client(); let output_client = self.create_output_client(); - block_on(client.get(GetRequest {})) + block_on_tokio(client.get(GetRequest {})) .unwrap() .into_inner() .window_ids @@ -157,7 +156,7 @@ impl Window { pub fn add_window_rule(&self, cond: WindowRuleCondition, rule: WindowRule) { let mut client = self.create_window_client(); - block_on(client.add_window_rule(AddWindowRuleRequest { + block_on_tokio(client.add_window_rule(AddWindowRuleRequest { cond: Some(cond.0), rule: Some(rule.0), })) @@ -236,7 +235,7 @@ impl WindowHandle { /// window.get_focused()?.close() /// ``` pub fn close(mut self) { - block_on(self.client.close(CloseRequest { + block_on_tokio(self.client.close(CloseRequest { window_id: Some(self.id), })) .unwrap(); @@ -254,7 +253,7 @@ impl WindowHandle { /// ``` pub fn set_fullscreen(&self, set: bool) { let mut client = self.client.clone(); - block_on(client.set_fullscreen(SetFullscreenRequest { + block_on_tokio(client.set_fullscreen(SetFullscreenRequest { window_id: Some(self.id), set_or_toggle: Some(window::v0alpha1::set_fullscreen_request::SetOrToggle::Set( set, @@ -275,7 +274,7 @@ impl WindowHandle { /// ``` pub fn toggle_fullscreen(&self) { let mut client = self.client.clone(); - block_on(client.set_fullscreen(SetFullscreenRequest { + block_on_tokio(client.set_fullscreen(SetFullscreenRequest { window_id: Some(self.id), set_or_toggle: Some(window::v0alpha1::set_fullscreen_request::SetOrToggle::Toggle(())), })) @@ -294,7 +293,7 @@ impl WindowHandle { /// ``` pub fn set_maximized(&self, set: bool) { let mut client = self.client.clone(); - block_on(client.set_maximized(SetMaximizedRequest { + block_on_tokio(client.set_maximized(SetMaximizedRequest { window_id: Some(self.id), set_or_toggle: Some(window::v0alpha1::set_maximized_request::SetOrToggle::Set( set, @@ -315,7 +314,7 @@ impl WindowHandle { /// ``` pub fn toggle_maximized(&self) { let mut client = self.client.clone(); - block_on(client.set_maximized(SetMaximizedRequest { + block_on_tokio(client.set_maximized(SetMaximizedRequest { window_id: Some(self.id), set_or_toggle: Some(window::v0alpha1::set_maximized_request::SetOrToggle::Toggle(())), })) @@ -337,7 +336,7 @@ impl WindowHandle { /// ``` pub fn set_floating(&self, set: bool) { let mut client = self.client.clone(); - block_on(client.set_floating(SetFloatingRequest { + block_on_tokio(client.set_floating(SetFloatingRequest { window_id: Some(self.id), set_or_toggle: Some(window::v0alpha1::set_floating_request::SetOrToggle::Set( set, @@ -361,7 +360,7 @@ impl WindowHandle { /// ``` pub fn toggle_floating(&self) { let mut client = self.client.clone(); - block_on(client.set_floating(SetFloatingRequest { + block_on_tokio(client.set_floating(SetFloatingRequest { window_id: Some(self.id), set_or_toggle: Some(window::v0alpha1::set_floating_request::SetOrToggle::Toggle( (), @@ -384,7 +383,7 @@ impl WindowHandle { pub fn move_to_tag(&self, tag: &TagHandle) { let mut client = self.client.clone(); - block_on(client.move_to_tag(MoveToTagRequest { + block_on_tokio(client.move_to_tag(MoveToTagRequest { window_id: Some(self.id), tag_id: Some(tag.id), })) @@ -405,7 +404,7 @@ impl WindowHandle { pub fn set_tag(&self, tag: &TagHandle, set: bool) { let mut client = self.client.clone(); - block_on(client.set_tag(SetTagRequest { + block_on_tokio(client.set_tag(SetTagRequest { window_id: Some(self.id), tag_id: Some(tag.id), set_or_toggle: Some(window::v0alpha1::set_tag_request::SetOrToggle::Set(set)), @@ -429,7 +428,7 @@ impl WindowHandle { pub fn toggle_tag(&self, tag: &TagHandle) { let mut client = self.client.clone(); - block_on(client.set_tag(SetTagRequest { + block_on_tokio(client.set_tag(SetTagRequest { window_id: Some(self.id), tag_id: Some(tag.id), set_or_toggle: Some(window::v0alpha1::set_tag_request::SetOrToggle::Toggle(())), @@ -457,11 +456,11 @@ impl WindowHandle { pub fn props(&self) -> WindowProperties { let mut client = self.client.clone(); let tag_client = self.tag_client.clone(); - let response = block_on( - client.get_properties(window::v0alpha1::GetPropertiesRequest { + let response = block_on_tokio(client.get_properties( + window::v0alpha1::GetPropertiesRequest { window_id: Some(self.id), - }), - ) + }, + )) .unwrap() .into_inner();