pub struct UsersSnapshot { /* private fields */ }Expand description
A container of user and group instances.
Included users and groups are determined by the method used to construct the snapshot:
UsersSnapshot::new()includes all system users and groups,UsersSnapshot::only_users()filters users and includes only their primary groups,UsersSnapshot::filtered()filters users and groups separately.
This cache is immutable: it’s not possible to alter or refresh it in any
way after creation. Create a new UsersSnapshot to see changes in the
underlying system database.
§Examples
use std::sync::Arc;
use uzers::{Users, UsersSnapshot};
let cache = unsafe { UsersSnapshot::new() };
let user = cache.get_user_by_uid(502).expect("User not found");
let same_user = cache.get_user_by_uid(502).unwrap();
// The two returned values point to the same User
assert!(Arc::ptr_eq(&user, &same_user));use uzers::{AllUsers, UsersSnapshot};
// Exclude MacOS system users
let cache = unsafe { UsersSnapshot::only_users(|u| u.uid() >= 500) };
// Users and groups can be iterated
let user_count = cache.get_all_users().count();§See also
Unless iteration is required, UsersCache is likely safer, easier and
faster.
For thread safety considerations, see the
users::cache module documentation.
Implementations§
Source§impl UsersSnapshot
impl UsersSnapshot
Sourcepub unsafe fn filtered<U, G>(user_filter: U, group_filter: G) -> Self
pub unsafe fn filtered<U, G>(user_filter: U, group_filter: G) -> Self
Creates a new snapshot containing all system users and groups that pass the filter.
§Safety
This is unsafe because we cannot prevent data races if two caches
were attempted to be initialised on different threads at the same time.
For more information, see the all_users documentation.
Note that this method uses both all_users and all_groups.
§Examples
use uzers::cache::UsersSnapshot;
// Exclude Linux system users, include all groups
let snapshot = unsafe {
UsersSnapshot::filtered(|u| u.uid() >= 1000, |_| true)
};§See also
UsersSnapshot::only_users()- if only primary groups of users are neededUsersSnapshot::new()- if no filtering is needed
Sourcepub unsafe fn only_users<F>(user_filter: F) -> Self
pub unsafe fn only_users<F>(user_filter: F) -> Self
Creates a new snapshot containing all system users that pass the filter and their primary groups.
Note that some primary groups may be missing on the system.
§Safety
This is unsafe because we cannot prevent data races if two caches
were attempted to be initialised on different threads at the same time.
For more information, see the all_users documentation.
Note that this method uses both all_users and all_groups.
§Examples
use uzers::cache::UsersSnapshot;
// Include Linux system users and their primary groups
let snapshot = unsafe { UsersSnapshot::only_users(|u| u.uid() < 1000) };§See also
UsersSnapshot::filtered()- for more elaborate group filteringUsersSnapshot::new()- if no filtering is needed
Sourcepub unsafe fn new() -> Self
pub unsafe fn new() -> Self
Creates a new snapshot containing all system users and groups.
§Safety
This is unsafe because we cannot prevent data races if two caches
were attempted to be initialised on different threads at the same time.
For more information, see the all_users documentation.
Note that this method uses both all_users and all_groups.
§Examples
use uzers::cache::UsersSnapshot;
let snapshot = unsafe { UsersSnapshot::new() };§See also
UsersSnapshot::only_users(), UsersSnapshot::filtered() provide
performance benefits if only some users and groups will be needed.
Trait Implementations§
Source§impl AllGroups for UsersSnapshot
impl AllGroups for UsersSnapshot
Source§impl AllUsers for UsersSnapshot
impl AllUsers for UsersSnapshot
Source§impl Default for UsersSnapshot
impl Default for UsersSnapshot
Source§fn default() -> UsersSnapshot
fn default() -> UsersSnapshot
Source§impl Groups for UsersSnapshot
impl Groups for UsersSnapshot
Source§fn get_group_by_gid(&self, gid: gid_t) -> Option<Arc<Group>>
fn get_group_by_gid(&self, gid: gid_t) -> Option<Arc<Group>>
Group if one exists for the given group ID; otherwise, returns None.Source§fn get_group_by_name<S: AsRef<OsStr> + ?Sized>(
&self,
group_name: &S,
) -> Option<Arc<Group>>
fn get_group_by_name<S: AsRef<OsStr> + ?Sized>( &self, group_name: &S, ) -> Option<Arc<Group>>
Group if one exists for the given groupname; otherwise, returns None.Source§fn get_current_gid(&self) -> gid_t
fn get_current_gid(&self) -> gid_t
Source§fn get_current_groupname(&self) -> Option<Arc<OsStr>>
fn get_current_groupname(&self) -> Option<Arc<OsStr>>
Source§fn get_effective_gid(&self) -> gid_t
fn get_effective_gid(&self) -> gid_t
Source§impl Users for UsersSnapshot
impl Users for UsersSnapshot
Source§fn get_user_by_uid(&self, uid: uid_t) -> Option<Arc<User>>
fn get_user_by_uid(&self, uid: uid_t) -> Option<Arc<User>>
User if one exists for the given user ID; otherwise, returns None.Source§fn get_user_by_name<S: AsRef<OsStr> + ?Sized>(
&self,
username: &S,
) -> Option<Arc<User>>
fn get_user_by_name<S: AsRef<OsStr> + ?Sized>( &self, username: &S, ) -> Option<Arc<User>>
User if one exists for the given username; otherwise, returns None.