Skip to content

Commit

Permalink
Make Component::temperature and Component::max return Option
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Dec 4, 2024
1 parent 3436601 commit 1bd7f1c
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 67 deletions.
22 changes: 16 additions & 6 deletions src/common/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<f32> {
self.inner.temperature()
}

Expand All @@ -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<f32> {
self.inner.max()
}

Expand All @@ -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<f32> {
Expand Down
28 changes: 13 additions & 15 deletions src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/unix/apple/app_store/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ pub(crate) struct ComponentInner {
}

impl ComponentInner {
pub(crate) fn temperature(&self) -> f32 {
0.0
pub(crate) fn temperature(&self) -> Option<f32> {
None
}

pub(crate) fn max(&self) -> f32 {
0.0
pub(crate) fn max(&self) -> Option<f32> {
None
}

pub(crate) fn critical(&self) -> Option<f32> {
Expand Down
25 changes: 13 additions & 12 deletions src/unix/apple/macos/component/arm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl ComponentsInner {

pub(crate) struct ComponentInner {
service: *mut __IOHIDServiceClient,
temperature: f32,
temperature: Option<f32>,
label: String,
max: f32,
critical: Option<f32>,
Expand All @@ -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<f32> {
self.temperature
}

pub(crate) fn max(&self) -> f32 {
self.max
pub(crate) fn max(&self) -> Option<f32> {
Some(self.max)
}

pub(crate) fn critical(&self) -> Option<f32> {
Expand All @@ -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;
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions src/unix/apple/macos/component/x86.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl ComponentsInner {
}

pub(crate) struct ComponentInner {
temperature: f32,
temperature: Option<f32>,
max: f32,
critical: Option<f32>,
label: String,
Expand All @@ -120,7 +120,7 @@ impl ComponentInner {
) -> Option<Self> {
let ffi_part = ComponentFFI::new(key, connection)?;
ffi_part.temperature().map(|temperature| Self {
temperature,
temperature: Some(temperature),
label,
max: max.unwrap_or(temperature),
critical,
Expand All @@ -129,12 +129,12 @@ impl ComponentInner {
})
}

pub(crate) fn temperature(&self) -> f32 {
pub(crate) fn temperature(&self) -> Option<f32> {
self.temperature
}

pub(crate) fn max(&self) -> f32 {
self.max
pub(crate) fn max(&self) -> Option<f32> {
Some(self.max)
}

pub(crate) fn critical(&self) -> Option<f32> {
Expand All @@ -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;
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions src/unix/freebsd/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ use crate::Component;
pub(crate) struct ComponentInner {
id: Vec<u8>,
label: String,
temperature: f32,
temperature: Option<f32>,
max: f32,
pub(crate) updated: bool,
}

impl ComponentInner {
pub(crate) fn temperature(&self) -> f32 {
pub(crate) fn temperature(&self) -> Option<f32> {
self.temperature
}

pub(crate) fn max(&self) -> f32 {
self.max
pub(crate) fn max(&self) -> Option<f32> {
Some(self.max)
}

pub(crate) fn critical(&self) -> Option<f32> {
Expand All @@ -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;
}
}
}
Expand Down Expand Up @@ -93,7 +93,7 @@ impl ComponentsInner {
inner: ComponentInner {
id,
label: format!("CPU {}", core + 1),
temperature,
temperature: Some(temperature),
max: temperature,
updated: true,
},
Expand Down
8 changes: 4 additions & 4 deletions src/unix/linux/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,12 +343,12 @@ impl ComponentInner {
}
}

pub(crate) fn temperature(&self) -> f32 {
self.temperature.unwrap_or(f32::NAN)
pub(crate) fn temperature(&self) -> Option<f32> {
self.temperature
}

pub(crate) fn max(&self) -> f32 {
self.max.unwrap_or(f32::NAN)
pub(crate) fn max(&self) -> Option<f32> {
self.max
}

pub(crate) fn critical(&self) -> Option<f32> {
Expand Down
8 changes: 4 additions & 4 deletions src/unknown/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ pub(crate) struct ComponentInner {
}

impl ComponentInner {
pub(crate) fn temperature(&self) -> f32 {
0.0
pub(crate) fn temperature(&self) -> Option<f32> {
None
}

pub(crate) fn max(&self) -> f32 {
0.0
pub(crate) fn max(&self) -> Option<f32> {
None
}

pub(crate) fn critical(&self) -> Option<f32> {
Expand Down
8 changes: 4 additions & 4 deletions src/windows/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ impl ComponentInner {
})
}

pub(crate) fn temperature(&self) -> f32 {
self.temperature
pub(crate) fn temperature(&self) -> Option<f32> {
Some(self.temperature)
}

pub(crate) fn max(&self) -> f32 {
self.max
pub(crate) fn max(&self) -> Option<f32> {
Some(self.max)
}

pub(crate) fn critical(&self) -> Option<f32> {
Expand Down

0 comments on commit 1bd7f1c

Please sign in to comment.