Skip to content

Commit b162b29

Browse files
Merge pull request #1565 from GuillaumeGomez/improve-component-code
Improve code to retrieve components on linux
2 parents 10d536f + 03f8561 commit b162b29

File tree

1 file changed

+9
-31
lines changed

1 file changed

+9
-31
lines changed

src/unix/linux/component.rs

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ impl ComponentInner {
373373
}
374374
}
375375

376-
fn read_temp_dir<F: FnMut(PathBuf)>(path: &str, starts_with: &str, mut f: F) {
376+
fn read_temp_dir<F: FnMut(PathBuf)>(path: &Path, starts_with: &str, mut f: F) {
377377
if let Ok(dir) = read_dir(path) {
378378
for entry in dir.flatten() {
379379
if !entry
@@ -420,18 +420,16 @@ impl ComponentsInner {
420420
}
421421

422422
pub(crate) fn refresh(&mut self) {
423-
self.refresh_from_sys_class_path("/sys/class");
423+
self.refresh_from_sys_class_path(Path::new("/sys/class"));
424424
}
425425

426-
fn refresh_from_sys_class_path(&mut self, path: &str) {
427-
let hwmon_path = format!("{path}/hwmon");
428-
read_temp_dir(&hwmon_path, "hwmon", |path| {
426+
fn refresh_from_sys_class_path(&mut self, path: &Path) {
427+
read_temp_dir(&path.join("hwmon"), "hwmon", |path| {
429428
ComponentInner::from_hwmon(&mut self.components, &path);
430429
});
431430
if self.components.is_empty() {
432431
// Normally should only be used by raspberry pi.
433-
let thermal_path = format!("{path}/thermal");
434-
read_temp_dir(&thermal_path, "thermal_", |path| {
432+
read_temp_dir(&path.join("thermal"), "thermal_", |path| {
435433
let temp = path.join("temp");
436434
if temp.exists() {
437435
let Some(name) = get_file_line(&path.join("type"), 16) else {
@@ -468,12 +466,7 @@ mod tests {
468466
.expect("failed to write to temp1_input file");
469467

470468
let mut components = ComponentsInner::new();
471-
components.refresh_from_sys_class_path(
472-
temp_dir
473-
.path()
474-
.to_str()
475-
.expect("failed to convert path to string"),
476-
);
469+
components.refresh_from_sys_class_path(temp_dir.path());
477470
let components = components.into_vec();
478471

479472
assert_eq!(components.len(), 1);
@@ -502,12 +495,7 @@ mod tests {
502495
fs::write(hwmon0_dir.join("temp1_crit"), "100").expect("failed to write to temp1_min file");
503496

504497
let mut components = ComponentsInner::new();
505-
components.refresh_from_sys_class_path(
506-
temp_dir
507-
.path()
508-
.to_str()
509-
.expect("failed to convert path to string"),
510-
);
498+
components.refresh_from_sys_class_path(temp_dir.path());
511499
let components = components.into_vec();
512500

513501
assert_eq!(components.len(), 1);
@@ -539,12 +527,7 @@ mod tests {
539527
fs::write(hwmon0_dir.join("temp2_crit"), "200").expect("failed to write to temp2_min file");
540528

541529
let mut components = ComponentsInner::new();
542-
components.refresh_from_sys_class_path(
543-
temp_dir
544-
.path()
545-
.to_str()
546-
.expect("failed to convert path to string"),
547-
);
530+
components.refresh_from_sys_class_path(temp_dir.path());
548531
let mut components = components.into_vec();
549532
components.sort_by_key(|c| c.inner.label.clone());
550533

@@ -588,12 +571,7 @@ mod tests {
588571
fs::write(hwmon0_dir.join("temp2_crit"), "200").expect("failed to write to temp2_min file");
589572

590573
let mut components = ComponentsInner::new();
591-
components.refresh_from_sys_class_path(
592-
temp_dir
593-
.path()
594-
.to_str()
595-
.expect("failed to convert path to string"),
596-
);
574+
components.refresh_from_sys_class_path(temp_dir.path());
597575
let mut components = components.into_vec();
598576
components.sort_by_key(|c| c.inner.label.clone());
599577

0 commit comments

Comments
 (0)