heim_sensors/
temperatures.rs

1use std::fmt;
2
3use heim_common::prelude::*;
4use heim_common::units::ThermodynamicTemperature;
5
6use crate::sys;
7
8/// Hardware temperature sensor.
9pub struct TemperatureSensor {
10    pub(crate) unit: String,
11    pub(crate) label: Option<String>,
12    pub(crate) current: ThermodynamicTemperature,
13    pub(crate) high: Option<ThermodynamicTemperature>,
14    pub(crate) critical: Option<ThermodynamicTemperature>,
15}
16
17impl TemperatureSensor {
18    /// Returns sensor unit name.
19    pub fn unit(&self) -> &str {
20        &self.unit
21    }
22
23    /// Returns sensor label.
24    #[allow(clippy::option_as_ref_deref)] // >= 1.40.0
25    pub fn label(&self) -> Option<&str> {
26        self.label.as_ref().map(|s| s.as_str())
27    }
28
29    /// Returns current temperature reported by sensor.
30    pub fn current(&self) -> ThermodynamicTemperature {
31        self.current
32    }
33
34    /// Returns high trip point for sensor if available.
35    pub fn high(&self) -> Option<ThermodynamicTemperature> {
36        self.high
37    }
38
39    /// Returns critical trip point for sensor if available.
40    pub fn critical(&self) -> Option<ThermodynamicTemperature> {
41        self.critical
42    }
43}
44
45impl fmt::Debug for TemperatureSensor {
46    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
47        f.debug_struct("TemperatureSensor")
48            .field("unit", &self.unit())
49            .field("label", &self.label())
50            .field("current", &self.current())
51            .field("high", &self.high())
52            .field("critical", &self.critical())
53            .finish()
54    }
55}
56
57/// Returns stream which yields [temperature sensors].
58///
59/// ## Compatibility
60///
61/// At the moment, this function works only with Linux.
62/// For other platforms it returns an empty stream.
63///
64/// [temperature sensors]: ./struct.TemperatureSensor.html
65pub fn temperatures() -> impl Stream<Item = Result<TemperatureSensor>> {
66    sys::temperatures()
67}