use self::implement::PlatformData;
use super::process::{External, Process};
pub mod implement;
#[derive(Debug)]
pub struct ProcessList(Box<[ProcessListEntry]>);
#[derive(Debug)]
pub struct ProcessListEntry {
pub pid: u32,
pd: PlatformData,
}
impl std::ops::Deref for ProcessList {
type Target = [ProcessListEntry];
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl std::ops::Deref for ProcessListEntry {
type Target = PlatformData;
fn deref(&self) -> &Self::Target {
&self.pd
}
}
#[derive(Debug, thiserror::Error)]
pub enum ProcListError {
#[error("unable to get process list")]
UnableToGetProcessList,
}
pub trait ProcList {
fn get_list() -> Result<ProcessList, ProcListError> {
let iter = Self::get_iter()?;
let hint = iter.size_hint();
let hint = hint.1.unwrap_or(hint.0);
let mut vec = Vec::with_capacity(hint);
for i in iter {
vec.push(i);
}
let proclist = vec.into_boxed_slice();
Ok(ProcessList(proclist))
}
fn get_iter() -> Result<impl Iterator<Item = ProcessListEntry>, ProcListError>;
}
impl ProcessListEntry {
pub fn upgrade(&self) -> Result<Process<External>, crate::structures::process::ProcessError> {
Process::find_pid(self.pid)
}
}