From 720f5ec140ea97a4058d7dbf25536e30a8daaa6c Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 15 Oct 2023 16:00:31 +0200 Subject: [PATCH] Remove UsersExt trait --- benches/basic.rs | 1 - examples/simple.rs | 2 +- src/common.rs | 165 +++++++++++++++++++++++++++++++++++++-------- src/lib.rs | 2 - src/traits.rs | 128 ----------------------------------- 5 files changed, 138 insertions(+), 160 deletions(-) delete mode 100644 src/traits.rs diff --git a/benches/basic.rs b/benches/basic.rs index 48fa885e1..784aebe77 100644 --- a/benches/basic.rs +++ b/benches/basic.rs @@ -3,7 +3,6 @@ extern crate test; use sysinfo::get_current_pid; -use sysinfo::UsersExt; #[bench] fn bench_new(b: &mut test::Bencher) { diff --git a/examples/simple.rs b/examples/simple.rs index 066dcc813..d60edbd2c 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -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, diff --git a/src/common.rs b/src/common.rs index 4f4eea539..ad6b8621e 100644 --- a/src/common.rs +++ b/src/common.rs @@ -2,7 +2,7 @@ use crate::{ ComponentInner, ComponentsInner, CpuInner, NetworkDataInner, NetworksInner, ProcessInner, - SystemInner, UserInner, UsersExt, + SystemInner, UserInner, }; use std::cmp::Ordering; @@ -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}; @@ -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 @@ -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() { @@ -2220,21 +2220,9 @@ pub struct Users { users: Vec, } -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() } } @@ -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(&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 @@ -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(); @@ -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(); @@ -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(); @@ -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(); @@ -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(); @@ -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(); /// @@ -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(); /// @@ -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(); /// diff --git a/src/lib.rs b/src/lib.rs index 2d433d817..524033215 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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::*; @@ -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 diff --git a/src/traits.rs b/src/traits.rs deleted file mode 100644 index eff52f715..000000000 --- a/src/traits.rs +++ /dev/null @@ -1,128 +0,0 @@ -// Take a look at the license at the top of the repository in the LICENSE file. - -use crate::common::Uid; -use crate::User; - -use std::fmt::Debug; - -/// Interacting with users. -/// -/// ```no_run -/// use sysinfo::{Users, UsersExt}; -/// -/// let mut users = Users::new(); -/// users.refresh_list(); -/// for user in users.users() { -/// eprintln!("{user:?}"); -/// } -/// ``` -pub trait UsersExt: Debug { - /// Creates a new [`Components`][crate::Components] type. - /// - /// ```no_run - /// use sysinfo::{Users, UsersExt}; - /// - /// let mut users = Users::new(); - /// users.refresh_list(); - /// for user in users.users() { - /// eprintln!("{user:?}"); - /// } - /// ``` - fn new() -> Self; - - /// Returns the users list. - /// - /// ```no_run - /// use sysinfo::{Users, UsersExt}; - /// - /// let mut users = Users::new(); - /// users.refresh_list(); - /// for user in users.users() { - /// eprintln!("{user:?}"); - /// } - /// ``` - fn users(&self) -> &[User]; - - /// Returns the users list. - /// - /// ```no_run - /// use sysinfo::{Users, UsersExt}; - /// - /// let mut users = Users::new(); - /// users.refresh_list(); - /// users.users_mut().sort_by(|user1, user2| { - /// user1.name().partial_cmp(user2.name()).unwrap() - /// }); - /// ``` - fn users_mut(&mut self) -> &mut [User]; - - /// 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, UsersExt}; - /// - /// let mut users = Users::new(); - /// users.refresh_list(); - /// users.sort_by(|user1, user2| { - /// user1.name().partial_cmp(user2.name()).unwrap() - /// }); - /// ``` - /// - /// ⚠️ If you use [`UsersExt::refresh_list`], you will need to call this method to sort the - /// users again. - fn sort_by(&mut self, compare: F) - where - F: FnMut(&User, &User) -> std::cmp::Ordering, - { - self.users_mut().sort_unstable_by(compare); - } - - /// The user list will be emptied then completely recomputed. - /// - /// ```no_run - /// use sysinfo::{Users, UsersExt}; - /// - /// let mut users = Users::new(); - /// users.refresh_list(); - /// ``` - fn refresh_list(&mut self); - - /// 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, UsersExt}; - /// 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, UsersExt}; - /// - /// 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)); - /// } - /// } - /// ``` - fn get_user_by_id(&self, user_id: &Uid) -> Option<&User> { - self.users().iter().find(|user| user.id() == user_id) - } -}