Skip to content

Commit

Permalink
Remove ComponentsExt trait
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Oct 14, 2023
1 parent 813f247 commit a37d3c1
Show file tree
Hide file tree
Showing 22 changed files with 207 additions and 230 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Otherwise, here is a little code sample:

```rust
use sysinfo::{
Components, ComponentsExt, Disks, Networks, System,
Components, Disks, Networks, 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, UsersExt};
use sysinfo::UsersExt;

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

const signals: &[Signal] = &[
Hangup,
Expand Down
16 changes: 0 additions & 16 deletions md_doc/component.md

This file was deleted.

11 changes: 0 additions & 11 deletions md_doc/components.md

This file was deleted.

148 changes: 140 additions & 8 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::{
ComponentInner, Components, ComponentsExt, CpuInner, NetworkDataInner, NetworksInner,
ProcessInner, SystemInner, User, UserExt, UsersExt,
ComponentInner, ComponentsInner, CpuInner, NetworkDataInner, NetworksInner, ProcessInner,
SystemInner, User, UserExt, UsersExt,
};

use std::cmp::Ordering;
Expand Down Expand Up @@ -2797,10 +2797,142 @@ impl fmt::Display for MacAddr {
}
}

/// Interacting with components.
///
/// ```no_run
/// use sysinfo::Components;
///
/// let mut components = Components::new();
/// components.refresh_list();
/// for component in components.iter() {
/// eprintln!("{component:?}");
/// }
/// ```
pub struct Components {
pub(crate) inner: ComponentsInner,
}

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

impl Components {
/// Creates a new [`Components`][crate::Components] type.
///
/// ```no_run
/// use sysinfo::Components;
///
/// let mut components = Components::new();
/// components.refresh_list();
/// for component in components.iter() {
/// eprintln!("{component:?}");
/// }
/// ```
pub fn new() -> Self {
Self {
inner: ComponentsInner::new(),
}
}

/// Returns the components list.
///
/// ```no_run
/// use sysinfo::Components;
///
/// let mut components = Components::new();
/// components.refresh_list();
/// for component in components.components() {
/// eprintln!("{component:?}");
/// }
/// ```
pub fn components(&self) -> &[Component] {
self.inner.components()
}

/// Returns the components list.
///
/// ```no_run
/// use sysinfo::Components;
///
/// let mut components = Components::new();
/// components.refresh_list();
/// for component in components.components_mut() {
/// component.refresh();
/// eprintln!("{component:?}");
/// }
/// ```
pub fn components_mut(&mut self) -> &mut [Component] {
self.inner.components_mut()
}

/// Sort the components list with the provided callback.
///
/// Internally, it is using the [`slice::sort_unstable_by`] function, so please refer to it
/// for implementation details.
///
/// You can do the same without this method by calling:
///
/// ```no_run
/// use sysinfo::Components;
///
/// let mut components = Components::new();
/// components.refresh_list();
/// components.sort_by(|component1, component2| {
/// component2.label().partial_cmp(component2.label()).unwrap()
/// });
/// ```
///
/// ⚠️ If you use [`Components::refresh_list`], you will need to call this method to sort the
/// components again.
pub fn sort_by<F>(&mut self, compare: F)
where
F: FnMut(&Component, &Component) -> std::cmp::Ordering,
{
self.components_mut().sort_unstable_by(compare);
}

/// Refreshes the listed components' information.
///
/// ⚠️ If a component is added or removed, this method won't take it into account. Use
/// [`Components::refresh_list`] instead.
///
/// ⚠️ If you didn't call [`Components::refresh_list`] beforehand, this method will do
/// nothing as the component list will be empty.
///
/// ```no_run
/// use sysinfo::Components;
///
/// let mut components = Components::new();
/// // We get the component list.
/// components.refresh_list();
/// // We wait some time...?
/// components.refresh();
/// ```
pub fn refresh(&mut self) {
for component in self.components_mut() {
component.refresh();
}
}

/// The component list will be emptied then completely recomputed.
///
/// ```no_run
/// use sysinfo::Components;
///
/// let mut components = Components::new();
/// components.refresh_list();
/// ```
pub fn refresh_list(&mut self) {
self.inner.refresh_list()
}
}

/// Getting a component temperature information.
///
/// ```no_run
/// use sysinfo::{Components, ComponentsExt};
/// use sysinfo::Components;
///
/// let mut components = Components::new();
/// components.refresh_list();
Expand All @@ -2820,7 +2952,7 @@ impl Component {
/// Returns `f32::NAN` if it failed to retrieve it.
///
/// ```no_run
/// use sysinfo::{Components, ComponentsExt};
/// use sysinfo::Components;
///
/// let mut components = Components::new();
/// components.refresh_list();
Expand All @@ -2838,7 +2970,7 @@ impl Component {
/// `max` value will be updated on refresh.
///
/// ```no_run
/// use sysinfo::{Components, ComponentsExt};
/// use sysinfo::Components;
///
/// let mut components = Components::new();
/// components.refresh_list();
Expand All @@ -2858,7 +2990,7 @@ impl Component {
/// Returns the highest temperature before the component halts (in celsius degree).
///
/// ```no_run
/// use sysinfo::{Components, ComponentsExt};
/// use sysinfo::Components;
///
/// let mut components = Components::new();
/// components.refresh_list();
Expand All @@ -2877,7 +3009,7 @@ impl Component {
/// Returns the label of the component.
///
/// ```no_run
/// use sysinfo::{Components, ComponentsExt};
/// use sysinfo::Components;
///
/// let mut components = Components::new();
/// components.refresh_list();
Expand Down Expand Up @@ -2905,7 +3037,7 @@ impl Component {
/// Refreshes component.
///
/// ```no_run
/// use sysinfo::{Components, ComponentsExt};
/// use sysinfo::Components;
///
/// let mut components = Components::new();
/// components.refresh_list();
Expand Down
14 changes: 6 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,17 @@ cfg_if::cfg_if! {
}

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

pub(crate) use crate::sys::{
ComponentInner, CpuInner, DiskInner, DisksInner, NetworkDataInner, NetworksInner, ProcessInner,
SystemInner,
ComponentInner, ComponentsInner, CpuInner, DiskInner, DisksInner, NetworkDataInner,
NetworksInner, ProcessInner, SystemInner,
};
pub use crate::sys::{
Components, User, IS_SUPPORTED, MINIMUM_CPU_UPDATE_INTERVAL, SUPPORTED_SIGNALS,
};
pub use crate::traits::{ComponentsExt, UserExt, UsersExt};
pub use crate::sys::{User, IS_SUPPORTED, MINIMUM_CPU_UPDATE_INTERVAL, SUPPORTED_SIGNALS};
pub use crate::traits::{UserExt, UsersExt};

#[cfg(feature = "c-interface")]
pub use crate::c_interface::*;
Expand Down
Loading

0 comments on commit a37d3c1

Please sign in to comment.