yash_env/system/
id.rs

1// This file is part of yash, an extended POSIX shell.
2// Copyright (C) 2024 WATANABE Yuki
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program.  If not, see <https://www.gnu.org/licenses/>.
16
17//! Definitions of ID types
18
19#[cfg(unix)]
20type RawUidDef = libc::uid_t;
21#[cfg(not(unix))]
22type RawUidDef = u32;
23
24/// Raw user ID type
25///
26/// This is a type alias for the raw user ID type `uid_t` declared in the
27/// [`libc`] crate. The exact representation of this type is platform-dependent
28/// while POSIX requires the type to be an integer. On non-Unix platforms, this
29/// type is hard-coded to `u32`.
30///
31/// User IDs are usually wrapped in the [`Uid`] type for better type safety, so
32/// this type is not used directly in most cases.
33pub type RawUid = RawUidDef;
34
35/// User ID
36///
37/// This type implements the new type pattern for the raw user ID type
38/// [`RawUid`]. The advantage of using this type is that it is more type-safe
39/// than using the raw integer value directly.
40#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
41#[repr(transparent)]
42pub struct Uid(pub RawUid);
43
44#[cfg(unix)]
45type RawGidDef = libc::gid_t;
46#[cfg(not(unix))]
47type RawGidDef = u32;
48
49/// Raw group ID type
50///
51/// This is a type alias for the raw group ID type `gid_t` declared in the
52/// [`libc`] crate. The exact representation of this type is platform-dependent
53/// while POSIX requires the type to be an integer. On non-Unix platforms, this
54/// type is hard-coded to `u32`.
55///
56/// Group IDs are usually wrapped in the [`Gid`] type for better type safety, so
57/// this type is not used directly in most cases.
58pub type RawGid = RawGidDef;
59
60/// Group ID
61///
62/// This type implements the new type pattern for the raw group ID type
63/// [`RawGid`]. The advantage of using this type is that it is more type-safe
64/// than using the raw integer value directly.
65#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
66#[repr(transparent)]
67pub struct Gid(pub RawGid);