Skip to content

Commit

Permalink
Merge pull request #1100 from GuillaumeGomez/rm-pidext
Browse files Browse the repository at this point in the history
Remove `PidExt` trait
  • Loading branch information
GuillaumeGomez authored Oct 15, 2023
2 parents 8eba68f + a0e0dbd commit 554e974
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 65 deletions.
2 changes: 1 addition & 1 deletion md_doc/pid.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
59 changes: 23 additions & 36 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -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> {
Expand All @@ -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 _)
}
}
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down
1 change: 0 additions & 1 deletion src/serde.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/windows/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
42 changes: 19 additions & 23 deletions tests/process.rs
Original file line number Diff line number Diff line change
@@ -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") {
Expand Down Expand Up @@ -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.");

Expand Down Expand Up @@ -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.");
Expand Down Expand Up @@ -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.");

Expand Down Expand Up @@ -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.");

Expand All @@ -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") {
Expand Down Expand Up @@ -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")
Expand All @@ -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());
Expand Down Expand Up @@ -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") {
Expand Down Expand Up @@ -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.");

Expand Down Expand Up @@ -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()));
}
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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!");

Expand Down Expand Up @@ -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()),
);

Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 554e974

Please sign in to comment.