From 742fb3db7fa9d9c7706a3365b0d64c59ea68a2cd Mon Sep 17 00:00:00 2001 From: Jan Trefil <8711792+htrefil@users.noreply.github.com> Date: Wed, 21 Jun 2023 21:14:11 +0200 Subject: [PATCH] Asyncify WriterBuilder::build() --- rkvm-client/src/client.rs | 1 + rkvm-input/src/writer.rs | 26 +++++++++++++------------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/rkvm-client/src/client.rs b/rkvm-client/src/client.rs index 4fd3aa0..95e3464 100644 --- a/rkvm-client/src/client.rs +++ b/rkvm-client/src/client.rs @@ -115,6 +115,7 @@ pub async fn run( .abs(abs)? .key(keys)? .build() + .await } .await .map_err(Error::Input)?; diff --git a/rkvm-input/src/writer.rs b/rkvm-input/src/writer.rs index 6ab5fd8..9da06a6 100644 --- a/rkvm-input/src/writer.rs +++ b/rkvm-input/src/writer.rs @@ -15,6 +15,7 @@ use std::path::Path; use std::ptr; use std::ptr::NonNull; use tokio::io::unix::AsyncFd; +use tokio::task; pub struct Writer { file: AsyncFd, @@ -112,6 +113,8 @@ impl Writer { } } +unsafe impl Send for Writer {} + pub struct WriterBuilder { evdev: NonNull, } @@ -129,7 +132,7 @@ impl WriterBuilder { Ok(Self { evdev }) } - pub fn name(&mut self, name: &CStr) -> &mut Self { + pub fn name(self, name: &CStr) -> Self { unsafe { glue::libevdev_set_name(self.evdev.as_ptr(), name.as_ptr()); } @@ -137,7 +140,7 @@ impl WriterBuilder { self } - pub fn vendor(&mut self, value: u16) -> &mut Self { + pub fn vendor(self, value: u16) -> Self { unsafe { glue::libevdev_set_id_vendor(self.evdev.as_ptr(), value as _); } @@ -145,7 +148,7 @@ impl WriterBuilder { self } - pub fn product(&mut self, value: u16) -> &mut Self { + pub fn product(self, value: u16) -> Self { unsafe { glue::libevdev_set_id_product(self.evdev.as_ptr(), value as _); } @@ -153,7 +156,7 @@ impl WriterBuilder { self } - pub fn version(&mut self, value: u16) -> &mut Self { + pub fn version(self, value: u16) -> Self { unsafe { glue::libevdev_set_id_version(self.evdev.as_ptr(), value as _); } @@ -161,7 +164,7 @@ impl WriterBuilder { self } - pub fn rel>(&mut self, items: T) -> Result<&mut Self, Error> { + pub fn rel>(self, items: T) -> Result { for axis in items { let ret = unsafe { glue::libevdev_enable_event_code( @@ -180,10 +183,7 @@ impl WriterBuilder { Ok(self) } - pub fn abs>( - &mut self, - items: T, - ) -> Result<&mut Self, Error> { + pub fn abs>(self, items: T) -> Result { let ret = unsafe { glue::libevdev_enable_event_code( self.evdev.as_ptr(), @@ -224,7 +224,7 @@ impl WriterBuilder { Ok(self) } - pub fn key>(&mut self, items: T) -> Result<&mut Self, Error> { + pub fn key>(self, items: T) -> Result { for key in items { let ret = unsafe { glue::libevdev_enable_event_code( @@ -243,12 +243,12 @@ impl WriterBuilder { Ok(self) } - pub fn build(&self) -> Result { - unsafe { Writer::from_evdev(self.evdev.as_ref()) } + pub async fn build(self) -> Result { + task::spawn_blocking(move || unsafe { Writer::from_evdev(self.evdev.as_ref()) }).await? } } -unsafe impl Send for Writer {} +unsafe impl Send for WriterBuilder {} impl Drop for WriterBuilder { fn drop(&mut self) {