process: improve get_processes call

This commit is contained in:
2025-12-03 12:52:02 +00:00
parent 99db8bcd72
commit 93bb675333
+10 -11
View File
@@ -1,6 +1,6 @@
use crate::{ use crate::{
sys::{handle::Handle, memory, snapshot},
Error, Error,
sys::{handle::Handle, memory, snapshot},
}; };
use derive_more::derive::Display; use derive_more::derive::Display;
@@ -45,18 +45,17 @@ impl Process {
/// initialize a `Process` from a pid or a process name /// initialize a `Process` from a pid or a process name
pub fn from<T: Into<Identifier>>(identifier: T) -> Result<Self, Error> { pub fn from<T: Into<Identifier>>(identifier: T) -> Result<Self, Error> {
let identifier = identifier.into(); let identifier = identifier.into();
let Some(snapshot) = (match identifier { let snapshot = snapshot::ProcessSnapshot::get_processes()?
Identifier::Pid(pid) => snapshot::ProcessSnapshot::get_processes()?
.into_iter() .into_iter()
.find(|snapshot| snapshot.id == pid), .find(|snapshot| match identifier {
Identifier::Name(ref name) => snapshot::ProcessSnapshot::get_processes()? Identifier::Pid(pid) => snapshot.id == pid,
.into_iter() Identifier::Name(ref name) => snapshot.name == *name,
.find(|snapshot| snapshot.name == *name), })
}) else { .ok_or_else(|| {
return Err(Error::CreateProcessError(format!( Error::CreateProcessError(format!(
"failed to find a process with identifier `{identifier}`", "failed to find a process with identifier `{identifier}`",
))); ))
}; })?;
let mut process = Self { let mut process = Self {
name: snapshot.name, name: snapshot.name,
id: snapshot.id, id: snapshot.id,