Skip to content

Commit 34b9619

Browse files
authored
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
1 parent 3db287e commit 34b9619

File tree

3 files changed

+14
-23
lines changed

3 files changed

+14
-23
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ target
44
*.out
55
com.github.swhkd.pkexec.policy
66
*rc
7+
!*rc/
78
.direnv

swhkd/src/daemon.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ fn socket_write(command: &str, socket_path: PathBuf) -> Result<(), Box<dyn Error
441441
pub fn check_input_group() -> Result<(), Box<dyn Error>> {
442442
if !Uid::current().is_root() {
443443
let groups = nix::unistd::getgroups();
444-
for (_, groups) in groups.iter().enumerate() {
444+
for groups in groups.iter() {
445445
for group in groups {
446446
let group = Group::from_gid(*group);
447447
if group.unwrap().unwrap().name == "input" {

swhkd/src/uinput.rs

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,21 @@ use std::os::unix::io::AsRawFd;
1010
ioctl_none!(rfkill_noinput, b'R', 1);
1111

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

18-
let mut relative_axes = AttributeSet::<RelativeAxisType>::new();
19-
for axis in get_all_relative_axes() {
20-
relative_axes.insert(axis);
21-
}
15+
let relative_axes: AttributeSet<RelativeAxisType> =
16+
get_all_relative_axes().iter().copied().collect();
2217

2318
let device = VirtualDeviceBuilder::new()?
2419
.name("swhkd virtual output")
2520
.with_keys(&keys)?
2621
.with_relative_axes(&relative_axes)?
27-
.build()
28-
.unwrap();
22+
.build()?;
2923
Ok(device)
3024
}
3125

3226
pub fn create_uinput_switches_device() -> Result<VirtualDevice, Box<dyn std::error::Error>> {
33-
let mut switches = AttributeSet::<SwitchType>::new();
34-
for switch in get_all_switches() {
35-
switches.insert(switch);
36-
}
27+
let switches: AttributeSet<SwitchType> = get_all_switches().iter().copied().collect();
3728

3829
// We have to disable rfkill-input to avoid blocking all radio devices. When
3930
// a new device (virtual or physical) with the SW_RFKILL_ALL capability bit
@@ -52,12 +43,11 @@ pub fn create_uinput_switches_device() -> Result<VirtualDevice, Box<dyn std::err
5243
let device = VirtualDeviceBuilder::new()?
5344
.name("swhkd switches virtual output")
5445
.with_switches(&switches)?
55-
.build()
56-
.unwrap();
46+
.build()?;
5747
Ok(device)
5848
}
59-
pub fn get_all_keys() -> Vec<Key> {
60-
vec![
49+
pub fn get_all_keys() -> &'static [Key] {
50+
&[
6151
evdev::Key::KEY_RESERVED,
6252
evdev::Key::KEY_ESC,
6353
evdev::Key::KEY_1,
@@ -609,8 +599,8 @@ pub fn get_all_keys() -> Vec<Key> {
609599
]
610600
}
611601

612-
pub fn get_all_relative_axes() -> Vec<RelativeAxisType> {
613-
vec![
602+
pub fn get_all_relative_axes() -> &'static [RelativeAxisType] {
603+
&[
614604
RelativeAxisType::REL_X,
615605
RelativeAxisType::REL_Y,
616606
RelativeAxisType::REL_Z,
@@ -627,8 +617,8 @@ pub fn get_all_relative_axes() -> Vec<RelativeAxisType> {
627617
]
628618
}
629619

630-
pub fn get_all_switches() -> Vec<SwitchType> {
631-
vec![
620+
pub fn get_all_switches() -> &'static [SwitchType] {
621+
&[
632622
SwitchType::SW_LID,
633623
SwitchType::SW_TABLET_MODE,
634624
SwitchType::SW_HEADPHONE_INSERT,

0 commit comments

Comments
 (0)