diff --git a/src/common/component.rs b/src/common/component.rs index 7a532c597..ba8de2659 100644 --- a/src/common/component.rs +++ b/src/common/component.rs @@ -166,7 +166,11 @@ impl Components { /// /// let components = Components::new_with_refreshed_list(); /// for component in &components { -/// println!("{} {}°C", component.label(), component.temperature()); +/// if let Some(temperature) = component.temperature() { +/// println!("{} {temperature}°C", component.label()); +/// } else { +/// println!("{} (unknown temperature)", component.label()); +/// } /// } /// ``` pub struct Component { @@ -185,10 +189,12 @@ impl Component { /// /// let components = Components::new_with_refreshed_list(); /// for component in &components { - /// println!("{}°C", component.temperature()); + /// if let Some(temperature) = component.temperature() { + /// println!("{temperature}°C"); + /// } /// } /// ``` - pub fn temperature(&self) -> f32 { + pub fn temperature(&self) -> Option { self.inner.temperature() } @@ -207,10 +213,12 @@ impl Component { /// /// let components = Components::new_with_refreshed_list(); /// for component in &components { - /// println!("{}°C", component.max()); + /// if let Some(max) = component.max() { + /// println!("{max}°C"); + /// } /// } /// ``` - pub fn max(&self) -> f32 { + pub fn max(&self) -> Option { self.inner.max() } @@ -225,7 +233,9 @@ impl Component { /// /// let components = Components::new_with_refreshed_list(); /// for component in &components { - /// println!("{:?}°C", component.critical()); + /// if let Some(critical) = component.critical() { + /// println!("{critical}°C"); + /// } /// } /// ``` pub fn critical(&self) -> Option { diff --git a/src/debug.rs b/src/debug.rs index 4f4dfcebc..82fad1372 100644 --- a/src/debug.rs +++ b/src/debug.rs @@ -87,23 +87,21 @@ impl std::fmt::Debug for crate::Components { #[cfg(feature = "component")] impl std::fmt::Debug for crate::Component { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{} ", self.label())?; + if let Some(temperature) = self.temperature() { + write!(f, "temperature: {temperature}°C (")?; + } else { + f.write_str("temperature: unknown (")?; + } + if let Some(max) = self.max() { + write!(f, "max: {max}°C / ")?; + } else { + f.write_str("max: unknown / ")?; + } if let Some(critical) = self.critical() { - write!( - f, - "{}: {}°C (max: {}°C / critical: {}°C)", - self.label(), - self.temperature(), - self.max(), - critical - ) + write!(f, "critical: {critical}°C)") } else { - write!( - f, - "{}: {}°C (max: {}°C)", - self.label(), - self.temperature(), - self.max() - ) + f.write_str("critical: unknown)") } } } diff --git a/src/unix/apple/app_store/component.rs b/src/unix/apple/app_store/component.rs index e7a999f95..ab49cbd48 100644 --- a/src/unix/apple/app_store/component.rs +++ b/src/unix/apple/app_store/component.rs @@ -7,12 +7,12 @@ pub(crate) struct ComponentInner { } impl ComponentInner { - pub(crate) fn temperature(&self) -> f32 { - 0.0 + pub(crate) fn temperature(&self) -> Option { + None } - pub(crate) fn max(&self) -> f32 { - 0.0 + pub(crate) fn max(&self) -> Option { + None } pub(crate) fn critical(&self) -> Option { diff --git a/src/unix/apple/macos/component/arm.rs b/src/unix/apple/macos/component/arm.rs index af5a82c7f..6577fd15d 100644 --- a/src/unix/apple/macos/component/arm.rs +++ b/src/unix/apple/macos/component/arm.rs @@ -137,7 +137,7 @@ impl ComponentsInner { pub(crate) struct ComponentInner { service: *mut __IOHIDServiceClient, - temperature: f32, + temperature: Option, label: String, max: f32, critical: Option, @@ -159,17 +159,17 @@ impl ComponentInner { label, max: max.unwrap_or(0.), critical, - temperature: 0., + temperature: None, updated: true, } } - pub(crate) fn temperature(&self) -> f32 { + pub(crate) fn temperature(&self) -> Option { self.temperature } - pub(crate) fn max(&self) -> f32 { - self.max + pub(crate) fn max(&self) -> Option { + Some(self.max) } pub(crate) fn critical(&self) -> Option { @@ -182,22 +182,23 @@ impl ComponentInner { pub(crate) fn refresh(&mut self) { unsafe { - let event = match CFReleaser::new(IOHIDServiceClientCopyEvent( + let Some(event) = CFReleaser::new(IOHIDServiceClientCopyEvent( self.service as *const _, kIOHIDEventTypeTemperature, 0, 0, - )) { - Some(e) => e, - None => return, + )) else { + self.temperature = None; + return; }; - self.temperature = IOHIDEventGetFloatValue( + let temperature = IOHIDEventGetFloatValue( event.inner(), IOHIDEventFieldBase(kIOHIDEventTypeTemperature), ) as _; - if self.temperature > self.max { - self.max = self.temperature; + self.temperature = Some(temperature); + if temperature > self.max { + self.max = temperature; } } } diff --git a/src/unix/apple/macos/component/x86.rs b/src/unix/apple/macos/component/x86.rs index 3621cbe09..a921179bf 100644 --- a/src/unix/apple/macos/component/x86.rs +++ b/src/unix/apple/macos/component/x86.rs @@ -101,7 +101,7 @@ impl ComponentsInner { } pub(crate) struct ComponentInner { - temperature: f32, + temperature: Option, max: f32, critical: Option, label: String, @@ -120,7 +120,7 @@ impl ComponentInner { ) -> Option { let ffi_part = ComponentFFI::new(key, connection)?; ffi_part.temperature().map(|temperature| Self { - temperature, + temperature: Some(temperature), label, max: max.unwrap_or(temperature), critical, @@ -129,12 +129,12 @@ impl ComponentInner { }) } - pub(crate) fn temperature(&self) -> f32 { + pub(crate) fn temperature(&self) -> Option { self.temperature } - pub(crate) fn max(&self) -> f32 { - self.max + pub(crate) fn max(&self) -> Option { + Some(self.max) } pub(crate) fn critical(&self) -> Option { @@ -146,10 +146,10 @@ impl ComponentInner { } pub(crate) fn refresh(&mut self) { - if let Some(temp) = self.ffi_part.temperature() { - self.temperature = temp; - if self.temperature > self.max { - self.max = self.temperature; + self.temperature = self.ffi_part.temperature(); + if let Some(temperature) = self.temperature { + if temperature > self.max { + self.max = temperature; } } } diff --git a/src/unix/freebsd/component.rs b/src/unix/freebsd/component.rs index c849a7d70..0c1922637 100644 --- a/src/unix/freebsd/component.rs +++ b/src/unix/freebsd/component.rs @@ -6,18 +6,18 @@ use crate::Component; pub(crate) struct ComponentInner { id: Vec, label: String, - temperature: f32, + temperature: Option, max: f32, pub(crate) updated: bool, } impl ComponentInner { - pub(crate) fn temperature(&self) -> f32 { + pub(crate) fn temperature(&self) -> Option { self.temperature } - pub(crate) fn max(&self) -> f32 { - self.max + pub(crate) fn max(&self) -> Option { + Some(self.max) } pub(crate) fn critical(&self) -> Option { @@ -30,10 +30,10 @@ impl ComponentInner { pub(crate) fn refresh(&mut self) { unsafe { - if let Some(temperature) = refresh_component(&self.id) { - self.temperature = temperature; - if self.temperature > self.max { - self.max = self.temperature; + self.temperature = refresh_component(&self.id); + if let Some(temperature) = self.temperature { + if temperature > self.max { + self.max = temperature; } } } @@ -93,7 +93,7 @@ impl ComponentsInner { inner: ComponentInner { id, label: format!("CPU {}", core + 1), - temperature, + temperature: Some(temperature), max: temperature, updated: true, }, diff --git a/src/unix/linux/component.rs b/src/unix/linux/component.rs index 3c21d0ab9..e25e9a38b 100644 --- a/src/unix/linux/component.rs +++ b/src/unix/linux/component.rs @@ -343,12 +343,12 @@ impl ComponentInner { } } - pub(crate) fn temperature(&self) -> f32 { - self.temperature.unwrap_or(f32::NAN) + pub(crate) fn temperature(&self) -> Option { + self.temperature } - pub(crate) fn max(&self) -> f32 { - self.max.unwrap_or(f32::NAN) + pub(crate) fn max(&self) -> Option { + self.max } pub(crate) fn critical(&self) -> Option { diff --git a/src/unknown/component.rs b/src/unknown/component.rs index e7a999f95..ab49cbd48 100644 --- a/src/unknown/component.rs +++ b/src/unknown/component.rs @@ -7,12 +7,12 @@ pub(crate) struct ComponentInner { } impl ComponentInner { - pub(crate) fn temperature(&self) -> f32 { - 0.0 + pub(crate) fn temperature(&self) -> Option { + None } - pub(crate) fn max(&self) -> f32 { - 0.0 + pub(crate) fn max(&self) -> Option { + None } pub(crate) fn critical(&self) -> Option { diff --git a/src/windows/component.rs b/src/windows/component.rs index 9acd22232..9b92fc0e9 100644 --- a/src/windows/component.rs +++ b/src/windows/component.rs @@ -49,12 +49,12 @@ impl ComponentInner { }) } - pub(crate) fn temperature(&self) -> f32 { - self.temperature + pub(crate) fn temperature(&self) -> Option { + Some(self.temperature) } - pub(crate) fn max(&self) -> f32 { - self.max + pub(crate) fn max(&self) -> Option { + Some(self.max) } pub(crate) fn critical(&self) -> Option {