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.
115 lines
2.9 KiB
115 lines
2.9 KiB
//! impls for `User`
|
|
use super::*;
|
|
use std::borrow::Cow;
|
|
use std::iter;
|
|
|
|
/// A container of `Group` references in a `Userspace` from `GroupID`s
|
|
#[derive(Debug, Clone)]
|
|
pub struct Groups<'u>(usize, Cow<'u, [GroupID]>, &'u Userspace);
|
|
/// An iterator over all groups a user is a part of.
|
|
#[derive(Debug, Clone)]
|
|
pub struct AllGroups<'u>(Vec<(Option<&'u Group>, GroupInheritanceIter<'u>)>);
|
|
|
|
impl User
|
|
{
|
|
/// All groups the user is explicitly a part of
|
|
pub fn groups_explicit<'a, 'u>(&'a self, space: &'u Userspace) -> Groups<'u>
|
|
where 'a: 'u
|
|
{
|
|
Groups(0, Cow::Borrowed(&self.groups[..]), space)
|
|
}
|
|
|
|
/// All groups the user is a part of, including inherited groups and implcit (todo) ones
|
|
pub fn all_groups<'a, 'u>(&'a self, space: &'u Userspace) -> AllGroups<'u>
|
|
where 'a: 'u
|
|
{
|
|
AllGroups(self.groups_explicit(space).map(|group| (Some(group), group.inherits_from(space))).rev().collect())
|
|
}
|
|
}
|
|
|
|
impl<'u> Groups<'u>
|
|
{
|
|
/// The group IDs remaining in this iterator.
|
|
pub fn ids(&self) -> &[GroupID]
|
|
{
|
|
&self.1[self.0..]
|
|
}
|
|
/// The userspace for this iterator.
|
|
pub fn space(&self) -> &Userspace
|
|
{
|
|
&self.2
|
|
}
|
|
/// Copy as a reference with a lower lifetime.
|
|
pub fn copied<'a>(&'a self) -> Groups<'a>
|
|
{
|
|
Groups(self.0, Cow::Borrowed(&self.1[..]), self.2)
|
|
}
|
|
}
|
|
impl<'u> Iterator for Groups<'u>
|
|
{
|
|
type Item = &'u Group;
|
|
fn next(&mut self) -> Option<Self::Item>
|
|
{
|
|
(if self.0>=self.1.len() {
|
|
None
|
|
} else {
|
|
Some(self.2.groups.get(&self.1[self.0]).expect("Groups contained invalid group ID for its userspace"))
|
|
}, self.0+=1).0
|
|
}
|
|
#[inline] fn nth(&mut self, n: usize) -> Option<Self::Item>
|
|
{
|
|
if (..self.len()).contains(&n) {
|
|
Some(self.2.groups.get(&self.1[n]).expect("Groups contained invalid group ID for its userspace"))
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
let v = self.1.len() - self.0;
|
|
(v, Some(v))
|
|
}
|
|
}
|
|
|
|
impl<'u> iter::DoubleEndedIterator for Groups<'u>
|
|
{
|
|
fn next_back(&mut self) -> Option<Self::Item>
|
|
{
|
|
if self.0 == 0 || self.0 >= self.1.len() {
|
|
self.0 = self.1.len()-1;
|
|
} else {
|
|
self.0-=1;
|
|
}
|
|
debug_assert!(self.0<self.1.len(), "bad DEI impl");
|
|
Some(self.2.groups.get(&self.1[self.0]).expect("Groups contained invalid group ID for its userspace"))
|
|
|
|
}
|
|
}
|
|
impl<'u> iter::ExactSizeIterator for Groups<'u>{}
|
|
impl<'u> iter::FusedIterator for Groups<'u>{}
|
|
|
|
impl<'u> Iterator for AllGroups<'u>
|
|
{
|
|
type Item = &'u Group;
|
|
fn next(&mut self) -> Option<Self::Item>
|
|
{
|
|
match self.0.last_mut()
|
|
{
|
|
Some((mut v @ Some(_), _)) => v.take(),
|
|
Some((None, iter)) => {
|
|
if let Some(igroup) = iter.next() {
|
|
Some(igroup)
|
|
} else {
|
|
self.0.pop();
|
|
self.next()
|
|
}
|
|
},
|
|
_ => None
|
|
}
|
|
}
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
(self.0.iter().map(|(g, i)| (g.is_some() as usize) + i.size_hint().0).sum(), None)
|
|
}
|
|
}
|
|
impl<'u> iter::FusedIterator for AllGroups<'u>{}
|