Repository: GuillaumeGomez/sysinfo
Pull request #1424 (2024-12-08 19:12:19)

Convert `physical_core_count` to an associated function


Patch:
diff --git a/examples/simple.rs b/examples/simple.rs
index 137eb57..ff2c7d9 100644
--- a/examples/simple.rs
+++ b/examples/simple.rs
@@ -195,7 +195,7 @@ fn interpret_input(
             writeln!(
                 &mut io::stdout(),
                 "number of physical cores: {}",
-                sys.physical_core_count()
+                System::physical_core_count()
                     .map(|c| c.to_string())
                     .unwrap_or_else(|| "Unknown".to_owned()),
             );
diff --git a/src/c_interface.rs b/src/c_interface.rs
index eb4d131..18c3c10 100644
--- a/src/c_interface.rs
+++ b/src/c_interface.rs
@@ -531,18 +531,6 @@ pub extern "C" fn sysinfo_cpu_brand(system: CSystem) -> RString {
     }
 }
 
-/// Equivalent of [`system::physical_core_count()`].
-#[no_mangle]
-pub extern "C" fn sysinfo_cpu_physical_cores(system: CSystem) -> u32 {
-    assert!(!system.is_null());
-    unsafe {
-        let system: Box<System> = Box::from_raw(system as *mut System);
-        let count = system.physical_core_count().unwrap_or(0);
-        let _ = Box::into_raw(system);
-        count as u32
-    }
-}
-
 /// Equivalent of [`cpu::frequency()`].
 #[no_mangle]
 pub extern "C" fn sysinfo_cpu_frequency(system: CSystem) -> u64 {
@@ -608,3 +596,9 @@ pub extern "C" fn sysinfo_system_long_version() -> RString {
         std::ptr::null()
     }
 }
+
+/// Equivalent of [`system::physical_core_count()`].
+#[no_mangle]
+pub extern "C" fn sysinfo_cpu_physical_cores() -> u32 {
+    System::physical_core_count().unwrap_or(0) as u32
+}
diff --git a/src/common/system.rs b/src/common/system.rs
index 7c1907d..d7b1df5 100644
--- a/src/common/system.rs
+++ b/src/common/system.rs
@@ -504,22 +504,6 @@ impl System {
         self.inner.cpus()
     }
 
-    /// Returns the number of physical cores on the CPU or `None` if it couldn't get it.
-    ///
-    /// In case there are multiple CPUs, it will combine the physical core count of all the CPUs.
-    ///
-    /// **Important**: this information is computed every time this function is called.
-    ///
-    /// ```no_run
-    /// use sysinfo::System;
-    ///
-    /// let s = System::new();
-    /// println!("{:?}", s.physical_core_count());
-    /// ```
-    pub fn physical_core_count(&self) -> Option<usize> {
-        self.inner.physical_core_count()
-    }
-
     /// Returns the RAM size in bytes.
     ///
     /// ```no_run
@@ -817,6 +801,22 @@ impl System {
     pub fn cpu_arch() -> String {
         SystemInner::cpu_arch().unwrap_or_else(|| std::env::consts::ARCH.to_owned())
     }
+
+    /// Returns the number of physical cores on the CPU or `None` if it couldn't get it.
+    ///
+    /// In case there are multiple CPUs, it will combine the physical core count of all the CPUs.
+    ///
+    /// **Important**: this information is computed every time this function is called.
+    ///
+    /// ```no_run
+    /// use sysinfo::System;
+    ///
+    /// let s = System::new();
+    /// println!("{:?}", System::physical_core_count());
+    /// ```
+    pub fn physical_core_count() -> Option<usize> {
+        SystemInner::physical_core_count()
+    }
 }
 
 /// A struct representing system load average value.
@@ -2496,9 +2496,8 @@ mod test {
         if IS_SUPPORTED_SYSTEM {
             // The physical cores count is recomputed every time the function is called, so the
             // information must be relevant even with nothing initialized.
-            let physical_cores_count = s
-                .physical_core_count()
-                .expect("failed to get number of physical cores");
+            let physical_cores_count =
+                System::physical_core_count().expect("failed to get number of physical cores");
 
             s.refresh_cpu_usage();
             // The cpus shouldn't be empty anymore.
@@ -2506,15 +2505,14 @@ mod test {
 
             // In case we are running inside a VM, it's possible to not have a physical core, only
             // logical ones, which is why we don't test `physical_cores_count > 0`.
-            let physical_cores_count2 = s
-                .physical_core_count()
-                .expect("failed to get number of physical cores");
+            let physical_cores_count2 =
+                System::physical_core_count().expect("failed to get number of physical cores");
             assert!(physical_cores_count2 <= s.cpus().len());
             assert_eq!(physical_cores_count, physical_cores_count2);
         } else {
-            assert_eq!(s.physical_core_count(), None);
+            assert_eq!(System::physical_core_count(), None);
         }
-        assert!(s.physical_core_count().unwrap_or(0) <= s.cpus().len());
+        assert!(System::physical_core_count().unwrap_or(0) <= s.cpus().len());
     }
 
     // This test only exists to ensure that the `Display` and `Debug` traits are implemented on the
diff --git a/src/serde.rs b/src/serde.rs
index a370689..ced7895 100644
--- a/src/serde.rs
+++ b/src/serde.rs
@@ -135,7 +135,7 @@ impl serde::Serialize for crate::System {
         state.serialize_field("global_cpu_usage", &self.global_cpu_usage())?;
         state.serialize_field("cpus", &self.cpus())?;
 
-        state.serialize_field("physical_core_count", &self.physical_core_count())?;
+        state.serialize_field("physical_core_count", &Self::physical_core_count())?;
         state.serialize_field("total_memory", &self.total_memory())?;
         state.serialize_field("free_memory", &self.free_memory())?;
         state.serialize_field("available_memory", &self.available_memory())?;
diff --git a/src/sysinfo.h b/src/sysinfo.h
index 01f6ce3..c558def 100644
--- a/src/sysinfo.h
+++ b/src/sysinfo.h
@@ -63,12 +63,12 @@ size_t      sysinfo_networks_transmitted(CNetworks networks);
 RString     sysinfo_cpu_vendor_id(CSystem system);
 RString     sysinfo_cpu_brand(CSystem system);
 uint64_t    sysinfo_cpu_frequency(CSystem system);
-uint32_t    sysinfo_cpu_physical_cores(CSystem system);
 
 RString     sysinfo_system_name();
 RString     sysinfo_system_kernel_version();
 RString     sysinfo_system_version();
 RString     sysinfo_system_host_name();
 RString     sysinfo_system_long_version();
+uint32_t    sysinfo_cpu_physical_cores();
 
 void        sysinfo_rstring_free(RString str);
diff --git a/src/unix/apple/system.rs b/src/unix/apple/system.rs
index 536c9f3..36db26e 100644
--- a/src/unix/apple/system.rs
+++ b/src/unix/apple/system.rs
@@ -331,10 +331,6 @@ impl SystemInner {
         &self.cpus.cpus
     }
 
-    pub(crate) fn physical_core_count(&self) -> Option<usize> {
-        physical_core_count()
-    }
-
     pub(crate) fn total_memory(&self) -> u64 {
         self.mem_total
     }
@@ -507,6 +503,10 @@ impl SystemInner {
             }
         }
     }
+
+    pub(crate) fn physical_core_count() -> Option<usize> {
+        physical_core_count()
+    }
 }
 
 fn get_system_info(value: c_int, default: Option<&str>) -> Option<String> {
diff --git a/src/unix/freebsd/system.rs b/src/unix/freebsd/system.rs
index 32d2b7f..fcd0e89 100644
--- a/src/unix/freebsd/system.rs
+++ b/src/unix/freebsd/system.rs
@@ -148,10 +148,6 @@ impl SystemInner {
         &self.cpus.cpus
     }
 
-    pub(crate) fn physical_core_count(&self) -> Option<usize> {
-        physical_core_count()
-    }
-
     pub(crate) fn total_memory(&self) -> u64 {
         self.mem_total
     }
@@ -268,6 +264,10 @@ impl SystemInner {
             }
         }
     }
+
+    pub(crate) fn physical_core_count() -> Option<usize> {
+        physical_core_count()
+    }
 }
 
 impl SystemInner {
diff --git a/src/unix/linux/system.rs b/src/unix/linux/system.rs
index cc11bac..3f14e48 100644
--- a/src/unix/linux/system.rs
+++ b/src/unix/linux/system.rs
@@ -308,10 +308,6 @@ impl SystemInner {
         &self.cpus.cpus
     }
 
-    pub(crate) fn physical_core_count(&self) -> Option<usize> {
-        get_physical_core_count()
-    }
-
     pub(crate) fn total_memory(&self) -> u64 {
         self.mem_total
     }
@@ -503,6 +499,10 @@ impl SystemInner {
         }
     }
 
+    pub(crate) fn physical_core_count() -> Option<usize> {
+        get_physical_core_count()
+    }
+
     pub(crate) fn refresh_cpu_list(&mut self, refresh_kind: CpuRefreshKind) {
         self.cpus = CpusWrapper::new();
         self.refresh_cpu_specifics(refresh_kind);
diff --git a/src/unknown/system.rs b/src/unknown/system.rs
index a6162e2..56bcddb 100644
--- a/src/unknown/system.rs
+++ b/src/unknown/system.rs
@@ -71,10 +71,6 @@ impl SystemInner {
         &[]
     }
 
-    pub(crate) fn physical_core_count(&self) -> Option<usize> {
-        None
-    }
-
     pub(crate) fn total_memory(&self) -> u64 {
         0
     }
@@ -145,4 +141,8 @@ impl SystemInner {
     pub(crate) fn cpu_arch() -> Option<String> {
         None
     }
+
+    pub(crate) fn physical_core_count() -> Option<usize> {
+        None
+    }
 }
diff --git a/src/windows/system.rs b/src/windows/system.rs
index 71a358c..c71aacb 100644
--- a/src/windows/system.rs
+++ b/src/windows/system.rs
@@ -380,10 +380,6 @@ impl SystemInner {
         self.cpus.cpus()
     }
 
-    pub(crate) fn physical_core_count(&self) -> Option<usize> {
-        get_physical_core_count()
-    }
-
     pub(crate) fn total_memory(&self) -> u64 {
         self.mem_total
     }
@@ -506,6 +502,10 @@ impl SystemInner {
             }
         }
     }
+
+    pub(crate) fn physical_core_count() -> Option<usize> {
+        get_physical_core_count()
+    }
 }
 
 pub(crate) fn is_proc_running(handle: HANDLE) -> bool {


Test patch:
diff --git a/tests/cpu.rs b/tests/cpu.rs
index 4fb73c1..19651db 100644
--- a/tests/cpu.rs
+++ b/tests/cpu.rs
@@ -30,8 +30,7 @@ fn test_cpu() {
 #[test]
 fn test_physical_core_numbers() {
     if sysinfo::IS_SUPPORTED_SYSTEM {
-        let s = sysinfo::System::new();
-        let count = s.physical_core_count();
+        let count = sysinfo::System::physical_core_count();
         assert_ne!(count, None);
         assert!(count.unwrap() > 0);
     }
diff --git a/tests/disk.rs b/tests/disk.rs
index 73ddc16..4290469 100644
--- a/tests/disk.rs
+++ b/tests/disk.rs
@@ -6,10 +6,8 @@ fn should_skip() -> bool {
         return true;
     }
 
-    let s = sysinfo::System::new_all();
-
     // If we don't have any physical core present, it's very likely that we're inside a VM...
-    s.physical_core_count().unwrap_or_default() == 0
+    sysinfo::System::physical_core_count().unwrap_or_default() == 0
 }
 
 #[test]