1 | // Take a look at the license at the top of the repository in the LICENSE file. |
2 | |
3 | #[cfg (feature = "component" )] |
4 | pub(crate) mod component; |
5 | #[cfg (feature = "disk" )] |
6 | pub(crate) mod disk; |
7 | #[cfg (feature = "network" )] |
8 | pub(crate) mod network; |
9 | #[cfg (feature = "system" )] |
10 | pub(crate) mod system; |
11 | #[cfg (feature = "user" )] |
12 | pub(crate) mod user; |
13 | |
14 | macro_rules! xid { |
15 | ($(#[$outer:meta])+ $name:ident, $type:ty $(, $trait:ty)?) => { |
16 | #[cfg(any(feature = "system" , feature = "user" ))] |
17 | $(#[$outer])+ |
18 | #[repr(transparent)] |
19 | #[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)] |
20 | pub struct $name(pub(crate) $type); |
21 | |
22 | #[cfg(any(feature = "system" , feature = "user" ))] |
23 | impl std::ops::Deref for $name { |
24 | type Target = $type; |
25 | |
26 | fn deref(&self) -> &Self::Target { |
27 | &self.0 |
28 | } |
29 | } |
30 | |
31 | $( |
32 | #[cfg(any(feature = "system" , feature = "user" ))] |
33 | impl TryFrom<usize> for $name { |
34 | type Error = <$type as TryFrom<usize>>::Error; |
35 | |
36 | fn try_from(t: usize) -> Result<Self, <$type as TryFrom<usize>>::Error> { |
37 | Ok(Self(<$type>::try_from(t)?)) |
38 | } |
39 | } |
40 | |
41 | #[cfg(any(feature = "system" , feature = "user" ))] |
42 | impl $trait for $name { |
43 | type Err = <$type as $trait>::Err; |
44 | |
45 | fn from_str(t: &str) -> Result<Self, <$type as $trait>::Err> { |
46 | Ok(Self(<$type>::from_str(t)?)) |
47 | } |
48 | } |
49 | )? |
50 | }; |
51 | } |
52 | |
53 | macro_rules! uid { |
54 | ($type:ty$(, $trait:ty)?) => { |
55 | xid!( |
56 | /// A user id wrapping a platform specific type. |
57 | Uid, |
58 | $type |
59 | $(, $trait)? |
60 | ); |
61 | }; |
62 | } |
63 | |
64 | macro_rules! gid { |
65 | ($type:ty) => { |
66 | xid!( |
67 | /// A group id wrapping a platform specific type. |
68 | #[derive(Copy)] |
69 | Gid, |
70 | $type, |
71 | std::str::FromStr |
72 | ); |
73 | }; |
74 | } |
75 | |
76 | cfg_if! { |
77 | if #[cfg(all( |
78 | not(feature = "unknown-ci" ), |
79 | any( |
80 | target_os = "freebsd" , |
81 | target_os = "linux" , |
82 | target_os = "android" , |
83 | target_os = "macos" , |
84 | target_os = "ios" , |
85 | ) |
86 | ))] { |
87 | uid!(libc::uid_t, std::str::FromStr); |
88 | gid!(libc::gid_t); |
89 | } else if #[cfg(windows)] { |
90 | uid!(crate::windows::Sid); |
91 | gid!(u32); |
92 | // Manual implementation outside of the macro... |
93 | #[cfg(any(feature = "system" , feature = "user" ))] |
94 | impl std::str::FromStr for Uid { |
95 | type Err = <crate::windows::Sid as std::str::FromStr>::Err; |
96 | |
97 | fn from_str(t: &str) -> Result<Self, Self::Err> { |
98 | Ok(Self(t.parse()?)) |
99 | } |
100 | } |
101 | } else { |
102 | uid!(u32, std::str::FromStr); |
103 | gid!(u32); |
104 | } |
105 | } |
106 | |