Skip to main content

sysinfo/common/
mod.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3#[cfg(feature = "component")]
4pub(crate) mod component;
5#[cfg(feature = "disk")]
6pub(crate) mod disk;
7#[cfg(feature = "gpu")]
8pub(crate) mod gpu;
9#[cfg(any(feature = "system", feature = "disk"))]
10pub(crate) mod impl_get_set;
11#[cfg(feature = "network")]
12pub(crate) mod network;
13#[cfg(feature = "system")]
14pub(crate) mod system;
15#[cfg(feature = "user")]
16pub(crate) mod user;
17
18/// Type containing read and written bytes.
19///
20/// It is returned by [`Process::disk_usage`][crate::Process::disk_usage] and [`Disk::usage`][crate::Disk::usage].
21///
22#[cfg_attr(not(all(feature = "system", feature = "disk")), doc = "```ignore")]
23/// ```no_run
24/// use sysinfo::{Disks, System};
25///
26/// if let Ok(s) = System::new_all() {
27///     for (pid, process) in s.processes() {
28///         let disk_usage = process.disk_usage();
29///         println!("[{}] read bytes   : new/total => {}/{} B",
30///             pid,
31///             disk_usage.read_bytes,
32///             disk_usage.total_read_bytes,
33///         );
34///         println!("[{}] written bytes: new/total => {}/{} B",
35///             pid,
36///             disk_usage.written_bytes,
37///             disk_usage.total_written_bytes,
38///         );
39///     }
40/// }
41///
42/// if let Ok(disks) = Disks::new_with_refreshed_list() {
43///     for disk in &disks {
44///         println!("[{:?}] disk usage: {:?}", disk.name(), disk.usage());
45///     }
46/// }
47/// ```
48#[cfg(any(feature = "disk", feature = "system"))]
49#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd)]
50pub struct DiskUsage {
51    /// Total number of written bytes.
52    pub total_written_bytes: u64,
53    /// Number of written bytes since the last refresh.
54    pub written_bytes: u64,
55    /// Total number of read bytes.
56    pub total_read_bytes: u64,
57    /// Number of read bytes since the last refresh.
58    pub read_bytes: u64,
59}
60
61macro_rules! xid {
62    ($(#[$outer:meta])+ $name:ident, $type:ty $(, $trait:ty)?) => {
63        #[cfg(any(feature = "system", feature = "user"))]
64        $(#[$outer])+
65        #[repr(transparent)]
66        #[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
67        pub struct $name(pub(crate) $type);
68
69        #[cfg(any(feature = "system", feature = "user"))]
70        impl std::ops::Deref for $name {
71            type Target = $type;
72
73            fn deref(&self) -> &Self::Target {
74                &self.0
75            }
76        }
77
78        $(
79        #[cfg(any(feature = "system", feature = "user"))]
80        impl TryFrom<usize> for $name {
81            type Error = <$type as TryFrom<usize>>::Error;
82
83            fn try_from(t: usize) -> Result<Self, <$type as TryFrom<usize>>::Error> {
84                Ok(Self(<$type>::try_from(t)?))
85            }
86        }
87
88        #[cfg(any(feature = "system", feature = "user"))]
89        impl $trait for $name {
90            type Err = <$type as $trait>::Err;
91
92            fn from_str(t: &str) -> Result<Self, <$type as $trait>::Err> {
93                Ok(Self(<$type>::from_str(t)?))
94            }
95        }
96        )?
97    };
98}
99
100macro_rules! uid {
101    ($type:ty$(, $trait:ty)?) => {
102        xid!(
103            /// A user id wrapping a platform specific type.
104            Uid,
105            $type
106            $(, $trait)?
107        );
108    };
109}
110
111macro_rules! gid {
112    ($type:ty) => {
113        xid!(
114            /// A group id wrapping a platform specific type.
115            #[derive(Copy)]
116            Gid,
117            $type,
118            std::str::FromStr
119        );
120    };
121}
122
123cfg_select! {
124    all(
125        not(feature = "unknown-ci"),
126        any(
127            target_os = "freebsd",
128            target_os = "netbsd",
129            target_os = "linux",
130            target_os = "android",
131            target_os = "macos",
132            target_os = "ios",
133        )
134    ) => {
135        uid!(libc::uid_t, std::str::FromStr);
136        gid!(libc::gid_t);
137    }
138    windows => {
139        uid!(crate::windows::Sid);
140        gid!(u32);
141        // Manual implementation outside of the macro...
142        #[cfg(any(feature = "system", feature = "user"))]
143        impl std::str::FromStr for Uid {
144            type Err = <crate::windows::Sid as std::str::FromStr>::Err;
145
146            fn from_str(t: &str) -> Result<Self, Self::Err> {
147                Ok(Self(t.parse()?))
148            }
149        }
150    }
151    _ => {
152        uid!(u32, std::str::FromStr);
153        gid!(u32);
154    }
155}