Skip to content

Commit

Permalink
Merge pull request #1090 from GuillaumeGomez/remove-disksext-trait
Browse files Browse the repository at this point in the history
Remove DisksExt trait
  • Loading branch information
GuillaumeGomez authored Oct 7, 2023
2 parents f6c1a25 + 612954c commit 89eb3cb
Show file tree
Hide file tree
Showing 19 changed files with 202 additions and 182 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, DisksExt, NetworkExt, Networks,
Components, ComponentsExt, Disks, NetworkExt, Networks,
NetworksExt, ProcessExt, System, SystemExt,
};

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, DiskExt, DisksExt, NetworksExt, SystemExt, UsersExt};
use sysinfo::{ComponentsExt, DiskExt, NetworksExt, SystemExt, 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, DisksExt, NetworkExt, Networks, NetworksExt, Pid,
ProcessExt, Signal, System, SystemExt, UserExt, Users, UsersExt,
Components, ComponentsExt, CpuExt, Disks, NetworkExt, Networks, NetworksExt, Pid, ProcessExt,
Signal, System, SystemExt, UserExt, Users, UsersExt,
};

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

use crate::{
CpuExt, Disks, DisksExt, NetworkExt, Networks, NetworksExt, Pid, Process, ProcessExt, System,
SystemExt,
CpuExt, Disks, NetworkExt, Networks, NetworksExt, Pid, Process, ProcessExt, System, SystemExt,
};
use libc::{self, c_char, c_float, c_uint, c_void, size_t};
use std::borrow::BorrowMut;
Expand Down
143 changes: 135 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::{
Component, Components, ComponentsExt, Disk, GroupExt, NetworkData, NetworksExt, User, UserExt,
UsersExt,
Component, Components, ComponentsExt, Disk, DiskExt, GroupExt, NetworkData, NetworksExt, User,
UserExt, UsersExt,
};

use std::cmp::Ordering;
Expand Down Expand Up @@ -473,10 +473,10 @@ impl<'a> Iterator for NetworksIter<'a> {
}
}

/// Disk interfaces.
/// Disks interface.
///
/// ```no_run
/// use sysinfo::{Disks, DisksExt};
/// use sysinfo::Disks;
///
/// let mut disks = Disks::new();
/// disks.refresh_list();
Expand All @@ -485,20 +485,147 @@ impl<'a> Iterator for NetworksIter<'a> {
/// }
/// ```
pub struct Disks {
pub(crate) disks: Vec<Disk>,
pub(crate) inner: crate::DisksInner,
}

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

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

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

/// Returns the disks list.
///
/// ```no_run
/// use sysinfo::{DiskExt, Disks};
///
/// let mut disks = Disks::new();
/// disks.refresh_list();
/// for disk in disks.disks_mut() {
/// disk.refresh();
/// eprintln!("{disk:?}");
/// }
/// ```
pub fn disks_mut(&mut self) -> &mut [Disk] {
self.inner.disks_mut()
}

/// Sort the disk 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::{DiskExt, Disks};
///
/// let mut disks = Disks::new();
/// disks.refresh_list();
/// disks.sort_by(|disk1, disk2| {
/// disk1.name().partial_cmp(disk2.name()).unwrap()
/// });
/// ```
///
/// ⚠️ If you use [`Disks::refresh_list`], you will need to call this method to sort the
/// disks again.
pub fn sort_by<F>(&mut self, compare: F)
where
F: FnMut(&Disk, &Disk) -> std::cmp::Ordering,
{
self.disks_mut().sort_unstable_by(compare);
}

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

/// The disk list will be emptied then completely recomputed.
///
/// ## Linux
///
/// ⚠️ On Linux, the [NFS](https://en.wikipedia.org/wiki/Network_File_System) file
/// systems are ignored and the information of a mounted NFS **cannot** be obtained
/// via [`Disks::refresh_list`]. This is due to the fact that I/O function
/// `statvfs` used by [`Disks::refresh_list`] is blocking and
/// [may hang](https://github.com/GuillaumeGomez/sysinfo/pull/876) in some cases,
/// requiring to call `systemctl stop` to terminate the NFS service from the remote
/// server in some cases.
///
/// ```no_run
/// use sysinfo::Disks;
///
/// let mut disks = Disks::new();
/// disks.refresh_list();
/// ```
pub fn refresh_list(&mut self) {
self.inner.refresh_list();
}
}

impl std::ops::Deref for Disks {
type Target = [Disk];

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

impl std::ops::DerefMut for Disks {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.disks
self.disks_mut()
}
}

Expand All @@ -507,7 +634,7 @@ impl std::ops::DerefMut for Disks {
/// This type is returned by [`DiskExt::kind`](`crate::DiskExt::kind`).
///
/// ```no_run
/// use sysinfo::{DiskExt, Disks, DisksExt};
/// use sysinfo::{DiskExt, Disks};
///
/// let mut disks = Disks::new();
/// disks.refresh_list();
Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ pub use crate::common::{
Networks, NetworksIter, Pid, PidExt, ProcessRefreshKind, ProcessStatus, RefreshKind, Signal,
Uid, Users,
};
pub(crate) use crate::sys::DisksInner;
pub use crate::sys::{Component, Components, Cpu, Disk, NetworkData, Process, System, User};
pub use crate::traits::{
ComponentExt, ComponentsExt, CpuExt, DiskExt, DisksExt, GroupExt, NetworkExt, NetworksExt,
ProcessExt, SystemExt, UserExt, UsersExt,
ComponentExt, ComponentsExt, CpuExt, DiskExt, GroupExt, NetworkExt, NetworksExt, ProcessExt,
SystemExt, UserExt, UsersExt,
};

#[cfg(feature = "c-interface")]
Expand Down
Loading

0 comments on commit 89eb3cb

Please sign in to comment.