Skip to content

Commit

Permalink
Remove UserExt trait
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Oct 15, 2023
1 parent b5b301c commit b3767d0
Show file tree
Hide file tree
Showing 15 changed files with 152 additions and 153 deletions.
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, UserExt, Users, UsersExt};
use sysinfo::{Components, Disks, Networks, Pid, Signal, System, Users, UsersExt};

const signals: &[Signal] = &[
Hangup,
Expand Down
13 changes: 0 additions & 13 deletions md_doc/user.md

This file was deleted.

98 changes: 93 additions & 5 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, User, UserExt, UsersExt,
SystemInner, UserInner, UsersExt,
};

use std::cmp::Ordering;
Expand Down Expand Up @@ -2209,7 +2209,7 @@ impl std::ops::DerefMut for Components {
/// User interfaces.
///
/// ```no_run
/// use sysinfo::{Users, UsersExt, UserExt};
/// use sysinfo::{Users, UsersExt};
///
/// let mut users = Users::new();
/// for user in users.users() {
Expand Down Expand Up @@ -2484,6 +2484,23 @@ cfg_if::cfg_if! {
}
}

/// Type containing user information.
///
/// It is returned by [`Users`][crate::Users].
///
/// ```no_run
/// use sysinfo::{Users, UsersExt};
///
/// let mut users = Users::new();
/// users.refresh_list();
/// for user in users.users() {
/// println!("{:?}", user);
/// }
/// ```
pub struct User {
pub(crate) inner: UserInner,
}

impl PartialEq for User {
fn eq(&self, other: &Self) -> bool {
self.id() == other.id()
Expand All @@ -2506,12 +2523,83 @@ impl Ord for User {
}
}

impl User {
/// Returns the ID of the user.
///
/// ```no_run
/// use sysinfo::{Users, UsersExt};
///
/// let mut users = Users::new();
/// users.refresh_list();
/// for user in users.users() {
/// println!("{:?}", *user.id());
/// }
/// ```
pub fn id(&self) -> &Uid {
self.inner.id()
}

/// Returns the group ID of the user.
///
/// ⚠️ This information is not set on Windows. Windows doesn't have a `username` specific
/// group assigned to the user. They do however have unique
/// [Security Identifiers](https://docs.microsoft.com/en-us/windows/win32/secauthz/security-identifiers)
/// made up of various [Components](https://docs.microsoft.com/en-us/windows/win32/secauthz/sid-components).
/// Pieces of the SID may be a candidate for this field, but it doesn't map well to a single
/// group ID.
///
/// ```no_run
/// use sysinfo::{Users, UsersExt};
///
/// let mut users = Users::new();
/// users.refresh_list();
/// for user in users.users() {
/// println!("{}", *user.group_id());
/// }
/// ```
pub fn group_id(&self) -> Gid {
self.inner.group_id()
}

/// Returns the name of the user.
///
/// ```no_run
/// use sysinfo::{Users, UsersExt};
///
/// let mut users = Users::new();
/// users.refresh_list();
/// for user in users.users() {
/// println!("{}", user.name());
/// }
/// ```
pub fn name(&self) -> &str {
self.inner.name()
}

/// Returns the groups of the user.
///
/// ⚠️ This is computed every time this method is called.
///
/// ```no_run
/// use sysinfo::{Users, UsersExt};
///
/// let mut users = Users::new();
/// users.refresh_list();
/// for user in users.users() {
/// println!("{} is in {:?}", user.name(), user.groups());
/// }
/// ```
pub fn groups(&self) -> Vec<Group> {
self.inner.groups()
}
}

/// Type containing group information.
///
/// It is returned by [`User::groups`].
///
/// ```no_run
/// use sysinfo::{UserExt, Users, UsersExt};
/// use sysinfo::{Users, UsersExt};
///
/// let mut users = Users::new();
///
Expand Down Expand Up @@ -2539,7 +2627,7 @@ impl Group {
/// ⚠️ This information is not set on Windows.
///
/// ```no_run
/// use sysinfo::{UserExt, Users, UsersExt};
/// use sysinfo::{Users, UsersExt};
///
/// let mut users = Users::new();
///
Expand All @@ -2556,7 +2644,7 @@ impl Group {
/// Returns the name of the group.
///
/// ```no_run
/// use sysinfo::{UserExt, Users, UsersExt};
/// use sysinfo::{Users, UsersExt};
///
/// let mut users = Users::new();
///
Expand Down
3 changes: 1 addition & 2 deletions src/debug.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::{
Component, Components, Cpu, Disk, Disks, NetworkData, Networks, Process, System, User, UserExt,
Users,
Component, Components, Cpu, Disk, Disks, NetworkData, Networks, Process, System, User, Users,
};

use std::fmt;
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ cfg_if::cfg_if! {
pub use crate::common::{
get_current_pid, Component, Components, Cpu, CpuRefreshKind, Disk, DiskKind, DiskUsage, Disks,
Gid, Group, LoadAvg, MacAddr, NetworkData, Networks, NetworksIter, Pid, PidExt, Process,
ProcessRefreshKind, ProcessStatus, RefreshKind, Signal, System, Uid, Users,
ProcessRefreshKind, ProcessStatus, RefreshKind, Signal, System, Uid, User, Users,
};

pub(crate) use crate::sys::{
ComponentInner, ComponentsInner, CpuInner, DiskInner, DisksInner, NetworkDataInner,
NetworksInner, ProcessInner, SystemInner,
NetworksInner, ProcessInner, SystemInner, UserInner,
};
pub use crate::sys::{User, IS_SUPPORTED, MINIMUM_CPU_UPDATE_INTERVAL, SUPPORTED_SIGNALS};
pub use crate::traits::{UserExt, UsersExt};
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 Down
2 changes: 1 addition & 1 deletion src/serde.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::common::PidExt;
use crate::{DiskKind, DiskUsage, MacAddr, ProcessStatus, Signal, UserExt};
use crate::{DiskKind, DiskUsage, MacAddr, ProcessStatus, Signal};
use serde::{ser::SerializeStruct, Serialize, Serializer};
use std::ops::Deref;

Expand Down
86 changes: 5 additions & 81 deletions src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,86 +1,10 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::common::{Gid, Uid};
use crate::{Group, User};
use crate::common::Uid;
use crate::User;

use std::fmt::Debug;

/// Getting information for a user.
///
/// It is returned from [`UsersExt::users`].
///
/// ```no_run
/// use sysinfo::{Users, UsersExt, UserExt};
///
/// let mut users = Users::new();
/// users.refresh_list();
/// for user in users.users() {
/// println!("{} is in {} groups", user.name(), user.groups().len());
/// }
/// ```
pub trait UserExt: Debug + PartialEq + Eq + PartialOrd + Ord {
/// Returns the ID of the user.
///
/// ```no_run
/// use sysinfo::{Users, UsersExt, UserExt};
///
/// let mut users = Users::new();
/// users.refresh_list();
/// for user in users.users() {
/// println!("{:?}", *user.id());
/// }
/// ```
fn id(&self) -> &Uid;

/// Returns the group ID of the user.
///
/// ⚠️ This information is not set on Windows. Windows doesn't have a `username` specific
/// group assigned to the user. They do however have unique
/// [Security Identifiers](https://docs.microsoft.com/en-us/windows/win32/secauthz/security-identifiers)
/// made up of various [Components](https://docs.microsoft.com/en-us/windows/win32/secauthz/sid-components).
/// Pieces of the SID may be a candidate for this field, but it doesn't map well to a single
/// group ID.
///
/// ```no_run
/// use sysinfo::{Users, UsersExt, UserExt};
///
/// let mut users = Users::new();
/// users.refresh_list();
/// for user in users.users() {
/// println!("{}", *user.group_id());
/// }
/// ```
fn group_id(&self) -> Gid;

/// Returns the name of the user.
///
/// ```no_run
/// use sysinfo::{Users, UsersExt, UserExt};
///
/// let mut users = Users::new();
/// users.refresh_list();
/// for user in users.users() {
/// println!("{}", user.name());
/// }
/// ```
fn name(&self) -> &str;

/// Returns the groups of the user.
///
/// ⚠️ This is computed every time this method is called.
///
/// ```no_run
/// use sysinfo::{Users, UsersExt, UserExt};
///
/// let mut users = Users::new();
/// users.refresh_list();
/// for user in users.users() {
/// println!("{} is in {:?}", user.name(), user.groups());
/// }
/// ```
fn groups(&self) -> Vec<Group>;
}

/// Interacting with users.
///
/// ```no_run
Expand Down Expand Up @@ -122,7 +46,7 @@ pub trait UsersExt: Debug {
/// Returns the users list.
///
/// ```no_run
/// use sysinfo::{UserExt, Users, UsersExt};
/// use sysinfo::{Users, UsersExt};
///
/// let mut users = Users::new();
/// users.refresh_list();
Expand All @@ -140,7 +64,7 @@ pub trait UsersExt: Debug {
/// You can do the same without this method by calling:
///
/// ```no_run
/// use sysinfo::{UserExt, Users, UsersExt};
/// use sysinfo::{Users, UsersExt};
///
/// let mut users = Users::new();
/// users.refresh_list();
Expand Down Expand Up @@ -176,7 +100,7 @@ pub trait UsersExt: Debug {
/// It is a shorthand for:
///
/// ```ignore
/// # use sysinfo::{UserExt, Users, UsersExt};
/// # use sysinfo::{Users, UsersExt};
/// let mut users = Users::new();
/// users.refresh_list();
/// users.users().find(|user| user.id() == user_id);
Expand Down
3 changes: 1 addition & 2 deletions src/unix/apple/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ pub(crate) use self::disk::DiskInner;
pub(crate) use self::network::{NetworkDataInner, NetworksInner};
pub(crate) use self::process::ProcessInner;
pub(crate) use self::system::SystemInner;
pub(crate) use crate::unix::users::get_users;
pub use crate::unix::users::User;
pub(crate) use crate::unix::users::{get_users, UserInner};
pub(crate) use crate::unix::DisksInner;

use std::time::Duration;
Expand Down
6 changes: 4 additions & 2 deletions src/unix/apple/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::{
common::{Gid, Uid},
User,
User, UserInner,
};

use libc::{c_char, endpwent, getpwent, setpwent, strlen};
Expand Down Expand Up @@ -61,7 +61,9 @@ pub(crate) fn get_users(users: &mut Vec<User>) {
endpwent();
}
for (name, (uid, gid)) in users_map {
users.push(User::new(uid, gid, name));
users.push(User {
inner: UserInner::new(uid, gid, name),
});
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/unix/freebsd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ pub(crate) use self::disk::DiskInner;
pub(crate) use self::network::{NetworkDataInner, NetworksInner};
pub(crate) use self::process::ProcessInner;
pub(crate) use self::system::SystemInner;
pub(crate) use crate::unix::users::get_users;
pub use crate::unix::users::User;
pub(crate) use crate::unix::users::{get_users, UserInner};
pub(crate) use crate::unix::DisksInner;

use libc::c_int;
Expand Down
3 changes: 1 addition & 2 deletions src/unix/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ pub(crate) use self::disk::DiskInner;
pub(crate) use self::network::{NetworkDataInner, NetworksInner};
pub(crate) use self::process::ProcessInner;
pub(crate) use self::system::SystemInner;
pub(crate) use crate::unix::users::get_users;
pub use crate::unix::users::User;
pub(crate) use crate::unix::users::{get_users, UserInner};
pub(crate) use crate::unix::DisksInner;

use std::time::Duration;
Expand Down
Loading

0 comments on commit b3767d0

Please sign in to comment.