use NetworkData;
use Networks;
use NetworksExt;
use UserExt;
pub trait AsU32 {
fn as_u32(&self) -> u32;
}
cfg_if! {
if #[cfg(any(windows, target_os = "unknown", target_arch = "wasm32"))] {
pub type Pid = usize;
impl AsU32 for Pid {
fn as_u32(&self) -> u32 {
*self as u32
}
}
} else {
use libc::pid_t;
pub type Pid = pid_t;
impl AsU32 for Pid {
fn as_u32(&self) -> u32 {
*self as u32
}
}
}
}
macro_rules! impl_get_set {
($name:ident, $with:ident, $without:ident) => {
doc_comment! {
concat!("Returns the value of the \"", stringify!($name), "\" refresh kind.
```
use sysinfo::RefreshKind;
let r = RefreshKind::new();
assert_eq!(r.", stringify!($name), "(), false);
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_comment! {
concat!("Sets the value of the \"", stringify!($name), "\" refresh kind to `true`.
```
use sysinfo::RefreshKind;
let r = RefreshKind::new();
assert_eq!(r.", stringify!($name), "(), false);
let r = r.with_", stringify!($name), "();
assert_eq!(r.", stringify!($name), "(), true);
```"),
pub fn $with(mut self) -> RefreshKind {
self.$name = true;
self
}
}
doc_comment! {
concat!("Sets the value of the \"", stringify!($name), "\" refresh kind to `false`.
```
use sysinfo::RefreshKind;
let r = RefreshKind::everything();
assert_eq!(r.", stringify!($name), "(), true);
let r = r.without_", stringify!($name), "();
assert_eq!(r.", stringify!($name), "(), false);
```"),
pub fn $without(mut self) -> RefreshKind {
self.$name = false;
self
}
}
};
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct RefreshKind {
networks: bool,
networks_list: bool,
processes: bool,
disks_list: bool,
disks: bool,
memory: bool,
cpu: bool,
components: bool,
components_list: bool,
users_list: bool,
}
impl RefreshKind {
pub fn new() -> RefreshKind {
RefreshKind::default()
}
pub fn everything() -> RefreshKind {
RefreshKind {
networks: true,
networks_list: true,
processes: true,
disks: true,
disks_list: true,
memory: true,
cpu: true,
components: true,
components_list: true,
users_list: true,
}
}
impl_get_set!(networks, with_networks, without_networks);
impl_get_set!(networks_list, with_networks_list, without_networks_list);
impl_get_set!(processes, with_processes, without_processes);
impl_get_set!(disks, with_disks, without_disks);
impl_get_set!(disks_list, with_disks_list, without_disks_list);
impl_get_set!(memory, with_memory, without_memory);
impl_get_set!(cpu, with_cpu, without_cpu);
impl_get_set!(components, with_components, without_components);
impl_get_set!(
components_list,
with_components_list,
without_components_list
);
impl_get_set!(users_list, with_users_list, without_users_list);
}
pub struct NetworksIter<'a> {
inner: std::collections::hash_map::Iter<'a, String, NetworkData>,
}
impl<'a> NetworksIter<'a> {
pub(crate) fn new(v: std::collections::hash_map::Iter<'a, String, NetworkData>) -> Self {
NetworksIter { inner: v }
}
}
impl<'a> Iterator for NetworksIter<'a> {
type Item = (&'a String, &'a NetworkData);
fn next(&mut self) -> Option<Self::Item> {
self.inner.next()
}
}
impl<'a> IntoIterator for &'a Networks {
type Item = (&'a String, &'a NetworkData);
type IntoIter = NetworksIter<'a>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum DiskType {
HDD,
SSD,
Removable,
Unknown(isize),
}
#[repr(C)]
#[derive(Clone, PartialEq, PartialOrd, Debug, Copy)]
pub enum Signal {
Hangup = 1,
Interrupt = 2,
Quit = 3,
Illegal = 4,
Trap = 5,
Abort = 6,
Bus = 7,
FloatingPointException = 8,
Kill = 9,
User1 = 10,
Segv = 11,
User2 = 12,
Pipe = 13,
Alarm = 14,
Term = 15,
Stklft = 16,
Child = 17,
Continue = 18,
Stop = 19,
TSTP = 20,
TTIN = 21,
TTOU = 22,
Urgent = 23,
XCPU = 24,
XFSZ = 25,
VirtualAlarm = 26,
Profiling = 27,
Winch = 28,
IO = 29,
Power = 30,
Sys = 31,
}
#[repr(C)]
#[derive(Default, Debug, Clone)]
pub struct LoadAvg {
pub one: f64,
pub five: f64,
pub fifteen: f64,
}
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug)]
pub struct User {
pub(crate) name: String,
pub(crate) groups: Vec<String>,
}
impl UserExt for User {
fn get_name(&self) -> &str {
&self.name
}
fn get_groups(&self) -> &[String] {
&self.groups
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq, PartialOrd)]
pub struct DiskUsage {
pub total_written_bytes: u64,
pub written_bytes: u64,
pub total_read_bytes: u64,
pub read_bytes: u64,
}