From 245335a7b463203e4bcd60a3c173271797b1aa7b Mon Sep 17 00:00:00 2001 From: "J. David Lowe" Date: Fri, 22 Nov 2024 13:19:54 -0800 Subject: [PATCH] impl_get_set is now needed by more than just `system` feature --- src/common/disk.rs | 30 +++++-- src/common/impl_get_set.rs | 171 ++++++++++++++++++++++++++++++++++++ src/common/mod.rs | 2 + src/common/system.rs | 173 +------------------------------------ 4 files changed, 199 insertions(+), 177 deletions(-) create mode 100644 src/common/impl_get_set.rs diff --git a/src/common/disk.rs b/src/common/disk.rs index 1c18f9850..f55880911 100644 --- a/src/common/disk.rs +++ b/src/common/disk.rs @@ -4,8 +4,8 @@ use std::ffi::OsStr; use std::fmt; use std::path::Path; +use crate::common::impl_get_set::impl_get_set; use crate::DiskUsage; -use crate::common::system::impl_get_set; /// Struct containing a disk information. /// @@ -516,9 +516,29 @@ impl DiskRefreshKind { } impl_get_set!(DiskRefreshKind, kind, with_kind, without_kind); - impl_get_set!(DiskRefreshKind, total_space, with_total_space, without_total_space); - impl_get_set!(DiskRefreshKind, available_space, with_available_space, without_available_space); - impl_get_set!(DiskRefreshKind, is_removable, with_is_removable, without_is_removable); - impl_get_set!(DiskRefreshKind, is_read_only, with_is_read_only, without_is_read_only); + impl_get_set!( + DiskRefreshKind, + total_space, + with_total_space, + without_total_space + ); + impl_get_set!( + DiskRefreshKind, + available_space, + with_available_space, + without_available_space + ); + impl_get_set!( + DiskRefreshKind, + is_removable, + with_is_removable, + without_is_removable + ); + impl_get_set!( + DiskRefreshKind, + is_read_only, + with_is_read_only, + without_is_read_only + ); impl_get_set!(DiskRefreshKind, usage, with_usage, without_usage); } diff --git a/src/common/impl_get_set.rs b/src/common/impl_get_set.rs new file mode 100644 index 000000000..4bcdb914a --- /dev/null +++ b/src/common/impl_get_set.rs @@ -0,0 +1,171 @@ +macro_rules! impl_get_set { + ($ty_name:ident, $name:ident, $with:ident, $without:ident $(, $extra_doc:literal)? $(,)?) => { + #[doc = concat!("Returns the value of the \"", stringify!($name), "\" refresh kind.")] + $(#[doc = concat!(" +", $extra_doc, " +")])? + #[doc = concat!(" +``` +use sysinfo::", stringify!($ty_name), "; + +let r = ", stringify!($ty_name), "::new(); + +let r = r.with_", stringify!($name), "(); +assert_eq!(r.", stringify!($name), "(), true); + +let r = r.without_", stringify!($name), "(); +assert_eq!(r.", stringify!($name), "(), false); +```")] + pub fn $name(&self) -> bool { + self.$name + } + + #[doc = concat!("Sets the value of the \"", stringify!($name), "\" refresh kind to `true`. + +``` +use sysinfo::", stringify!($ty_name), "; + +let r = ", stringify!($ty_name), "::new(); + +let r = r.with_", stringify!($name), "(); +assert_eq!(r.", stringify!($name), "(), true); +```")] + #[must_use] + pub fn $with(mut self) -> Self { + self.$name = true; + self + } + + #[doc = concat!("Sets the value of the \"", stringify!($name), "\" refresh kind to `false`. + +``` +use sysinfo::", stringify!($ty_name), "; + +let r = ", stringify!($ty_name), "::everything(); +assert_eq!(r.", stringify!($name), "(), true); + +let r = r.without_", stringify!($name), "(); +assert_eq!(r.", stringify!($name), "(), false); +```")] + #[must_use] + pub fn $without(mut self) -> Self { + self.$name = false; + self + } + }; + + // To handle `UpdateKind`. + ($ty_name:ident, $name:ident, $with:ident, $without:ident, UpdateKind $(, $extra_doc:literal)? $(,)?) => { + #[doc = concat!("Returns the value of the \"", stringify!($name), "\" refresh kind.")] + $(#[doc = concat!(" +", $extra_doc, " +")])? + #[doc = concat!(" +``` +use sysinfo::{", stringify!($ty_name), ", UpdateKind}; + +let r = ", stringify!($ty_name), "::new(); +assert_eq!(r.", stringify!($name), "(), UpdateKind::Never); + +let r = r.with_", stringify!($name), "(UpdateKind::OnlyIfNotSet); +assert_eq!(r.", stringify!($name), "(), UpdateKind::OnlyIfNotSet); + +let r = r.without_", stringify!($name), "(); +assert_eq!(r.", stringify!($name), "(), UpdateKind::Never); +```")] + pub fn $name(&self) -> UpdateKind { + self.$name + } + + #[doc = concat!("Sets the value of the \"", stringify!($name), "\" refresh kind. + +``` +use sysinfo::{", stringify!($ty_name), ", UpdateKind}; + +let r = ", stringify!($ty_name), "::new(); +assert_eq!(r.", stringify!($name), "(), UpdateKind::Never); + +let r = r.with_", stringify!($name), "(UpdateKind::OnlyIfNotSet); +assert_eq!(r.", stringify!($name), "(), UpdateKind::OnlyIfNotSet); +```")] + #[must_use] + pub fn $with(mut self, kind: UpdateKind) -> Self { + self.$name = kind; + self + } + + #[doc = concat!("Sets the value of the \"", stringify!($name), "\" refresh kind to `UpdateKind::Never`. + +``` +use sysinfo::{", stringify!($ty_name), ", UpdateKind}; + +let r = ", stringify!($ty_name), "::everything(); +assert_eq!(r.", stringify!($name), "(), UpdateKind::OnlyIfNotSet); + +let r = r.without_", stringify!($name), "(); +assert_eq!(r.", stringify!($name), "(), UpdateKind::Never); +```")] + #[must_use] + pub fn $without(mut self) -> Self { + self.$name = UpdateKind::Never; + self + } + }; + + // To handle `*RefreshKind`. + ($ty_name:ident, $name:ident, $with:ident, $without:ident, $typ:ty $(,)?) => { + #[doc = concat!("Returns the value of the \"", stringify!($name), "\" refresh kind. + +``` +use sysinfo::{", stringify!($ty_name), ", ", stringify!($typ), "}; + +let r = ", stringify!($ty_name), "::new(); +assert_eq!(r.", stringify!($name), "().is_some(), false); + +let r = r.with_", stringify!($name), "(", stringify!($typ), "::everything()); +assert_eq!(r.", stringify!($name), "().is_some(), true); + +let r = r.without_", stringify!($name), "(); +assert_eq!(r.", stringify!($name), "().is_some(), false); +```")] + pub fn $name(&self) -> Option<$typ> { + self.$name + } + + #[doc = concat!("Sets the value of the \"", stringify!($name), "\" refresh kind to `Some(...)`. + +``` +use sysinfo::{", stringify!($ty_name), ", ", stringify!($typ), "}; + +let r = ", stringify!($ty_name), "::new(); +assert_eq!(r.", stringify!($name), "().is_some(), false); + +let r = r.with_", stringify!($name), "(", stringify!($typ), "::everything()); +assert_eq!(r.", stringify!($name), "().is_some(), true); +```")] + #[must_use] + pub fn $with(mut self, kind: $typ) -> Self { + self.$name = Some(kind); + self + } + + #[doc = concat!("Sets the value of the \"", stringify!($name), "\" refresh kind to `None`. + +``` +use sysinfo::", stringify!($ty_name), "; + +let r = ", stringify!($ty_name), "::everything(); +assert_eq!(r.", stringify!($name), "().is_some(), true); + +let r = r.without_", stringify!($name), "(); +assert_eq!(r.", stringify!($name), "().is_some(), false); +```")] + #[must_use] + pub fn $without(mut self) -> Self { + self.$name = None; + self + } + }; +} + +pub(crate) use impl_get_set; diff --git a/src/common/mod.rs b/src/common/mod.rs index cbbf52131..b59ba48f3 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -4,6 +4,8 @@ pub(crate) mod component; #[cfg(feature = "disk")] pub(crate) mod disk; +#[cfg(any(feature = "system", feature = "disk"))] +pub(crate) mod impl_get_set; #[cfg(feature = "network")] pub(crate) mod network; #[cfg(feature = "system")] diff --git a/src/common/system.rs b/src/common/system.rs index e75008676..73fd41078 100644 --- a/src/common/system.rs +++ b/src/common/system.rs @@ -6,6 +6,7 @@ use std::fmt; use std::path::Path; use std::str::FromStr; +use crate::common::impl_get_set::impl_get_set; use crate::common::DiskUsage; use crate::{CpuInner, Gid, ProcessInner, SystemInner, Uid}; @@ -1686,178 +1687,6 @@ cfg_if! { } } -macro_rules! impl_get_set { - ($ty_name:ident, $name:ident, $with:ident, $without:ident $(, $extra_doc:literal)? $(,)?) => { - #[doc = concat!("Returns the value of the \"", stringify!($name), "\" refresh kind.")] - $(#[doc = concat!(" -", $extra_doc, " -")])? - #[doc = concat!(" -``` -use sysinfo::", stringify!($ty_name), "; - -let r = ", stringify!($ty_name), "::new(); - -let r = r.with_", stringify!($name), "(); -assert_eq!(r.", stringify!($name), "(), true); - -let r = r.without_", stringify!($name), "(); -assert_eq!(r.", stringify!($name), "(), false); -```")] - pub fn $name(&self) -> bool { - self.$name - } - - #[doc = concat!("Sets the value of the \"", stringify!($name), "\" refresh kind to `true`. - -``` -use sysinfo::", stringify!($ty_name), "; - -let r = ", stringify!($ty_name), "::new(); - -let r = r.with_", stringify!($name), "(); -assert_eq!(r.", stringify!($name), "(), true); -```")] - #[must_use] - pub fn $with(mut self) -> Self { - self.$name = true; - self - } - - #[doc = concat!("Sets the value of the \"", stringify!($name), "\" refresh kind to `false`. - -``` -use sysinfo::", stringify!($ty_name), "; - -let r = ", stringify!($ty_name), "::everything(); -assert_eq!(r.", stringify!($name), "(), true); - -let r = r.without_", stringify!($name), "(); -assert_eq!(r.", stringify!($name), "(), false); -```")] - #[must_use] - pub fn $without(mut self) -> Self { - self.$name = false; - self - } - }; - - // To handle `UpdateKind`. - ($ty_name:ident, $name:ident, $with:ident, $without:ident, UpdateKind $(, $extra_doc:literal)? $(,)?) => { - #[doc = concat!("Returns the value of the \"", stringify!($name), "\" refresh kind.")] - $(#[doc = concat!(" -", $extra_doc, " -")])? - #[doc = concat!(" -``` -use sysinfo::{", stringify!($ty_name), ", UpdateKind}; - -let r = ", stringify!($ty_name), "::new(); -assert_eq!(r.", stringify!($name), "(), UpdateKind::Never); - -let r = r.with_", stringify!($name), "(UpdateKind::OnlyIfNotSet); -assert_eq!(r.", stringify!($name), "(), UpdateKind::OnlyIfNotSet); - -let r = r.without_", stringify!($name), "(); -assert_eq!(r.", stringify!($name), "(), UpdateKind::Never); -```")] - pub fn $name(&self) -> UpdateKind { - self.$name - } - - #[doc = concat!("Sets the value of the \"", stringify!($name), "\" refresh kind. - -``` -use sysinfo::{", stringify!($ty_name), ", UpdateKind}; - -let r = ", stringify!($ty_name), "::new(); -assert_eq!(r.", stringify!($name), "(), UpdateKind::Never); - -let r = r.with_", stringify!($name), "(UpdateKind::OnlyIfNotSet); -assert_eq!(r.", stringify!($name), "(), UpdateKind::OnlyIfNotSet); -```")] - #[must_use] - pub fn $with(mut self, kind: UpdateKind) -> Self { - self.$name = kind; - self - } - - #[doc = concat!("Sets the value of the \"", stringify!($name), "\" refresh kind to `UpdateKind::Never`. - -``` -use sysinfo::{", stringify!($ty_name), ", UpdateKind}; - -let r = ", stringify!($ty_name), "::everything(); -assert_eq!(r.", stringify!($name), "(), UpdateKind::OnlyIfNotSet); - -let r = r.without_", stringify!($name), "(); -assert_eq!(r.", stringify!($name), "(), UpdateKind::Never); -```")] - #[must_use] - pub fn $without(mut self) -> Self { - self.$name = UpdateKind::Never; - self - } - }; - - // To handle `*RefreshKind`. - ($ty_name:ident, $name:ident, $with:ident, $without:ident, $typ:ty $(,)?) => { - #[doc = concat!("Returns the value of the \"", stringify!($name), "\" refresh kind. - -``` -use sysinfo::{", stringify!($ty_name), ", ", stringify!($typ), "}; - -let r = ", stringify!($ty_name), "::new(); -assert_eq!(r.", stringify!($name), "().is_some(), false); - -let r = r.with_", stringify!($name), "(", stringify!($typ), "::everything()); -assert_eq!(r.", stringify!($name), "().is_some(), true); - -let r = r.without_", stringify!($name), "(); -assert_eq!(r.", stringify!($name), "().is_some(), false); -```")] - pub fn $name(&self) -> Option<$typ> { - self.$name - } - - #[doc = concat!("Sets the value of the \"", stringify!($name), "\" refresh kind to `Some(...)`. - -``` -use sysinfo::{", stringify!($ty_name), ", ", stringify!($typ), "}; - -let r = ", stringify!($ty_name), "::new(); -assert_eq!(r.", stringify!($name), "().is_some(), false); - -let r = r.with_", stringify!($name), "(", stringify!($typ), "::everything()); -assert_eq!(r.", stringify!($name), "().is_some(), true); -```")] - #[must_use] - pub fn $with(mut self, kind: $typ) -> Self { - self.$name = Some(kind); - self - } - - #[doc = concat!("Sets the value of the \"", stringify!($name), "\" refresh kind to `None`. - -``` -use sysinfo::", stringify!($ty_name), "; - -let r = ", stringify!($ty_name), "::everything(); -assert_eq!(r.", stringify!($name), "().is_some(), true); - -let r = r.without_", stringify!($name), "(); -assert_eq!(r.", stringify!($name), "().is_some(), false); -```")] - #[must_use] - pub fn $without(mut self) -> Self { - self.$name = None; - self - } - }; -} - -pub(crate) use impl_get_set; - /// This enum allows you to specify when you want the related information to be updated. /// /// For example if you only want the [`Process::exe()`] information to be refreshed only if it's not