proc_tree/traits.rs
1//! Traits for process tree storage backends.
2//!
3//! Implement `ProcessStore` to provide your own storage
4//! (e.g., moka cache, Redis, dashmap) while reusing the process tree
5//! algorithms in [`crate::ops`].
6
7use crate::types::ProcessInfo;
8
9/// Trait for process tree storage.
10///
11/// Implement this trait to provide your own storage backend.
12pub trait ProcessStore {
13 /// Get process info by PID.
14 fn get_process(&self, pid: u32) -> Option<ProcessInfo>;
15
16 /// Insert or update process info.
17 fn insert_process(&self, pid: u32, info: ProcessInfo);
18
19 /// Remove a process by PID. Returns the removed process info.
20 fn remove_process(&self, pid: u32) -> Option<ProcessInfo>;
21
22 /// Get all PIDs in the tree.
23 fn all_pids(&self) -> Vec<u32>;
24
25 /// Iterate direct children of a PID.
26 ///
27 /// Calls `f` for each child PID. Avoids allocating a return Vec.
28 fn for_each_child(&self, pid: u32, f: &mut dyn FnMut(u32));
29
30 /// Get direct children of a PID as a Vec.
31 ///
32 /// Convenience wrapper around [`for_each_child`](ProcessStore::for_each_child).
33 fn children_of(&self, pid: u32) -> Vec<u32> {
34 let mut v = Vec::new();
35 self.for_each_child(pid, &mut |c| v.push(c));
36 v
37 }
38}