sinf 0.1.0

Library for obtaining in-depth system information at both the hardware and software levels
Documentation
use std::collections::HashMap;
use std::ffi::OsString;

use crate::{Error, NetworkData, NetworksInner};

/// Provides access to network interface information.
#[doc = include_str!("../../docs/snippets/network.md")]
pub struct Networks {
    pub(crate) inner: NetworksInner,
}

impl<'a> IntoIterator for &'a Networks {
    type Item = (&'a OsString, &'a NetworkData);
    type IntoIter = std::collections::hash_map::Iter<'a, OsString, NetworkData>;

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

impl Networks {
    /// Creates an empty [`Networks`][crate::Networks].
    pub fn new() -> Result<Self, Error> {
        Ok(Self {
            inner: NetworksInner::new()?,
        })
    }

    impl_new_with_refreshed_list!(Networks, false);

    /// Refreshes network interface data.
    pub fn refresh(&mut self, remove_not_listed_interfaces: bool) {
        // Most supported systems first list all interfaces, then `retain` only the refreshed ones, then read each interface.
        self.inner.refresh(remove_not_listed_interfaces);
    }
}

impl std::ops::Deref for Networks {
    type Target = HashMap<OsString, NetworkData>;

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