Repository: GuillaumeGomez/sysinfo
Pull request #1100 (2023-10-15 20:00:04)

Remove `PidExt` trait
Fixes https://github.com/GuillaumeGomez/sysinfo/issues/1088.


Patch:
diff --git a/md_doc/pid.md b/md_doc/pid.md
index 0513153..fa5fbcb 100644
--- a/md_doc/pid.md
+++ b/md_doc/pid.md
@@ -3,7 +3,7 @@ Process ID.
 Can be used as an integer type by simple casting. For example:
 
 ```
-use sysinfo::{PidExt, Pid};
+use sysinfo::Pid;
 
 // 0's type will be different depending on the platform!
 let p = Pid::from(0);
diff --git a/src/common.rs b/src/common.rs
index 80f6222..fedee64 100644
--- a/src/common.rs
+++ b/src/common.rs
@@ -1127,34 +1127,6 @@ impl Process {
     }
 }
 
-/// Trait to have a common conversions for the [`Pid`][crate::Pid] type.
-///
-/// ```
-/// use sysinfo::{Pid, PidExt};
-///
-/// let p = Pid::from_u32(0);
-/// let value: u32 = p.as_u32();
-/// ```
-pub trait PidExt: Copy + From<usize> + FromStr + fmt::Display {
-    /// Allows to convert [`Pid`][crate::Pid] into [`u32`].
-    ///
-    /// ```
-    /// use sysinfo::{Pid, PidExt};
-    ///
-    /// let p = Pid::from_u32(0);
-    /// let value: u32 = p.as_u32();
-    /// ```
-    fn as_u32(self) -> u32;
-    /// Allows to convert a [`u32`] into [`Pid`][crate::Pid].
-    ///
-    /// ```
-    /// use sysinfo::{Pid, PidExt};
-    ///
-    /// let p = Pid::from_u32(0);
-    /// ```
-    fn from_u32(v: u32) -> Self;
-}
-
 macro_rules! pid_decl {
     ($typ:ty) => {
         #[doc = include_str!("../md_doc/pid.md")]
@@ -1172,14 +1144,6 @@ macro_rules! pid_decl {
                 v.0 as _
             }
         }
-        impl PidExt for Pid {
-            fn as_u32(self) -> u32 {
-                self.0 as _
-            }
-            fn from_u32(v: u32) -> Self {
-                Self(v as _)
-            }
-        }
         impl FromStr for Pid {
             type Err = <$typ as FromStr>::Err;
             fn from_str(s: &str) -> Result<Self, Self::Err> {
@@ -1191,6 +1155,29 @@ macro_rules! pid_decl {
                 write!(f, "{}", self.0)
             }
         }
+        impl Pid {
+            /// Allows to convert [`Pid`][crate::Pid] into [`u32`].
+            ///
+            /// ```
+            /// use sysinfo::Pid;
+            ///
+            /// let pid = Pid::from_u32(0);
+            /// let value: u32 = pid.as_u32();
+            /// ```
+            pub fn as_u32(self) -> u32 {
+                self.0 as _
+            }
+            /// Allows to convert a [`u32`] into [`Pid`][crate::Pid].
+            ///
+            /// ```
+            /// use sysinfo::Pid;
+            ///
+            /// let pid = Pid::from_u32(0);
+            /// ```
+            pub fn from_u32(v: u32) -> Self {
+                Self(v as _)
+            }
+        }
     };
 }
 
diff --git a/src/lib.rs b/src/lib.rs
index 5240332..5cf209b 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -53,7 +53,7 @@ cfg_if::cfg_if! {
 
 pub use crate::common::{
     get_current_pid, Component, Components, Cpu, CpuRefreshKind, Disk, DiskKind, DiskUsage, Disks,
-    Gid, Group, LoadAvg, MacAddr, NetworkData, Networks, NetworksIter, Pid, PidExt, Process,
+    Gid, Group, LoadAvg, MacAddr, NetworkData, Networks, NetworksIter, Pid, Process,
     ProcessRefreshKind, ProcessStatus, RefreshKind, Signal, System, Uid, User, Users,
 };
 
diff --git a/src/serde.rs b/src/serde.rs
index a533d57..90b5ddf 100644
--- a/src/serde.rs
+++ b/src/serde.rs
@@ -1,6 +1,5 @@
 // Take a look at the license at the top of the repository in the LICENSE file.
 
-use crate::common::PidExt;
 use crate::{DiskKind, DiskUsage, MacAddr, ProcessStatus, Signal};
 use serde::{ser::SerializeStruct, Serialize, Serializer};
 use std::ops::Deref;
diff --git a/src/system.rs b/src/system.rs
index bf9f5c5..7017ee6 100644
--- a/src/system.rs
+++ b/src/system.rs
@@ -120,7 +120,7 @@ mod tests {
     #[test]
     #[ignore] // This test MUST be run on its own to prevent wrong CPU usage measurements.
     fn test_consecutive_cpu_usage_update() {
-        use crate::{PidExt, ProcessRefreshKind, System};
+        use crate::{Pid, ProcessRefreshKind, System};
         use std::sync::atomic::{AtomicBool, Ordering};
         use std::sync::Arc;
         use std::time::Duration;
@@ -154,7 +154,7 @@ mod tests {
             .take(2)
             .collect::<Vec<_>>();
         let pid = std::process::id();
-        pids.push(PidExt::from_u32(pid));
+        pids.push(Pid::from_u32(pid));
         assert_eq!(pids.len(), 3);
 
         for it in 0..3 {
diff --git a/src/windows/process.rs b/src/windows/process.rs
index c09d603..f2f8b14 100644
--- a/src/windows/process.rs
+++ b/src/windows/process.rs
@@ -2,7 +2,7 @@
 
 use crate::sys::system::is_proc_running;
 use crate::windows::Sid;
-use crate::{DiskUsage, Gid, Pid, PidExt, ProcessRefreshKind, ProcessStatus, Signal, Uid};
+use crate::{DiskUsage, Gid, Pid, ProcessRefreshKind, ProcessStatus, Signal, Uid};
 
 use std::ffi::OsString;
 use std::fmt;


Test patch:
diff --git a/tests/process.rs b/tests/process.rs
index dc263fc..9107895 100644
--- a/tests/process.rs
+++ b/tests/process.rs
@@ -1,10 +1,10 @@
 // Take a look at the license at the top of the repository in the LICENSE file.
 
-use sysinfo::{Pid, PidExt};
+use sysinfo::{Pid, System};
 
 #[test]
 fn test_process() {
-    let mut s = sysinfo::System::new();
+    let mut s = System::new();
     assert_eq!(s.processes().len(), 0);
     s.refresh_processes();
     if !sysinfo::IS_SUPPORTED || cfg!(feature = "apple-sandbox") {
@@ -40,7 +40,7 @@ fn test_cwd() {
 
     let pid = Pid::from_u32(p.id() as _);
     std::thread::sleep(std::time::Duration::from_secs(1));
-    let mut s = sysinfo::System::new();
+    let mut s = System::new();
     s.refresh_processes();
     p.kill().expect("Unable to kill process.");
 
@@ -76,7 +76,7 @@ fn test_cmd() {
             .unwrap()
     };
     std::thread::sleep(std::time::Duration::from_millis(500));
-    let mut s = sysinfo::System::new();
+    let mut s = System::new();
     assert!(s.processes().is_empty());
     s.refresh_processes();
     p.kill().expect("Unable to kill process.");
@@ -122,7 +122,7 @@ fn test_environ() {
 
     let pid = Pid::from_u32(p.id() as _);
     std::thread::sleep(std::time::Duration::from_secs(1));
-    let mut s = sysinfo::System::new();
+    let mut s = System::new();
     s.refresh_processes();
     p.kill().expect("Unable to kill process.");
 
@@ -173,7 +173,7 @@ fn test_big_environ() {
 
     let pid = Pid::from_u32(p.id() as _);
     std::thread::sleep(std::time::Duration::from_secs(1));
-    let mut s = sysinfo::System::new();
+    let mut s = System::new();
     s.refresh_processes();
     p.kill().expect("Unable to kill process.");
 
@@ -194,7 +194,7 @@ fn test_big_environ() {
 
 #[test]
 fn test_process_refresh() {
-    let mut s = sysinfo::System::new();
+    let mut s = System::new();
     assert_eq!(s.processes().len(), 0);
 
     if !sysinfo::IS_SUPPORTED || cfg!(feature = "apple-sandbox") {
@@ -222,7 +222,7 @@ fn test_process_disk_usage() {
         return;
     }
 
-    fn inner() -> sysinfo::System {
+    fn inner() -> System {
         {
             let mut file = File::create("test.txt").expect("failed to create file");
             file.write_all(b"This is a test file\nwith test data.\n")
@@ -231,7 +231,7 @@ fn test_process_disk_usage() {
         fs::remove_file("test.txt").expect("failed to remove file");
         // Waiting a bit just in case...
         std::thread::sleep(std::time::Duration::from_millis(250));
-        let mut system = sysinfo::System::new();
+        let mut system = System::new();
         assert!(system.processes().is_empty());
         system.refresh_processes();
         assert!(!system.processes().is_empty());
@@ -268,7 +268,7 @@ fn test_process_disk_usage() {
 
 #[test]
 fn cpu_usage_is_not_nan() {
-    let mut system = sysinfo::System::new();
+    let mut system = System::new();
     system.refresh_processes();
 
     if !sysinfo::IS_SUPPORTED || cfg!(feature = "apple-sandbox") {
@@ -320,7 +320,7 @@ fn test_process_times() {
 
     let pid = Pid::from_u32(p.id() as _);
     std::thread::sleep(std::time::Duration::from_secs(1));
-    let mut s = sysinfo::System::new();
+    let mut s = System::new();
     s.refresh_processes();
     p.kill().expect("Unable to kill process.");
 
@@ -350,7 +350,7 @@ fn test_process_session_id() {
     if !sysinfo::IS_SUPPORTED || cfg!(feature = "apple-sandbox") {
         return;
     }
-    let mut s = sysinfo::System::new();
+    let mut s = System::new();
     s.refresh_processes();
     assert!(s.processes().values().any(|p| p.session_id().is_some()));
 }
@@ -381,7 +381,7 @@ fn test_refresh_processes() {
     std::thread::sleep(std::time::Duration::from_secs(1));
 
     // Checks that the process is listed as it should.
-    let mut s = sysinfo::System::new();
+    let mut s = System::new();
     s.refresh_processes();
     assert!(s.process(pid).is_some());
 
@@ -420,7 +420,7 @@ fn test_refresh_tasks() {
     let pid = Pid::from_u32(std::process::id() as _);
 
     // Checks that the task is listed as it should.
-    let mut s = sysinfo::System::new();
+    let mut s = System::new();
     s.refresh_processes();
 
     assert!(s
@@ -469,7 +469,7 @@ fn test_refresh_process() {
     std::thread::sleep(std::time::Duration::from_secs(1));
 
     // Checks that the process is listed as it should.
-    let mut s = sysinfo::System::new();
+    let mut s = System::new();
     s.refresh_process(pid);
     assert!(s.process(pid).is_some());
 
@@ -511,7 +511,7 @@ fn test_wait_child() {
     let before = std::time::Instant::now();
     let pid = Pid::from_u32(p.id() as _);
 
-    let mut s = sysinfo::System::new();
+    let mut s = System::new();
     s.refresh_process(pid);
     let process = s.process(pid).unwrap();
 
@@ -547,7 +547,7 @@ fn test_wait_non_child() {
     };
     let pid = Pid::from_u32(p.id());
 
-    let mut s = sysinfo::System::new();
+    let mut s = System::new();
     s.refresh_process(pid);
     let process = s.process(pid).expect("Process not found!");
 
@@ -576,7 +576,7 @@ fn test_process_iterator_lifetimes() {
         return;
     }
 
-    let s = sysinfo::System::new_with_specifics(
+    let s = System::new_with_specifics(
         sysinfo::RefreshKind::new().with_processes(sysinfo::ProcessRefreshKind::new()),
     );
 
@@ -599,8 +599,6 @@ fn test_process_iterator_lifetimes() {
 // Regression test for <https://github.com/GuillaumeGomez/sysinfo/issues/918>.
 #[test]
 fn test_process_cpu_usage() {
-    use sysinfo::System;
-
     if !sysinfo::IS_SUPPORTED || cfg!(feature = "apple-sandbox") {
         return;
     }
@@ -618,8 +616,6 @@ fn test_process_cpu_usage() {
 
 #[test]
 fn test_process_creds() {
-    use sysinfo::System;
-
     if !sysinfo::IS_SUPPORTED || cfg!(feature = "apple-sandbox") {
         return;
     }
@@ -668,7 +664,7 @@ fn test_process_memory_refresh() {
     }
 
     // Ensure the process memory is available on the first refresh.
-    let mut s = sysinfo::System::new();
+    let mut s = System::new();
 
     // Refresh our own process
     let pid = Pid::from_u32(std::process::id());