From 065e131fea1e560b710e988b150f84e9f2fd41bd Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 14 Oct 2023 12:07:28 +0200 Subject: [PATCH] Remove ComponentExt and NetworksExt traits --- README.md | 2 +- benches/basic.rs | 2 +- examples/simple.rs | 4 +- src/c_interface.rs | 2 +- src/common.rs | 218 +++++++++++++++++++++++-- src/debug.rs | 4 +- src/lib.rs | 16 +- src/serde.rs | 3 +- src/traits.rs | 219 +++----------------------- src/unix/apple/app_store/component.rs | 17 +- src/unix/apple/macos/component/arm.rs | 23 ++- src/unix/apple/macos/component/x86.rs | 31 ++-- src/unix/apple/mod.rs | 4 +- src/unix/apple/network.rs | 58 +++---- src/unix/freebsd/component.rs | 27 ++-- src/unix/freebsd/mod.rs | 4 +- src/unix/freebsd/network.rs | 18 ++- src/unix/linux/component.rs | 34 ++-- src/unix/linux/mod.rs | 4 +- src/unix/linux/network.rs | 18 ++- src/unknown/component.rs | 17 +- src/unknown/mod.rs | 4 +- src/unknown/network.rs | 16 +- src/windows/component.rs | 29 ++-- src/windows/mod.rs | 4 +- src/windows/network.rs | 16 +- tests/network.rs | 2 +- 27 files changed, 412 insertions(+), 384 deletions(-) diff --git a/README.md b/README.md index 444b8da1f..ec919dcf1 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ Otherwise, here is a little code sample: ```rust use sysinfo::{ Components, ComponentsExt, Disks, NetworkExt, Networks, - NetworksExt, System, + System, }; // Please note that we use "new_all" to ensure that all list of diff --git a/benches/basic.rs b/benches/basic.rs index d44d7108d..9745fa064 100644 --- a/benches/basic.rs +++ b/benches/basic.rs @@ -3,7 +3,7 @@ extern crate test; use sysinfo::get_current_pid; -use sysinfo::{ComponentsExt, NetworksExt, UsersExt}; +use sysinfo::{ComponentsExt, UsersExt}; #[bench] fn bench_new(b: &mut test::Bencher) { diff --git a/examples/simple.rs b/examples/simple.rs index 15eae8e7d..526a56ab3 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -8,8 +8,8 @@ use std::io::{self, BufRead, Write}; use std::str::FromStr; use sysinfo::Signal::*; use sysinfo::{ - Components, ComponentsExt, CpuExt, Disks, NetworkExt, Networks, NetworksExt, Pid, Signal, - System, UserExt, Users, UsersExt, + Components, ComponentsExt, CpuExt, Disks, NetworkExt, Networks, Pid, Signal, System, UserExt, + Users, UsersExt, }; const signals: &[Signal] = &[ diff --git a/src/c_interface.rs b/src/c_interface.rs index b99f90359..60b7632be 100644 --- a/src/c_interface.rs +++ b/src/c_interface.rs @@ -1,6 +1,6 @@ // Take a look at the license at the top of the repository in the LICENSE file. -use crate::{CpuExt, Disks, NetworkExt, Networks, NetworksExt, Pid, Process, System}; +use crate::{CpuExt, Disks, NetworkExt, Networks, Pid, Process, System}; use libc::{self, c_char, c_float, c_uint, c_void, size_t}; use std::borrow::BorrowMut; use std::ffi::CString; diff --git a/src/common.rs b/src/common.rs index 6446c4044..492442ca8 100644 --- a/src/common.rs +++ b/src/common.rs @@ -1,8 +1,8 @@ // Take a look at the license at the top of the repository in the LICENSE file. use crate::{ - Component, Components, ComponentsExt, Cpu, GroupExt, NetworkData, NetworksExt, ProcessInner, - SystemInner, User, UserExt, UsersExt, + ComponentInner, Components, ComponentsExt, Cpu, GroupExt, NetworkData, NetworksInner, + ProcessInner, SystemInner, User, UserExt, UsersExt, }; use std::cmp::Ordering; @@ -1534,22 +1534,19 @@ impl RefreshKind { impl_get_set!(RefreshKind, cpu, with_cpu, without_cpu, CpuRefreshKind); } -/// Network interfaces. -/// -/// Don't forget to also take a look at the [`NetworksExt`] trait to see the list of available -/// methods. +/// Interacting with network interfaces. /// /// ```no_run -/// use sysinfo::{Networks, NetworksExt}; +/// use sysinfo::Networks; /// /// let mut networks = Networks::new(); /// networks.refresh_list(); -/// for network in networks.iter() { -/// println!("{:?}", network); +/// for (interface_name, network) in &networks { +/// println!("[{interface_name}]: {network:?}"); /// } /// ``` pub struct Networks { - pub(crate) interfaces: HashMap, + pub(crate) inner: NetworksInner, } impl<'a> IntoIterator for &'a Networks { @@ -1561,12 +1558,90 @@ impl<'a> IntoIterator for &'a Networks { } } +impl Default for Networks { + fn default() -> Self { + Networks::new() + } +} + +impl Networks { + /// Creates a new [`Networks`][crate::Networks] type. + /// + /// ```no_run + /// use sysinfo::Networks; + /// + /// let mut networks = Networks::new(); + /// networks.refresh_list(); + /// for (interface_name, network) in &networks { + /// println!("[{interface_name}]: {network:?}"); + /// } + /// ``` + pub fn new() -> Self { + Self { + inner: NetworksInner::new(), + } + } + + /// Returns an iterator over the network interfaces. + /// + /// ```no_run + /// use sysinfo::{Networks, NetworkExt, System}; + /// + /// let mut networks = Networks::new(); + /// networks.refresh_list(); + /// for (interface_name, data) in &networks { + /// println!( + /// "[{interface_name}] in: {}, out: {}", + /// data.received(), + /// data.transmitted(), + /// ); + /// } + /// ``` + pub fn iter(&self) -> NetworksIter { + self.inner.iter() + } + + /// Refreshes the network interfaces list. + /// + /// ```no_run + /// use sysinfo::{Networks, System}; + /// + /// let mut networks = Networks::new(); + /// networks.refresh_list(); + /// ``` + pub fn refresh_list(&mut self) { + self.inner.refresh_list() + } + + /// Refreshes the network interfaces' content. If you didn't run [`Networks::refresh_list`] + /// before, calling this method won't do anything as no interfaces are present. + /// + /// ⚠️ If a user is added or removed, this method won't take it into account. Use + /// [`Networks::refresh_list`] instead. + /// + /// ⚠️ If you didn't call [`Networks::refresh_list`] beforehand, this method will do nothing + /// as the network list will be empty. + /// + /// ```no_run + /// use sysinfo::{Networks, System}; + /// + /// let mut networks = Networks::new(); + /// // Refreshes the network interfaces list. + /// networks.refresh_list(); + /// // Wait some time...? Then refresh the data of each network. + /// networks.refresh(); + /// ``` + pub fn refresh(&mut self) { + self.inner.refresh() + } +} + /// Iterator over network interfaces. /// /// It is returned by [`Networks::iter`][crate::Networks#method.iter]. /// /// ```no_run -/// use sysinfo::{Networks, NetworksExt}; +/// use sysinfo::Networks; /// /// let networks = Networks::new(); /// let networks_iter = networks.iter(); @@ -2482,6 +2557,127 @@ impl fmt::Display for MacAddr { } } +/// Getting a component temperature information. +/// +/// ```no_run +/// use sysinfo::{Components, ComponentsExt}; +/// +/// let mut components = Components::new(); +/// components.refresh_list(); +/// for component in components.iter() { +/// println!("{}°C", component.temperature()); +/// } +/// ``` +pub struct Component { + pub(crate) inner: ComponentInner, +} + +impl Component { + /// Returns the temperature of the component (in celsius degree). + /// + /// ## Linux + /// + /// Returns `f32::NAN` if it failed to retrieve it. + /// + /// ```no_run + /// use sysinfo::{Components, ComponentsExt}; + /// + /// let mut components = Components::new(); + /// components.refresh_list(); + /// for component in components.iter() { + /// println!("{}°C", component.temperature()); + /// } + /// ``` + pub fn temperature(&self) -> f32 { + self.inner.temperature() + } + + /// Returns the maximum temperature of the component (in celsius degree). + /// + /// Note: if `temperature` is higher than the current `max`, + /// `max` value will be updated on refresh. + /// + /// ```no_run + /// use sysinfo::{Components, ComponentsExt}; + /// + /// let mut components = Components::new(); + /// components.refresh_list(); + /// for component in components.iter() { + /// println!("{}°C", component.max()); + /// } + /// ``` + /// + /// ## Linux + /// + /// May be computed by `sysinfo` from kernel. + /// Returns `f32::NAN` if it failed to retrieve it. + pub fn max(&self) -> f32 { + self.inner.max() + } + + /// Returns the highest temperature before the component halts (in celsius degree). + /// + /// ```no_run + /// use sysinfo::{Components, ComponentsExt}; + /// + /// let mut components = Components::new(); + /// components.refresh_list(); + /// for component in components.iter() { + /// println!("{:?}°C", component.critical()); + /// } + /// ``` + /// + /// ## Linux + /// + /// Critical threshold defined by chip or kernel. + pub fn critical(&self) -> Option { + self.inner.critical() + } + + /// Returns the label of the component. + /// + /// ```no_run + /// use sysinfo::{Components, ComponentsExt}; + /// + /// let mut components = Components::new(); + /// components.refresh_list(); + /// for component in components.iter() { + /// println!("{}", component.label()); + /// } + /// ``` + /// + /// ## Linux + /// + /// Since components information is retrieved thanks to `hwmon`, + /// the labels are generated as follows. + /// Note: it may change and it was inspired by `sensors` own formatting. + /// + /// | name | label | device_model | id_sensor | Computed label by `sysinfo` | + /// |---------|--------|------------|----------|----------------------| + /// | ✓ | ✓ | ✓ | ✓ | `"{name} {label} {device_model} temp{id}"` | + /// | ✓ | ✓ | ✗ | ✓ | `"{name} {label} {id}"` | + /// | ✓ | ✗ | ✓ | ✓ | `"{name} {device_model}"` | + /// | ✓ | ✗ | ✗ | ✓ | `"{name} temp{id}"` | + pub fn label(&self) -> &str { + self.inner.label() + } + + /// Refreshes component. + /// + /// ```no_run + /// use sysinfo::{Components, ComponentsExt}; + /// + /// let mut components = Components::new(); + /// components.refresh_list(); + /// for component in components.iter_mut() { + /// component.refresh(); + /// } + /// ``` + pub fn refresh(&mut self) { + self.inner.refresh() + } +} + #[cfg(test)] mod tests { use super::{MacAddr, ProcessStatus}; diff --git a/src/debug.rs b/src/debug.rs index 137dbc520..2a3f3a356 100644 --- a/src/debug.rs +++ b/src/debug.rs @@ -1,8 +1,8 @@ // Take a look at the license at the top of the repository in the LICENSE file. use crate::{ - Component, ComponentExt, Components, Cpu, CpuExt, Disk, Disks, NetworkData, NetworkExt, - Networks, NetworksExt, Process, System, User, UserExt, Users, + Component, Components, Cpu, CpuExt, Disk, Disks, NetworkData, NetworkExt, Networks, Process, + System, User, UserExt, Users, }; use std::fmt; diff --git a/src/lib.rs b/src/lib.rs index fc0040c21..f63de8999 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -52,19 +52,19 @@ cfg_if::cfg_if! { } pub use crate::common::{ - get_current_pid, CpuRefreshKind, Disk, DiskKind, DiskUsage, Disks, Gid, Group, LoadAvg, - MacAddr, Networks, NetworksIter, Pid, PidExt, Process, ProcessRefreshKind, ProcessStatus, - RefreshKind, Signal, System, Uid, Users, + get_current_pid, Component, CpuRefreshKind, Disk, DiskKind, DiskUsage, Disks, Gid, Group, + LoadAvg, MacAddr, Networks, NetworksIter, Pid, PidExt, Process, ProcessRefreshKind, + ProcessStatus, RefreshKind, Signal, System, Uid, Users, }; +pub(crate) use crate::sys::{ + ComponentInner, DiskInner, DisksInner, NetworksInner, ProcessInner, SystemInner, +}; pub use crate::sys::{ - Component, Components, Cpu, NetworkData, User, IS_SUPPORTED, MINIMUM_CPU_UPDATE_INTERVAL, + Components, Cpu, NetworkData, User, IS_SUPPORTED, MINIMUM_CPU_UPDATE_INTERVAL, SUPPORTED_SIGNALS, }; -pub(crate) use crate::sys::{DiskInner, DisksInner, ProcessInner, SystemInner}; -pub use crate::traits::{ - ComponentExt, ComponentsExt, CpuExt, GroupExt, NetworkExt, NetworksExt, UserExt, UsersExt, -}; +pub use crate::traits::{ComponentsExt, CpuExt, GroupExt, NetworkExt, UserExt, UsersExt}; #[cfg(feature = "c-interface")] pub use crate::c_interface::*; diff --git a/src/serde.rs b/src/serde.rs index 2fc7b1ffe..1f88cfdd5 100644 --- a/src/serde.rs +++ b/src/serde.rs @@ -2,8 +2,7 @@ use crate::common::PidExt; use crate::{ - ComponentExt, CpuExt, DiskKind, DiskUsage, GroupExt, MacAddr, NetworkExt, NetworksExt, - ProcessStatus, Signal, UserExt, + CpuExt, DiskKind, DiskUsage, GroupExt, MacAddr, NetworkExt, ProcessStatus, Signal, UserExt, }; use serde::{ser::SerializeStruct, Serialize, Serializer}; use std::ops::Deref; diff --git a/src/traits.rs b/src/traits.rs index ab44465c1..6b5933740 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -1,10 +1,7 @@ // Take a look at the license at the top of the repository in the LICENSE file. -use crate::{ - common::{Gid, MacAddr, Uid}, - sys::Component, -}; -use crate::{Group, NetworksIter, User}; +use crate::common::{Gid, MacAddr, Uid}; +use crate::{Component, Group, User}; use std::fmt::Debug; @@ -110,7 +107,7 @@ pub trait CpuExt: Debug { /// Getting volume of received and transmitted data. /// /// ```no_run -/// use sysinfo::{Networks, NetworkExt, NetworksExt}; +/// use sysinfo::{Networks, NetworkExt}; /// /// let mut networks = Networks::new(); /// networks.refresh_list(); @@ -122,7 +119,7 @@ pub trait NetworkExt: Debug { /// Returns the number of received bytes since the last refresh. /// /// ```no_run - /// use sysinfo::{Networks, NetworkExt, NetworksExt}; + /// use sysinfo::{Networks, NetworkExt}; /// /// let mut networks = Networks::new(); /// networks.refresh_list(); @@ -135,7 +132,7 @@ pub trait NetworkExt: Debug { /// Returns the total number of received bytes. /// /// ```no_run - /// use sysinfo::{Networks, NetworkExt, NetworksExt}; + /// use sysinfo::{Networks, NetworkExt}; /// /// let mut networks = Networks::new(); /// networks.refresh_list(); @@ -148,7 +145,7 @@ pub trait NetworkExt: Debug { /// Returns the number of transmitted bytes since the last refresh. /// /// ```no_run - /// use sysinfo::{Networks, NetworkExt, NetworksExt}; + /// use sysinfo::{Networks, NetworkExt}; /// /// let mut networks = Networks::new(); /// networks.refresh_list(); @@ -161,7 +158,7 @@ pub trait NetworkExt: Debug { /// Returns the total number of transmitted bytes. /// /// ```no_run - /// use sysinfo::{Networks, NetworkExt, NetworksExt}; + /// use sysinfo::{Networks, NetworkExt}; /// /// let mut networks = Networks::new(); /// networks.refresh_list(); @@ -174,7 +171,7 @@ pub trait NetworkExt: Debug { /// Returns the number of incoming packets since the last refresh. /// /// ```no_run - /// use sysinfo::{Networks, NetworkExt, NetworksExt}; + /// use sysinfo::{Networks, NetworkExt}; /// /// let mut networks = Networks::new(); /// networks.refresh_list(); @@ -187,7 +184,7 @@ pub trait NetworkExt: Debug { /// Returns the total number of incoming packets. /// /// ```no_run - /// use sysinfo::{Networks, NetworkExt, NetworksExt}; + /// use sysinfo::{Networks, NetworkExt}; /// /// let mut networks = Networks::new(); /// networks.refresh_list(); @@ -200,7 +197,7 @@ pub trait NetworkExt: Debug { /// Returns the number of outcoming packets since the last refresh. /// /// ```no_run - /// use sysinfo::{Networks, NetworkExt, NetworksExt}; + /// use sysinfo::{Networks, NetworkExt}; /// /// let mut networks = Networks::new(); /// networks.refresh_list(); @@ -213,7 +210,7 @@ pub trait NetworkExt: Debug { /// Returns the total number of outcoming packets. /// /// ```no_run - /// use sysinfo::{Networks, NetworkExt, NetworksExt}; + /// use sysinfo::{Networks, NetworkExt}; /// /// let mut networks = Networks::new(); /// networks.refresh_list(); @@ -226,7 +223,7 @@ pub trait NetworkExt: Debug { /// Returns the number of incoming errors since the last refresh. /// /// ```no_run - /// use sysinfo::{Networks, NetworkExt, NetworksExt}; + /// use sysinfo::{Networks, NetworkExt}; /// /// let mut networks = Networks::new(); /// networks.refresh_list(); @@ -239,7 +236,7 @@ pub trait NetworkExt: Debug { /// Returns the total number of incoming errors. /// /// ```no_run - /// use sysinfo::{Networks, NetworkExt, NetworksExt}; + /// use sysinfo::{Networks, NetworkExt}; /// /// let mut networks = Networks::new(); /// networks.refresh_list(); @@ -252,7 +249,7 @@ pub trait NetworkExt: Debug { /// Returns the number of outcoming errors since the last refresh. /// /// ```no_run - /// use sysinfo::{Networks, NetworkExt, NetworksExt}; + /// use sysinfo::{Networks, NetworkExt}; /// /// let mut networks = Networks::new(); /// networks.refresh_list(); @@ -265,7 +262,7 @@ pub trait NetworkExt: Debug { /// Returns the total number of outcoming errors. /// /// ```no_run - /// use sysinfo::{Networks, NetworkExt, NetworksExt}; + /// use sysinfo::{Networks, NetworkExt}; /// /// let mut networks = Networks::new(); /// networks.refresh_list(); @@ -278,7 +275,7 @@ pub trait NetworkExt: Debug { /// Returns the MAC address associated to current interface. /// /// ```no_run - /// use sysinfo::{Networks, NetworkExt, NetworksExt}; + /// use sysinfo::{Networks, NetworkExt}; /// /// let mut networks = Networks::new(); /// networks.refresh_list(); @@ -289,186 +286,6 @@ pub trait NetworkExt: Debug { fn mac_address(&self) -> MacAddr; } -/// Interacting with network interfaces. -/// -/// ```no_run -/// use sysinfo::{Networks, NetworksExt}; -/// -/// let mut networks = Networks::new(); -/// networks.refresh_list(); -/// for (interface_name, network) in &networks { -/// println!("[{interface_name}]: {network:?}"); -/// } -/// ``` -pub trait NetworksExt: Debug { - /// Creates a new [`Networks`][crate::Networks] type. - /// - /// ```no_run - /// use sysinfo::{Networks, NetworksExt}; - /// - /// let mut networks = Networks::new(); - /// networks.refresh_list(); - /// for (interface_name, network) in &networks { - /// println!("[{interface_name}]: {network:?}"); - /// } - /// ``` - fn new() -> Self; - - /// Returns an iterator over the network interfaces. - /// - /// ```no_run - /// use sysinfo::{Networks, NetworkExt, NetworksExt, System}; - /// - /// let mut networks = Networks::new(); - /// networks.refresh_list(); - /// for (interface_name, data) in &networks { - /// println!( - /// "[{interface_name}] in: {}, out: {}", - /// data.received(), - /// data.transmitted(), - /// ); - /// } - /// ``` - fn iter(&self) -> NetworksIter; - - /// Refreshes the network interfaces list. - /// - /// ```no_run - /// use sysinfo::{Networks, NetworksExt, System}; - /// - /// let mut networks = Networks::new(); - /// networks.refresh_list(); - /// ``` - fn refresh_list(&mut self); - - /// Refreshes the network interfaces' content. If you didn't run [`NetworksExt::refresh_list`] - /// before, calling this method won't do anything as no interfaces are present. - /// - /// ⚠️ If a user is added or removed, this method won't take it into account. Use - /// [`NetworksExt::refresh_list`] instead. - /// - /// ⚠️ If you didn't call [`NetworksExt::refresh_list`] beforehand, this method will do nothing - /// as the network list will be empty. - /// - /// ```no_run - /// use sysinfo::{Networks, NetworksExt, System}; - /// - /// let mut networks = Networks::new(); - /// // Refreshes the network interfaces list. - /// networks.refresh_list(); - /// // Wait some time...? Then refresh the data of each network. - /// networks.refresh(); - /// ``` - fn refresh(&mut self); -} - -/// Getting a component temperature information. -/// -/// ```no_run -/// use sysinfo::{ComponentExt, Components, ComponentsExt}; -/// -/// let mut components = Components::new(); -/// components.refresh_list(); -/// for component in components.iter() { -/// println!("{}°C", component.temperature()); -/// } -/// ``` -pub trait ComponentExt: Debug { - /// Returns the temperature of the component (in celsius degree). - /// - /// ## Linux - /// - /// Returns `f32::NAN` if it failed to retrieve it. - /// - /// ```no_run - /// use sysinfo::{ComponentExt, Components, ComponentsExt}; - /// - /// let mut components = Components::new(); - /// components.refresh_list(); - /// for component in components.iter() { - /// println!("{}°C", component.temperature()); - /// } - /// ``` - fn temperature(&self) -> f32; - - /// Returns the maximum temperature of the component (in celsius degree). - /// - /// Note: if `temperature` is higher than the current `max`, - /// `max` value will be updated on refresh. - /// - /// ```no_run - /// use sysinfo::{ComponentExt, Components, ComponentsExt}; - /// - /// let mut components = Components::new(); - /// components.refresh_list(); - /// for component in components.iter() { - /// println!("{}°C", component.max()); - /// } - /// ``` - /// - /// ## Linux - /// - /// May be computed by `sysinfo` from kernel. - /// Returns `f32::NAN` if it failed to retrieve it. - fn max(&self) -> f32; - - /// Returns the highest temperature before the component halts (in celsius degree). - /// - /// ```no_run - /// use sysinfo::{ComponentExt, Components, ComponentsExt}; - /// - /// let mut components = Components::new(); - /// components.refresh_list(); - /// for component in components.iter() { - /// println!("{:?}°C", component.critical()); - /// } - /// ``` - /// - /// ## Linux - /// - /// Critical threshold defined by chip or kernel. - fn critical(&self) -> Option; - - /// Returns the label of the component. - /// - /// ```no_run - /// use sysinfo::{ComponentExt, Components, ComponentsExt}; - /// - /// let mut components = Components::new(); - /// components.refresh_list(); - /// for component in components.iter() { - /// println!("{}", component.label()); - /// } - /// ``` - /// - /// ## Linux - /// - /// Since components information is retrieved thanks to `hwmon`, - /// the labels are generated as follows. - /// Note: it may change and it was inspired by `sensors` own formatting. - /// - /// | name | label | device_model | id_sensor | Computed label by `sysinfo` | - /// |---------|--------|------------|----------|----------------------| - /// | ✓ | ✓ | ✓ | ✓ | `"{name} {label} {device_model} temp{id}"` | - /// | ✓ | ✓ | ✗ | ✓ | `"{name} {label} {id}"` | - /// | ✓ | ✗ | ✓ | ✓ | `"{name} {device_model}"` | - /// | ✓ | ✗ | ✗ | ✓ | `"{name} temp{id}"` | - fn label(&self) -> &str; - - /// Refreshes component. - /// - /// ```no_run - /// use sysinfo::{ComponentExt, Components, ComponentsExt}; - /// - /// let mut components = Components::new(); - /// components.refresh_list(); - /// for component in components.iter_mut() { - /// component.refresh(); - /// } - /// ``` - fn refresh(&mut self); -} - /// Interacting with components. /// /// ```no_run @@ -510,7 +327,7 @@ pub trait ComponentsExt: Debug { /// Returns the components list. /// /// ```no_run - /// use sysinfo::{ComponentExt, Components, ComponentsExt}; + /// use sysinfo::{Components, ComponentsExt}; /// /// let mut components = Components::new(); /// components.refresh_list(); @@ -529,7 +346,7 @@ pub trait ComponentsExt: Debug { /// You can do the same without this method by calling: /// /// ```no_run - /// use sysinfo::{ComponentExt, Components, ComponentsExt}; + /// use sysinfo::{Components, ComponentsExt}; /// /// let mut components = Components::new(); /// components.refresh_list(); diff --git a/src/unix/apple/app_store/component.rs b/src/unix/apple/app_store/component.rs index 541a05696..effe44646 100644 --- a/src/unix/apple/app_store/component.rs +++ b/src/unix/apple/app_store/component.rs @@ -1,28 +1,27 @@ // Take a look at the license at the top of the repository in the LICENSE file. -use crate::{ComponentExt, ComponentsExt}; +use crate::{Component, ComponentsExt}; -#[doc = include_str!("../../../../md_doc/component.md")] -pub struct Component {} +pub(crate) struct ComponentInner; -impl ComponentExt for Component { - fn temperature(&self) -> f32 { +impl ComponentInner { + pub(crate) fn temperature(&self) -> f32 { 0.0 } - fn max(&self) -> f32 { + pub(crate) fn max(&self) -> f32 { 0.0 } - fn critical(&self) -> Option { + pub(crate) fn critical(&self) -> Option { None } - fn label(&self) -> &str { + pub(crate) fn label(&self) -> &str { "" } - fn refresh(&mut self) {} + pub(crate) fn refresh(&mut self) {} } #[doc = include_str!("../../../../md_doc/components.md")] diff --git a/src/unix/apple/macos/component/arm.rs b/src/unix/apple/macos/component/arm.rs index f9a9e8227..e3e0de130 100644 --- a/src/unix/apple/macos/component/arm.rs +++ b/src/unix/apple/macos/component/arm.rs @@ -16,7 +16,7 @@ use crate::sys::inner::ffi::{ HID_DEVICE_PROPERTY_PRODUCT, }; use crate::sys::utils::CFReleaser; -use crate::{ComponentExt, ComponentsExt}; +use crate::{Component, ComponentsExt}; #[doc = include_str!("../../../../../md_doc/components.md")] pub struct Components { @@ -106,17 +106,16 @@ impl ComponentsExt for Components { CFStringGetCStringPtr(name.inner() as *const _, kCFStringEncodingUTF8); let name_str = CStr::from_ptr(name_ptr).to_string_lossy().to_string(); - let mut component = Component::new(name_str, None, None, service); + let mut component = ComponentInner::new(name_str, None, None, service); component.refresh(); - self.components.push(component); + self.components.push(Component { inner: component }); } } } } -#[doc = include_str!("../../../../../md_doc/component.md")] -pub struct Component { +pub(crate) struct ComponentInner { service: CFReleaser<__IOHIDServiceClient>, temperature: f32, label: String, @@ -124,7 +123,7 @@ pub struct Component { critical: Option, } -impl Component { +impl ComponentInner { pub(crate) fn new( label: String, max: Option, @@ -139,26 +138,24 @@ impl Component { temperature: 0., } } -} -impl ComponentExt for Component { - fn temperature(&self) -> f32 { + pub(crate) fn temperature(&self) -> f32 { self.temperature } - fn max(&self) -> f32 { + pub(crate) fn max(&self) -> f32 { self.max } - fn critical(&self) -> Option { + pub(crate) fn critical(&self) -> Option { self.critical } - fn label(&self) -> &str { + pub(crate) fn label(&self) -> &str { &self.label } - fn refresh(&mut self) { + pub(crate) fn refresh(&mut self) { unsafe { let event = match CFReleaser::new(IOHIDServiceClientCopyEvent( self.service.inner() as *const _, diff --git a/src/unix/apple/macos/component/x86.rs b/src/unix/apple/macos/component/x86.rs index 24659763a..282c875a5 100644 --- a/src/unix/apple/macos/component/x86.rs +++ b/src/unix/apple/macos/component/x86.rs @@ -1,7 +1,7 @@ // Take a look at the license at the top of the repository in the LICENSE file. use crate::sys::{ffi, macos::utils::IOReleaser}; -use crate::{ComponentExt, ComponentsExt}; +use crate::{Component, ComponentsExt}; use libc::{c_char, c_int, c_void}; @@ -22,7 +22,7 @@ pub(crate) struct ComponentFFI { input_structure: ffi::KeyData_t, val: ffi::Val_t, /// It is the `System::connection`. We need it to not require an extra argument - /// in `ComponentExt::refresh`. + /// in `ComponentInner::refresh`. connection: ffi::io_connect_t, } @@ -77,17 +77,16 @@ impl ComponentsExt for Components { for (id, v) in COMPONENTS_TEMPERATURE_IDS.iter() { if let Some(c) = - Component::new((*id).to_owned(), None, critical_temp, v, connection) + ComponentInner::new((*id).to_owned(), None, critical_temp, v, connection) { - self.components.push(c); + self.components.push(Component { inner: c }); } } } } } -#[doc = include_str!("../../../../../md_doc/component.md")] -pub struct Component { +pub(crate) struct ComponentInner { temperature: f32, max: f32, critical: Option, @@ -95,17 +94,17 @@ pub struct Component { ffi_part: ComponentFFI, } -impl Component { - /// Creates a new `Component` with the given information. +impl ComponentInner { + /// Creates a new `ComponentInner` with the given information. pub(crate) fn new( label: String, max: Option, critical: Option, key: &[i8], connection: ffi::io_connect_t, - ) -> Option { + ) -> Option { let ffi_part = ComponentFFI::new(key, connection)?; - ffi_part.temperature().map(|temperature| Component { + ffi_part.temperature().map(|temperature| Self { temperature, label, max: max.unwrap_or(temperature), @@ -113,26 +112,24 @@ impl Component { ffi_part, }) } -} -impl ComponentExt for Component { - fn temperature(&self) -> f32 { + pub(crate) fn temperature(&self) -> f32 { self.temperature } - fn max(&self) -> f32 { + pub(crate) fn max(&self) -> f32 { self.max } - fn critical(&self) -> Option { + pub(crate) fn critical(&self) -> Option { self.critical } - fn label(&self) -> &str { + pub(crate) fn label(&self) -> &str { &self.label } - fn refresh(&mut self) { + pub(crate) fn refresh(&mut self) { if let Some(temp) = self.ffi_part.temperature() { self.temperature = temp; if self.temperature > self.max { diff --git a/src/unix/apple/mod.rs b/src/unix/apple/mod.rs index bd5ca29f1..5329b7457 100644 --- a/src/unix/apple/mod.rs +++ b/src/unix/apple/mod.rs @@ -24,10 +24,12 @@ pub mod system; pub mod users; mod utils; -pub use self::component::{Component, Components}; +pub(crate) use self::component::ComponentInner; +pub use self::component::Components; pub use self::cpu::Cpu; pub(crate) use self::disk::DiskInner; pub use self::network::NetworkData; +pub(crate) use self::network::NetworksInner; pub(crate) use self::process::ProcessInner; pub(crate) use self::system::SystemInner; pub(crate) use crate::unix::users::get_users; diff --git a/src/unix/apple/network.rs b/src/unix/apple/network.rs index 3fe90d8b8..76d55c062 100644 --- a/src/unix/apple/network.rs +++ b/src/unix/apple/network.rs @@ -7,7 +7,7 @@ use std::ptr::null_mut; use crate::common::MacAddr; use crate::network::refresh_networks_addresses; -use crate::{NetworkExt, Networks, NetworksExt, NetworksIter}; +use crate::{NetworkExt, NetworksIter}; macro_rules! old_and_new { ($ty_:expr, $name:ident, $old:ident, $new_val:expr) => {{ @@ -16,7 +16,35 @@ macro_rules! old_and_new { }}; } -impl Networks { +pub(crate) struct NetworksInner { + pub(crate) interfaces: HashMap, +} + +impl NetworksInner { + pub(crate) fn new() -> Self { + Self { + interfaces: HashMap::new(), + } + } + + #[allow(clippy::needless_lifetimes)] + pub(crate) fn iter<'a>(&'a self) -> NetworksIter<'a> { + NetworksIter::new(self.interfaces.iter()) + } + + pub(crate) fn refresh_list(&mut self) { + for (_, data) in self.interfaces.iter_mut() { + data.updated = false; + } + self.update_networks(true); + self.interfaces.retain(|_, data| data.updated); + refresh_networks_addresses(&mut self.interfaces); + } + + pub(crate) fn refresh(&mut self) { + self.update_networks(false); + } + #[allow(unknown_lints)] #[allow(clippy::cast_ptr_alignment)] #[allow(clippy::uninit_vec)] @@ -147,32 +175,6 @@ impl Networks { } } -impl NetworksExt for Networks { - fn new() -> Self { - Self { - interfaces: HashMap::new(), - } - } - - #[allow(clippy::needless_lifetimes)] - fn iter<'a>(&'a self) -> NetworksIter<'a> { - NetworksIter::new(self.interfaces.iter()) - } - - fn refresh_list(&mut self) { - for (_, data) in self.interfaces.iter_mut() { - data.updated = false; - } - self.update_networks(true); - self.interfaces.retain(|_, data| data.updated); - refresh_networks_addresses(&mut self.interfaces); - } - - fn refresh(&mut self) { - self.update_networks(false); - } -} - #[doc = include_str!("../../../md_doc/network_data.md")] #[derive(PartialEq, Eq)] pub struct NetworkData { diff --git a/src/unix/freebsd/component.rs b/src/unix/freebsd/component.rs index e35e2fa4d..21fe47a0f 100644 --- a/src/unix/freebsd/component.rs +++ b/src/unix/freebsd/component.rs @@ -1,34 +1,33 @@ // Take a look at the license at the top of the repository in the LICENSE file. use super::utils::get_sys_value_by_name; -use crate::{ComponentExt, ComponentsExt}; +use crate::{Component, ComponentsExt}; -#[doc = include_str!("../../../md_doc/component.md")] -pub struct Component { +pub(crate) struct ComponentInner { id: Vec, label: String, temperature: f32, max: f32, } -impl ComponentExt for Component { - fn temperature(&self) -> f32 { +impl ComponentInner { + pub(crate) fn temperature(&self) -> f32 { self.temperature } - fn max(&self) -> f32 { + pub(crate) fn max(&self) -> f32 { self.max } - fn critical(&self) -> Option { + pub(crate) fn critical(&self) -> Option { None } - fn label(&self) -> &str { + pub(crate) fn label(&self) -> &str { &self.label } - fn refresh(&mut self) { + pub(crate) fn refresh(&mut self) { unsafe { if let Some(temperature) = refresh_component(&self.id) { self.temperature = temperature; @@ -80,10 +79,12 @@ impl ComponentsExt for Components { let id = format!("dev.cpu.{core}.temperature\0").as_bytes().to_vec(); if let Some(temperature) = refresh_component(&id) { self.components.push(Component { - id, - label: format!("CPU {}", core + 1), - temperature, - max: temperature, + inner: ComponentInner { + id, + label: format!("CPU {}", core + 1), + temperature, + max: temperature, + }, }); } } diff --git a/src/unix/freebsd/mod.rs b/src/unix/freebsd/mod.rs index 6a0de4bd7..a538ee8ae 100644 --- a/src/unix/freebsd/mod.rs +++ b/src/unix/freebsd/mod.rs @@ -8,10 +8,12 @@ pub mod process; pub mod system; mod utils; -pub use self::component::{Component, Components}; +pub(crate) use self::component::ComponentInner; +pub use self::component::Components; pub use self::cpu::Cpu; pub(crate) use self::disk::DiskInner; pub use self::network::NetworkData; +pub(crate) use self::network::NetworksInner; pub(crate) use self::process::ProcessInner; pub(crate) use self::system::SystemInner; pub(crate) use crate::unix::users::get_users; diff --git a/src/unix/freebsd/network.rs b/src/unix/freebsd/network.rs index 85549acdb..1a7081b0e 100644 --- a/src/unix/freebsd/network.rs +++ b/src/unix/freebsd/network.rs @@ -6,7 +6,7 @@ use std::mem::MaybeUninit; use super::utils; use crate::common::MacAddr; use crate::network::refresh_networks_addresses; -use crate::{NetworkExt, Networks, NetworksExt, NetworksIter}; +use crate::{NetworkExt, NetworksIter}; macro_rules! old_and_new { ($ty_:expr, $name:ident, $old:ident, $data:expr) => {{ @@ -15,18 +15,22 @@ macro_rules! old_and_new { }}; } -impl NetworksExt for Networks { - fn new() -> Self { +pub(crate) struct NetworksInner { + pub(crate) interfaces: HashMap, +} + +impl NetworksInner { + pub(crate) fn new() -> Self { Self { interfaces: HashMap::new(), } } - fn iter(&self) -> NetworksIter { + pub(crate) fn iter(&self) -> NetworksIter { NetworksIter::new(self.interfaces.iter()) } - fn refresh_list(&mut self) { + pub(crate) fn refresh_list(&mut self) { unsafe { self.refresh_interfaces(true); } @@ -35,14 +39,12 @@ impl NetworksExt for Networks { refresh_networks_addresses(&mut self.interfaces); } - fn refresh(&mut self) { + pub(crate) fn refresh(&mut self) { unsafe { self.refresh_interfaces(false); } } -} -impl Networks { unsafe fn refresh_interfaces(&mut self, refresh_all: bool) { let mut nb_interfaces: libc::c_int = 0; if !utils::get_sys_value( diff --git a/src/unix/linux/component.rs b/src/unix/linux/component.rs index 6b65f63c0..5efc68ce0 100644 --- a/src/unix/linux/component.rs +++ b/src/unix/linux/component.rs @@ -4,16 +4,15 @@ // // Values in /sys/class/hwmonN are `c_long` or `c_ulong` // transposed to rust we only read `u32` or `i32` values. -use crate::{ComponentExt, ComponentsExt}; +use crate::{Component, ComponentsExt}; use std::collections::HashMap; use std::fs::{read_dir, File}; use std::io::Read; use std::path::{Path, PathBuf}; -#[doc = include_str!("../../../md_doc/component.md")] #[derive(Default)] -pub struct Component { +pub(crate) struct ComponentInner { /// Optional associated device of a `Component`. device_model: Option, /// The chip name. @@ -175,7 +174,7 @@ impl From for TermalSensorType { /// Check given `item` dispatch to read the right `file` with the right parsing and store data in /// given `component`. `id` is provided for `label` creation. -fn fill_component(component: &mut Component, item: &str, folder: &Path, file: &str) { +fn fill_component(component: &mut ComponentInner, item: &str, folder: &Path, file: &str) { let hwmon_file = folder.join(file); match item { "type" => { @@ -209,7 +208,7 @@ fn fill_component(component: &mut Component, item: &str, folder: &Path, file: &s } } -impl Component { +impl ComponentInner { /// Read out `hwmon` info (hardware monitor) from `folder` /// to get values' path to be used on refresh as well as files containing `max`, /// `critical value` and `label`. Then we store everything into `components`. @@ -249,7 +248,10 @@ impl Component { let (id, item) = filename.split_once('_')?; let id = id.get(4..)?.parse::().ok()?; - let component = matchings.entry(id).or_default(); + let component = matchings.entry(id).or_insert_with(|| Component { + inner: ComponentInner::default(), + }); + let component = &mut component.inner; let name = get_file_line(&folder.join("name"), 16); component.name = name.unwrap_or_default(); let device_model = get_file_line(&folder.join("device/model"), 16); @@ -263,11 +265,11 @@ impl Component { // Problem: a lot of sensors don't have a label or a device model! ¯\_(ツ)_/¯ // So let's pretend we have a unique label! // See the table in `Component::label` documentation for the table detail. - c.label = c.format_label("temp", id); + c.inner.label = c.inner.format_label("temp", id); c }) // Remove components without `tempN_input` file termal. `Component` doesn't support this kind of sensors yet - .filter(|c| c.input_file.is_some()); + .filter(|c| c.inner.input_file.is_some()); components.extend(compo); Some(()) @@ -276,7 +278,7 @@ impl Component { /// Compute a label out of available information. /// See the table in `Component::label`'s documentation. fn format_label(&self, class: &str, id: u32) -> String { - let Component { + let ComponentInner { device_model, name, label, @@ -292,26 +294,24 @@ impl Component { (false, None) => format!("{name} {class}{id}"), } } -} -impl ComponentExt for Component { - fn temperature(&self) -> f32 { + pub(crate) fn temperature(&self) -> f32 { self.temperature.unwrap_or(f32::NAN) } - fn max(&self) -> f32 { + pub(crate) fn max(&self) -> f32 { self.max.unwrap_or(f32::NAN) } - fn critical(&self) -> Option { + pub(crate) fn critical(&self) -> Option { self.threshold_critical } - fn label(&self) -> &str { + pub(crate) fn label(&self) -> &str { &self.label } - fn refresh(&mut self) { + pub(crate) fn refresh(&mut self) { let current = self .input_file .as_ref() @@ -365,7 +365,7 @@ impl ComponentsExt for Components { { continue; } - Component::from_hwmon(&mut self.components, &entry); + ComponentInner::from_hwmon(&mut self.components, &entry); } } } diff --git a/src/unix/linux/mod.rs b/src/unix/linux/mod.rs index bd98b0fc8..890fceb23 100644 --- a/src/unix/linux/mod.rs +++ b/src/unix/linux/mod.rs @@ -8,10 +8,12 @@ pub mod process; pub mod system; pub(crate) mod utils; -pub use self::component::{Component, Components}; +pub(crate) use self::component::ComponentInner; +pub use self::component::Components; pub use self::cpu::Cpu; pub(crate) use self::disk::DiskInner; pub use self::network::NetworkData; +pub(crate) use self::network::NetworksInner; pub(crate) use self::process::ProcessInner; pub(crate) use self::system::SystemInner; pub(crate) use crate::unix::users::get_users; diff --git a/src/unix/linux/network.rs b/src/unix/linux/network.rs index d94b57935..127a62e3d 100644 --- a/src/unix/linux/network.rs +++ b/src/unix/linux/network.rs @@ -4,9 +4,9 @@ use std::io::Read; use std::path::Path; use std::{fs::File, u8}; -use crate::common::{MacAddr, Networks}; +use crate::common::MacAddr; use crate::network::refresh_networks_addresses; -use crate::{NetworkExt, NetworksExt, NetworksIter}; +use crate::{NetworkExt, NetworksIter}; use std::collections::{hash_map, HashMap}; macro_rules! old_and_new { @@ -107,18 +107,22 @@ fn refresh_networks_list_from_sysfs( } } -impl NetworksExt for Networks { - fn new() -> Self { +pub(crate) struct NetworksInner { + pub(crate) interfaces: HashMap, +} + +impl NetworksInner { + pub(crate) fn new() -> Self { Self { interfaces: HashMap::new(), } } - fn iter(&self) -> NetworksIter { + pub(crate) fn iter(&self) -> NetworksIter { NetworksIter::new(self.interfaces.iter()) } - fn refresh(&mut self) { + pub(crate) fn refresh(&mut self) { let mut v = vec![0; 30]; for (interface_name, data) in self.interfaces.iter_mut() { @@ -126,7 +130,7 @@ impl NetworksExt for Networks { } } - fn refresh_list(&mut self) { + pub(crate) fn refresh_list(&mut self) { refresh_networks_list_from_sysfs(&mut self.interfaces, Path::new("/sys/class/net/")); refresh_networks_addresses(&mut self.interfaces); } diff --git a/src/unknown/component.rs b/src/unknown/component.rs index 07a71a1af..04aa940b4 100644 --- a/src/unknown/component.rs +++ b/src/unknown/component.rs @@ -1,28 +1,27 @@ // Take a look at the license at the top of the repository in the LICENSE file. -use crate::{ComponentExt, ComponentsExt}; +use crate::{Component, ComponentsExt}; -#[doc = include_str!("../../md_doc/component.md")] -pub struct Component {} +pub(crate) struct ComponentInner; -impl ComponentExt for Component { - fn temperature(&self) -> f32 { +impl ComponentInner { + pub(crate) fn temperature(&self) -> f32 { 0.0 } - fn max(&self) -> f32 { + pub(crate) fn max(&self) -> f32 { 0.0 } - fn critical(&self) -> Option { + pub(crate) fn critical(&self) -> Option { None } - fn label(&self) -> &str { + pub(crate) fn label(&self) -> &str { "" } - fn refresh(&mut self) {} + pub(crate) fn refresh(&mut self) {} } #[doc = include_str!("../../md_doc/components.md")] diff --git a/src/unknown/mod.rs b/src/unknown/mod.rs index 53bb589f7..038ff8e73 100644 --- a/src/unknown/mod.rs +++ b/src/unknown/mod.rs @@ -8,10 +8,12 @@ pub mod process; pub mod system; pub mod users; -pub use self::component::{Component, Components}; +pub(crate) use self::component::ComponentInner; +pub use self::component::Components; pub use self::cpu::Cpu; pub(crate) use self::disk::{DiskInner, DisksInner}; pub use self::network::NetworkData; +pub(crate) use self::network::NetworksInner; pub(crate) use self::process::ProcessInner; pub(crate) use self::system::SystemInner; pub(crate) use self::users::get_users; diff --git a/src/unknown/network.rs b/src/unknown/network.rs index 1dbd8c06a..53f050419 100644 --- a/src/unknown/network.rs +++ b/src/unknown/network.rs @@ -1,24 +1,28 @@ // Take a look at the license at the top of the repository in the LICENSE file. use crate::common::MacAddr; -use crate::{NetworkExt, Networks, NetworksExt, NetworksIter}; +use crate::{NetworkExt, NetworksIter}; use std::collections::HashMap; -impl NetworksExt for Networks { - fn new() -> Self { +pub(crate) struct NetworksInner { + pub(crate) interfaces: HashMap, +} + +impl NetworksInner { + pub(crate) fn new() -> Self { Self { interfaces: HashMap::new(), } } - fn iter(&self) -> NetworksIter { + pub(crate) fn iter(&self) -> NetworksIter { NetworksIter::new(self.interfaces.iter()) } - fn refresh_list(&mut self) {} + pub(crate) fn refresh_list(&mut self) {} - fn refresh(&mut self) {} + pub(crate) fn refresh(&mut self) {} } #[doc = include_str!("../../md_doc/network_data.md")] diff --git a/src/windows/component.rs b/src/windows/component.rs index 296d6266f..b0223cea3 100644 --- a/src/windows/component.rs +++ b/src/windows/component.rs @@ -1,6 +1,6 @@ // Take a look at the license at the top of the repository in the LICENSE file. -use crate::{ComponentExt, ComponentsExt}; +use crate::{Component, ComponentsExt}; use windows::core::w; use windows::Win32::Foundation::{SysAllocString, SysFreeString}; @@ -17,8 +17,7 @@ use windows::Win32::System::Wmi::{ WBEM_FLAG_NONSYSTEM_ONLY, WBEM_FLAG_RETURN_IMMEDIATELY, WBEM_INFINITE, }; -#[doc = include_str!("../../md_doc/component.md")] -pub struct Component { +pub(crate) struct ComponentInner { temperature: f32, max: f32, critical: Option, @@ -26,9 +25,9 @@ pub struct Component { connection: Option, } -impl Component { - /// Creates a new `Component` with the given information. - fn new() -> Option { +impl ComponentInner { + /// Creates a new `ComponentInner` with the given information. + fn new() -> Option { let mut c = Connection::new() .and_then(|x| x.initialize_security()) .and_then(|x| x.create_instance()) @@ -37,7 +36,7 @@ impl Component { .and_then(|x| x.exec_query())?; c.temperature(true) - .map(|(temperature, critical)| Component { + .map(|(temperature, critical)| ComponentInner { temperature, label: "Computer".to_owned(), max: temperature, @@ -45,26 +44,24 @@ impl Component { connection: Some(c), }) } -} -impl ComponentExt for Component { - fn temperature(&self) -> f32 { + pub(crate) fn temperature(&self) -> f32 { self.temperature } - fn max(&self) -> f32 { + pub(crate) fn max(&self) -> f32 { self.max } - fn critical(&self) -> Option { + pub(crate) fn critical(&self) -> Option { self.critical } - fn label(&self) -> &str { + pub(crate) fn label(&self) -> &str { &self.label } - fn refresh(&mut self) { + pub(crate) fn refresh(&mut self) { if self.connection.is_none() { self.connection = Connection::new() .and_then(|x| x.initialize_security()) @@ -109,8 +106,8 @@ impl ComponentsExt for Components { } fn refresh_list(&mut self) { - self.components = match Component::new() { - Some(c) => vec![c], + self.components = match ComponentInner::new() { + Some(c) => vec![Component { inner: c }], None => Vec::new(), }; } diff --git a/src/windows/mod.rs b/src/windows/mod.rs index c525e5762..62d2e4909 100644 --- a/src/windows/mod.rs +++ b/src/windows/mod.rs @@ -12,10 +12,12 @@ mod tools; mod users; mod utils; -pub use self::component::{Component, Components}; +pub(crate) use self::component::ComponentInner; +pub use self::component::Components; pub use self::cpu::Cpu; pub(crate) use self::disk::{DiskInner, DisksInner}; pub use self::network::NetworkData; +pub(crate) use self::network::NetworksInner; pub(crate) use self::process::ProcessInner; pub use self::sid::Sid; pub(crate) use self::system::SystemInner; diff --git a/src/windows/network.rs b/src/windows/network.rs index aaed06546..bf54e4cf2 100644 --- a/src/windows/network.rs +++ b/src/windows/network.rs @@ -2,7 +2,7 @@ use crate::common::MacAddr; use crate::network::refresh_networks_addresses; -use crate::{NetworkExt, Networks, NetworksExt, NetworksIter}; +use crate::{NetworkExt, NetworksIter}; use std::collections::{hash_map, HashMap}; @@ -18,19 +18,23 @@ macro_rules! old_and_new { }}; } -impl NetworksExt for Networks { - fn new() -> Self { +pub(crate) struct NetworksInner { + pub(crate) interfaces: HashMap, +} + +impl NetworksInner { + pub(crate) fn new() -> Self { Self { interfaces: HashMap::new(), } } #[allow(clippy::needless_lifetimes)] - fn iter<'a>(&'a self) -> NetworksIter<'a> { + pub(crate) fn iter<'a>(&'a self) -> NetworksIter<'a> { NetworksIter::new(self.interfaces.iter()) } - fn refresh_list(&mut self) { + pub(crate) fn refresh_list(&mut self) { let mut table: *mut MIB_IF_TABLE2 = std::ptr::null_mut(); unsafe { @@ -145,7 +149,7 @@ impl NetworksExt for Networks { refresh_networks_addresses(&mut self.interfaces); } - fn refresh(&mut self) { + pub(crate) fn refresh(&mut self) { let entry = std::mem::MaybeUninit::::zeroed(); unsafe { diff --git a/tests/network.rs b/tests/network.rs index 0b01ac9cd..321bd981b 100644 --- a/tests/network.rs +++ b/tests/network.rs @@ -4,7 +4,7 @@ #[test] fn test_networks() { - use sysinfo::{Networks, NetworksExt}; + use sysinfo::Networks; if sysinfo::IS_SUPPORTED { let mut n = Networks::new();