Skip to content

Commit

Permalink
Merge pull request #1122 from GuillaumeGomez/network-cleanup
Browse files Browse the repository at this point in the history
More network cleanup
  • Loading branch information
GuillaumeGomez authored Nov 1, 2023
2 parents 7515fb6 + 6b7f14a commit b4c087e
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 55 deletions.
55 changes: 20 additions & 35 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1562,7 +1562,7 @@ pub struct Networks {

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

fn into_iter(self) -> Self::IntoIter {
self.iter()
Expand All @@ -1576,7 +1576,9 @@ impl Default for Networks {
}

impl Networks {
/// Creates a new [`Networks`][crate::Networks] type.
/// Creates a new empty [`Networks`][crate::Networks] type.
///
/// If you want it to be filled directly, take a look at [`Networks::new_with_refreshed_list`].
///
/// ```no_run
/// use sysinfo::Networks;
Expand Down Expand Up @@ -1611,22 +1613,18 @@ impl Networks {
networks
}

/// Returns an iterator over the network interfaces.
/// Returns the network interfaces map.
///
/// ```no_run
/// use sysinfo::Networks;
///
/// let networks = Networks::new_with_refreshed_list();
/// for (interface_name, data) in &networks {
/// println!(
/// "[{interface_name}] in: {}, out: {}",
/// data.received(),
/// data.transmitted(),
/// );
/// for network in networks.list() {
/// eprintln!("{network:?}");
/// }
/// ```
pub fn iter(&self) -> NetworksIter {
self.inner.iter()
pub fn list(&self) -> &HashMap<String, NetworkData> {
self.inner.list()
}

/// Refreshes the network interfaces list.
Expand Down Expand Up @@ -1662,31 +1660,11 @@ impl Networks {
}
}

/// Iterator over network interfaces.
///
/// It is returned by [`Networks::iter`][crate::Networks#method.iter].
///
/// ```no_run
/// use sysinfo::Networks;
///
/// let networks = Networks::new();
/// let networks_iter = networks.iter();
/// ```
pub struct NetworksIter<'a> {
inner: std::collections::hash_map::Iter<'a, String, NetworkData>,
}
impl std::ops::Deref for Networks {
type Target = HashMap<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()
fn deref(&self) -> &Self::Target {
self.list()
}
}

Expand Down Expand Up @@ -2057,6 +2035,8 @@ impl<'a> IntoIterator for &'a mut Disks {
impl Disks {
/// Creates a new empty [`Disks`][crate::Disks] type.
///
/// If you want it to be filled directly, take a look at [`Disks::new_with_refreshed_list`].
///
/// ```no_run
/// use sysinfo::Disks;
///
Expand Down Expand Up @@ -2263,6 +2243,8 @@ impl<'a> IntoIterator for &'a mut Users {
impl Users {
/// Creates a new empty [`Users`][crate::Users] type.
///
/// If you want it to be filled directly, take a look at [`Users::new_with_refreshed_list`].
///
/// ```no_run
/// use sysinfo::Users;
///
Expand Down Expand Up @@ -3059,6 +3041,9 @@ impl<'a> IntoIterator for &'a mut Components {
impl Components {
/// Creates a new empty [`Components`][crate::Components] type.
///
/// If you want it to be filled directly, take a look at
/// [`Components::new_with_refreshed_list`].
///
/// ```no_run
/// use sysinfo::Components;
///
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ cfg_if::cfg_if! {

pub use crate::common::{
get_current_pid, CGroupLimits, Component, Components, Cpu, CpuRefreshKind, Disk, DiskKind,
DiskUsage, Disks, Gid, Group, LoadAvg, MacAddr, NetworkData, Networks, NetworksIter, Pid,
Process, ProcessRefreshKind, ProcessStatus, RefreshKind, Signal, System, Uid, User, Users,
DiskUsage, Disks, Gid, Group, LoadAvg, MacAddr, NetworkData, Networks, Pid, Process,
ProcessRefreshKind, ProcessStatus, RefreshKind, Signal, System, Uid, User, Users,
};

pub(crate) use crate::sys::{
Expand Down
7 changes: 3 additions & 4 deletions src/unix/apple/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::ptr::null_mut;

use crate::common::MacAddr;
use crate::network::refresh_networks_addresses;
use crate::{NetworkData, NetworksIter};
use crate::NetworkData;

macro_rules! old_and_new {
($ty_:expr, $name:ident, $old:ident, $new_val:expr) => {{
Expand All @@ -27,9 +27,8 @@ impl NetworksInner {
}
}

#[allow(clippy::needless_lifetimes)]
pub(crate) fn iter<'a>(&'a self) -> NetworksIter<'a> {
NetworksIter::new(self.interfaces.iter())
pub(crate) fn list(&self) -> &HashMap<String, NetworkData> {
&self.interfaces
}

pub(crate) fn refresh_list(&mut self) {
Expand Down
6 changes: 3 additions & 3 deletions src/unix/freebsd/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::mem::MaybeUninit;
use super::utils;
use crate::common::MacAddr;
use crate::network::refresh_networks_addresses;
use crate::{NetworkData, NetworksIter};
use crate::NetworkData;

macro_rules! old_and_new {
($ty_:expr, $name:ident, $old:ident, $data:expr) => {{
Expand All @@ -26,8 +26,8 @@ impl NetworksInner {
}
}

pub(crate) fn iter(&self) -> NetworksIter {
NetworksIter::new(self.interfaces.iter())
pub(crate) fn list(&self) -> &HashMap<String, NetworkData> {
&self.interfaces
}

pub(crate) fn refresh_list(&mut self) {
Expand Down
8 changes: 4 additions & 4 deletions src/unix/linux/network.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use std::collections::{hash_map, HashMap};
use std::io::Read;
use std::path::Path;
use std::{fs::File, u8};

use crate::common::MacAddr;
use crate::network::refresh_networks_addresses;
use crate::{NetworkData, NetworksIter};
use std::collections::{hash_map, HashMap};
use crate::NetworkData;

macro_rules! old_and_new {
($ty_:expr, $name:ident, $old:ident) => {{
Expand Down Expand Up @@ -122,8 +122,8 @@ impl NetworksInner {
}
}

pub(crate) fn iter(&self) -> NetworksIter {
NetworksIter::new(self.interfaces.iter())
pub(crate) fn list(&self) -> &HashMap<String, NetworkData> {
&self.interfaces
}

pub(crate) fn refresh(&mut self) {
Expand Down
6 changes: 3 additions & 3 deletions src/unknown/network.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::common::MacAddr;
use crate::{NetworkData, NetworksIter};
use crate::NetworkData;

use std::collections::HashMap;

Expand All @@ -16,8 +16,8 @@ impl NetworksInner {
}
}

pub(crate) fn iter(&self) -> NetworksIter {
NetworksIter::new(self.interfaces.iter())
pub(crate) fn list(&self) -> &HashMap<String, NetworkData> {
&self.interfaces
}

pub(crate) fn refresh_list(&mut self) {}
Expand Down
7 changes: 3 additions & 4 deletions src/windows/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::common::MacAddr;
use crate::network::refresh_networks_addresses;
use crate::{NetworkData, NetworksIter};
use crate::NetworkData;

use std::collections::{hash_map, HashMap};

Expand All @@ -29,9 +29,8 @@ impl NetworksInner {
}
}

#[allow(clippy::needless_lifetimes)]
pub(crate) fn iter<'a>(&'a self) -> NetworksIter<'a> {
NetworksIter::new(self.interfaces.iter())
pub(crate) fn list(&self) -> &HashMap<String, NetworkData> {
&self.interfaces
}

pub(crate) fn refresh_list(&mut self) {
Expand Down

0 comments on commit b4c087e

Please sign in to comment.