Skip to content

Commit

Permalink
[refactor] replace manual insertions with collects
Browse files Browse the repository at this point in the history
- This helps remove unnecessary mut's
- Using static slice prevents the need to create Vec which is discarded anyway
  • Loading branch information
aadilshabier committed Feb 25, 2024
1 parent 3db287e commit 8ffc2cb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
2 changes: 1 addition & 1 deletion swhkd/src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ fn socket_write(command: &str, socket_path: PathBuf) -> Result<(), Box<dyn Error
pub fn check_input_group() -> Result<(), Box<dyn Error>> {
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" {
Expand Down
28 changes: 10 additions & 18 deletions swhkd/src/uinput.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,10 @@ use std::os::unix::io::AsRawFd;
ioctl_none!(rfkill_noinput, b'R', 1);

pub fn create_uinput_device() -> Result<VirtualDevice, Box<dyn std::error::Error>> {
let mut keys = AttributeSet::<Key>::new();
for key in get_all_keys() {
keys.insert(key);
}
let keys: AttributeSet<Key> = get_all_keys().iter().copied().collect();

let mut relative_axes = AttributeSet::<RelativeAxisType>::new();
for axis in get_all_relative_axes() {
relative_axes.insert(axis);
}
let relative_axes: AttributeSet<RelativeAxisType> =
get_all_relative_axes().iter().copied().collect();

let device = VirtualDeviceBuilder::new()?
.name("swhkd virtual output")
Expand All @@ -30,10 +25,7 @@ pub fn create_uinput_device() -> Result<VirtualDevice, Box<dyn std::error::Error
}

pub fn create_uinput_switches_device() -> Result<VirtualDevice, Box<dyn std::error::Error>> {
let mut switches = AttributeSet::<SwitchType>::new();
for switch in get_all_switches() {
switches.insert(switch);
}
let switches: AttributeSet<SwitchType> = 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
Expand All @@ -56,8 +48,8 @@ pub fn create_uinput_switches_device() -> Result<VirtualDevice, Box<dyn std::err
.unwrap();
Ok(device)
}
pub fn get_all_keys() -> Vec<Key> {
vec![
pub fn get_all_keys() -> &'static [Key] {
&[
evdev::Key::KEY_RESERVED,
evdev::Key::KEY_ESC,
evdev::Key::KEY_1,
Expand Down Expand Up @@ -609,8 +601,8 @@ pub fn get_all_keys() -> Vec<Key> {
]
}

pub fn get_all_relative_axes() -> Vec<RelativeAxisType> {
vec![
pub fn get_all_relative_axes() -> &'static [RelativeAxisType] {
&[
RelativeAxisType::REL_X,
RelativeAxisType::REL_Y,
RelativeAxisType::REL_Z,
Expand All @@ -627,8 +619,8 @@ pub fn get_all_relative_axes() -> Vec<RelativeAxisType> {
]
}

pub fn get_all_switches() -> Vec<SwitchType> {
vec![
pub fn get_all_switches() -> &'static [SwitchType] {
&[
SwitchType::SW_LID,
SwitchType::SW_TABLET_MODE,
SwitchType::SW_HEADPHONE_INSERT,
Expand Down

0 comments on commit 8ffc2cb

Please sign in to comment.