Skip to content

Commit

Permalink
Remove DiskExt trait
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Oct 8, 2023
1 parent 612954c commit 15f06a6
Show file tree
Hide file tree
Showing 17 changed files with 257 additions and 238 deletions.
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, NetworksExt, SystemExt, UsersExt};
use sysinfo::{ComponentsExt, NetworksExt, SystemExt, UsersExt};

#[bench]
fn bench_new(b: &mut test::Bencher) {
Expand Down
1 change: 0 additions & 1 deletion md_doc/disk.md

This file was deleted.

153 changes: 146 additions & 7 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::{
Component, Components, ComponentsExt, Disk, DiskExt, GroupExt, NetworkData, NetworksExt, User,
UserExt, UsersExt,
Component, Components, ComponentsExt, GroupExt, NetworkData, NetworksExt, User, UserExt,
UsersExt,
};

use std::cmp::Ordering;
use std::collections::HashMap;
use std::convert::{From, TryFrom};
use std::ffi::OsStr;
use std::fmt;
use std::path::Path;
use std::str::FromStr;

/// Trait to have a common conversions for the [`Pid`][crate::Pid] type.
Expand Down Expand Up @@ -473,6 +475,143 @@ impl<'a> Iterator for NetworksIter<'a> {
}
}

/// Struct containing a disk information.
///
/// ```no_run
/// use sysinfo::Disks;
///
/// let mut disks = Disks::new();
/// disks.refresh_list();
/// for disk in disks.disks() {
/// println!("{:?}: {:?}", disk.name(), disk.kind());
/// }
/// ```
pub struct Disk {
pub(crate) inner: crate::DiskInner,
}

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

/// Returns the disk name.
///
/// ```no_run
/// use sysinfo::Disks;
///
/// let mut disks = Disks::new();
/// disks.refresh_list();
/// for disk in disks.disks() {
/// println!("{:?}", disk.name());
/// }
/// ```
pub fn name(&self) -> &OsStr {
self.inner.name()
}

/// Returns the file system used on this disk (so for example: `EXT4`, `NTFS`, etc...).
///
/// ```no_run
/// use sysinfo::Disks;
///
/// let mut disks = Disks::new();
/// disks.refresh_list();
/// for disk in disks.disks() {
/// println!("[{:?}] {:?}", disk.name(), disk.file_system());
/// }
/// ```
pub fn file_system(&self) -> &[u8] {
self.inner.file_system()
}

/// Returns the mount point of the disk (`/` for example).
///
/// ```no_run
/// use sysinfo::Disks;
///
/// let mut disks = Disks::new();
/// disks.refresh_list();
/// for disk in disks.disks() {
/// println!("[{:?}] {:?}", disk.name(), disk.mount_point());
/// }
/// ```
pub fn mount_point(&self) -> &Path {
self.inner.mount_point()
}

/// Returns the total disk size, in bytes.
///
/// ```no_run
/// use sysinfo::Disks;
///
/// let mut disks = Disks::new();
/// disks.refresh_list();
/// for disk in disks.disks() {
/// println!("[{:?}] {}B", disk.name(), disk.total_space());
/// }
/// ```
pub fn total_space(&self) -> u64 {
self.inner.total_space()
}

/// Returns the available disk size, in bytes.
///
/// ```no_run
/// use sysinfo::Disks;
///
/// let mut disks = Disks::new();
/// disks.refresh_list();
/// for disk in disks.disks() {
/// println!("[{:?}] {}B", disk.name(), disk.available_space());
/// }
/// ```
pub fn available_space(&self) -> u64 {
self.inner.available_space()
}

/// Returns `true` if the disk is removable.
///
/// ```no_run
/// use sysinfo::Disks;
///
/// let mut disks = Disks::new();
/// disks.refresh_list();
/// for disk in disks.disks() {
/// println!("[{:?}] {}", disk.name(), disk.is_removable());
/// }
/// ```
pub fn is_removable(&self) -> bool {
self.inner.is_removable()
}

/// Updates the disk' information.
///
/// ```no_run
/// use sysinfo::Disks;
///
/// let mut disks = Disks::new();
/// disks.refresh_list();
/// for disk in disks.disks_mut() {
/// disk.refresh();
/// }
/// ```
pub fn refresh(&mut self) -> bool {
self.inner.refresh()
}
}

/// Disks interface.
///
/// ```no_run
Expand All @@ -485,7 +624,7 @@ impl<'a> Iterator for NetworksIter<'a> {
/// }
/// ```
pub struct Disks {
pub(crate) inner: crate::DisksInner,
inner: crate::DisksInner,
}

impl Default for Disks {
Expand Down Expand Up @@ -530,7 +669,7 @@ impl Disks {
/// Returns the disks list.
///
/// ```no_run
/// use sysinfo::{DiskExt, Disks};
/// use sysinfo::Disks;
///
/// let mut disks = Disks::new();
/// disks.refresh_list();
Expand All @@ -551,7 +690,7 @@ impl Disks {
/// You can do the same without this method by calling:
///
/// ```no_run
/// use sysinfo::{DiskExt, Disks};
/// use sysinfo::Disks;
///
/// let mut disks = Disks::new();
/// disks.refresh_list();
Expand Down Expand Up @@ -631,10 +770,10 @@ impl std::ops::DerefMut for Disks {

/// Enum containing the different supported kinds of disks.
///
/// This type is returned by [`DiskExt::kind`](`crate::DiskExt::kind`).
/// This type is returned by [`Disk::kind`](`crate::Disk::kind`).
///
/// ```no_run
/// use sysinfo::{DiskExt, Disks};
/// use sysinfo::Disks;
///
/// let mut disks = Disks::new();
/// disks.refresh_list();
Expand Down
5 changes: 2 additions & 3 deletions src/debug.rs
Original file line number Diff line number Diff line change
@@ -1,9 +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, DiskExt, Disks, NetworkData,
NetworkExt, Networks, NetworksExt, Process, ProcessExt, System, SystemExt, User, UserExt,
Users,
Component, ComponentExt, Components, Cpu, CpuExt, Disk, Disks, NetworkData, NetworkExt,
Networks, NetworksExt, Process, ProcessExt, System, SystemExt, User, UserExt, Users,
};

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

pub use crate::common::{
get_current_pid, CpuRefreshKind, DiskKind, DiskUsage, Disks, Gid, Group, LoadAvg, MacAddr,
Networks, NetworksIter, Pid, PidExt, ProcessRefreshKind, ProcessStatus, RefreshKind, Signal,
Uid, Users,
get_current_pid, CpuRefreshKind, Disk, DiskKind, DiskUsage, Disks, Gid, Group, LoadAvg,
MacAddr, 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::sys::{Component, Components, Cpu, NetworkData, Process, System, User};
pub(crate) use crate::sys::{DiskInner, DisksInner};
pub use crate::traits::{
ComponentExt, ComponentsExt, CpuExt, DiskExt, GroupExt, NetworkExt, NetworksExt, ProcessExt,
SystemExt, UserExt, UsersExt,
ComponentExt, ComponentsExt, CpuExt, GroupExt, NetworkExt, NetworksExt, ProcessExt, SystemExt,
UserExt, UsersExt,
};

#[cfg(feature = "c-interface")]
Expand Down
2 changes: 1 addition & 1 deletion src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::common::PidExt;
use crate::{
ComponentExt, CpuExt, DiskExt, DiskKind, DiskUsage, GroupExt, MacAddr, NetworkExt, NetworksExt,
ComponentExt, CpuExt, DiskKind, DiskUsage, GroupExt, MacAddr, NetworkExt, NetworksExt,
ProcessExt, ProcessStatus, Signal, SystemExt, UserExt,
};
use serde::{ser::SerializeStruct, Serialize, Serializer};
Expand Down
120 changes: 1 addition & 119 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,133 +5,15 @@ use crate::{
sys::{Component, Cpu, Process},
};
use crate::{
CpuRefreshKind, DiskKind, DiskUsage, Group, LoadAvg, NetworksIter, Pid, ProcessRefreshKind,
CpuRefreshKind, DiskUsage, Group, LoadAvg, NetworksIter, Pid, ProcessRefreshKind,
ProcessStatus, RefreshKind, Signal, User,
};

use std::collections::HashMap;
use std::ffi::OsStr;
use std::fmt::Debug;
use std::path::Path;
use std::time::Duration;

/// Contains all the methods of the [`Disk`][crate::Disk] struct.
///
/// ```no_run
/// use sysinfo::{DiskExt, Disks};
///
/// let mut disks = Disks::new();
/// disks.refresh_list();
/// for disk in disks.disks() {
/// println!("{:?}: {:?}", disk.name(), disk.kind());
/// }
/// ```
pub trait DiskExt: Debug {
/// Returns the kind of disk.
///
/// ```no_run
/// use sysinfo::{DiskExt, Disks};
///
/// let mut disks = Disks::new();
/// disks.refresh_list();
/// for disk in disks.disks() {
/// println!("[{:?}] {:?}", disk.name(), disk.kind());
/// }
/// ```
fn kind(&self) -> DiskKind;

/// Returns the disk name.
///
/// ```no_run
/// use sysinfo::{DiskExt, Disks};
///
/// let mut disks = Disks::new();
/// disks.refresh_list();
/// for disk in disks.disks() {
/// println!("{:?}", disk.name());
/// }
/// ```
fn name(&self) -> &OsStr;

/// Returns the file system used on this disk (so for example: `EXT4`, `NTFS`, etc...).
///
/// ```no_run
/// use sysinfo::{DiskExt, Disks};
///
/// let mut disks = Disks::new();
/// disks.refresh_list();
/// for disk in disks.disks() {
/// println!("[{:?}] {:?}", disk.name(), disk.file_system());
/// }
/// ```
fn file_system(&self) -> &[u8];

/// Returns the mount point of the disk (`/` for example).
///
/// ```no_run
/// use sysinfo::{DiskExt, Disks};
///
/// let mut disks = Disks::new();
/// disks.refresh_list();
/// for disk in disks.disks() {
/// println!("[{:?}] {:?}", disk.name(), disk.mount_point());
/// }
/// ```
fn mount_point(&self) -> &Path;

/// Returns the total disk size, in bytes.
///
/// ```no_run
/// use sysinfo::{DiskExt, Disks};
///
/// let mut disks = Disks::new();
/// disks.refresh_list();
/// for disk in disks.disks() {
/// println!("[{:?}] {}B", disk.name(), disk.total_space());
/// }
/// ```
fn total_space(&self) -> u64;

/// Returns the available disk size, in bytes.
///
/// ```no_run
/// use sysinfo::{DiskExt, Disks};
///
/// let mut disks = Disks::new();
/// disks.refresh_list();
/// for disk in disks.disks() {
/// println!("[{:?}] {}B", disk.name(), disk.available_space());
/// }
/// ```
fn available_space(&self) -> u64;

/// Returns `true` if the disk is removable.
///
/// ```no_run
/// use sysinfo::{DiskExt, Disks};
///
/// let mut disks = Disks::new();
/// disks.refresh_list();
/// for disk in disks.disks() {
/// println!("[{:?}] {}", disk.name(), disk.is_removable());
/// }
/// ```
fn is_removable(&self) -> bool;

/// Updates the disk' information.
///
/// ```no_run
/// use sysinfo::{DiskExt, Disks};
///
/// let mut disks = Disks::new();
/// disks.refresh_list();
/// for disk in disks.disks_mut() {
/// disk.refresh();
/// }
/// ```
fn refresh(&mut self) -> bool;
}

/// Contains all the methods of the [`Process`][crate::Process] struct.
///
/// ```no_run
Expand Down
Loading

0 comments on commit 15f06a6

Please sign in to comment.