Skip to content

Commit

Permalink
Add derive traits (i.e., Clone, etc.)
Browse files Browse the repository at this point in the history
  • Loading branch information
psengrith committed Feb 20, 2025
1 parent baa46ef commit 1b5ae87
Show file tree
Hide file tree
Showing 27 changed files with 183 additions and 11 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ cargo-args = ["-Zbuild-std"]
rustdoc-args = ["--generate-link-to-definition"]

[dependencies]
derivative = "2.2.0"
memchr = { version = "2.5", optional = true }
rayon = { version = "^1.8", optional = true }
serde = { version = "^1.0.190", optional = true, features = ["derive"] }
Expand Down
2 changes: 2 additions & 0 deletions src/common/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::DiskUsage;
/// println!("{:?}: {:?}", disk.name(), disk.kind());
/// }
/// ```
#[derive(Clone, PartialEq, Eq)]
pub struct Disk {
pub(crate) inner: crate::DiskInner,
}
Expand Down Expand Up @@ -198,6 +199,7 @@ impl Disk {
/// feature must be enabled. Note, however, that sysinfo may hang under certain
/// circumstances. For example, if a CIFS or NFS share has been mounted with
/// the _hard_ option, but the connection has an error, such as the share server has stopped.
#[derive(Clone, PartialEq, Eq)]
pub struct Disks {
inner: crate::DisksInner,
}
Expand Down
32 changes: 32 additions & 0 deletions src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub(crate) mod user;
/// ```
#[cfg(any(feature = "disk", feature = "system"))]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub struct DiskUsage {
/// Total number of written bytes.
pub total_written_bytes: u64,
Expand Down Expand Up @@ -109,6 +110,7 @@ macro_rules! gid {
xid!(
/// A group id wrapping a platform specific type.
#[derive(Copy)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
Gid,
$type,
std::str::FromStr
Expand All @@ -129,6 +131,16 @@ cfg_if! {
))] {
uid!(libc::uid_t, std::str::FromStr);
gid!(libc::gid_t);

#[cfg(all(feature = "serde", any(feature = "system", feature = "user")))]
impl<'de> serde::Deserialize<'de> for Uid {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
Ok(Self(libc::uid_t::deserialize(deserializer)?))
}
}
} else if #[cfg(windows)] {
uid!(crate::windows::Sid);
gid!(u32);
Expand All @@ -141,8 +153,28 @@ cfg_if! {
Ok(Self(t.parse()?))
}
}

#[cfg(all(feature = "serde", any(feature = "system", feature = "user")))]
impl<'de> serde::Deserialize<'de> for Uid {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
Ok(Self(crate::windows::Sid::deserialize(deserializer)?))
}
}
} else {
uid!(u32, std::str::FromStr);
gid!(u32);

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for Uid {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
Ok(Self(u32::deserialize(deserializer)?))
}
}
}
}
4 changes: 4 additions & 0 deletions src/common/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use crate::{NetworkDataInner, NetworksInner};
/// println!("[{interface_name}]: {network:?}");
/// }
/// ```
#[derive(Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub struct Networks {
pub(crate) inner: NetworksInner,
}
Expand Down Expand Up @@ -120,6 +122,8 @@ impl std::ops::Deref for Networks {
/// println!("[{interface_name}] {network:?}");
/// }
/// ```
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub struct NetworkData {
pub(crate) inner: NetworkDataInner,
}
Expand Down
34 changes: 27 additions & 7 deletions src/common/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,7 @@ impl std::fmt::Display for Signal {
}

/// Contains memory limits for the current process.
#[derive(Default, Debug, Clone)]
#[derive(Default, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct CGroupLimits {
/// Total memory (in bytes) for the current cgroup.
pub total_memory: u64,
Expand Down Expand Up @@ -1138,7 +1138,7 @@ pub enum ProcessStatus {
}

/// Enum describing the different kind of threads.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub enum ThreadKind {
/// Kernel thread.
Expand Down Expand Up @@ -1816,8 +1816,28 @@ cfg_if! {
use libc::pid_t;

pid_decl!(pid_t);

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for Pid {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
Ok(Self(pid_t::deserialize(deserializer)?))
}
}
} else {
pid_decl!(usize);

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for Pid {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
Ok(Self(usize::deserialize(deserializer)?))
}
}
}
}

Expand All @@ -1836,7 +1856,7 @@ cfg_if! {
/// ProcessRefreshKind::nothing().with_exe(UpdateKind::OnlyIfNotSet),
/// );
/// ```
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
pub enum UpdateKind {
/// Never update the related information.
#[default]
Expand Down Expand Up @@ -1927,7 +1947,7 @@ pub enum ProcessesToUpdate<'a> {
/// ```
///
/// [`Process`]: crate::Process
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct ProcessRefreshKind {
cpu: bool,
disk_usage: bool,
Expand Down Expand Up @@ -2074,7 +2094,7 @@ It will retrieve the following information:
/// ```
///
/// [`Cpu`]: crate::Cpu
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
pub struct CpuRefreshKind {
cpu_usage: bool,
frequency: bool,
Expand Down Expand Up @@ -2133,7 +2153,7 @@ impl CpuRefreshKind {
/// println!("total RAM: {}", system.total_memory());
/// println!("free RAM: {}", system.free_memory());
/// ```
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
pub struct MemoryRefreshKind {
ram: bool,
swap: bool,
Expand Down Expand Up @@ -2192,7 +2212,7 @@ impl MemoryRefreshKind {
/// assert!(system.processes().len() > 0);
/// # }
/// ```
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
pub struct RefreshKind {
processes: Option<ProcessRefreshKind>,
memory: Option<MemoryRefreshKind>,
Expand Down
12 changes: 10 additions & 2 deletions src/common/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ use crate::{Gid, Uid, UserInner};
/// println!("{:?}", user);
/// }
/// ```
#[derive(Clone)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub struct User {
pub(crate) inner: UserInner,
}
Expand Down Expand Up @@ -109,7 +111,8 @@ impl User {
}
}

#[derive(PartialEq, Eq, PartialOrd, Ord, Debug)]
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub(crate) struct GroupInner {
pub(crate) id: Gid,
pub(crate) name: String,
Expand All @@ -136,7 +139,8 @@ pub(crate) struct GroupInner {
/// }
/// }
/// ```
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug)]
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub struct Group {
pub(crate) inner: GroupInner,
}
Expand Down Expand Up @@ -189,6 +193,8 @@ impl Group {
/// println!("{} is in {} groups", user.name(), user.groups().len());
/// }
/// ```
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub struct Users {
users: Vec<User>,
}
Expand Down Expand Up @@ -360,6 +366,8 @@ impl Users {
/// println!("{}", group.name());
/// }
/// ```
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub struct Groups {
groups: Vec<Group>,
}
Expand Down
1 change: 1 addition & 0 deletions src/unix/apple/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use std::os::unix::ffi::{OsStrExt, OsStringExt};
use std::path::{Path, PathBuf};
use std::ptr;

#[derive(Clone, PartialEq, Eq)]
pub(crate) struct DiskInner {
pub(crate) type_: DiskKind,
pub(crate) name: OsString,
Expand Down
5 changes: 4 additions & 1 deletion src/unix/apple/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ fn update_network_data(inner: &mut NetworkDataInner, data: &if_data64) {
);
}

#[derive(Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub(crate) struct NetworksInner {
pub(crate) interfaces: HashMap<String, NetworkData>,
}
Expand Down Expand Up @@ -252,7 +254,8 @@ impl NetworksInner {
}
}

#[derive(PartialEq, Eq)]
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub(crate) struct NetworkDataInner {
current_in: u64,
old_in: u64,
Expand Down
1 change: 1 addition & 0 deletions src/unix/apple/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ impl fmt::Display for ProcessStatus {

/// Enum describing the different status of a thread.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub(crate) enum ThreadStatus {
/// Thread is running normally.
Running,
Expand Down
2 changes: 1 addition & 1 deletion src/unix/freebsd/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use super::ffi::{
use super::utils::{c_buf_to_utf8_str, get_sys_value_str_by_name};
use crate::{Disk, DiskKind, DiskRefreshKind, DiskUsage};

#[derive(Debug)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) struct DiskInner {
name: OsString,
c_mount_point: Vec<libc::c_char>,
Expand Down
4 changes: 4 additions & 0 deletions src/unix/freebsd/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ macro_rules! old_and_new {
}};
}

#[derive(Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub(crate) struct NetworksInner {
pub(crate) interfaces: HashMap<String, NetworkData>,
}
Expand Down Expand Up @@ -131,6 +133,8 @@ impl NetworksInner {
}
}

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub(crate) struct NetworkDataInner {
/// Total number of bytes received over interface.
ifi_ibytes: u64,
Expand Down
4 changes: 4 additions & 0 deletions src/unix/freebsd/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ impl fmt::Display for ProcessStatus {
}
}

#[derive(Clone, derivative::Derivative)]
#[derivative(PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub(crate) struct ProcessInner {
pub(crate) name: OsString,
pub(crate) cmd: Vec<OsString>,
Expand All @@ -52,6 +55,7 @@ pub(crate) struct ProcessInner {
pub(crate) memory: u64,
pub(crate) virtual_memory: u64,
pub(crate) updated: bool,
#[derivative(Ord = "ignore", PartialEq = "ignore")]
cpu_usage: f32,
start_time: u64,
run_time: u64,
Expand Down
1 change: 1 addition & 0 deletions src/unix/linux/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ macro_rules! cast {
};
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) struct DiskInner {
type_: DiskKind,
device_name: OsString,
Expand Down
4 changes: 4 additions & 0 deletions src/unix/linux/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ fn refresh_networks_list_from_sysfs(
}
}

#[derive(Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub(crate) struct NetworksInner {
pub(crate) interfaces: HashMap<String, NetworkData>,
}
Expand All @@ -153,6 +155,8 @@ impl NetworksInner {
}
}

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub(crate) struct NetworkDataInner {
/// Total number of bytes received over interface.
rx_bytes: u64,
Expand Down
Loading

0 comments on commit 1b5ae87

Please sign in to comment.