[go: up one dir, main page]

sysinfo 0.10.3

Library to handle processes
Documentation
//
// Sysinfo
//
// Copyright (c) 2015 Guillaume Gomez
//

/// Trait to have a common fallback for the `Pid` type.
pub trait AsU32 {
    /// Allows to convert `Pid` into `u32`.
    fn as_u32(&self) -> u32;
}

cfg_if! {
    if #[cfg(any(windows, target_os = "unknown", target_arch = "wasm32"))] {
        /// Process id.
        pub type Pid = usize;

        impl AsU32 for Pid {
            fn as_u32(&self) -> u32 {
                *self as u32
            }
        }
    } else {
        use libc::pid_t;

        /// Process id.
        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.

# Examples

```
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 $name(&self) -> bool {
                        self.$name
                    }
                }

        doc_comment! {
        concat!("Sets the value of the \"", stringify!($name), "\" refresh kind to `true`.

# Examples

```
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`.

# Examples

```
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
                    }
                }
    };
}

/// Used to determine what you want to refresh specifically on [`System`] type.
///
/// # Example
///
/// ```
/// use sysinfo::{RefreshKind, System, SystemExt};
///
/// // We want everything except disks.
/// let mut system = System::new_with_specifics(RefreshKind::everything().without_disk_list());
///
/// assert_eq!(system.get_disks().len(), 0);
/// assert!(system.get_process_list().len() > 0);
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct RefreshKind {
    network: bool,
    processes: bool,
    disk_list: bool,
    disks: bool,
    memory: bool,
    cpu: bool,
    temperatures: bool,
}

impl RefreshKind {
    /// Creates a new `RefreshKind` with every refresh set to `false`.
    ///
    /// # Examples
    ///
    /// ```
    /// use sysinfo::RefreshKind;
    ///
    /// let r = RefreshKind::new();
    ///
    /// assert_eq!(r.network(), false);
    /// assert_eq!(r.processes(), false);
    /// assert_eq!(r.disk_list(), false);
    /// assert_eq!(r.disks(), false);
    /// assert_eq!(r.memory(), false);
    /// assert_eq!(r.cpu(), false);
    /// assert_eq!(r.temperatures(), false);
    /// ```
    pub fn new() -> RefreshKind {
        RefreshKind {
            network: false,
            processes: false,
            disks: false,
            disk_list: false,
            memory: false,
            cpu: false,
            temperatures: false,
        }
    }

    /// Creates a new `RefreshKind` with every refresh set to `true`.
    ///
    /// # Examples
    ///
    /// ```
    /// use sysinfo::RefreshKind;
    ///
    /// let r = RefreshKind::everything();
    ///
    /// assert_eq!(r.network(), true);
    /// assert_eq!(r.processes(), true);
    /// assert_eq!(r.disk_list(), true);
    /// assert_eq!(r.disks(), true);
    /// assert_eq!(r.memory(), true);
    /// assert_eq!(r.cpu(), true);
    /// assert_eq!(r.temperatures(), true);
    /// ```
    pub fn everything() -> RefreshKind {
        RefreshKind {
            network: true,
            processes: true,
            disks: true,
            disk_list: true,
            memory: true,
            cpu: true,
            temperatures: true,
        }
    }

    impl_get_set!(network, with_network, without_network);
    impl_get_set!(processes, with_processes, without_processes);
    impl_get_set!(disks, with_disks, without_disks);
    impl_get_set!(disk_list, with_disk_list, without_disk_list);
    impl_get_set!(memory, with_memory, without_memory);
    impl_get_set!(cpu, with_cpu, without_cpu);
    impl_get_set!(temperatures, with_temperatures, without_temperatures);
}