Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 <vkkoskie@gmail.com>
  • Loading branch information
bors[bot] and vkkoskie committed May 30, 2022
2 parents e3932c4 + 51f1ef3 commit 7e8757f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
- Added `aio_writev` and `aio_readv`.
(#[1713](https://github.com/nix-rust/nix/pull/1713))

- impl `From<uid_t>` for `Uid` and `From<gid_t>` for `Gid`
(#[1727](https://github.com/nix-rust/nix/pull/1727))
- impl From<SockaddrIn> for std::net::SocketAddrV4 and
impl From<SockaddrIn6> for std::net::SocketAddrV6.
(#[1711](https://github.com/nix-rust/nix/pull/1711))
Expand Down
12 changes: 12 additions & 0 deletions src/unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ impl From<Uid> for uid_t {
}
}

impl From<uid_t> for Uid {
fn from(uid: uid_t) -> Self {
Uid(uid)
}
}

impl fmt::Display for Uid {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
Expand Down Expand Up @@ -131,6 +137,12 @@ impl From<Gid> for gid_t {
}
}

impl From<gid_t> for Gid {
fn from(gid: gid_t) -> Self {
Gid(gid)
}
}

impl fmt::Display for Gid {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
Expand Down

0 comments on commit 7e8757f

Please sign in to comment.