From 34b961943be6958e094713807f8a722836d8e7ce Mon Sep 17 00:00:00 2001 From: Mohammad Aadil Shabier <64919407+aadilshabier@users.noreply.github.com> Date: Mon, 26 Feb 2024 22:28:54 +0530 Subject: [PATCH] refactor: replace manual insertions with collects (#241) - Remove unnecessary mut's - Using static slice prevents the need to create Vec which is discarded anyway - Prevent *rc patten from matching src/ directories - Propogate errors in uinput::create_uinput_device*() instead of unwrap --- .gitignore | 1 + swhkd/src/daemon.rs | 2 +- swhkd/src/uinput.rs | 34 ++++++++++++---------------------- 3 files changed, 14 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index e5a7404..c65e36a 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ target *.out com.github.swhkd.pkexec.policy *rc +!*rc/ .direnv diff --git a/swhkd/src/daemon.rs b/swhkd/src/daemon.rs index 108f175..af75dd4 100644 --- a/swhkd/src/daemon.rs +++ b/swhkd/src/daemon.rs @@ -441,7 +441,7 @@ fn socket_write(command: &str, socket_path: PathBuf) -> Result<(), Box Result<(), Box> { if !Uid::current().is_root() { let groups = nix::unistd::getgroups(); - for (_, groups) in groups.iter().enumerate() { + for groups in groups.iter() { for group in groups { let group = Group::from_gid(*group); if group.unwrap().unwrap().name == "input" { diff --git a/swhkd/src/uinput.rs b/swhkd/src/uinput.rs index c20fa52..f0684ae 100644 --- a/swhkd/src/uinput.rs +++ b/swhkd/src/uinput.rs @@ -10,30 +10,21 @@ use std::os::unix::io::AsRawFd; ioctl_none!(rfkill_noinput, b'R', 1); pub fn create_uinput_device() -> Result> { - let mut keys = AttributeSet::::new(); - for key in get_all_keys() { - keys.insert(key); - } + let keys: AttributeSet = get_all_keys().iter().copied().collect(); - let mut relative_axes = AttributeSet::::new(); - for axis in get_all_relative_axes() { - relative_axes.insert(axis); - } + let relative_axes: AttributeSet = + get_all_relative_axes().iter().copied().collect(); let device = VirtualDeviceBuilder::new()? .name("swhkd virtual output") .with_keys(&keys)? .with_relative_axes(&relative_axes)? - .build() - .unwrap(); + .build()?; Ok(device) } pub fn create_uinput_switches_device() -> Result> { - let mut switches = AttributeSet::::new(); - for switch in get_all_switches() { - switches.insert(switch); - } + let switches: AttributeSet = get_all_switches().iter().copied().collect(); // We have to disable rfkill-input to avoid blocking all radio devices. When // a new device (virtual or physical) with the SW_RFKILL_ALL capability bit @@ -52,12 +43,11 @@ pub fn create_uinput_switches_device() -> Result Vec { - vec![ +pub fn get_all_keys() -> &'static [Key] { + &[ evdev::Key::KEY_RESERVED, evdev::Key::KEY_ESC, evdev::Key::KEY_1, @@ -609,8 +599,8 @@ pub fn get_all_keys() -> Vec { ] } -pub fn get_all_relative_axes() -> Vec { - vec![ +pub fn get_all_relative_axes() -> &'static [RelativeAxisType] { + &[ RelativeAxisType::REL_X, RelativeAxisType::REL_Y, RelativeAxisType::REL_Z, @@ -627,8 +617,8 @@ pub fn get_all_relative_axes() -> Vec { ] } -pub fn get_all_switches() -> Vec { - vec![ +pub fn get_all_switches() -> &'static [SwitchType] { + &[ SwitchType::SW_LID, SwitchType::SW_TABLET_MODE, SwitchType::SW_HEADPHONE_INSERT,