1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use std::fmt;
use std::path::Path;

use heim_common::prelude::*;

use crate::{sys, Pid, ProcessState};
use crate::types::EnvOs;

#[derive(heim_derive::ImplWrap)]
pub struct Process(sys::Process);

impl Process {
    pub fn from_pid(pid: Pid) -> impl Future<Item=Process, Error=Error> {
        sys::Process::from_pid(pid).map(Into::into)
    }

    /// Returns process PID.
    pub fn pid(&self) -> Pid {
        self.as_ref().pid()
    }

    /// Returns process parent PID.
    pub fn parent_pid(&self) -> Pid {
        self.as_ref().ppid()
    }

    /// Returns process name.
    pub fn name(&self) -> &str {
        self.as_ref().name()
    }

    pub fn exe(&self) -> Option<&Path> {
        self.as_ref().exe()
    }

    /// Returns process state.
    pub fn state(&self) -> ProcessState {
        self.as_ref().state()
    }

//    /// Returns process environment
//    // TODO: Move to `ProcessExt`
//    #[doc(hidden)]
//    pub fn environ(&self) -> &EnvOs {
//        self.as_ref().environ()
//    }
}

impl fmt::Debug for Process {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("Process")
            .field("pid", &self.pid())
            .field("name", &self.name())
            .field("state", &self.state())
            .field("parent_pid", &self.parent_pid())
            .field("exe", &self.exe())
            .finish()
    }
}

pub fn processes() -> impl Stream<Item=Process, Error=Error> {
    sys::processes().map(Into::into)
}