Skip to content

Commit

Permalink
Improve disk list refresh and rename DiskRefreshKind::details into …
Browse files Browse the repository at this point in the history
…`DiskRefreshKind::storage`
  • Loading branch information
GuillaumeGomez committed Nov 30, 2024
1 parent 0de683f commit 2abe5db
Show file tree
Hide file tree
Showing 10 changed files with 424 additions and 367 deletions.
2 changes: 1 addition & 1 deletion benches/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ fn bench_refresh_disks_list(b: &mut test::Bencher) {
let mut disks = sysinfo::Disks::new_with_refreshed_list();

b.iter(move || {
disks.refresh_list();
disks.refresh_list(false);
});
}

Expand Down
2 changes: 1 addition & 1 deletion examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ fn interpret_input(
"help" => print_help(),
"refresh_disks" => {
writeln!(&mut io::stdout(), "Refreshing disk list...");
disks.refresh_list();
disks.refresh_list(true);
writeln!(&mut io::stdout(), "Done.");
}
"refresh_users" => {
Expand Down
2 changes: 1 addition & 1 deletion src/c_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ pub extern "C" fn sysinfo_disks_refresh_list(disks: CDisks) {
let mut disks: Box<Disks> = Box::from_raw(disks as *mut Disks);
{
let disks: &mut Disks = disks.borrow_mut();
disks.refresh_list();
disks.refresh_list(true);
}
let _ = Box::into_raw(disks);
}
Expand Down
37 changes: 23 additions & 14 deletions src/common/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ impl Disks {
/// use sysinfo::Disks;
///
/// let mut disks = Disks::new();
/// disks.refresh_list();
/// disks.refresh_list(false);
/// for disk in disks.list() {
/// println!("{disk:?}");
/// }
Expand Down Expand Up @@ -290,7 +290,7 @@ impl Disks {
/// ```
pub fn new_with_refreshed_list_specifics(refreshes: DiskRefreshKind) -> Self {
let mut disks = Self::new();
disks.refresh_list_specifics(refreshes);
disks.refresh_list_specifics(false, refreshes);
disks
}

Expand Down Expand Up @@ -357,10 +357,10 @@ impl Disks {
/// use sysinfo::Disks;
///
/// let mut disks = Disks::new();
/// disks.refresh_list();
/// disks.refresh_list(true);
/// ```
pub fn refresh_list(&mut self) {
self.refresh_list_specifics(DiskRefreshKind::everything());
pub fn refresh_list(&mut self, remove_not_listed_disks: bool) {
self.refresh_list_specifics(remove_not_listed_disks, DiskRefreshKind::everything());
}

/// The disk list will be emptied then completely recomputed according to the given
Expand All @@ -380,10 +380,15 @@ impl Disks {
/// use sysinfo::{Disks, DiskRefreshKind};
///
/// let mut disks = Disks::new();
/// disks.refresh_list_specifics(DiskRefreshKind::new());
/// disks.refresh_list_specifics(true, DiskRefreshKind::new());
/// ```
pub fn refresh_list_specifics(&mut self, refreshes: DiskRefreshKind) {
self.inner.refresh_list_specifics(refreshes);
pub fn refresh_list_specifics(
&mut self,
remove_not_listed_disks: bool,
refreshes: DiskRefreshKind,
) {
self.inner
.refresh_list_specifics(remove_not_listed_disks, refreshes);
}
}

Expand Down Expand Up @@ -435,19 +440,23 @@ impl fmt::Display for DiskKind {

/// Used to determine what you want to refresh specifically on the [`Disk`] type.
///
/// * `kind` is about refreshing the [`Disk::kind`] information.
/// * `storage` is about refreshing the [`Disk::available_space`] and [`Disk::total_space`] information.
/// * `io_usage` is about refreshing the [`Disk::usage`] information.
///
/// ```no_run
/// use sysinfo::{Disks, DiskRefreshKind};
///
/// let mut disks = Disks::new_with_refreshed_list_specifics(DiskRefreshKind::everything());
///
/// for disk in disks.list() {
/// assert_eq!(disk.total_space(), 0);
/// assert!(disk.total_space() != 0);
/// }
/// ```
#[derive(Clone, Copy, Debug, Default)]
pub struct DiskRefreshKind {
kind: bool,
details: bool,
storage: bool,
io_usage: bool,
}

Expand All @@ -460,7 +469,7 @@ impl DiskRefreshKind {
/// let r = DiskRefreshKind::new();
///
/// assert_eq!(r.kind(), false);
/// assert_eq!(r.details(), false);
/// assert_eq!(r.storage(), false);
/// assert_eq!(r.io_usage(), false);
/// ```
pub fn new() -> Self {
Expand All @@ -475,18 +484,18 @@ impl DiskRefreshKind {
/// let r = DiskRefreshKind::everything();
///
/// assert_eq!(r.kind(), true);
/// assert_eq!(r.details(), true);
/// assert_eq!(r.storage(), true);
/// assert_eq!(r.io_usage(), true);
/// ```
pub fn everything() -> Self {
Self {
kind: true,
details: true,
storage: true,
io_usage: true,
}
}

impl_get_set!(DiskRefreshKind, kind, with_kind, without_kind);
impl_get_set!(DiskRefreshKind, details, with_details, without_details,);
impl_get_set!(DiskRefreshKind, storage, with_storage, without_storage);
impl_get_set!(DiskRefreshKind, io_usage, with_io_usage, without_io_usage);
}
Loading

0 comments on commit 2abe5db

Please sign in to comment.