Skip to content

Commit

Permalink
Remove UsersExt trait
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Oct 15, 2023
1 parent f905646 commit 720f5ec
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 160 deletions.
1 change: 0 additions & 1 deletion benches/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
extern crate test;

use sysinfo::get_current_pid;
use sysinfo::UsersExt;

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

const signals: &[Signal] = &[
Hangup,
Expand Down
165 changes: 137 additions & 28 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::{
ComponentInner, ComponentsInner, CpuInner, NetworkDataInner, NetworksInner, ProcessInner,
SystemInner, UserInner, UsersExt,
SystemInner, UserInner,
};

use std::cmp::Ordering;
Expand Down Expand Up @@ -1004,7 +1004,7 @@ impl Process {

/// Returns the ID of the owner user of this process or `None` if this
/// information couldn't be retrieved. If you want to get the [`User`] from
/// it, take a look at [`UsersExt::get_user_by_id`].
/// it, take a look at [`Users::get_user_by_id`].
///
/// ```no_run
/// use sysinfo::{Pid, System};
Expand All @@ -1021,7 +1021,7 @@ impl Process {

/// Returns the user ID of the effective owner of this process or `None` if
/// this information couldn't be retrieved. If you want to get the [`User`]
/// from it, take a look at [`UsersExt::get_user_by_id`].
/// from it, take a look at [`Users::get_user_by_id`].
///
/// If you run something with `sudo`, the real user ID of the launched
/// process will be the ID of the user you are logged in as but effective
Expand Down Expand Up @@ -2206,10 +2206,10 @@ impl std::ops::DerefMut for Components {
}
}

/// User interfaces.
/// Interacting with users.
///
/// ```no_run
/// use sysinfo::{Users, UsersExt};
/// use sysinfo::Users;
///
/// let mut users = Users::new();
/// for user in users.users() {
Expand All @@ -2220,21 +2220,9 @@ pub struct Users {
users: Vec<User>,
}

impl UsersExt for Users {
fn new() -> Self {
Self { users: Vec::new() }
}

fn users(&self) -> &[User] {
&self.users
}

fn users_mut(&mut self) -> &mut [User] {
&mut self.users
}

fn refresh_list(&mut self) {
crate::sys::get_users(&mut self.users);
impl Default for Users {
fn default() -> Self {
Self::new()
}
}

Expand All @@ -2252,6 +2240,127 @@ impl std::ops::DerefMut for Users {
}
}

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

/// Returns the users list.
///
/// ```no_run
/// use sysinfo::Users;
///
/// let mut users = Users::new();
/// users.refresh_list();
/// for user in users.users() {
/// eprintln!("{user:?}");
/// }
/// ```
pub fn users(&self) -> &[User] {
&self.users
}

/// Returns the users list.
///
/// ```no_run
/// use sysinfo::Users;
///
/// let mut users = Users::new();
/// users.refresh_list();
/// users.users_mut().sort_by(|user1, user2| {
/// user1.name().partial_cmp(user2.name()).unwrap()
/// });
/// ```
pub fn users_mut(&mut self) -> &mut [User] {
&mut self.users
}

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

/// The user list will be emptied then completely recomputed.
///
/// ```no_run
/// use sysinfo::Users;
///
/// let mut users = Users::new();
/// users.refresh_list();
/// ```
pub fn refresh_list(&mut self) {
crate::sys::get_users(&mut self.users);
}

/// Returns the [`User`] matching the given `user_id`.
///
/// **Important**: The user list must be filled before using this method, otherwise it will
/// always return `None` (through the `refresh_*` methods).
///
/// It is a shorthand for:
///
/// ```ignore
/// # use sysinfo::Users;
/// let mut users = Users::new();
/// users.refresh_list();
/// users.users().find(|user| user.id() == user_id);
/// ```
///
/// Full example:
///
/// ```no_run
/// use sysinfo::{Pid, System, Users};
///
/// let mut s = System::new_all();
/// let mut users = Users::new();
///
/// users.refresh_list();
///
/// if let Some(process) = s.process(Pid::from(1337)) {
/// if let Some(user_id) = process.user_id() {
/// eprintln!("User for process 1337: {:?}", users.get_user_by_id(user_id));
/// }
/// }
/// ```
pub fn get_user_by_id(&self, user_id: &Uid) -> Option<&User> {
self.users.iter().find(|user| user.id() == user_id)
}
}

/// An enum representing signals on UNIX-like systems.
///
/// On non-unix systems, this enum is mostly useless and is only there to keep coherency between
Expand Down Expand Up @@ -2489,7 +2598,7 @@ cfg_if::cfg_if! {
/// It is returned by [`Users`][crate::Users].
///
/// ```no_run
/// use sysinfo::{Users, UsersExt};
/// use sysinfo::Users;
///
/// let mut users = Users::new();
/// users.refresh_list();
Expand Down Expand Up @@ -2527,7 +2636,7 @@ impl User {
/// Returns the ID of the user.
///
/// ```no_run
/// use sysinfo::{Users, UsersExt};
/// use sysinfo::Users;
///
/// let mut users = Users::new();
/// users.refresh_list();
Expand All @@ -2549,7 +2658,7 @@ impl User {
/// group ID.
///
/// ```no_run
/// use sysinfo::{Users, UsersExt};
/// use sysinfo::Users;
///
/// let mut users = Users::new();
/// users.refresh_list();
Expand All @@ -2564,7 +2673,7 @@ impl User {
/// Returns the name of the user.
///
/// ```no_run
/// use sysinfo::{Users, UsersExt};
/// use sysinfo::Users;
///
/// let mut users = Users::new();
/// users.refresh_list();
Expand All @@ -2581,7 +2690,7 @@ impl User {
/// ⚠️ This is computed every time this method is called.
///
/// ```no_run
/// use sysinfo::{Users, UsersExt};
/// use sysinfo::Users;
///
/// let mut users = Users::new();
/// users.refresh_list();
Expand All @@ -2599,7 +2708,7 @@ impl User {
/// It is returned by [`User::groups`].
///
/// ```no_run
/// use sysinfo::{Users, UsersExt};
/// use sysinfo::Users;
///
/// let mut users = Users::new();
///
Expand Down Expand Up @@ -2627,7 +2736,7 @@ impl Group {
/// ⚠️ This information is not set on Windows.
///
/// ```no_run
/// use sysinfo::{Users, UsersExt};
/// use sysinfo::Users;
///
/// let mut users = Users::new();
///
Expand All @@ -2644,7 +2753,7 @@ impl Group {
/// Returns the name of the group.
///
/// ```no_run
/// use sysinfo::{Users, UsersExt};
/// use sysinfo::Users;
///
/// let mut users = Users::new();
///
Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ pub(crate) use crate::sys::{
NetworksInner, ProcessInner, SystemInner, UserInner,
};
pub use crate::sys::{IS_SUPPORTED, MINIMUM_CPU_UPDATE_INTERVAL, SUPPORTED_SIGNALS};
pub use crate::traits::UsersExt;

#[cfg(feature = "c-interface")]
pub use crate::c_interface::*;
Expand All @@ -74,7 +73,6 @@ mod debug;
#[cfg(feature = "serde")]
mod serde;
mod system;
mod traits;
mod utils;

/// This function is only used on Linux targets, on the other platforms it does nothing and returns
Expand Down
Loading

0 comments on commit 720f5ec

Please sign in to comment.