use crate::utils::into_iter_mut;
use crate::{ComponentInner, ComponentsInner};
pub struct Components {
pub(crate) inner: ComponentsInner,
}
impl Default for Components {
fn default() -> Self {
Self::new()
}
}
impl From<Components> for Vec<Component> {
fn from(components: Components) -> Self {
components.inner.into_vec()
}
}
impl From<Vec<Component>> for Components {
fn from(components: Vec<Component>) -> Self {
Self {
inner: ComponentsInner::from_vec(components),
}
}
}
impl std::ops::Deref for Components {
type Target = [Component];
fn deref(&self) -> &Self::Target {
self.list()
}
}
impl std::ops::DerefMut for Components {
fn deref_mut(&mut self) -> &mut Self::Target {
self.list_mut()
}
}
impl<'a> IntoIterator for &'a Components {
type Item = &'a Component;
type IntoIter = std::slice::Iter<'a, Component>;
fn into_iter(self) -> Self::IntoIter {
self.list().iter()
}
}
impl<'a> IntoIterator for &'a mut Components {
type Item = &'a mut Component;
type IntoIter = std::slice::IterMut<'a, Component>;
fn into_iter(self) -> Self::IntoIter {
self.list_mut().iter_mut()
}
}
impl Components {
pub fn new() -> Self {
Self {
inner: ComponentsInner::new(),
}
}
pub fn new_with_refreshed_list() -> Self {
let mut components = Self::new();
components.refresh_list();
components
}
pub fn list(&self) -> &[Component] {
self.inner.list()
}
pub fn list_mut(&mut self) -> &mut [Component] {
self.inner.list_mut()
}
pub fn refresh(&mut self) {
#[cfg(all(
feature = "multithread",
not(feature = "unknown-ci"),
not(all(target_os = "macos", feature = "apple-sandbox")),
))]
use rayon::iter::ParallelIterator;
into_iter_mut(self.list_mut()).for_each(|component| component.refresh());
}
pub fn refresh_list(&mut self) {
self.inner.refresh_list()
}
}
pub struct Component {
pub(crate) inner: ComponentInner,
}
impl Component {
pub fn temperature(&self) -> f32 {
self.inner.temperature()
}
pub fn max(&self) -> f32 {
self.inner.max()
}
pub fn critical(&self) -> Option<f32> {
self.inner.critical()
}
pub fn label(&self) -> &str {
self.inner.label()
}
pub fn refresh(&mut self) {
self.inner.refresh()
}
}
#[cfg(test)]
mod tests {
use crate::*;
#[test]
fn test_components_mac_m1() {
let mut components = Components::new();
components.refresh_list();
components.refresh_list();
}
}