You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
144 lines
3.2 KiB
144 lines
3.2 KiB
use super::*;
|
|
use std::collections::hash_map;
|
|
use std::iter;
|
|
|
|
impl Userspace
|
|
{
|
|
/// Is this `User` or `Group` ID present in this space?
|
|
pub fn contains_id(&self, ent: &impl AsEntityId) -> bool
|
|
{
|
|
ent.entity_id().find_in_space(self).is_some()
|
|
}
|
|
|
|
/// Get a user reference from their ID
|
|
pub fn user_ref(&self, id: UserID) -> Option<UserRef<'_>>
|
|
{
|
|
if self.users.contains_key(&id) {
|
|
Some(UserRef(id, &self))
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
|
|
/// Get a group reference from their ID
|
|
pub fn group_ref(&self, id: GroupID) -> Option<GroupRef<'_>>
|
|
{
|
|
if self.groups.contains_key(&id) {
|
|
Some(GroupRef(id, &self))
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
|
|
/// An iterator over the users in this instance
|
|
pub fn users(&self) -> UserIter<'_>
|
|
{
|
|
UserIter(self.users.iter())
|
|
}
|
|
|
|
/// A mutable iterator over the users in this instance
|
|
pub fn users_mut(&mut self) -> UserIterMut<'_>
|
|
{
|
|
UserIterMut(self.users.iter_mut())
|
|
}
|
|
|
|
/// An iterator over the groups in this instance
|
|
pub fn groups(&self) -> GroupIter<'_>
|
|
{
|
|
GroupIter(self.groups.iter())
|
|
}
|
|
|
|
/// A mutable iterator over the groups in this instance
|
|
pub fn groups_mut(&mut self) -> GroupIterMut<'_>
|
|
{
|
|
GroupIterMut(self.groups.iter_mut())
|
|
}
|
|
|
|
/// Number of users
|
|
pub fn users_len(&self) -> usize
|
|
{
|
|
self.users.len()
|
|
}
|
|
/// Number of groups
|
|
pub fn groups_len(&self) -> usize
|
|
{
|
|
self.groups.len()
|
|
}
|
|
}
|
|
|
|
/// Iterator over users
|
|
#[derive(Debug, Clone)]
|
|
pub struct UserIter<'a>(hash_map::Iter<'a, UserID, User>);
|
|
|
|
impl<'a> Iterator for UserIter<'a>
|
|
{
|
|
type Item = &'a User;
|
|
fn next(&mut self) -> Option<Self::Item>
|
|
{
|
|
self.0.next().map(|x| x.1)
|
|
}
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
self.0.size_hint()
|
|
}
|
|
}
|
|
impl<'a> iter::FusedIterator for UserIter<'a>{}
|
|
impl<'a> iter::ExactSizeIterator for UserIter<'a>{}
|
|
|
|
|
|
/// Mutable iterator over users
|
|
#[derive(Debug)]
|
|
pub struct UserIterMut<'a>(hash_map::IterMut<'a, UserID, User>);
|
|
|
|
impl<'a> Iterator for UserIterMut<'a>
|
|
{
|
|
type Item = &'a mut User;
|
|
fn next(&mut self) -> Option<Self::Item>
|
|
{
|
|
self.0.next().map(|x| x.1)
|
|
}
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
self.0.size_hint()
|
|
}
|
|
}
|
|
impl<'a> iter::FusedIterator for UserIterMut<'a>{}
|
|
impl<'a> iter::ExactSizeIterator for UserIterMut<'a>{}
|
|
|
|
|
|
/// Iterator over groups in a `Userspace`
|
|
#[derive(Debug, Clone)]
|
|
pub struct GroupIter<'a>(hash_map::Iter<'a, GroupID, Group>);
|
|
|
|
impl<'a> Iterator for GroupIter<'a>
|
|
{
|
|
type Item = &'a Group;
|
|
fn next(&mut self) -> Option<Self::Item>
|
|
{
|
|
self.0.next().map(|x| x.1)
|
|
}
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
self.0.size_hint()
|
|
}
|
|
}
|
|
impl<'a> iter::FusedIterator for GroupIter<'a>{}
|
|
impl<'a> iter::ExactSizeIterator for GroupIter<'a>{}
|
|
|
|
|
|
|
|
/// Mutable iterator over groups in a `Userspace`
|
|
#[derive(Debug)]
|
|
pub struct GroupIterMut<'a>(hash_map::IterMut<'a, GroupID, Group>);
|
|
|
|
impl<'a> Iterator for GroupIterMut<'a>
|
|
{
|
|
type Item = &'a mut Group;
|
|
fn next(&mut self) -> Option<Self::Item>
|
|
{
|
|
self.0.next().map(|x| x.1)
|
|
}
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
self.0.size_hint()
|
|
}
|
|
}
|
|
impl<'a> iter::FusedIterator for GroupIterMut<'a>{}
|
|
impl<'a> iter::ExactSizeIterator for GroupIterMut<'a>{}
|