Skip to content

Commit 82c0800

Browse files
committed
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.
1 parent 012d5ea commit 82c0800

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

src/pid.rs

Lines changed: 20 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,21 @@ 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::NonZero;
130+
assert!(Pid::from_raw(0).is_none());
131+
assert_eq!(
132+
Pid::from_raw(77).unwrap().as_raw_nonzero(),
133+
NonZero::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+
}
124143
}

0 commit comments

Comments
 (0)