Skip to content

Commit

Permalink
Merge #1429
Browse files Browse the repository at this point in the history
1429: constify from_raw and as_raw for Uid, Gid and Pid and Uid::is_root r=asomers a=Blub

Make some integer wrapper methods `const fn`.

* `Uid::from_raw`
* `Uid::as_raw`
* `Uid::is_root`
* `Gid::from_raw`
* `Gid::as_raw`
* `Pid::from_raw`
* `Pid::as_raw`

(Changelog mentions rust 1.40.0 as minimum required version which seems to compile this fine (using 1.40.0 from rustup))

Co-authored-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
  • Loading branch information
bors[bot] and Blub committed May 1, 2021
2 parents 0e1bbcf + 8fa391c commit bb875f2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### Changed
- Made `forkpty` unsafe, like `fork`
(#[1390](https://github.com/nix-rust/nix/pull/1390))
- Made `Uid`, `Gid` and `Pid` methods `from_raw` and `as_raw` a `const fn`
(#[1429](https://github.com/nix-rust/nix/pull/1429))
- Made `Uid::is_root` a `const fn`
(#[1429](https://github.com/nix-rust/nix/pull/1429))

### Fixed
- Allow `sockaddr_ll` size, as reported by the Linux kernel, to be smaller then it's definition
Expand Down
16 changes: 8 additions & 8 deletions src/unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub struct Uid(uid_t);

impl Uid {
/// Creates `Uid` from raw `uid_t`.
pub fn from_raw(uid: uid_t) -> Self {
pub const fn from_raw(uid: uid_t) -> Self {
Uid(uid)
}

Expand All @@ -53,12 +53,12 @@ impl Uid {
}

/// Returns true if the `Uid` represents privileged user - root. (If it equals zero.)
pub fn is_root(self) -> bool {
self == ROOT
pub const fn is_root(self) -> bool {
self.0 == ROOT.0
}

/// Get the raw `uid_t` wrapped by `self`.
pub fn as_raw(self) -> uid_t {
pub const fn as_raw(self) -> uid_t {
self.0
}
}
Expand Down Expand Up @@ -87,7 +87,7 @@ pub struct Gid(gid_t);

impl Gid {
/// Creates `Gid` from raw `gid_t`.
pub fn from_raw(gid: gid_t) -> Self {
pub const fn from_raw(gid: gid_t) -> Self {
Gid(gid)
}

Expand All @@ -102,7 +102,7 @@ impl Gid {
}

/// Get the raw `gid_t` wrapped by `self`.
pub fn as_raw(self) -> gid_t {
pub const fn as_raw(self) -> gid_t {
self.0
}
}
Expand All @@ -128,7 +128,7 @@ pub struct Pid(pid_t);

impl Pid {
/// Creates `Pid` from raw `pid_t`.
pub fn from_raw(pid: pid_t) -> Self {
pub const fn from_raw(pid: pid_t) -> Self {
Pid(pid)
}

Expand All @@ -143,7 +143,7 @@ impl Pid {
}

/// Get the raw `pid_t` wrapped by `self`.
pub fn as_raw(self) -> pid_t {
pub const fn as_raw(self) -> pid_t {
self.0
}
}
Expand Down

0 comments on commit bb875f2

Please sign in to comment.