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