From a0e0dbde8d61e5737dba1fe8b1a1fb718d4d69c6 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 15 Oct 2023 21:59:09 +0200 Subject: [PATCH] Remove PidExt trait --- md_doc/pid.md | 2 +- src/common.rs | 59 ++++++++++++++++-------------------------- src/lib.rs | 2 +- src/serde.rs | 1 - src/system.rs | 4 +-- src/windows/process.rs | 2 +- tests/process.rs | 42 ++++++++++++++---------------- 7 files changed, 47 insertions(+), 65 deletions(-) diff --git a/md_doc/pid.md b/md_doc/pid.md index 051315314..fa5fbcb58 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 80f62223e..fedee640a 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 + 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 { @@ -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 524033215..5cf209b04 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 a533d5709..90b5ddfef 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 bf9f5c5f4..7017ee66a 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::>(); 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 c09d60393..f2f8b148a 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; diff --git a/tests/process.rs b/tests/process.rs index dc263fcbf..91078958c 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 . #[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());