heim_process/sys/
common.rs

1use ordered_float::NotNan;
2
3use crate::Pid;
4use heim_common::units::{time, Time};
5
6/// Process unique ID.
7///
8/// Processes can't be compared just by their PIDs,
9/// as the PIDs can be re-used, so the minimal information amount
10/// needed to unique identify a process is a (pid, create_time)
11/// tuple.
12///
13/// In addition, since `create_time` is basically the `f64` type
14/// internally, it is wrapped into a `NotNan` type
15/// in order to provide `Hash`, `PartialEq` and `Eq` traits.
16///
17/// This struct is shared across multiple OS-specific implementations.
18#[derive(Debug, Hash, PartialEq, Eq, Clone)]
19pub struct UniqueId {
20    pid: Pid,
21    create_time: NotNan<f64>,
22}
23
24impl UniqueId {
25    /// Create new `UniqueId` based on process' `pid` and `create_time`.
26    ///
27    /// ## Panics
28    ///
29    /// Will panic if `create_time` internally is `NaN`,
30    /// which should be considered as a bug, since it should
31    /// be impossible to get such a time value for process.
32    pub fn new(pid: Pid, create_time: Time) -> UniqueId {
33        let seconds = create_time.get::<time::second>();
34        let time = NotNan::new(seconds).expect("Process create time can't be NaN");
35
36        UniqueId {
37            pid,
38            create_time: time,
39        }
40    }
41
42    /// Get back the process creation time.
43    ///
44    /// Mostly used to reduce `Process` struct size
45    /// and re-use already loaded values
46    pub fn create_time(&self) -> Time {
47        Time::new::<time::second>(*self.create_time)
48    }
49}