From df28dca77a2cd0596884d250764daef70bb0aa6a Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 17 Sep 2023 16:59:46 +0200 Subject: [PATCH 1/3] Extract `Components` from `System` --- README.md | 19 +-- benches/basic.rs | 12 +- examples/simple.c | 11 +- examples/simple.rs | 27 ++-- md_doc/components.md | 11 ++ src/c_interface.rs | 14 -- src/common.rs | 37 ++--- src/debug.rs | 18 ++- src/lib.rs | 6 +- src/serde.rs | 10 +- src/sysinfo.h | 1 - src/traits.rs | 197 ++++++++++++++++---------- src/unix/apple/app_store/component.rs | 27 +++- src/unix/apple/macos/component/arm.rs | 33 +++-- src/unix/apple/macos/component/x86.rs | 29 ++-- src/unix/apple/mod.rs | 2 +- src/unix/apple/system.rs | 36 ----- src/unix/freebsd/component.rs | 51 +++++-- src/unix/freebsd/cpu.rs | 32 +++-- src/unix/freebsd/mod.rs | 2 +- src/unix/freebsd/system.rs | 19 +-- src/unix/linux/component.rs | 54 ++++--- src/unix/linux/mod.rs | 2 +- src/unix/linux/system.rs | 15 -- src/unknown/component.rs | 27 +++- src/unknown/mod.rs | 2 +- src/unknown/system.rs | 12 +- src/windows/component.rs | 31 +++- src/windows/mod.rs | 2 +- src/windows/system.rs | 15 -- 30 files changed, 437 insertions(+), 317 deletions(-) create mode 100644 md_doc/components.md diff --git a/README.md b/README.md index 1efde0cac..fb010476f 100644 --- a/README.md +++ b/README.md @@ -35,8 +35,8 @@ Otherwise, here is a little code sample: ```rust use sysinfo::{ - Disks, DisksExt, NetworkExt, Networks, NetworksExt, ProcessExt, System, - SystemExt, + Components, ComponentsExt, Disks, DisksExt, NetworkExt, Networks, + NetworksExt, ProcessExt, System, SystemExt, }; // Please note that we use "new_all" to ensure that all list of @@ -65,7 +65,7 @@ println!("NB CPUs: {}", sys.cpus().len()); // Display processes ID, name na disk usage: for (pid, process) in sys.processes() { - println!("[{}] {} {:?}", pid, process.name(), process.disk_usage()); + println!("[{pid}] {} {:?}", process.name(), process.disk_usage()); } // We display all disks' information: @@ -73,8 +73,8 @@ println!("=> disks:"); let mut disks = Disks::new(); // We refresh the disk list. disks.refresh_list(); -for disk in disks.disks() { - println!("{:?}", disk); +for disk in disks.iter() { + println!("{disk:?}"); } // Network interfaces name, data received and data transmitted: @@ -83,13 +83,16 @@ let mut networks = Networks::new(); networks.refresh_list(); println!("=> networks:"); for (interface_name, data) in &networks { - println!("{}: {}/{} B", interface_name, data.received(), data.transmitted()); + println!("{interface_name}: {}/{} B", data.received(), data.transmitted()); } // Components temperature: +let mut components = Components::new(); +// We refresh the component list. +components.refresh_list(); println!("=> components:"); -for component in sys.components() { - println!("{:?}", component); +for component in components.iter() { + println!("{component:?}"); } ``` diff --git a/benches/basic.rs b/benches/basic.rs index 21517ea72..2b89975c8 100644 --- a/benches/basic.rs +++ b/benches/basic.rs @@ -3,7 +3,7 @@ extern crate test; use sysinfo::get_current_pid; -use sysinfo::{DiskExt, DisksExt, NetworksExt, SystemExt}; +use sysinfo::{ComponentsExt, DiskExt, DisksExt, NetworksExt, SystemExt}; #[bench] fn bench_new(b: &mut test::Bencher) { @@ -140,19 +140,21 @@ fn bench_refresh_cpu(b: &mut test::Bencher) { #[bench] fn bench_refresh_components(b: &mut test::Bencher) { - let mut s = sysinfo::System::new_all(); + let mut c = sysinfo::Components::new(); + c.refresh_list(); b.iter(move || { - s.refresh_components(); + c.refresh(); }); } #[bench] fn bench_refresh_components_list(b: &mut test::Bencher) { - let mut s = sysinfo::System::new_all(); + let mut c = sysinfo::Components::new(); + c.refresh_list(); b.iter(move || { - s.refresh_components_list(); + c.refresh_list(); }); } diff --git a/examples/simple.c b/examples/simple.c index 87c4c750f..0dc994869 100644 --- a/examples/simple.c +++ b/examples/simple.c @@ -58,15 +58,19 @@ bool process_loop(pid_t pid, CProcess process, void *data) { int main() { CSystem system = sysinfo_init(); + CNetworks networks = sysinfo_networks_init(); + sysinfo_refresh_all(system); + sysinfo_networks_refresh_list(networks); + printf("total memory: %ld\n", sysinfo_total_memory(system)); printf("free memory: %ld\n", sysinfo_free_memory(system)); printf("used memory: %ld\n", sysinfo_used_memory(system)); printf("total swap: %ld\n", sysinfo_total_swap(system)); printf("free swap: %ld\n", sysinfo_free_swap(system)); printf("used swap: %ld\n", sysinfo_used_swap(system)); - printf("networks received: %ld\n", sysinfo_networks_received(system)); - printf("networks transmitted: %ld\n", sysinfo_networks_transmitted(system)); + printf("networks received: %ld\n", sysinfo_networks_received(networks)); + printf("networks transmitted: %ld\n", sysinfo_networks_transmitted(networks)); unsigned int len = 0, i = 0; float *procs = NULL; sysinfo_cpus_usage(system, &len, &procs); @@ -80,7 +84,8 @@ int main() { i = 0; printf("For a total of %ld processes.\n", sysinfo_processes(system, process_loop, &i)); check_tasks(system); - // we can now free the CSystem object. + // we can now free the CSystem and the CNetworks objects. sysinfo_destroy(system); + sysinfo_networks_destroy(networks); return 0; } diff --git a/examples/simple.rs b/examples/simple.rs index deae70d95..9c4338c23 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::{ - CpuExt, Disks, DisksExt, NetworkExt, Networks, NetworksExt, Pid, ProcessExt, Signal, System, - SystemExt, UserExt, + Components, ComponentsExt, CpuExt, Disks, DisksExt, NetworkExt, Networks, NetworksExt, Pid, + ProcessExt, Signal, System, SystemExt, UserExt, }; const signals: &[Signal] = &[ @@ -147,6 +147,7 @@ fn interpret_input( sys: &mut System, networks: &mut Networks, disks: &mut Disks, + components: &mut Components, ) -> bool { match input.trim() { "help" => print_help(), @@ -277,7 +278,7 @@ fn interpret_input( } } "temperature" => { - for component in sys.components() { + for component in components.iter() { writeln!(&mut io::stdout(), "{component:?}"); } } @@ -438,11 +439,13 @@ fn interpret_input( fn main() { println!("Getting system information..."); - let mut t = System::new_all(); - let mut n = Networks::new(); - let mut d = Disks::new(); - n.refresh_list(); - d.refresh_list(); + let mut system = System::new_all(); + let mut networks = Networks::new(); + let mut disks = Disks::new(); + let mut components = Components::new(); + networks.refresh_list(); + disks.refresh_list(); + components.refresh_list(); println!("Done."); let t_stin = io::stdin(); let mut stin = t_stin.lock(); @@ -464,6 +467,12 @@ fn main() { if (&input as &str).ends_with('\n') { input.pop(); } - done = interpret_input(input.as_ref(), &mut t, &mut n, &mut d); + done = interpret_input( + input.as_ref(), + &mut system, + &mut networks, + &mut disks, + &mut components, + ); } } diff --git a/md_doc/components.md b/md_doc/components.md new file mode 100644 index 000000000..7ab71b67b --- /dev/null +++ b/md_doc/components.md @@ -0,0 +1,11 @@ +Component interfaces. + +```no_run +use sysinfo::{Components, ComponentsExt}; + +let mut components = Components::new(); +components.refresh_list(); +for component in components.components() { + println!("{component:?}"); +} +``` \ No newline at end of file diff --git a/src/c_interface.rs b/src/c_interface.rs index 621258397..2461bcfb4 100644 --- a/src/c_interface.rs +++ b/src/c_interface.rs @@ -79,20 +79,6 @@ pub extern "C" fn sysinfo_refresh_cpu(system: CSystem) { } } -/// Equivalent of [`System::refresh_components()`][crate::System#method.refresh_temperatures]. -#[no_mangle] -pub extern "C" fn sysinfo_refresh_components(system: CSystem) { - assert!(!system.is_null()); - unsafe { - let mut system: Box = Box::from_raw(system as *mut System); - { - let system: &mut System = system.borrow_mut(); - system.refresh_components(); - } - Box::into_raw(system); - } -} - /// Equivalent of [`System::refresh_all()`][crate::System#method.refresh_all]. #[no_mangle] pub extern "C" fn sysinfo_refresh_all(system: CSystem) { diff --git a/src/common.rs b/src/common.rs index d11a365a4..fa19d5209 100644 --- a/src/common.rs +++ b/src/common.rs @@ -1,6 +1,8 @@ // Take a look at the license at the top of the repository in the LICENSE file. -use crate::{Disk, GroupExt, NetworkData, NetworksExt, User, UserExt}; +use crate::{ + Component, Components, ComponentsExt, Disk, GroupExt, NetworkData, NetworksExt, User, UserExt, +}; use std::cmp::Ordering; use std::collections::HashMap; @@ -367,8 +369,6 @@ pub struct RefreshKind { processes: Option, memory: bool, cpu: Option, - components: bool, - components_list: bool, users_list: bool, } @@ -383,8 +383,6 @@ impl RefreshKind { /// assert_eq!(r.processes().is_some(), false); /// assert_eq!(r.memory(), false); /// assert_eq!(r.cpu().is_some(), false); - /// assert_eq!(r.components(), false); - /// assert_eq!(r.components_list(), false); /// assert_eq!(r.users_list(), false); /// ``` pub fn new() -> Self { @@ -401,8 +399,6 @@ impl RefreshKind { /// assert_eq!(r.processes().is_some(), true); /// assert_eq!(r.memory(), true); /// assert_eq!(r.cpu().is_some(), true); - /// assert_eq!(r.components(), true); - /// assert_eq!(r.components_list(), true); /// assert_eq!(r.users_list(), true); /// ``` pub fn everything() -> Self { @@ -410,8 +406,6 @@ impl RefreshKind { processes: Some(ProcessRefreshKind::everything()), memory: true, cpu: Some(CpuRefreshKind::everything()), - components: true, - components_list: true, users_list: true, } } @@ -425,13 +419,6 @@ impl RefreshKind { ); impl_get_set!(RefreshKind, memory, with_memory, without_memory); impl_get_set!(RefreshKind, cpu, with_cpu, without_cpu, CpuRefreshKind); - impl_get_set!(RefreshKind, components, with_components, without_components); - impl_get_set!( - RefreshKind, - components_list, - with_components_list, - without_components_list - ); impl_get_set!(RefreshKind, users_list, with_users_list, without_users_list); } @@ -490,7 +477,7 @@ impl<'a> Iterator for NetworksIter<'a> { } } -/// Disks interfaces. +/// Disk interfaces. /// /// ```no_run /// use sysinfo::{Disks, DisksExt}; @@ -498,7 +485,7 @@ impl<'a> Iterator for NetworksIter<'a> { /// let mut disks = Disks::new(); /// disks.refresh_list(); /// for disk in disks.disks() { -/// println!("{:?}", disk); +/// println!("{disk:?}"); /// } /// ``` pub struct Disks { @@ -542,6 +529,20 @@ pub enum DiskKind { Unknown(isize), } +impl std::ops::Deref for Components { + type Target = [Component]; + + fn deref(&self) -> &Self::Target { + self.components() + } +} + +impl std::ops::DerefMut for Components { + fn deref_mut(&mut self) -> &mut Self::Target { + self.components_mut() + } +} + /// An enum representing signals on UNIX-like systems. /// /// On non-unix systems, this enum is mostly useless and is only there to keep coherency between diff --git a/src/debug.rs b/src/debug.rs index ad4353885..fcc254264 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, Cpu, CpuExt, Disk, DiskExt, Disks, NetworkData, NetworkExt, Networks, - NetworksExt, Process, ProcessExt, System, SystemExt, User, UserExt, + Component, ComponentExt, Components, Cpu, CpuExt, Disk, DiskExt, Disks, NetworkData, + NetworkExt, Networks, NetworksExt, Process, ProcessExt, System, SystemExt, User, UserExt, }; use std::fmt; @@ -30,7 +30,6 @@ impl fmt::Debug for System { .field("free swap", &self.free_swap()) .field("nb CPUs", &self.cpus().len()) .field("nb processes", &self.processes().len()) - .field("nb components", &self.components().len()) .finish() } } @@ -76,6 +75,19 @@ impl fmt::Debug for Process { } } +impl fmt::Debug for Components { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "Components {{ {} }}", + self.iter() + .map(|x| format!("{x:?}")) + .collect::>() + .join(", ") + ) + } +} + impl fmt::Debug for Component { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { if let Some(critical) = self.critical() { diff --git a/src/lib.rs b/src/lib.rs index f86d9a650..d30ecc72d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -56,10 +56,10 @@ pub use common::{ Networks, NetworksIter, Pid, PidExt, ProcessRefreshKind, ProcessStatus, RefreshKind, Signal, Uid, }; -pub use sys::{Component, Cpu, Disk, NetworkData, Process, System, User}; +pub use sys::{Component, Components, Cpu, Disk, NetworkData, Process, System, User}; pub use traits::{ - ComponentExt, CpuExt, DiskExt, DisksExt, GroupExt, NetworkExt, NetworksExt, ProcessExt, - SystemExt, UserExt, + ComponentExt, ComponentsExt, CpuExt, DiskExt, DisksExt, GroupExt, NetworkExt, NetworksExt, + ProcessExt, SystemExt, UserExt, }; #[cfg(feature = "c-interface")] diff --git a/src/serde.rs b/src/serde.rs index 651325f44..9da7e35d0 100644 --- a/src/serde.rs +++ b/src/serde.rs @@ -111,7 +111,6 @@ impl serde::Serialize for crate::System { state.serialize_field("free_swap", &self.free_swap())?; state.serialize_field("used_swap", &self.used_swap())?; - state.serialize_field("components", &self.components())?; state.serialize_field("users", &self.users())?; state.serialize_field("uptime", &self.uptime())?; @@ -146,6 +145,15 @@ impl Serialize for crate::Disks { } } +impl Serialize for crate::Components { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + serializer.collect_seq(self.iter()) + } +} + impl Serialize for Signal { fn serialize(&self, serializer: S) -> Result where diff --git a/src/sysinfo.h b/src/sysinfo.h index 9b638af54..b20d4b4d2 100644 --- a/src/sysinfo.h +++ b/src/sysinfo.h @@ -19,7 +19,6 @@ void sysinfo_networks_destroy(CNetworks networks); void sysinfo_refresh_system(CSystem system); void sysinfo_refresh_memory(CSystem system); void sysinfo_refresh_cpu(CSystem system); -void sysinfo_refresh_components(CSystem system); void sysinfo_refresh_all(CSystem system); void sysinfo_refresh_processes(CSystem system); #ifdef __linux__ diff --git a/src/traits.rs b/src/traits.rs index b9f59744c..af90f9e35 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -679,11 +679,6 @@ pub trait SystemExt: Sized + Debug + Default + Send + Sync { if let Some(kind) = refreshes.cpu() { self.refresh_cpu_specifics(kind); } - if refreshes.components_list() { - self.refresh_components_list(); - } else if refreshes.components() { - self.refresh_components(); - } if let Some(kind) = refreshes.processes() { self.refresh_processes_specifics(kind); } @@ -708,11 +703,10 @@ pub trait SystemExt: Sized + Debug + Default + Send + Sync { /// Refreshes system information (RAM, swap, CPU usage and components' temperature). /// /// If you want some more specific refreshes, you might be interested into looking at - /// [`refresh_memory`], [`refresh_cpu`] and [`refresh_components`]. + /// [`refresh_memory`] and [`refresh_cpu`]. /// /// [`refresh_memory`]: SystemExt::refresh_memory /// [`refresh_cpu`]: SystemExt::refresh_memory - /// [`refresh_components`]: SystemExt::refresh_components /// /// ```no_run /// use sysinfo::{System, SystemExt}; @@ -723,7 +717,6 @@ pub trait SystemExt: Sized + Debug + Default + Send + Sync { fn refresh_system(&mut self) { self.refresh_memory(); self.refresh_cpu_usage(); - self.refresh_components(); } /// Refreshes RAM and SWAP usage. @@ -801,33 +794,6 @@ pub trait SystemExt: Sized + Debug + Default + Send + Sync { /// ``` fn refresh_cpu_specifics(&mut self, refresh_kind: CpuRefreshKind); - /// Refreshes components' temperature. - /// - /// ```no_run - /// use sysinfo::{System, SystemExt}; - /// - /// let mut s = System::new_all(); - /// s.refresh_components(); - /// ``` - fn refresh_components(&mut self) { - for component in self.components_mut() { - component.refresh(); - } - } - - /// Refreshes components list. - /// - /// ```no_run - /// use sysinfo::{System, SystemExt}; - /// - /// let mut s = System::new(); - /// assert!(s.components().is_empty()); - /// - /// s.refresh_components_list(); - /// assert!(!s.components().is_empty()); - /// ``` - fn refresh_components_list(&mut self); - /// Gets all processes and updates their information. /// /// It does the same as `system.refresh_processes_specifics(ProcessRefreshKind::everything())`. @@ -1111,30 +1077,6 @@ pub trait SystemExt: Sized + Debug + Default + Send + Sync { /// ``` fn used_swap(&self) -> u64; - /// Returns the components list. - /// - /// ```no_run - /// use sysinfo::{ComponentExt, System, SystemExt}; - /// - /// let s = System::new_all(); - /// for component in s.components() { - /// println!("{}: {}°C", component.label(), component.temperature()); - /// } - /// ``` - fn components(&self) -> &[Component]; - - /// Returns a mutable components list. - /// - /// ```no_run - /// use sysinfo::{ComponentExt, System, SystemExt}; - /// - /// let mut s = System::new_all(); - /// for component in s.components_mut() { - /// component.refresh(); - /// } - /// ``` - fn components_mut(&mut self) -> &mut [Component]; - /// Returns the users list. /// /// ```no_run @@ -1521,7 +1463,7 @@ pub trait NetworksExt: Debug { /// Interacting with disks. pub trait DisksExt: Debug { - /// Creates a new `Disks` type. + /// Creates a new [`Disks`][crate::Disks] type. /// /// ```no_run /// use sysinfo::{Disks, DisksExt}; @@ -1633,10 +1575,11 @@ pub trait ComponentExt: Debug { /// Returns the temperature of the component (in celsius degree). /// /// ```no_run - /// use sysinfo::{ComponentExt, System, SystemExt}; + /// use sysinfo::{ComponentExt, Components, ComponentsExt}; /// - /// let s = System::new_all(); - /// for component in s.components() { + /// let mut components = Components::new(); + /// components.refresh_list(); + /// for component in components.iter() { /// println!("{}°C", component.temperature()); /// } /// ``` @@ -1652,10 +1595,11 @@ pub trait ComponentExt: Debug { /// `max` value will be updated on refresh. /// /// ```no_run - /// use sysinfo::{ComponentExt, System, SystemExt}; + /// use sysinfo::{ComponentExt, Components, ComponentsExt}; /// - /// let s = System::new_all(); - /// for component in s.components() { + /// let mut components = Components::new(); + /// components.refresh_list(); + /// for component in components.iter() { /// println!("{}°C", component.max()); /// } /// ``` @@ -1669,10 +1613,11 @@ pub trait ComponentExt: Debug { /// Returns the highest temperature before the component halts (in celsius degree). /// /// ```no_run - /// use sysinfo::{ComponentExt, System, SystemExt}; + /// use sysinfo::{ComponentExt, Components, ComponentsExt}; /// - /// let s = System::new_all(); - /// for component in s.components() { + /// let mut components = Components::new(); + /// components.refresh_list(); + /// for component in components.iter() { /// println!("{:?}°C", component.critical()); /// } /// ``` @@ -1685,10 +1630,11 @@ pub trait ComponentExt: Debug { /// Returns the label of the component. /// /// ```no_run - /// use sysinfo::{ComponentExt, System, SystemExt}; + /// use sysinfo::{ComponentExt, Components, ComponentsExt}; /// - /// let s = System::new_all(); - /// for component in s.components() { + /// let mut components = Components::new(); + /// components.refresh_list(); + /// for component in components.iter() { /// println!("{}", component.label()); /// } /// ``` @@ -1710,16 +1656,117 @@ pub trait ComponentExt: Debug { /// Refreshes component. /// /// ```no_run - /// use sysinfo::{ComponentExt, System, SystemExt}; + /// use sysinfo::{ComponentExt, Components, ComponentsExt}; /// - /// let mut s = System::new_all(); - /// for component in s.components_mut() { + /// let mut components = Components::new(); + /// components.refresh_list(); + /// for component in components.iter_mut() { /// component.refresh(); /// } /// ``` fn refresh(&mut self); } +/// Interacting with components. +pub trait ComponentsExt: Debug { + /// Creates a new [`Components`][crate::Components] type. + /// + /// ```no_run + /// use sysinfo::{Components, ComponentsExt}; + /// + /// let mut components = Components::new(); + /// components.refresh_list(); + /// for component in components.iter() { + /// eprintln!("{component:?}"); + /// } + /// ``` + fn new() -> Self; + + /// Returns the components list. + /// + /// ```no_run + /// use sysinfo::{Components, ComponentsExt}; + /// + /// let mut components = Components::new(); + /// components.refresh_list(); + /// for component in components.components() { + /// eprintln!("{component:?}"); + /// } + /// ``` + fn components(&self) -> &[Component]; + + /// Returns the components list. + /// + /// ```no_run + /// use sysinfo::{ComponentExt, Components, ComponentsExt}; + /// + /// let mut components = Components::new(); + /// components.refresh_list(); + /// for component in components.components_mut() { + /// component.refresh(); + /// eprintln!("{component:?}"); + /// } + /// ``` + fn components_mut(&mut self) -> &mut [Component]; + + /// Sort the components list with the provided callback. + /// + /// Internally, it is using the [`slice::sort_unstable_by`] function, so please refer to it + /// for implementation details. + /// + /// You can do the same without this method by calling: + /// + /// ```no_run + /// use sysinfo::{ComponentExt, Components, ComponentsExt}; + /// + /// let mut components = Components::new(); + /// components.refresh_list(); + /// components.sort_by(|component1, component2| { + /// component2.label().partial_cmp(component2.label()).unwrap() + /// }); + /// ``` + /// + /// ⚠️ If you use [`ComponentsExt::refresh_list`], you will need to call this method to sort the + /// components again. + fn sort_by(&mut self, compare: F) + where + F: FnMut(&Component, &Component) -> std::cmp::Ordering, + { + self.components_mut().sort_unstable_by(compare); + } + + /// Refreshes the listed components' information. + /// + /// ⚠️ If you didn't call [`ComponentsExt::refresh_list`] beforehand, this method will do nothing as + /// the component list will be empty. + /// + /// ```no_run + /// use sysinfo::{Components, ComponentsExt}; + /// + /// let mut components = Components::new(); + /// components.refresh_list(); + /// // We get the component list. + /// components.refresh_list(); + /// // We wait some time...? + /// components.refresh(); + /// ``` + fn refresh(&mut self) { + for component in self.components_mut() { + component.refresh(); + } + } + + /// The component list will be emptied then completely recomputed. + /// + /// ```no_run + /// use sysinfo::{Components, ComponentsExt}; + /// + /// let mut components = Components::new(); + /// components.refresh_list(); + /// ``` + fn refresh_list(&mut self); +} + /// Getting information for a user. /// /// It is returned from [`SystemExt::users`]. diff --git a/src/unix/apple/app_store/component.rs b/src/unix/apple/app_store/component.rs index 339914b7c..541a05696 100644 --- a/src/unix/apple/app_store/component.rs +++ b/src/unix/apple/app_store/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; +use crate::{ComponentExt, ComponentsExt}; #[doc = include_str!("../../../../md_doc/component.md")] pub struct Component {} @@ -24,3 +24,28 @@ impl ComponentExt for Component { fn refresh(&mut self) {} } + +#[doc = include_str!("../../../../md_doc/components.md")] +pub struct Components { + components: Vec, +} + +impl ComponentsExt for Components { + fn new() -> Self { + Self { + components: Vec::new(), + } + } + + fn components(&self) -> &[Component] { + &self.components + } + + fn components_mut(&mut self) -> &mut [Component] { + &mut self.components + } + + fn refresh_list(&mut self) { + // Doesn't do anything. + } +} diff --git a/src/unix/apple/macos/component/arm.rs b/src/unix/apple/macos/component/arm.rs index 4d46748a7..cb14ba329 100644 --- a/src/unix/apple/macos/component/arm.rs +++ b/src/unix/apple/macos/component/arm.rs @@ -16,23 +16,32 @@ use crate::sys::inner::ffi::{ HID_DEVICE_PROPERTY_PRODUCT, }; use crate::sys::utils::CFReleaser; -use crate::ComponentExt; +use crate::{ComponentExt, ComponentsExt}; -pub(crate) struct Components { - pub inner: Vec, +#[doc = include_str!("../../../../../md_doc/components.md")] +pub struct Components { + pub(crate) components: Vec, client: Option>, } -impl Components { - pub(crate) fn new() -> Self { +impl ComponentsExt for Components { + fn new() -> Self { Self { - inner: vec![], + components: vec![], client: None, } } - pub(crate) fn refresh(&mut self) { - self.inner.clear(); + fn components(&self) -> &[Component] { + &self.components + } + + fn components_mut(&mut self) -> &mut [Component] { + &mut self.components + } + + fn refresh_list(&mut self) { + self.components.clear(); unsafe { let matches = match CFReleaser::new(matching( @@ -100,15 +109,12 @@ impl Components { let mut component = Component::new(name_str, None, None, service); component.refresh(); - self.inner.push(component); + self.components.push(component); } } } } -unsafe impl Send for Components {} -unsafe impl Sync for Components {} - #[doc = include_str!("../../../../../md_doc/component.md")] pub struct Component { service: CFReleaser<__IOHIDServiceClient>, @@ -135,9 +141,6 @@ impl Component { } } -unsafe impl Send for Component {} -unsafe impl Sync for Component {} - impl ComponentExt for Component { fn temperature(&self) -> f32 { self.temperature diff --git a/src/unix/apple/macos/component/x86.rs b/src/unix/apple/macos/component/x86.rs index aebb84f41..c7e529644 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; +use crate::{ComponentExt, ComponentsExt}; use libc::{c_char, c_int, c_void}; @@ -44,24 +44,33 @@ impl ComponentFFI { } } -/// Used to get CPU information, not supported on iOS, or inside the default macOS sandbox. -pub(crate) struct Components { - pub inner: Vec, +// Used to get CPU information, not supported on iOS, or inside the default macOS sandbox. +#[doc = include_str!("../../../../../md_doc/components.md")] +pub struct Components { + pub components: Vec, connection: Option, } -impl Components { - pub(crate) fn new() -> Self { +impl ComponentsExt for Components { + fn new() -> Self { Self { - inner: Vec::with_capacity(2), + components: Vec::with_capacity(2), connection: IoService::new_connection(), } } - pub(crate) fn refresh(&mut self) { + fn components(&self) -> &[Component] { + &self.components + } + + fn components_mut(&mut self) -> &mut [Component] { + &mut self.components + } + + fn refresh_list(&mut self) { if let Some(ref connection) = self.connection { let connection = connection.inner(); - self.inner.clear(); + self.components.clear(); // getting CPU critical temperature let critical_temp = get_temperature(connection, &['T' as i8, 'C' as i8, '0' as i8, 'D' as i8, 0]); @@ -70,7 +79,7 @@ impl Components { if let Some(c) = Component::new((*id).to_owned(), None, critical_temp, v, connection) { - self.inner.push(c); + self.components.push(c); } } } diff --git a/src/unix/apple/mod.rs b/src/unix/apple/mod.rs index 2dd6bf447..27ef7627b 100644 --- a/src/unix/apple/mod.rs +++ b/src/unix/apple/mod.rs @@ -24,7 +24,7 @@ pub mod system; pub mod users; mod utils; -pub use self::component::Component; +pub use self::component::{Component, Components}; pub use self::cpu::Cpu; pub use self::disk::Disk; pub use self::network::NetworkData; diff --git a/src/unix/apple/system.rs b/src/unix/apple/system.rs index 859609055..04891eaa0 100644 --- a/src/unix/apple/system.rs +++ b/src/unix/apple/system.rs @@ -1,6 +1,5 @@ // Take a look at the license at the top of the repository in the LICENSE file. -use crate::sys::component::Component; use crate::sys::cpu::*; use crate::sys::process::*; use crate::sys::utils::{get_sys_value, get_sys_value_by_name}; @@ -25,9 +24,6 @@ use libc::{ _SC_PAGESIZE, }; -#[cfg(not(any(target_os = "ios", feature = "apple-sandbox")))] -use super::inner::component::Components; - #[cfg(all(target_os = "macos", not(feature = "apple-sandbox")))] declare_signals! { c_int, @@ -82,8 +78,6 @@ pub struct System { swap_total: u64, swap_free: u64, page_size_b: u64, - #[cfg(not(any(target_os = "ios", feature = "apple-sandbox")))] - components: Components, port: mach_port_t, users: Vec, boot_time: u64, @@ -148,8 +142,6 @@ impl SystemExt for System { swap_total: 0, swap_free: 0, page_size_b: sysconf(_SC_PAGESIZE) as _, - #[cfg(not(any(target_os = "ios", feature = "apple-sandbox")))] - components: Components::new(), port, users: Vec::new(), boot_time: boot_time(), @@ -223,14 +215,6 @@ impl SystemExt for System { } } - #[cfg(any(target_os = "ios", feature = "apple-sandbox"))] - fn refresh_components_list(&mut self) {} - - #[cfg(not(any(target_os = "ios", feature = "apple-sandbox")))] - fn refresh_components_list(&mut self) { - self.components.refresh(); - } - fn refresh_cpu_specifics(&mut self, refresh_kind: CpuRefreshKind) { self.cpus.refresh(refresh_kind, self.port); } @@ -377,26 +361,6 @@ impl SystemExt for System { self.swap_total - self.swap_free } - #[cfg(not(any(target_os = "ios", feature = "apple-sandbox")))] - fn components(&self) -> &[Component] { - &self.components.inner - } - - #[cfg(any(target_os = "ios", feature = "apple-sandbox"))] - fn components(&self) -> &[Component] { - &[] - } - - #[cfg(not(any(target_os = "ios", feature = "apple-sandbox")))] - fn components_mut(&mut self) -> &mut [Component] { - &mut self.components.inner - } - - #[cfg(any(target_os = "ios", feature = "apple-sandbox"))] - fn components_mut(&mut self) -> &mut [Component] { - &mut [] - } - fn uptime(&self) -> u64 { unsafe { let csec = libc::time(::std::ptr::null_mut()); diff --git a/src/unix/freebsd/component.rs b/src/unix/freebsd/component.rs index ba8220f91..e35e2fa4d 100644 --- a/src/unix/freebsd/component.rs +++ b/src/unix/freebsd/component.rs @@ -1,7 +1,7 @@ // 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; +use crate::{ComponentExt, ComponentsExt}; #[doc = include_str!("../../../md_doc/component.md")] pub struct Component { @@ -50,20 +50,43 @@ unsafe fn refresh_component(id: &[u8]) -> Option { } } -pub unsafe fn get_components(nb_cpus: usize) -> Vec { - // For now, we only have temperature for CPUs... - let mut components = Vec::with_capacity(nb_cpus); +#[doc = include_str!("../../../md_doc/components.md")] +pub struct Components { + nb_cpus: usize, + components: Vec, +} + +impl ComponentsExt for Components { + fn new() -> Self { + let nb_cpus = unsafe { super::cpu::get_nb_cpus() }; + Self { + nb_cpus, + components: Vec::with_capacity(nb_cpus), + } + } + + fn components(&self) -> &[Component] { + &self.components + } + + fn components_mut(&mut self) -> &mut [Component] { + &mut self.components + } - for core in 0..nb_cpus { - let id = format!("dev.cpu.{core}.temperature\0").as_bytes().to_vec(); - if let Some(temperature) = refresh_component(&id) { - components.push(Component { - id, - label: format!("CPU {}", core + 1), - temperature, - max: temperature, - }); + fn refresh_list(&mut self) { + self.components.clear(); + for core in 0..self.nb_cpus { + unsafe { + 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, + }); + } + } } } - components } diff --git a/src/unix/freebsd/cpu.rs b/src/unix/freebsd/cpu.rs index 4ff67f83f..9d136886c 100644 --- a/src/unix/freebsd/cpu.rs +++ b/src/unix/freebsd/cpu.rs @@ -7,6 +7,22 @@ use crate::{CpuExt, CpuRefreshKind}; use libc::{c_int, c_ulong}; +pub(crate) unsafe fn get_nb_cpus() -> usize { + let mut smp: c_int = 0; + let mut nb_cpus: c_int = 1; + + if !get_sys_value_by_name(b"kern.smp.active\0", &mut smp) { + smp = 0; + } + #[allow(clippy::collapsible_if)] // I keep as is for readability reasons. + if smp != 0 { + if !get_sys_value_by_name(b"kern.smp.cpus\0", &mut nb_cpus) || nb_cpus < 1 { + nb_cpus = 1; + } + } + nb_cpus as usize +} + pub(crate) struct CpusWrapper { pub(crate) global_cpu: Cpu, pub(crate) cpus: Vec, @@ -25,20 +41,8 @@ impl CpusWrapper { let mut mib_cp_time = [0; 2]; let mut mib_cp_times = [0; 2]; - let mut smp: c_int = 0; - let mut nb_cpus: c_int = 1; - unsafe { - if !get_sys_value_by_name(b"kern.smp.active\0", &mut smp) { - smp = 0; - } - #[allow(clippy::collapsible_if)] // I keep as is for readability reasons. - if smp != 0 { - if !get_sys_value_by_name(b"kern.smp.cpus\0", &mut nb_cpus) || nb_cpus < 1 { - nb_cpus = 1; - } - } - let nb_cpus = nb_cpus as usize; + let nb_cpus = get_nb_cpus(); init_mib(b"kern.cp_time\0", &mut mib_cp_time); init_mib(b"kern.cp_times\0", &mut mib_cp_times); Self { @@ -48,7 +52,7 @@ impl CpusWrapper { mib_cp_time, mib_cp_times, cp_time: VecSwitcher::new(vec![0; libc::CPUSTATES as usize]), - cp_times: VecSwitcher::new(vec![0; nb_cpus as usize * libc::CPUSTATES as usize]), + cp_times: VecSwitcher::new(vec![0; nb_cpus * libc::CPUSTATES as usize]), nb_cpus, } } diff --git a/src/unix/freebsd/mod.rs b/src/unix/freebsd/mod.rs index 2156a8cda..c0a7c8354 100644 --- a/src/unix/freebsd/mod.rs +++ b/src/unix/freebsd/mod.rs @@ -8,7 +8,7 @@ pub mod process; pub mod system; mod utils; -pub use self::component::Component; +pub use self::component::{Component, Components}; pub use self::cpu::Cpu; pub use self::disk::Disk; pub use self::network::NetworkData; diff --git a/src/unix/freebsd/system.rs b/src/unix/freebsd/system.rs index 95334719c..4932f2996 100644 --- a/src/unix/freebsd/system.rs +++ b/src/unix/freebsd/system.rs @@ -1,7 +1,7 @@ // Take a look at the license at the top of the repository in the LICENSE file. use crate::{ - sys::{component::Component, Cpu, Process}, + sys::{Cpu, Process}, CpuRefreshKind, LoadAvg, Pid, ProcessRefreshKind, RefreshKind, SystemExt, User, }; @@ -64,7 +64,6 @@ pub struct System { mem_used: u64, swap_total: u64, swap_used: u64, - components: Vec, users: Vec, boot_time: u64, system_info: SystemInfo, @@ -86,7 +85,6 @@ impl SystemExt for System { mem_used: 0, swap_total: 0, swap_used: 0, - components: Vec::with_capacity(2), users: Vec::new(), boot_time: boot_time(), system_info, @@ -111,13 +109,6 @@ impl SystemExt for System { self.cpus.refresh(refresh_kind) } - fn refresh_components_list(&mut self) { - if self.cpus.cpus.is_empty() { - self.refresh_cpu_usage(); - } - self.components = unsafe { super::component::get_components(self.cpus.cpus.len()) }; - } - fn refresh_processes_specifics(&mut self, refresh_kind: ProcessRefreshKind) { unsafe { self.refresh_procs(refresh_kind) } } @@ -234,14 +225,6 @@ impl SystemExt for System { self.swap_used } - fn components(&self) -> &[Component] { - &self.components - } - - fn components_mut(&mut self) -> &mut [Component] { - &mut self.components - } - fn uptime(&self) -> u64 { unsafe { let csec = libc::time(::std::ptr::null_mut()); diff --git a/src/unix/linux/component.rs b/src/unix/linux/component.rs index e04201231..6b65f63c0 100644 --- a/src/unix/linux/component.rs +++ b/src/unix/linux/component.rs @@ -4,7 +4,7 @@ // // Values in /sys/class/hwmonN are `c_long` or `c_ulong` // transposed to rust we only read `u32` or `i32` values. -use crate::ComponentExt; +use crate::{ComponentExt, ComponentsExt}; use std::collections::HashMap; use std::fs::{read_dir, File}; @@ -19,6 +19,7 @@ pub struct Component { /// The chip name. /// /// Kernel documentation extract: + /// /// ```txt /// This should be a short, lowercase string, not containing /// whitespace, dashes, or the wildcard character '*'. @@ -330,23 +331,42 @@ impl ComponentExt for Component { } } -pub(crate) fn get_components() -> Vec { - let mut components = Vec::with_capacity(10); - if let Ok(dir) = read_dir(Path::new("/sys/class/hwmon/")) { - for entry in dir.flatten() { - let entry = entry.path(); - if !entry.is_dir() - || !entry - .file_name() - .and_then(|x| x.to_str()) - .unwrap_or("") - .starts_with("hwmon") - { - continue; +#[doc = include_str!("../../../md_doc/components.md")] +pub struct Components { + components: Vec, +} + +impl ComponentsExt for Components { + fn new() -> Self { + Self { + components: Vec::with_capacity(4), + } + } + + fn components(&self) -> &[Component] { + &self.components + } + + fn components_mut(&mut self) -> &mut [Component] { + &mut self.components + } + + fn refresh_list(&mut self) { + self.components.clear(); + if let Ok(dir) = read_dir(Path::new("/sys/class/hwmon/")) { + for entry in dir.flatten() { + let entry = entry.path(); + if !entry.is_dir() + || !entry + .file_name() + .and_then(|x| x.to_str()) + .unwrap_or("") + .starts_with("hwmon") + { + continue; + } + Component::from_hwmon(&mut self.components, &entry); } - Component::from_hwmon(&mut components, &entry); } - components.sort_by(|c1, c2| c1.label.to_lowercase().cmp(&c2.label.to_lowercase())); } - components } diff --git a/src/unix/linux/mod.rs b/src/unix/linux/mod.rs index 09b108130..f9106fc86 100644 --- a/src/unix/linux/mod.rs +++ b/src/unix/linux/mod.rs @@ -8,7 +8,7 @@ pub mod process; pub mod system; pub(crate) mod utils; -pub use self::component::Component; +pub use self::component::{Component, Components}; pub use self::cpu::Cpu; pub use self::disk::Disk; pub use self::network::NetworkData; diff --git a/src/unix/linux/system.rs b/src/unix/linux/system.rs index 35497fb89..2ac80ecb8 100644 --- a/src/unix/linux/system.rs +++ b/src/unix/linux/system.rs @@ -1,6 +1,5 @@ // Take a look at the license at the top of the repository in the LICENSE file. -use crate::sys::component::{self, Component}; use crate::sys::cpu::*; use crate::sys::process::*; use crate::sys::utils::{get_all_data, to_u64}; @@ -157,7 +156,6 @@ pub struct System { mem_slab_reclaimable: u64, swap_total: u64, swap_free: u64, - components: Vec, users: Vec, info: SystemInfo, cpus: CpusWrapper, @@ -230,7 +228,6 @@ impl SystemExt for System { swap_total: 0, swap_free: 0, cpus: CpusWrapper::new(), - components: Vec::new(), users: Vec::new(), info: SystemInfo::new(), }; @@ -238,10 +235,6 @@ impl SystemExt for System { s } - fn refresh_components_list(&mut self) { - self.components = component::get_components(); - } - fn refresh_memory(&mut self) { let mut mem_available_found = false; read_table("/proc/meminfo", ':', |key, value_kib| { @@ -426,14 +419,6 @@ impl SystemExt for System { self.swap_total - self.swap_free } - fn components(&self) -> &[Component] { - &self.components - } - - fn components_mut(&mut self) -> &mut [Component] { - &mut self.components - } - fn uptime(&self) -> u64 { let content = get_all_data("/proc/uptime", 50).unwrap_or_default(); content diff --git a/src/unknown/component.rs b/src/unknown/component.rs index ec4a8e87a..07a71a1af 100644 --- a/src/unknown/component.rs +++ b/src/unknown/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; +use crate::{ComponentExt, ComponentsExt}; #[doc = include_str!("../../md_doc/component.md")] pub struct Component {} @@ -24,3 +24,28 @@ impl ComponentExt for Component { fn refresh(&mut self) {} } + +#[doc = include_str!("../../md_doc/components.md")] +pub struct Components { + components: Vec, +} + +impl ComponentsExt for Components { + fn new() -> Self { + Self { + components: Vec::new(), + } + } + + fn components(&self) -> &[Component] { + &self.components + } + + fn components_mut(&mut self) -> &mut [Component] { + &mut self.components + } + + fn refresh_list(&mut self) { + // Doesn't do anything. + } +} diff --git a/src/unknown/mod.rs b/src/unknown/mod.rs index a20aba1d6..28b7b2650 100644 --- a/src/unknown/mod.rs +++ b/src/unknown/mod.rs @@ -8,7 +8,7 @@ pub mod process; pub mod system; pub mod users; -pub use self::component::Component; +pub use self::component::{Component, Components}; pub use self::cpu::Cpu; pub use self::disk::Disk; pub use self::network::NetworkData; diff --git a/src/unknown/system.rs b/src/unknown/system.rs index 1caefa83c..345fae84b 100644 --- a/src/unknown/system.rs +++ b/src/unknown/system.rs @@ -1,7 +1,7 @@ // Take a look at the license at the top of the repository in the LICENSE file. use crate::{ - sys::{component::Component, Cpu, Process}, + sys::{Cpu, Process}, CpuRefreshKind, LoadAvg, Pid, ProcessRefreshKind, RefreshKind, SystemExt, User, }; @@ -35,8 +35,6 @@ impl SystemExt for System { fn refresh_cpu_specifics(&mut self, _refresh_kind: CpuRefreshKind) {} - fn refresh_components_list(&mut self) {} - fn refresh_processes_specifics(&mut self, _refresh_kind: ProcessRefreshKind) {} fn refresh_process_specifics(&mut self, _pid: Pid, _refresh_kind: ProcessRefreshKind) -> bool { @@ -97,14 +95,6 @@ impl SystemExt for System { 0 } - fn components(&self) -> &[Component] { - &[] - } - - fn components_mut(&mut self) -> &mut [Component] { - &mut [] - } - fn uptime(&self) -> u64 { 0 } diff --git a/src/windows/component.rs b/src/windows/component.rs index 502594e38..2cd43b0f9 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; +use crate::{ComponentExt, ComponentsExt}; use std::ptr::null_mut; @@ -92,10 +92,31 @@ impl ComponentExt for Component { } } -pub(crate) fn get_components() -> Vec { - match Component::new() { - Some(c) => vec![c], - None => Vec::new(), +#[doc = include_str!("../../md_doc/components.md")] +pub struct Components { + components: Vec, +} + +impl ComponentsExt for Components { + fn new() -> Self { + Self { + components: Vec::new(), + } + } + + fn components(&self) -> &[Component] { + &self.components + } + + fn components_mut(&mut self) -> &mut [Component] { + &mut self.components + } + + fn refresh_list(&mut self) { + self.components = match Component::new() { + Some(c) => vec![c], + None => Vec::new(), + }; } } diff --git a/src/windows/mod.rs b/src/windows/mod.rs index f7781038d..763bf12e3 100644 --- a/src/windows/mod.rs +++ b/src/windows/mod.rs @@ -12,7 +12,7 @@ mod tools; mod users; mod utils; -pub use self::component::Component; +pub use self::component::{Component, Components}; pub use self::cpu::Cpu; pub use self::disk::Disk; pub use self::network::NetworkData; diff --git a/src/windows/system.rs b/src/windows/system.rs index 2dc58389b..b140eca25 100644 --- a/src/windows/system.rs +++ b/src/windows/system.rs @@ -5,7 +5,6 @@ use crate::{ }; use winapi::um::winreg::HKEY_LOCAL_MACHINE; -use crate::sys::component::{self, Component}; use crate::sys::cpu::*; use crate::sys::process::{get_start_time, update_memory, Process}; use crate::sys::tools::*; @@ -49,7 +48,6 @@ pub struct System { swap_total: u64, swap_used: u64, cpus: CpusWrapper, - components: Vec, query: Option, boot_time: u64, users: Vec, @@ -99,7 +97,6 @@ impl SystemExt for System { swap_total: 0, swap_used: 0, cpus: CpusWrapper::new(), - components: Vec::new(), query: None, boot_time: unsafe { boot_time() }, users: Vec::new(), @@ -189,10 +186,6 @@ impl SystemExt for System { } } - fn refresh_components_list(&mut self) { - self.components = component::get_components(); - } - #[allow(clippy::map_entry)] fn refresh_process_specifics(&mut self, pid: Pid, refresh_kind: ProcessRefreshKind) -> bool { let now = get_now(); @@ -401,14 +394,6 @@ impl SystemExt for System { self.swap_used } - fn components(&self) -> &[Component] { - &self.components - } - - fn components_mut(&mut self) -> &mut [Component] { - &mut self.components - } - fn users(&self) -> &[User] { &self.users } From 225b2070bc2aad920b421bc8a49210eed80dba59 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 17 Sep 2023 17:02:16 +0200 Subject: [PATCH 2/3] Fix build instructions for C example --- README.md | 2 +- src/unix/apple/macos/component/arm.rs | 2 +- src/unix/apple/macos/component/x86.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index fb010476f..44b545253 100644 --- a/README.md +++ b/README.md @@ -201,7 +201,7 @@ To build the C example, just run: > make > ./simple # If needed: -> LD_LIBRARY_PATH=target/release/ ./simple +> LD_LIBRARY_PATH=target/debug/ ./simple ``` ### Benchmarks diff --git a/src/unix/apple/macos/component/arm.rs b/src/unix/apple/macos/component/arm.rs index cb14ba329..f9a9e8227 100644 --- a/src/unix/apple/macos/component/arm.rs +++ b/src/unix/apple/macos/component/arm.rs @@ -20,7 +20,7 @@ use crate::{ComponentExt, ComponentsExt}; #[doc = include_str!("../../../../../md_doc/components.md")] pub struct Components { - pub(crate) components: Vec, + components: Vec, client: Option>, } diff --git a/src/unix/apple/macos/component/x86.rs b/src/unix/apple/macos/component/x86.rs index c7e529644..24659763a 100644 --- a/src/unix/apple/macos/component/x86.rs +++ b/src/unix/apple/macos/component/x86.rs @@ -47,7 +47,7 @@ impl ComponentFFI { // Used to get CPU information, not supported on iOS, or inside the default macOS sandbox. #[doc = include_str!("../../../../../md_doc/components.md")] pub struct Components { - pub components: Vec, + components: Vec, connection: Option, } From a5cf3e179f8c10c67282eefab6cd71cfc697e0c1 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 17 Sep 2023 17:10:47 +0200 Subject: [PATCH 3/3] Add command to refresh components --- examples/simple.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/examples/simple.rs b/examples/simple.rs index 9c4338c23..14fec8641 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -62,15 +62,19 @@ fn print_help() { ); writeln!( &mut io::stdout(), - "refresh_disks : reloads only disks information" + "refresh_components : reloads components information" ); writeln!( &mut io::stdout(), - "refresh_users : reloads only users information" + "refresh_disks : reloads disks information" ); writeln!( &mut io::stdout(), - "refresh_networks : reloads only networks information" + "refresh_users : reloads users information" + ); + writeln!( + &mut io::stdout(), + "refresh_networks : reloads networks information" ); writeln!( &mut io::stdout(), @@ -166,6 +170,11 @@ fn interpret_input( networks.refresh_list(); writeln!(&mut io::stdout(), "Done."); } + "refresh_components" => { + writeln!(&mut io::stdout(), "Refreshing component list..."); + components.refresh_list(); + writeln!(&mut io::stdout(), "Done."); + } "signals" => { let mut nb = 1i32;