sinf 0.1.0

Library for obtaining in-depth system information at both the hardware and software levels
Documentation
#![forbid(unsafe_code)]

#[cfg(all(windows, any(feature = "user", feature = "system")))]
pub use crate::platform::windows::Sid;

/// Read and written byte counts.
#[cfg(any(feature = "disk", feature = "system"))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd)]
pub struct DiskUsage {
    /// Total number of bytes written.
    pub total_written_bytes: u64,
    /// Number of bytes written since the last refresh.
    pub written_bytes: u64,
    /// Total number of bytes read.
    pub total_read_bytes: u64,
    /// Number of bytes read since the last refresh.
    pub read_bytes: u64,
}

macro_rules! xid {
    ($(#[$outer:meta])+ $name:ident, $type:ty $(, $trait:ty)?) => {
        #[cfg(any(feature = "system", feature = "user"))]
        $(#[$outer])+
        #[repr(transparent)]
        #[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
        pub struct $name(pub(crate) $type);

        #[cfg(any(feature = "system", feature = "user"))]
        impl std::ops::Deref for $name {
            type Target = $type;

            fn deref(&self) -> &Self::Target {
                &self.0
            }
        }

        #[cfg(any(feature = "system", feature = "user"))]
        impl std::fmt::Display for $name {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                std::fmt::Display::fmt(&self.0, f)
            }
        }

        $(
        #[cfg(any(feature = "system", feature = "user"))]
        impl TryFrom<usize> for $name {
            type Error = <$type as TryFrom<usize>>::Error;

            fn try_from(t: usize) -> Result<Self, <$type as TryFrom<usize>>::Error> {
                Ok(Self(<$type>::try_from(t)?))
            }
        }

        #[cfg(any(feature = "system", feature = "user"))]
        impl $trait for $name {
            type Err = <$type as $trait>::Err;

            fn from_str(t: &str) -> Result<Self, <$type as $trait>::Err> {
                Ok(Self(<$type>::from_str(t)?))
            }
        }
        )?
    };
}

macro_rules! uid {
    ($type:ty$(, $trait:ty)?) => {
        xid!(
            /// A user ID wrapping a platform-specific type.
            Uid,
            $type
            $(, $trait)?
        );
    };
}

#[allow(unused_macros)]
macro_rules! gid {
    ($type:ty) => {
        xid!(
            /// A group ID wrapping a platform-specific type.
            #[derive(Copy)]
            Gid,
            $type,
            std::str::FromStr
        );
    };
}

#[allow(unused_macros)]
macro_rules! gid_sid {
    ($type:ty) => {
        xid!(
            /// A group ID wrapping a platform-specific security identifier.
            Gid,
            $type
        );

        #[cfg(any(feature = "system", feature = "user"))]
        impl std::str::FromStr for Gid {
            type Err = <$type as std::str::FromStr>::Err;

            fn from_str(t: &str) -> Result<Self, Self::Err> {
                Ok(Self(t.parse()?))
            }
        }
    };
}

cfg_select! {
    all(
        any(
            target_os = "freebsd",
            target_os = "netbsd",
            target_os = "linux",
            target_os = "android",
            target_os = "macos",
            target_os = "ios",
        )
    ) => {
        uid!(libc::uid_t, std::str::FromStr);
        gid!(libc::gid_t);
    }
    windows => {
        uid!(crate::platform::windows::Sid);
        gid_sid!(crate::platform::windows::Sid);
        // Manual implementation outside the macro...
        #[cfg(any(feature = "system", feature = "user"))]
        impl std::str::FromStr for Uid {
            type Err = <crate::platform::windows::Sid as std::str::FromStr>::Err;

            fn from_str(t: &str) -> Result<Self, Self::Err> {
                Ok(Self(t.parse()?))
            }
        }
    }
    _ => {
        uid!(u32, std::str::FromStr);
        gid!(u32);
    }
}