darwin_libproc/
pid_rusage.rs1use std::io;
2use std::mem;
3
4pub use darwin_libproc_sys::{
5 rusage_info_v0, rusage_info_v1, rusage_info_v2, rusage_info_v3,
6 rusage_info_v4,
7};
8
9mod private {
10 pub trait Sealed {}
11 impl Sealed for darwin_libproc_sys::rusage_info_v0 {}
12 impl Sealed for darwin_libproc_sys::rusage_info_v1 {}
13 impl Sealed for darwin_libproc_sys::rusage_info_v2 {}
14 impl Sealed for darwin_libproc_sys::rusage_info_v3 {}
15 impl Sealed for darwin_libproc_sys::rusage_info_v4 {}
16}
17
18pub trait RusageFlavor: private::Sealed {
23 #[doc(hidden)]
24 fn flavor() -> libc::c_int;
25}
26
27impl RusageFlavor for rusage_info_v0 {
28 fn flavor() -> libc::c_int {
29 darwin_libproc_sys::RUSAGE_INFO_V0
30 }
31}
32
33impl RusageFlavor for rusage_info_v1 {
34 fn flavor() -> libc::c_int {
35 darwin_libproc_sys::RUSAGE_INFO_V1
36 }
37}
38
39impl RusageFlavor for rusage_info_v2 {
40 fn flavor() -> libc::c_int {
41 darwin_libproc_sys::RUSAGE_INFO_V2
42 }
43}
44
45impl RusageFlavor for rusage_info_v3 {
46 fn flavor() -> libc::c_int {
47 darwin_libproc_sys::RUSAGE_INFO_V3
48 }
49}
50
51impl RusageFlavor for rusage_info_v4 {
52 fn flavor() -> libc::c_int {
53 darwin_libproc_sys::RUSAGE_INFO_V4
54 }
55}
56
57pub fn pid_rusage<T: RusageFlavor>(pid: libc::pid_t) -> io::Result<T> {
59 let mut rusage = mem::MaybeUninit::<T>::uninit();
60 let result = unsafe {
61 darwin_libproc_sys::proc_pid_rusage(
62 pid,
63 T::flavor(),
64 rusage.as_mut_ptr() as *mut _,
65 )
66 };
67
68 if result < 0 {
69 Err(io::Error::last_os_error())
70 } else {
71 Ok(unsafe { rusage.assume_init() })
72 }
73}