From 8ffc2cb9fbd8142694de47918742f99f0c30c25b Mon Sep 17 00:00:00 2001 From: Mohammad Aadil Shabier Date: Mon, 26 Feb 2024 02:51:48 +0530 Subject: [PATCH] [refactor] replace manual insertions with collects - This helps remove unnecessary mut's - Using static slice prevents the need to create Vec which is discarded anyway --- swhkd/src/daemon.rs | 2 +- swhkd/src/uinput.rs | 28 ++++++++++------------------ 2 files changed, 11 insertions(+), 19 deletions(-) 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..63f8e0c 100644 --- a/swhkd/src/uinput.rs +++ b/swhkd/src/uinput.rs @@ -10,15 +10,10 @@ 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") @@ -30,10 +25,7 @@ pub fn create_uinput_device() -> Result 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 @@ -56,8 +48,8 @@ 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 +601,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 +619,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,