Skip to content

Commit 5930dc0

Browse files
authored
Fix the assertion in Pid::from_raw to accept 0. (#1456)
* Fix the assertion in `Pid::from_raw` to accept 0. Fix a regression from #1443 which disallowed calling `Pid::from_raw` with the value 0. * Disable transmutes warnings for now. * Add a test. * Update CI to ubuntu-22.04, as ubuntu-20.04 is no longer supported. * Fix the build on Rust 1.63. * Temporarily work around nightly build errors on powerpc64-ibm-aix.
1 parent 012d5ea commit 5930dc0

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

.github/workflows/main.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,8 @@ jobs:
229229
- run: cargo check -Z build-std --target=x86_64-pc-nto-qnx710 --features=all-apis
230230
- run: cargo check -Z build-std --target=armv6k-nintendo-3ds --all-features
231231
- run: cargo check -Z build-std --target=armv7-sony-vita-newlibeabihf --features=all-apis
232-
- run: cargo check -Z build-std --target=powerpc64-ibm-aix --features=all-apis
232+
# Temporarily disable due to build errors on nightly.
233+
# - run: cargo check -Z build-std --target=powerpc64-ibm-aix --features=all-apis
233234
- run: cargo check -Z build-std --target=mipsel-unknown-linux-gnu --features=all-apis
234235
- run: cargo check -Z build-std --target=mips64el-unknown-linux-gnuabi64 --features=all-apis
235236
- run: cargo check -Z build-std --target=powerpc-unknown-linux-musl --features=all-apis
@@ -246,13 +247,13 @@ jobs:
246247
RUSTFLAGS: --cfg rustix_use_experimental_features
247248
strategy:
248249
matrix:
249-
build: [ubuntu, ubuntu-20.04, i686-linux, aarch64-linux, powerpc-linux, powerpc64le-linux, riscv64-linux, s390x-linux, arm-linux, ubuntu-stable, i686-linux-stable, aarch64-linux-stable, riscv64-linux-stable, s390x-linux-stable, powerpc-linux-stable, powerpc64le-linux-stable, arm-linux-stable, ubuntu-1.63, i686-linux-1.63, aarch64-linux-1.63, riscv64-linux-1.63, s390x-linux-1.63, powerpc64le-linux, powerpc64le-linux-1.63, arm-linux-1.63, macos-latest, macos-13, windows, windows-2019, musl]
250+
build: [ubuntu, ubuntu-22.04, i686-linux, aarch64-linux, powerpc-linux, powerpc64le-linux, riscv64-linux, s390x-linux, arm-linux, ubuntu-stable, i686-linux-stable, aarch64-linux-stable, riscv64-linux-stable, s390x-linux-stable, powerpc-linux-stable, powerpc64le-linux-stable, arm-linux-stable, ubuntu-1.63, i686-linux-1.63, aarch64-linux-1.63, riscv64-linux-1.63, s390x-linux-1.63, powerpc64le-linux, powerpc64le-linux-1.63, arm-linux-1.63, macos-latest, macos-13, windows, windows-2019, musl]
250251
include:
251252
- build: ubuntu
252253
os: ubuntu-latest
253254
rust: nightly
254-
- build: ubuntu-20.04
255-
os: ubuntu-20.04
255+
- build: ubuntu-22.04
256+
os: ubuntu-22.04
256257
rust: nightly
257258
- build: i686-linux
258259
os: ubuntu-latest

src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@
122122
#![allow(clippy::useless_conversion)]
123123
// This clippy lint gets too many false positives.
124124
#![allow(clippy::needless_lifetimes)]
125+
// Until `unnecessary_transmutes` is recognized by our MSRV, don't warn about
126+
// it being unrecognized.
127+
#![allow(unknown_lints)]
128+
// Until `cast_signed` and `cast_unsigned` are supported by our MSRV, don't
129+
// warn about transmutes that could be changed to them.
130+
#![allow(unnecessary_transmutes)]
125131
// Redox and WASI have enough differences that it isn't worth precisely
126132
// conditionalizing all the `use`s for them. Similar for if we don't have
127133
// "all-apis".

src/pid.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl Pid {
4141
/// [pidfd]: https://man7.org/linux/man-pages/man2/pidfd_open.2.html
4242
#[inline]
4343
pub const fn from_raw(raw: RawPid) -> Option<Self> {
44-
debug_assert!(raw > 0);
44+
debug_assert!(raw >= 0);
4545
match NonZeroI32::new(raw) {
4646
Some(non_zero) => Some(Self(non_zero)),
4747
None => None,
@@ -79,6 +79,8 @@ impl Pid {
7979
}
8080

8181
/// Converts a `Pid` into a `RawPid`.
82+
///
83+
/// This is the same as `self.as_raw_nonzero().get()`.
8284
#[inline]
8385
pub const fn as_raw_pid(self) -> RawPid {
8486
self.0.get()
@@ -121,4 +123,22 @@ mod tests {
121123
transmute::<Option<Pid>, RawPid>(Some(Pid::from_raw_unchecked(4567)))
122124
});
123125
}
126+
127+
#[test]
128+
fn test_ctors() {
129+
use std::num::NonZeroI32;
130+
assert!(Pid::from_raw(0).is_none());
131+
assert_eq!(
132+
Pid::from_raw(77).unwrap().as_raw_nonzero(),
133+
NonZeroI32::new(77).unwrap()
134+
);
135+
assert_eq!(Pid::from_raw(77).unwrap().as_raw_pid(), 77);
136+
assert_eq!(Pid::as_raw(Pid::from_raw(77)), 77);
137+
}
138+
139+
#[test]
140+
fn test_specials() {
141+
assert!(Pid::from_raw(1).unwrap().is_init());
142+
assert_eq!(Pid::from_raw(1).unwrap(), Pid::INIT);
143+
}
124144
}

0 commit comments

Comments
 (0)