Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove ComponentExt and NetworksExt traits #1095

Merged
merged 1 commit into from
Oct 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Otherwise, here is a little code sample:
```rust
use sysinfo::{
Components, ComponentsExt, Disks, NetworkExt, Networks,
NetworksExt, System,
System,
};

// Please note that we use "new_all" to ensure that all list of
Expand Down
2 changes: 1 addition & 1 deletion benches/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
extern crate test;

use sysinfo::get_current_pid;
use sysinfo::{ComponentsExt, NetworksExt, UsersExt};
use sysinfo::{ComponentsExt, UsersExt};

#[bench]
fn bench_new(b: &mut test::Bencher) {
Expand Down
4 changes: 2 additions & 2 deletions examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use std::io::{self, BufRead, Write};
use std::str::FromStr;
use sysinfo::Signal::*;
use sysinfo::{
Components, ComponentsExt, CpuExt, Disks, NetworkExt, Networks, NetworksExt, Pid, Signal,
System, UserExt, Users, UsersExt,
Components, ComponentsExt, CpuExt, Disks, NetworkExt, Networks, Pid, Signal, System, UserExt,
Users, UsersExt,
};

const signals: &[Signal] = &[
Expand Down
2 changes: 1 addition & 1 deletion src/c_interface.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::{CpuExt, Disks, NetworkExt, Networks, NetworksExt, Pid, Process, System};
use crate::{CpuExt, Disks, NetworkExt, Networks, Pid, Process, System};
use libc::{self, c_char, c_float, c_uint, c_void, size_t};
use std::borrow::BorrowMut;
use std::ffi::CString;
Expand Down
218 changes: 207 additions & 11 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::{
Component, Components, ComponentsExt, Cpu, GroupExt, NetworkData, NetworksExt, ProcessInner,
SystemInner, User, UserExt, UsersExt,
ComponentInner, Components, ComponentsExt, Cpu, GroupExt, NetworkData, NetworksInner,
ProcessInner, SystemInner, User, UserExt, UsersExt,
};

use std::cmp::Ordering;
Expand Down Expand Up @@ -1534,22 +1534,19 @@ impl RefreshKind {
impl_get_set!(RefreshKind, cpu, with_cpu, without_cpu, CpuRefreshKind);
}

/// Network interfaces.
///
/// Don't forget to also take a look at the [`NetworksExt`] trait to see the list of available
/// methods.
/// Interacting with network interfaces.
///
/// ```no_run
/// use sysinfo::{Networks, NetworksExt};
/// use sysinfo::Networks;
///
/// let mut networks = Networks::new();
/// networks.refresh_list();
/// for network in networks.iter() {
/// println!("{:?}", network);
/// for (interface_name, network) in &networks {
/// println!("[{interface_name}]: {network:?}");
/// }
/// ```
pub struct Networks {
pub(crate) interfaces: HashMap<String, NetworkData>,
pub(crate) inner: NetworksInner,
}

impl<'a> IntoIterator for &'a Networks {
Expand All @@ -1561,12 +1558,90 @@ impl<'a> IntoIterator for &'a Networks {
}
}

impl Default for Networks {
fn default() -> Self {
Networks::new()
}
}

impl Networks {
/// Creates a new [`Networks`][crate::Networks] type.
///
/// ```no_run
/// use sysinfo::Networks;
///
/// let mut networks = Networks::new();
/// networks.refresh_list();
/// for (interface_name, network) in &networks {
/// println!("[{interface_name}]: {network:?}");
/// }
/// ```
pub fn new() -> Self {
Self {
inner: NetworksInner::new(),
}
}

/// Returns an iterator over the network interfaces.
///
/// ```no_run
/// use sysinfo::{Networks, NetworkExt, System};
///
/// let mut networks = Networks::new();
/// networks.refresh_list();
/// for (interface_name, data) in &networks {
/// println!(
/// "[{interface_name}] in: {}, out: {}",
/// data.received(),
/// data.transmitted(),
/// );
/// }
/// ```
pub fn iter(&self) -> NetworksIter {
self.inner.iter()
}

/// Refreshes the network interfaces list.
///
/// ```no_run
/// use sysinfo::{Networks, System};
///
/// let mut networks = Networks::new();
/// networks.refresh_list();
/// ```
pub fn refresh_list(&mut self) {
self.inner.refresh_list()
}

/// Refreshes the network interfaces' content. If you didn't run [`Networks::refresh_list`]
/// before, calling this method won't do anything as no interfaces are present.
///
/// ⚠️ If a user is added or removed, this method won't take it into account. Use
/// [`Networks::refresh_list`] instead.
///
/// ⚠️ If you didn't call [`Networks::refresh_list`] beforehand, this method will do nothing
/// as the network list will be empty.
///
/// ```no_run
/// use sysinfo::{Networks, System};
///
/// let mut networks = Networks::new();
/// // Refreshes the network interfaces list.
/// networks.refresh_list();
/// // Wait some time...? Then refresh the data of each network.
/// networks.refresh();
/// ```
pub fn refresh(&mut self) {
self.inner.refresh()
}
}

/// Iterator over network interfaces.
///
/// It is returned by [`Networks::iter`][crate::Networks#method.iter].
///
/// ```no_run
/// use sysinfo::{Networks, NetworksExt};
/// use sysinfo::Networks;
///
/// let networks = Networks::new();
/// let networks_iter = networks.iter();
Expand Down Expand Up @@ -2482,6 +2557,127 @@ impl fmt::Display for MacAddr {
}
}

/// Getting a component temperature information.
///
/// ```no_run
/// use sysinfo::{Components, ComponentsExt};
///
/// let mut components = Components::new();
/// components.refresh_list();
/// for component in components.iter() {
/// println!("{}°C", component.temperature());
/// }
/// ```
pub struct Component {
pub(crate) inner: ComponentInner,
}

impl Component {
/// Returns the temperature of the component (in celsius degree).
///
/// ## Linux
///
/// Returns `f32::NAN` if it failed to retrieve it.
///
/// ```no_run
/// use sysinfo::{Components, ComponentsExt};
///
/// let mut components = Components::new();
/// components.refresh_list();
/// for component in components.iter() {
/// println!("{}°C", component.temperature());
/// }
/// ```
pub fn temperature(&self) -> f32 {
self.inner.temperature()
}

/// Returns the maximum temperature of the component (in celsius degree).
///
/// Note: if `temperature` is higher than the current `max`,
/// `max` value will be updated on refresh.
///
/// ```no_run
/// use sysinfo::{Components, ComponentsExt};
///
/// let mut components = Components::new();
/// components.refresh_list();
/// for component in components.iter() {
/// println!("{}°C", component.max());
/// }
/// ```
///
/// ## Linux
///
/// May be computed by `sysinfo` from kernel.
/// Returns `f32::NAN` if it failed to retrieve it.
pub fn max(&self) -> f32 {
self.inner.max()
}

/// Returns the highest temperature before the component halts (in celsius degree).
///
/// ```no_run
/// use sysinfo::{Components, ComponentsExt};
///
/// let mut components = Components::new();
/// components.refresh_list();
/// for component in components.iter() {
/// println!("{:?}°C", component.critical());
/// }
/// ```
///
/// ## Linux
///
/// Critical threshold defined by chip or kernel.
pub fn critical(&self) -> Option<f32> {
self.inner.critical()
}

/// Returns the label of the component.
///
/// ```no_run
/// use sysinfo::{Components, ComponentsExt};
///
/// let mut components = Components::new();
/// components.refresh_list();
/// for component in components.iter() {
/// println!("{}", component.label());
/// }
/// ```
///
/// ## Linux
///
/// Since components information is retrieved thanks to `hwmon`,
/// the labels are generated as follows.
/// Note: it may change and it was inspired by `sensors` own formatting.
///
/// | name | label | device_model | id_sensor | Computed label by `sysinfo` |
/// |---------|--------|------------|----------|----------------------|
/// | ✓ | ✓ | ✓ | ✓ | `"{name} {label} {device_model} temp{id}"` |
/// | ✓ | ✓ | ✗ | ✓ | `"{name} {label} {id}"` |
/// | ✓ | ✗ | ✓ | ✓ | `"{name} {device_model}"` |
/// | ✓ | ✗ | ✗ | ✓ | `"{name} temp{id}"` |
pub fn label(&self) -> &str {
self.inner.label()
}

/// Refreshes component.
///
/// ```no_run
/// use sysinfo::{Components, ComponentsExt};
///
/// let mut components = Components::new();
/// components.refresh_list();
/// for component in components.iter_mut() {
/// component.refresh();
/// }
/// ```
pub fn refresh(&mut self) {
self.inner.refresh()
}
}

#[cfg(test)]
mod tests {
use super::{MacAddr, ProcessStatus};
Expand Down
4 changes: 2 additions & 2 deletions src/debug.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::{
Component, ComponentExt, Components, Cpu, CpuExt, Disk, Disks, NetworkData, NetworkExt,
Networks, NetworksExt, Process, System, User, UserExt, Users,
Component, Components, Cpu, CpuExt, Disk, Disks, NetworkData, NetworkExt, Networks, Process,
System, User, UserExt, Users,
};

use std::fmt;
Expand Down
16 changes: 8 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,19 @@ cfg_if::cfg_if! {
}

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

pub(crate) use crate::sys::{
ComponentInner, DiskInner, DisksInner, NetworksInner, ProcessInner, SystemInner,
};
pub use crate::sys::{
Component, Components, Cpu, NetworkData, User, IS_SUPPORTED, MINIMUM_CPU_UPDATE_INTERVAL,
Components, Cpu, NetworkData, User, IS_SUPPORTED, MINIMUM_CPU_UPDATE_INTERVAL,
SUPPORTED_SIGNALS,
};
pub(crate) use crate::sys::{DiskInner, DisksInner, ProcessInner, SystemInner};
pub use crate::traits::{
ComponentExt, ComponentsExt, CpuExt, GroupExt, NetworkExt, NetworksExt, UserExt, UsersExt,
};
pub use crate::traits::{ComponentsExt, CpuExt, GroupExt, NetworkExt, UserExt, UsersExt};

#[cfg(feature = "c-interface")]
pub use crate::c_interface::*;
Expand Down
3 changes: 1 addition & 2 deletions src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

use crate::common::PidExt;
use crate::{
ComponentExt, CpuExt, DiskKind, DiskUsage, GroupExt, MacAddr, NetworkExt, NetworksExt,
ProcessStatus, Signal, UserExt,
CpuExt, DiskKind, DiskUsage, GroupExt, MacAddr, NetworkExt, ProcessStatus, Signal, UserExt,
};
use serde::{ser::SerializeStruct, Serialize, Serializer};
use std::ops::Deref;
Expand Down
Loading
Loading