sinf 0.1.0

Library for obtaining in-depth system information at both the hardware and software levels
Documentation
#[doc(hidden)]
#[allow(unused)]
macro_rules! sinf_debug {
    ($($x:tt)*) => {{
        #[cfg(feature = "debug")]
        eprintln!($($x)*);
    }}
}

#[cfg(feature = "system")]
macro_rules! declare_signals {
    ($kind:ty, _ => None,) => (
        use crate::Signal;

        pub(crate) const fn supported_signals() -> &'static [Signal] {
            &[]
        }
    );

    ($kind:ty, $(Signal::$signal:ident => $map:expr,)+ _ => None,) => (
        use crate::Signal;

        pub(crate) const fn supported_signals() -> &'static [Signal] {
            &[$(Signal::$signal,)*]
        }

        #[inline]
        pub(crate) fn convert_signal(s: Signal) -> Option<$kind> {
            match s {
                $(Signal::$signal => Some($map),)*
                _ => None,
            }
        }
    );

    ($kind:ty, $(Signal::$signal:ident => $map:expr,)+) => (
        use crate::Signal;

        pub(crate) const fn supported_signals() -> &'static [Signal] {
            &[$(Signal::$signal,)*]
        }

        #[inline]
        pub(crate) fn convert_signal(s: Signal) -> Option<$kind> {
            match s {
                $(Signal::$signal => Some($map),)*
            }
        }
    )
}

#[cfg(unix)]
#[allow(unused_macros)]
macro_rules! retry_eintr {
    // Reset `errno` before every attempt, not only the first: otherwise a stale `EINTR` from an interrupted attempt remains when the retried call succeeds without touching `errno`.
    (set_to_0 => $kind:ident => $($t:tt)+) => {
        retry_eintr!($kind => { retry_eintr!(@reset_errno); $($t)+ })
    };
    (set_to_0 => $($t:tt)+) => {
        retry_eintr!({ retry_eintr!(@reset_errno); $($t)+ })
    };
    (@reset_errno) => {{
        #[allow(unused_unsafe)]
        let errno = unsafe { crate::platform::unix::libc_errno() };
        if !errno.is_null() {
            #[allow(unused_unsafe)]
            unsafe { *errno = 0; }
        }
    }};
    (null => $($t:tt)+) => {{
        loop {
            let ret = $($t)+;
            if ret.is_null() && std::io::Error::last_os_error().kind() == std::io::ErrorKind::Interrupted {
                continue;
            }
            break ret;
        }
    }};
    ($errno_value:ident => $($t:tt)+) => {{
        loop {
            let ret = $($t)+;
            if ret < 0 {
                let tmp = std::io::Error::last_os_error();
                if tmp.kind() == std::io::ErrorKind::Interrupted {
                    continue;
                }
                $errno_value = tmp.raw_os_error().unwrap_or(0);
            }
            break ret;
        }
    }};
    ($($t:tt)+) => {{
        loop {
            let ret = $($t)+;
            if ret < 0 && std::io::Error::last_os_error().kind() == std::io::ErrorKind::Interrupted {
                continue;
            }
            break ret;
        }
    }};
}

#[cfg(test)]
#[allow(unused_macros)]
macro_rules! check_unsupported {
    ($e:expr) => {
        match $e {
            Ok(a) => a,
            Err(error) => {
                if !matches!(error, crate::Error::Unsupported) {
                    panic!("Expected `Error::Unsupported`, found {error:?}");
                }
                return;
            }
        }
    };
}

#[cfg(any(
    feature = "component",
    feature = "disk",
    feature = "gpu",
    feature = "network",
    feature = "user"
))]
#[allow(unused_macros)]
#[doc(hidden)]
macro_rules! impl_collection_field {
    ($collection:ty, $item:ty, $($field:ident).+) => {
        impl std::ops::Deref for $collection {
            type Target = [$item];

            fn deref(&self) -> &Self::Target {
                &self.$($field).+
            }
        }

        impl std::ops::DerefMut for $collection {
            fn deref_mut(&mut self) -> &mut Self::Target {
                &mut self.$($field).+
            }
        }

        impl<'a> IntoIterator for &'a $collection {
            type Item = &'a $item;
            type IntoIter = std::slice::Iter<'a, $item>;

            fn into_iter(self) -> Self::IntoIter {
                self.$($field).+.iter()
            }
        }

        impl<'a> IntoIterator for &'a mut $collection {
            type Item = &'a mut $item;
            type IntoIter = std::slice::IterMut<'a, $item>;

            fn into_iter(self) -> Self::IntoIter {
                self.$($field).+.iter_mut()
            }
        }
    };
}

#[cfg(any(
    feature = "component",
    feature = "disk",
    feature = "gpu",
    feature = "network",
    feature = "user"
))]
#[allow(unused_macros)]
#[doc(hidden)]
macro_rules! impl_collection_method {
    ($collection:ty, $item:ty, $inner:ident, $get:ident, $get_mut:ident) => {
        impl std::ops::Deref for $collection {
            type Target = [$item];

            fn deref(&self) -> &Self::Target {
                self.$inner.$get()
            }
        }

        impl std::ops::DerefMut for $collection {
            fn deref_mut(&mut self) -> &mut Self::Target {
                self.$inner.$get_mut()
            }
        }

        impl<'a> IntoIterator for &'a $collection {
            type Item = &'a $item;
            type IntoIter = std::slice::Iter<'a, $item>;

            fn into_iter(self) -> Self::IntoIter {
                self.$inner.$get().iter()
            }
        }

        impl<'a> IntoIterator for &'a mut $collection {
            type Item = &'a mut $item;
            type IntoIter = std::slice::IterMut<'a, $item>;

            fn into_iter(self) -> Self::IntoIter {
                self.$inner.$get_mut().iter_mut()
            }
        }
    };
}

#[cfg(any(
    feature = "component",
    feature = "disk",
    feature = "gpu",
    feature = "network",
    feature = "user"
))]
#[allow(unused_macros)]
#[doc(hidden)]
macro_rules! impl_new_with_refreshed_list {
    ($collection:ident) => {
        #[doc = concat!("Creates a [`", stringify!($collection), "`] with the list loaded.")]
        pub fn new_with_refreshed_list() -> Result<Self, crate::Error> {
            let mut collection = Self::new()?;
            collection.refresh();
            Ok(collection)
        }
    };
    ($collection:ident, $arg:expr) => {
        #[doc = concat!("Creates a [`", stringify!($collection), "`] with the list loaded.")]
        pub fn new_with_refreshed_list() -> Result<Self, crate::Error> {
            let mut collection = Self::new()?;
            collection.refresh($arg);
            Ok(collection)
        }
    };
}