Skip to content

Commit 7e8757f

Browse files
bors[bot]vkkoskie
andauthored
Merge #1727
1727: Add From<uid_t> and From<gid_t> r=rtzoeller a=vkkoskie Conversions to/from primitive uid_t and gid_t to newtype Uid and Gid types are valid and infallible, but are only implemented in one direction. This provides the counterparts in the other direction. These conversions are identical in behavior to the from_raw methods. However, using the more idiomatic From trait enables easier interoperability with other crates (e.g., deserialization with serde) Co-authored-by: Keith Koskie <[email protected]>
2 parents e3932c4 + 51f1ef3 commit 7e8757f

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
99
- Added `aio_writev` and `aio_readv`.
1010
(#[1713](https://github.com/nix-rust/nix/pull/1713))
1111

12+
- impl `From<uid_t>` for `Uid` and `From<gid_t>` for `Gid`
13+
(#[1727](https://github.com/nix-rust/nix/pull/1727))
1214
- impl From<SockaddrIn> for std::net::SocketAddrV4 and
1315
impl From<SockaddrIn6> for std::net::SocketAddrV6.
1416
(#[1711](https://github.com/nix-rust/nix/pull/1711))

src/unistd.rs

+12
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ impl From<Uid> for uid_t {
8787
}
8888
}
8989

90+
impl From<uid_t> for Uid {
91+
fn from(uid: uid_t) -> Self {
92+
Uid(uid)
93+
}
94+
}
95+
9096
impl fmt::Display for Uid {
9197
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
9298
fmt::Display::fmt(&self.0, f)
@@ -131,6 +137,12 @@ impl From<Gid> for gid_t {
131137
}
132138
}
133139

140+
impl From<gid_t> for Gid {
141+
fn from(gid: gid_t) -> Self {
142+
Gid(gid)
143+
}
144+
}
145+
134146
impl fmt::Display for Gid {
135147
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
136148
fmt::Display::fmt(&self.0, f)

0 commit comments

Comments
 (0)